<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="批次号：">
        <el-input v-model="form.no" placeholder="请输入批次号" clearable />
      </el-form-item>
      <el-form-item label="服务时长：">
        <el-select v-model="form.serviceTime" placeholder="请选择服务时长" clearable>
          <el-option v-for="item in serviceTimeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker v-model="date" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" @click="dialogVisible = true"
          v-hasPermi="['service:code:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="兑换数量" prop="num" align="center" />
      <el-table-column label="批次号" prop="no" align="center" />
      <el-table-column label="服务时长" prop="serviceTime" align="center" :formatter="serviceFormatter" />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getUpload(scope.row.id, 'zip')">二维码下载</el-button>
          <el-button type="text" @click="getUpload(scope.row.id, 'xlsx')">表格下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="新增" width="500px" :before-close="cancel">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-width="auto">
        <el-form-item label="兑换码数量：" prop="num">
          <el-input v-model.number="dialogForm.num" placeholder="请输入兑换码数量" />
        </el-form-item>
        <el-form-item label="服务时长：" prop="serviceTime">
          <el-select v-model="dialogForm.serviceTime" placeholder="请选择服务时长">
            <el-option v-for="item in serviceTimeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="getSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { create, list } from '@/api/service/code'
import { serviceTimeMixin } from '@/mixin/index'
export default {
  mixins: [serviceTimeMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      date: [],
      // 列表
      tableData: [],
      total: 0,
      // 弹窗
      dialogVisible: false,
      dialogForm: {},
      rules: {
        num: [{
          required: true, message: '请输入兑换码数量', trigger: 'blur'
        }, {
          type: 'number', message: '请输入数字', trigger: 'blur'
        }, {
          pattern: /^\+?[1-9]\d*$/, message: '兑换码必须大于0', trigger: 'blur'
        }],
        serviceTime: [{
          required: true, message: '请选择服务时长', trigger: 'change'
        }]
      }
    }
  },
  watch: {
    date(val) {
      this.form.startTime = val[0]
      this.form.endTime = val[1]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.date = []
      this.getList()
    },

    // 取消
    cancel() {
      this.$refs.dialogForm.resetFields()
      this.dialogVisible = false
    },

    // 确定
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          create(this.dialogForm).then(res => {
            if (res.code === 0) {
              this.$message.success('新增成功！')
              this.dialogVisible = false
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },

    // 二维码 / 表格 下载
    getUpload(id, type) {
      let url = type === 'zip' ? `/admin/batch/zipDownload` : `/admin/batch/excelDownload`
      this.download(url, {
        no: id
      }, `${id}.${type}`)
    },

    // 字典翻译
    serviceFormatter(row) {
      return this.selectDictLabel(this.serviceTimeOptions, row.serviceTime)
    }
  }
}
</script>