<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    append-to-body
    @close="handleClose"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="商品ID" prop="productId">
        <el-input
          v-model="formData.productId" maxlength="20" placeholder="商品ID" disabled
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="shelveState">
        <el-radio-group v-model="formData.shelveState">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="3">驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.shelveState === 3" label="驳回原因" prop="reject">
        <el-input v-model="formData.reject" type="textarea" placeholder="请输入驳回原因" maxlength="520" :rows="3" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { examine } from '@/api/commodity'

export default {
  name: 'ProductReview',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '620px',
        title: ''
      },
      visible: false,
      formData: {
        productId: '',
        shelveState: 1,
        reject: ''
      },
      formRules: {
        productId: [
          { required: true, message: '缺少商品ID' }
        ],
        shelveState: [
          { required: true, message: '缺少报名信息' }
        ],
        reject: []
      }
    }
  },
  watch: {
    'formData.shelveState': {
      deep: true,
      handler(val) {
        if (val === 3) {
          this.formRules.reject = [
            { required: true, message: '请输入驳回原因' }
          ]
        } else {
          this.formRules.reject = []
        }
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = '上架审核'
      //  重置表单
      this.$refs.formData && this.$refs.formData.resetFields()
      this.formData.productId = params.productId
      console.log(params.productId, this.formData.productId)
      // this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      // this.initList()
      // this.$refs.formData &&
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            await examine(params)
            loading.close()
            if (this.formData.shelveState === 1) {
              this.$message({ message: `审核成功!`, type: 'success' })
            } else {
              this.$message({ message: `已驳回!`, type: 'success' })
            }
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    },
    //  关闭弹框 重置 id
    diaClose() {
      // this.formData.productId = ''
    }
  }
}
</script>

