<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="editFormRules">
      <el-form-item
        :label="__('审核状态')"
        label-width="110px"
        prop="verify_state"
      >
        <el-select
          v-model="form.verify_state"
          :placeholder="__('请选择')"
          style="width: 190px"
        >
          <el-option
            v-for="item in verifyStateList"
            :key="item.value"
            :label="__(item.label)"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('向微信发送请求')" label-width="110px" prop="">
        <el-radio-group v-model="form.wx_rest_enable">
          <el-radio :label="1">{{ __('启用') }}</el-radio>
          <el-radio :label="0">{{ __('关闭') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="__('审核备注')"
        label-width="110px"
        prop="verify_remark"
      >
        <el-input
          v-model="form.verify_remark"
          :autosize="{ minRows: 10, maxRows: 10 }"
          :placeholder="__('请输入审核备注')"
          :style="{ width: '100%' }"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { editState } from '@/api/user/anchor'

  export default {
    name: 'TemplateBaseEdit',
    components: {},
    data() {
      return {
        editFormRules: {
          verify_state: [
            { required: true, trigger: 'blur', message: '审核状态' },
          ],
          wx_rest_enable: [
            { required: true, trigger: 'blur', message: '向微信发送请求' },
          ],
          verify_remark: [
            { required: true, trigger: 'blur', message: '请输入审核备注' },
          ],
        },
        form: {
          verify_state: undefined,
          wx_rest_enable: 1,
          verify_remark: undefined,
        },
        formData: {
          pageNum: 1,
          pageSize: 9999,
        },
        verifyStateList: [
          {
            value: 2,
            label: '审核通过',
          },
          {
            value: 3,
            label: '审核拒绝',
          },
        ],
        isIndeterminate: true,
        isAdd: true,
        value: '',
        title: '',
        dialogFormVisible: false,
        rules: {},
        loading: false,
      }
    },
    created() {},
    mounted() {
      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.isAdd = true
          this.title = this.__('添加')
        } else {
          this.isAdd = false
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
          this.form.wx_rest_enable = 1
          if (row.verify_state === 1) {
            this.form.verify_state = ''
          }
          this.form.verify_remark = row.verify_remark
        }
        this.dialogFormVisible = true
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const params = {
              user_id: this.form.user_id,
              verify_state: this.form.verify_state,
              verify_remark: this.form.verify_remark,
              wx_rest_enable: this.form.wx_rest_enable,
            }
            const { msg, status } = await editState(params)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }

            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      close() {
        this.checkedData = []
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
    },
  }
</script>
