<template>
  <div>
    <Modal v-model="showEditModal" class="modal-footer-none zIndex-1100">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-form-classify-edit" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="归类名称" prop="name">
            <Input v-model="formObj.name" name="name" placeholder="请输入归类名称" /></Input>
          </Form-item>
          <Form-item label="归类库存量" prop="stock">
            <InputNumber v-model="formObj.stock" name="stock" :min="0" style="width: 100%;" :disabled="true" />
          </Form-item>
          <Form-item label="计量方式" prop="unit">
            <Select v-model="formObj.unit" clearable filterable style="width:100%" placeholder="请选择计量方式">
              <Option v-for="item in unitList" :key="item.name" :value="item.name">
                {{ item.name }}
              </Option>
            </Select>
          </Form-item>
          <Form-item label="预警库存量" prop="warning">
            <InputNumber v-model="formObj.warning" name="warning" :min="0" style="width: 100%;" />
          </Form-item>
          <Form-item label="预警通知" prop="inform">
            <Select v-model="formObj.inform" name="inform" style="width:100%" @on-change="_inform">
              <Option :value="1">
                开启
              </Option>
              <Option :value="0">
                关闭
              </Option>
            </Select>
          </Form-item>
          <Form-item v-show="informType" label="预警通知类型" prop="informType">
            <Select v-model="formObj.informType" name="informType" multiple style="width:100%">
              <Option :value="0">
                PC
              </Option>
              <!--<Option :value="1">微信</Option>-->
              <!--<Option :value="2">短信</Option>-->
            </Select>
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注" /></Input>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <Button type="ghost" style="margin-left: 8px" @click="_cancel">
          取消
        </Button>
        <Button type="primary" @click="_ok">
          提交
        </Button>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑试验耗材归类表
 */
import { consumeClassify, dict } from '../../../api'
const defVal = {
  name: '',
  stock: 0,
  unit: '',
  warning: 0,
  inform: 0,
  informType: 0,
  remark: ''
}

export default {
  data() {
    return {
      id: '',
      modalTitle: '',
      formObj: defVal,
      ruleValidate: {
        name: [
          { required: true, message: '归类名称不能为空', trigger: 'blur' },
          { max: 100, message: '归类名称不能超过100字符', trigger: 'blur' }
        ],
        // stock: [{required: true, message: '归类库存量不能为空', trigger: 'blur',type:'number'}],
        unit: [
          { required: true, message: '计量方式不能为空', trigger: 'blur' }
        ],
        warning: [
          {
            required: true,
            message: '预警库存量不能为空',
            trigger: 'blur',
            type: 'number'
          }
        ],
        remark: [{ max: 100, message: '备注不能超过100字符', trigger: 'blur' }]
      },
      showEditModal: false,
      unitList: [],
      informType: false
    }
  },
  methods: {
    _resultChange(msg) {
      this.formObj = defVal
      this.showEditModal = false
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form-classify-edit')
          this.$extend(data, {
            unit: this.formObj.unit
          })
          if (data.inform === 0) {
            data.informType = ''
          }
          alert(this.id)
          if (this.$string(this.id).isEmpty()) {
            // 添加
            // this.$store.dispatch('LmsChemicalClassify/add', data).then(() => {
            //   this._resultChange('添加成功!')
            // })
            this._save(data)
          } else {
            this._edit({ id: this.id, obj: data })
          }
        } else {
          this.$Message.error('表单验证失败!')
        }
      })
    },

    _save: async function(data) {
      const result = await consumeClassify.add(data)
      if (result) {
        this._resultChange('保存成功')
      }
    },
    _edit: async function(data) {
      const result = await consumeClassify.edit(data)
      if (result) {
        this._resultChange('保存成功')
      }
    },
    _cancel() {
      this.showEditModal = false
    },
    _open(formObj) {
      this.showEditModal = true
      this._getUnit() // 获取单位列表
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.formObj = defVal
        this.informType = false
        this.modalTitle = '添加耗材归类信息'
      } else {
        this.id = formObj.id
        this.formObj = formObj
        let typeArr = []
        typeArr = formObj.informType.split(',')
        for (let i = 0; i < typeArr.length; i++) {
          typeArr[i] = Number(typeArr[i])
        }
        this.formObj.informType = typeArr
        this._checkInformType(formObj.inform)
        this.modalTitle = '编辑耗材归类信息'
      }
    },
    _checkInformType(data) {
      if (data === '1') {
        this.informType = true
      } else {
        this.informType = false
      }
    },
    _getUnit: async function() {
      const result = await dict.getItem('库存归类计量方式')
      this.unitList = result
    },
    _inform(data) {
      if (data === 1) {
        this.informType = true
      } else {
        this.informType = false
      }
    }
  }
}
</script>
