<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="false" width="600">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="80">
          <Form-item label="物品目录" prop="name">
            <Input
              v-model="formObj.name"
              name="name"
              placeholder="请输入物品目录"
              clearable
            />
          </Form-item>
          <Form-item label="物品编码" prop="code">
            <Input
              v-model="formObj.code"
              name="code"
              placeholder="请输入物品编码"
              clearable
            />
          </Form-item>
          <Form-item label="规格" prop="spec">
            <Input
              v-model="formObj.spec"
              name="spec"
              placeholder="请输入规格"
              clearable
            />
          </Form-item>
          <Form-item label="目录描述" prop="remark">
            <Input
              v-model="formObj.remark"
              name="remark"
              placeholder="请输入目录描述"
            />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { drugCatalogue } from '../../../api'
export default {
  components: {},
  data() {
    // const validateCont = (rule, value, callback) => {
    //   if (this.formObj.producerId === '') {
    //     callback(new Error('供应商不能为空'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      id: '',
      modalTitle: '添加',
      formObj: {
        spec: '',
        code: '',
        name: '',
        type: '',
        typeId: '',
        remark: ''
      },
      ruleValidate: {
        code: [
          { required: true, message: '物料编码不能为空', trigger: 'blur' }
        ],
        name: [{ required: true, message: '物料名称不能为空', trigger: 'blur' }]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      supplierList: []
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    // 去掉前后空格
    _trim(str) {
      return str ? str.replace(/(^\s*)|(\s*$)/g, '') : ''
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.typeId = this.formObj.typeId
          data.name = this.formObj.name
          data.spec = this.formObj.spec
          data.type = this.formObj.type
          data.remark = this.formObj.remark
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await drugCatalogue.add(this.$serializeForm(data))
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await drugCatalogue.edit({
          id: this.formObj.id,
          obj: this.$serializeForm(data)
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj, treeData, category) {
      this.$refs.formObj.resetFields()
      this.showEditModal = true
      this._hideLoading()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加' + treeData.type + '样本信息'
        this.formObj.typeId = treeData.typeId
        this.formObj.type = treeData.type
      } else {
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑' + category + '样本信息'
      }
    },
    // 详情信息
    _editorChange(data) {
      this.formObj.content = data.html
    },
    _selectSupplyName() {
      this.$refs.supplyModal._open()
    },
    //  回调供应商
    async _resultModal(data) {
      this.formObj.supplierId = data.id
      await this.$nextTick()
      this.$set(this.formObj, 'supplier', this._trim(data.name))
      console.log(this.formObj)
    }
  }
}
</script>
