<template>
  <a-modal
    :title="title"
    width="60%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入名称"
            v-decorator="[
              'name',
              {
                validateFirst: true,
                rules: [
                  { required: true, message: '请输入名称' },
                  { max: 50, message: '名称最多50个字' }
                ]
              }
            ]"
          />
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea
            :rows="4"
            placeholder="请输入备注"
            v-decorator="[
              'remark',
              {
                validateFirst: true,
                rules: [{ max: 200, message: '备注最多200个字' }]
              }
            ]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { GetClientDataList } from '@/utils/clientdata'
import { Submit } from '@/api/modular/main/industry'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      currentInfo: {},
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    GetClientDataList,
    // 初始化方法
    init(record) {
      this.visible = true
      this.currentInfo = record
      if (record && record.id) {
        setTimeout(() => {
          this.form.setFieldsValue({
            name: record.name,
            remark: record.remark
          })
        }, 100)
      }
    },
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          values['id'] = this.currentInfo.id ? this.currentInfo.id : null
          Submit(values)
            .then(res => {
              if (res.code === 0) {
                if (this.currentInfo && this.currentInfo.id) {
                  this.$message.success('编辑成功')
                } else {
                  this.$message.success('添加成功')
                }
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$myerror(res.message)
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
