<template>
  <div class="app-container">
    <xy-table ref="tableRef" :table="table" :columns="columns">

      <template #couponType="{ scope }">
        <dict-tag :options="dict.type.coupon_type" :value="scope.row.couponType"/>
      </template>
      <template #validityType="{ scope }">
        {{scope.row.validityType === '1' ? `${scope.row.validityStartDate} ~ ${scope.row.validityEndDate}` : `领取后${scope.row.validityNum}天内`}}
      </template>
      <template #examineStatus="{ scope }">
        <div style="display: flex; align-items: center; justify-content: center;">
          <xy-status-indicator :status="scope.row.examineStatus === 0 ? 'warning' : scope.row.examineStatus === 1 ? 'success' : scope.row.examineStatus === 2 ? 'error' : '' " pulse />
          <dict-tag
            style="margin-left: 4px; "
            :options="dict.type.examine_status"
            :value="scope.row.examineStatus"
          />
        </div>
      </template>

    </xy-table>

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="是否前端展示" prop="isShow">
          <el-radio-group v-model="form.isShow">
            <el-radio
              v-for="dict in dict.type.classify_isshow"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAudit, auditDetail } from '@/api/coupon/audit'

export default {
  name: 'CouponAudit',
  dicts: ['coupon_type', 'examine_status'],
  data() {
    return {
      open: false,
      title: undefined,
      form: {
        id: undefined,
        categoryName: undefined,
        isShow: '0'
      },
      rules: {
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ]
      },
      table: {
        rowKey: 'id',
        data: [],
        firstColumn: { type: 'index' },
        // hiddenLine: true,
        // 表格内操作列
        operator: [
          {
            text: '审核',
            icon: 'el-icon-edit',
            noShow: [{key: 'examineStatus', val: [1, 2]}],
            fun: (row) => this.handleAudit(row)
          },
          {
            text: '查看',
            icon: 'el-icon-view',
            noShow: [{key: 'examineStatus', val: [0]}],
            fun: (row) => this.handleDetail(row)
          }
        ],
        // 操作列样式
        operatorConfig: {
          width: 140
        },
        // 表格外操作
        toolbar: [
          // {
          //   text: '新增分类',
          //   icon: 'el-icon-plus',
          //   type: 'primary',
          //   fun: () => this.handleAdd()
          // }
        ],
        // 分页配置
        page: {
          total: 0,
          pageNum: 1,
          pageSize: 10
        },
        // 搜索表单配置
        searchForm: {
          formLabelWidth: '90px',
          formData: {
            couponTitle: undefined,
            examineStatus: undefined
          },
          fieldList: [
            { prop: 'couponTitle', label: '优惠券标题', type: 'input' },
            { prop: 'examineStatus', label: '状态', type: 'select', options: 'examineStatusOptions' }
          ],
          // 相关下拉列表
          listOptions: {
            examineStatusOptions: []
          }
        },
        // 请求
        request: listAudit
      },
      columns: [
        { prop: 'couponTitle', label: '优惠券标题' },
        { prop: 'couponType', label: '优惠券类型', slotName: 'couponType' },
        { prop: 'categoryName', label: '优惠券分类'},
        { prop: 'validityType', label: '有效期', slotName: 'validityType', minWidth: 200},
        { prop: 'createTime', label: '创建时间'},
        { prop: 'createBy', label: '创建人'},
        { prop: 'examineStatus', label: '状态', slotName: 'examineStatus'},
        { prop: 'examinePerson', label: '审核人'},
        { prop: 'examineTime', label: '审核时间'},
      ]
    }
  },
  mounted() {
    this.initOptions()
  },
  // 页面被触发
  activated() {
    if (this.$route.params && this.$route.params.refreshTable) {
      this.getList()
    }
  },
  methods: {
    initOptions() {
      this.table.searchForm.listOptions.examineStatusOptions = this.dict.type.examine_status
    },
    /** 查询列表 */
    getList() {
      this.$refs.tableRef.getData()
    },
    /** 新增按钮操作 */
    handleAdd() {

    },
    /** 编辑按钮操作 */
    handleEdit(row) {

    },
    /** 删除按钮操作 */
    async handleDelete(row) {

    },
    handleAudit(row) {
      this.$tab.closePage({ name: 'CouponAuditAudit' })
      this.$nextTick(() => {
        this.$router.push({ name: 'CouponAuditAudit', query: { id: row.id }})
      })
    },
    // 查询详情
    handleDetail(row) {
      this.$router.push({ path: '/coupon/couponAuditDetail/' + row.id })
    }
  }
}
</script>

<style>

</style>
