<template>
  <div>
    <Modal v-model="showEditModal" v-drag :mask-closable="false">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <Form id="edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="95">
          <Form-item label="检测项目" prop="name">
            <Input v-model="formObj.name" name="name" placeholder="请输入检测项目" clearable />
          </Form-item>
          <Form-item label="所属产品" prop="product">
            <Input v-model="formObj.product" name="product" placeholder="请输入所属产品" clearable />
          </Form-item>

          <Form-item label="检测项目类别" prop="type">
            <Input v-model="formObj.type" name="type" placeholder="请输入检测项目类别" clearable />
          </Form-item>
          <Form-item label="检测依据" prop="code">
            <Input
              v-model="formObj.code"
              name="code"
              readonly
              placeholder="请选择检测依据"
              icon="ios-add-circle-outline"
              @click.native="_selectjudgeBasis"
            />
          </Form-item>
          <Form-item label="检测依据名称" prop="standardName">
            <Input v-model="formObj.standardName" name="standardName" placeholder="检测依据名称" readonly />
            <input v-model.number="formObj.standardId" name="standardId" hidden>
          </Form-item>
          <Form-item label="检测科室" prop="groupName">
            <Select
              v-model="formObj.groupId"
              placeholder="请选择检测科室"
              style="width:100%"
              label-in-value
              @on-change="_groupChange"
            >
              <Option v-for="item in groupList" :key="item.id" :value="item.id">
                {{ item.name }}
              </Option>
            </Select>
            <input v-model="formObj.groupId" type="hidden" name="groupId">
            <input v-model="formObj.groupName" type="hidden" name="groupName">
          </Form-item>
          <Form-item label="比较符" prop="compareSymbol">
            <el-select v-model="formObj.compareSymbol"  placeholder="请选择比较符" style="width:100%">
              <el-option
                v-for="item in priceList "
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </Form-item>
          <Form-item label="限值" prop="limitValue">
            <Input v-model="formObj.limitValue" name="limitValue" placeholder="请输入限值" clearable />
          </Form-item>
          <Form-item label="单位" prop="unit">
            <Input v-model="formObj.unit" name="unit" placeholder="请输入单位" clearable />
          </Form-item>
          <Form-item label="是否系统判定" prop="judged">
            <Radio-group v-model="formObj.judged">
              <Radio v-for="item in isJudgeList" :key="item.value" :label="item.value">
                {{ item.label }}
              </Radio>
            </Radio-group>
          </Form-item>
          <Form-item label="备注" prop="remark">
            <Input v-model="formObj.remark" name="remark" placeholder="请输入备注" clearable />
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
    <!--选择检测项目-->
    <testItemModal ref="testItemModal" is-change @on-result-change="_backData" />
  </div>
</template>
<script>
/**
 * 添加编辑
 */
import { aptitudeItem, userGroup } from '../../../api'
import testItemModal from './testItemModal'
const defVal = {
  name: '',
  product: '',
  type: '',
  code: '',
  standardName: '',
  standardId: '',
  groupId: '',
  groupName: '',
  compareSymbol: '',
  limitValue: '',
  unit: '',
  judged: '1',
  remark: ''
}
export default {
  components: {
    testItemModal
  },
  data() {
    return {
      id: '',
      modalTitle: '添加',
      formObj: defVal,
      ruleValidate: {
        name: [
          { required: true, message: '检测项目不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '检测依据不能为空', trigger: 'change' }
        ],
        standardName: [
          { required: true, message: '检测依据名称不能为空', trigger: 'blur' }
        ],
        groupName: [
          { required: true, message: '检测科室不能为空', trigger: 'change' }
        ],
        // compareSymbol: [
        //   { required: true, message: '比较符不能为空', trigger: 'change' }
        // ],
        // limitValue: [
        //   { required: true, message: '限值不能为空', trigger: 'blur' }
        // ],
        // unit: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
        judged: [
          { required: true, message: '是否系统判定不能为空', trigger: 'blur' }
        ]
      },
      showEditModal: false,
      footerList: [
        { id: '', name: '取消' },
        { id: '', name: '保存', type: 'primary' }
      ],
      priceList: [
        { value: '>', label: '>' },
        { value: '<', label: '<' },
        { value: '>=', label: '>=' },
        { value: '<=', label: '<=' }
      ],
      isJudgeList: [{ value: '1', label: '是' }, { value: '0', label: '否' }],
      groupList: []
    }
  },
  methods: {
    // modal
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    // 检测科室
    _groupChange(data) {
      if (data) {
        this.formObj.groupId = data.value
        this.formObj.groupName = data.label.trim()
      } else {
        this.formObj.groupName = ''
        this.formObj.groupId = ''
      }
    },
    _getUserGroup: async function() {
      const result = await userGroup.list()
      if (result) {
        this.groupList = result
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _compareSelect() {
      alert(1111)
    },
    _resultChange(result, msg) {
      if (result) {
        this.showEditModal = false
        this.$Message.success(msg)
        this.$emit('on-result-change')
      }
      this._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          data.compareSymbol = this.formObj.compareSymbol
          data.judged = this.formObj.judged
          console.log('-----', data)
          this._save(data)
        } else {
          this.$Message.error('表单验证失败')
          this._hideLoading()
        }
      })
    },
    // 添加、编辑保存
    _save: async function(data) {
      // 添加
      if (this.$string(this.id).isEmpty()) {
        const result = await aptitudeItem.add(data)
        this._resultChange(result, '添加成功')
      } else {
        // 编辑
        const result = await aptitudeItem.edit({
          id: this.formObj.id,
          obj: data
        })
        this._resultChange(result, '编辑成功')
      }
    },
    _selectjudgeBasis() {
      this.$refs.testItemModal._open()
    },
    _cancel() {
      this.showEditModal = false
    },

    // 返回信息
    _backData(data) {
      this.formObj.code = data.code
      this.formObj.standardName = data.name
      this.formObj.standardId = data.id
    },
    _open(formObj) {
      this.$refs.formObj.resetFields()
      this.formObj = Object.assign({}, {}, defVal)
      this.formObj.groupId = ''
      console.log(this.formObj)
      this.showEditModal = true
      this._hideLoading()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加检测项目'
      } else {
        console.log('------', formObj)
        this.id = formObj.id
        this.formObj = formObj
        this.formObj.code = formObj.code
        this.formObj.standardName = formObj.standardName
        this.formObj.standardId = formObj.standardId
        this.formObj.groupName = formObj.groupName
        this.formObj.groupId = formObj.groupId
        this.formObj.judged = formObj.judged.toString()
        this.modalTitle = '编辑检测项目'
      }
      this._getUserGroup()
    }
  }
}
</script>
