<template>
  <el-dialog title="设置是否合规" :visible.sync="innerVisible" width="500px" :close-on-click-modal="false">
    <el-form
      ref="formRef"
      :rules="rules"
      :model="formData"
      class="demo-ruleForm"
      label-width="80px"
    >
      <el-form-item label="是否合规" prop="is_compliance">
        <el-select v-model="formData.is_compliance" style="width:100%">
          <el-option value="1" label="合规" />
          <el-option value="2" label="不合规" />
        </el-select>
      </el-form-item>

      <el-form-item label="审核备注" prop="compliance_remark">
        <el-input v-model="formData.compliance_remark" type="textarea" maxlength="250" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="innerVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import asyncRequest from '@/apis/service/goodStore/goodsOnline'
export default {
  props: ['visible', 'sitem'],
  data() {
    return {
      loading: false,
      formData: {
        is_compliance: '',
        compliance_remark: ''
      }
    }
  },
  computed: {
    rules() {
      const { is_compliance } = this.formData
      return {
        is_compliance: [{
          required: true,
          message: '请选择是否合规',
          trigger: 'blur'
        }],
        compliance_remark: [{
          required: is_compliance === '2',
          message: '请输入审核备注',
          trigger: 'blur'
        }]
      }
    },
    innerVisible: {
      get() {
        return this.visible
      },
      set(newVal) {
        this.$emit('update:visible', newVal)
      }
    }
  },
  watch: {
    visible() {
      if (!this.visible) {
        this.formData = {
          is_compliance: '',
          compliance_remark: ''
        }
      } else {
        this.$nextTick(() => {
          const { is_compliance, compliance_remark } = this.sitem
          this.formData = {
            is_compliance: is_compliance !== '0' ? is_compliance : '',
            compliance_remark
          }
        })
      }
    }
  },
  methods: {
    async submit() {
      const isValid = await this.$refs.formRef.validate()
      if (!isValid) return
      this.loading = true
      const { code, message } = await asyncRequest.goodcompliance({
        ...this.formData,
        skuCode: this.sitem.skuCode
      })
      this.loading = false
      switch (code) {
        case 0:
          this.$emit('refresh')
          this.innerVisible = false
          break
        default:
          this.$message.warning(message)
          break
      }
    }
  }
}
</script>
