<template>
  <div>
    <Modal v-model="showEditModal" :mask-closable="false" :width="500" class="zIndex-1200">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="100" inline>
          <Form-item label="检测依据" prop="code" style="width: 100%">
            <Input v-model="formObj.code" name="code" placeholder="请输入检测依据"/>
          </Form-item>
          <Form-item label="检测依据名称" prop="name" style="width: 100%">
            <Input v-model="formObj.name" name="name" placeholder="请输入检测依据名称"/>
          </Form-item>
<!--          <Form-item label="英文名称" prop="enName" class="width-48">-->
<!--            <Input v-model="formObj.enName" name="enName" placeholder="请输入英文名"/>-->
<!--          </Form-item>-->
<!--          <Form-item label="标准状态" prop="status" class="width-48">-->
<!--            <Select v-model="formObj.status" name='status'>-->
<!--              <Option v-for="item in statusList" :value="item.value" :key="item.value">{{ item.name }}</Option>-->
<!--            </Select>-->
<!--          </Form-item>-->
<!--          <Form-item label="标准类型" prop="type" class="width-48">-->
<!--            <Select v-model="formObj.type" name="type">-->
<!--              <Option v-for="item in typeList" :value="item.value" :key="item.value">{{ item.name }}</Option>-->
<!--            </Select>-->
<!--          </Form-item>-->
<!--          <Form-item label="发布日期" prop="publishDate" class="width-48">-->
<!--            <Date-picker v-model="formObj.publishDate" type="date" name="publishDate" placeholder="发布日期"-->
<!--                         format="yyyy-MM-dd" style="width: 100%;"></Date-picker>-->
<!--          </Form-item>-->
<!--          <Form-item label="实施日期" prop="effectDate" class="width-48">-->
<!--            <Date-picker v-model="formObj.effectDate" type="date" name="effectDate" placeholder="实施日期"-->
<!--                         format="yyyy-MM-dd" style="width: 100%;"></Date-picker>-->
<!--          </Form-item>-->
<!--          <Form-item label="归口单位" prop="belongUnit" class="width-48">-->
<!--            <Input v-model="formObj.belongUnit" name="belongUnit" placeholder="请输入归口单位"/>-->
<!--          </Form-item>-->
<!--          <Form-item label="发布单位" prop="publishUnit" class="width-48">-->
<!--            <Input v-model="formObj.publishUnit" name="publishUnit" placeholder="请输入发布单位"/>-->
<!--          </Form-item>-->
<!--          <Form-item label="标准介绍" prop="remark" style="width:98%">-->
<!--            <Input :rows="3" :maxlength="255" v-model="formObj.remark" placeholder="请输入标准介绍,不能超过256个字..."-->
<!--                   type="textarea" name="remark"/>-->
<!--          </Form-item>-->
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></ModalFooter>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import ModalFooter from '../../../components/base/modalFooter'
import { soilAptitude } from '../../../api'

export default {
  components: {
    ModalFooter
  },
  data() {
    return {
      formId: '',
      lengthLimitList: [
        { key: 'stdNum', title: '标准号' },
        { key: 'enName', title: '英文名称' },
        { key: 'belongUnit', title: '归口单位' },
        { key: 'publishUnit', title: '发布单位' }
      ],
      modalTitle: '添加食品标准表',
      formObj: {
        code: '',
        name: ''
      },
      ruleValidate: {
        code: [
          { required: true, message: '检测依据不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '检测依据名称不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      classifyList: [
        { value: 0, name: '判定依据' },
        { value: 1, name: '检测依据' },
        { value: 2, name: '其他' }
      ],
      typeList: [
        { value: 0, name: '国家标准' },
        { value: 1, name: '地方标准' },
        { value: 2, name: '行业标准' },
        { value: 3, name: '企业标准' }
      ],
      statusList: [
        { value: 0, name: '现行' },
        { value: 1, name: '即将实施' },
        { value: 2, name: '部分被代替' },
        { value: 3, name: '被代替' },
        { value: 4, name: '作废' }
      ],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ]
    }
  },
  methods: {
    /** *modal-footer */
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _resultChange(msg) {
      this.showEditModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          if (this.formObj.publishDate && this.formObj.effectDate) {
            if (this.formObj.publishDate > this.formObj.effectDate) {
              this.$Message.error('发布日期应早于实施日期')
              this._hideLoading()
              return
            }
          }
          const data = this.$serialize('edit-form')
          if (this.$string(this.formObj.id).isEmpty()) {
            // 添加
            this._save(data)
          } else {
            // 编辑
            this._edit({ id: this.formObj.id, obj: data })
          }
        } else {
          this.$Message.error('表单验证失败!')
          this._hideLoading()
        }
      })
    },
    _save: async function(data) {
      const result = await soilAptitude.standardSave(data)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.standardEdit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _cancel() {
      this._hideLoading()
      this.showEditModal = false
    },
    _open(formObj) {
      this.formId = this.$randomCode()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.modalTitle = '添加'
        this.formObj.id = ''
      } else {
        this.formObj = formObj
        this.formObj.id = formObj.id
        this.formObj.effectDate = this.formObj.effectDate
          ? new Date(formObj.effectDate)
          : ''
        this.formObj.publishDate = this.formObj.publishDate
          ? new Date(formObj.publishDate)
          : ''
        this.modalTitle = '编辑'
      }
      this.showEditModal = true
    },
    _registerAdd() {
      this.formId = this.$randomCode()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      this.modalTitle = '添加'
      this.formObj.id = ''
      this.formObj.type = 3
      this.showEditModal = true
    }
  }
}
</script>
