<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" @on-visible-change="_visibleChange" width="900">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form ref="formObj" :id="formId" :model="formObj" :rules="ruleValidate" :label-width="100" inline>
              <div>
                <Form-item label="试验指标名称" prop="name" class="width-48">
                  <Input v-model="formObj.name" name="name" placeholder="请输入试验名称"/>
                </Form-item>
                <Form-item label="试验指标大类" prop="mainType" class="width-48">
                  <Input v-model="formObj.mainType" name="mainType"
                         placeholder="请输入试验指标大类"/>
                </Form-item>
                <Form-item label="试验指标小类" prop="smallType" class="width-48">
                  <Input v-model="formObj.smallType" name="smallType" placeholder="请输入试验指标小类"/>
                </Form-item>
                <Form-item label="状态" prop="status" class="width-48">
                  <Input v-model="formObj.status"  name="status"
                         placeholder="请输入状态"/>
                </Form-item>
                <Form-item label="检测值" prop="testValue" class="width-48">
                  <Input v-model="formObj.testValue" name="testValue"
                         placeholder="请输入检测值"/>
                </Form-item>
                <Form-item label="单位" prop="unit" class="width-48">
                  <Input v-model="formObj.unit"
                         name="unit" placeholder="请输入单位"/>
                </Form-item>
              </div>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></modal-footer>
      </div>
    </Modal>
  </div>
</template>
<script>
/**
 * 添加编辑环境资质项目表
 */

import { soilAptitude } from '../../../api'

export default {
  components: {},
  data() {
    return {
      activeName: '',
      hideUserSel: true,
      id: '',
      modalTitle: '',
      expId: '',
      formObj: {
        name: '',
        unit: '',
        status: '',
        mainType: '',
        smallType: '',
        testValue: ''
      },
      ruleValidate: {
        name: [{ required: true, message: '指标名称不能为空', trigger: 'blur' }]
      },
      showModal: false,
      footerList: [
        { name: '取消', type: '' },
        { name: '保存', type: 'primary' }
      ],
      microbedList: [{ value: 0, name: '否' }, { value: 1, name: '是' }],
      // 检测依据
      testBasisData: [],
      // 类别下拉数组
      typeData: [],
      // 检测科室
      groupData: [],
      groupName: '',
      // 主检人
      userData: [],
      // 单位
      unitData: [],
      aptitudeData: [],

      aptitude: '',
      // testBasisName: '',
      computeTypeList: [],
      tester: '',
      marksList: [],
      detectionTypeList: [],
      testItemData: [],
      formId: ''
    }
  },
  mounted() {
    // 检测科室
    // this._getUserGroup()
    // 检测依据
    // this._getTestBasisList()
    // 资质
    // this._getAptitudeList()
    // this._getType()
  },
  methods: {
    // 字典
    _dicSearch() {
      this.$store.dispatch('LmsBaseDict/getItem', '食品检出类别').then(() => {
        const result = this.$store.state.LmsBaseDict.item
        this.detectionTypeList = result
        if (this.$string(this.id).isEmpty()) {
          // 添加界面默认第一个
          this.formObj.detectionType = result.length > 0 ? result[0].name : ''
        }
      })
    },
    _testBasisChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.stdId = data.id
          this.formObj.testBasis = data.stdNum
          this.formObj.testBasisName = data.name
          this.formObj.testMethod = data.stdNum
          this.$refs.formObj.validateField('testBasis')
          break
        case 'query':
          this.formObj.stdId = ''
          this.formObj.testMethod = ''
          this.formObj.testBasis = data.stdNum
          this.formObj.testBasisName = ''
          this._getTestBasisList(data)
          break
        case 'blur':
          if (this.formObj.stdId === '') {
            this.formObj.testBasis = ''
            this.formObj.testMethod = ''
            this._getTestBasisList()
          }
          this.$refs.formObj.validateField('testBasis')
      }
    },
    _selectJudgeType(data) {
      this.formObj.microbed = data
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
        case '填写说明':
          this.$refs.introduction._open()
          this._hideLoading()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open(id, formObj) {
      this.formId = 'edit-form'
      this._hideLoading()
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.expId = id
        this.modalTitle = '添加试验指标'
      } else {
        this.hideUserSel = true
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑试验指标'
      }
      this.showModal = true
    },
    // 获取检测项目
    _getItemList(value) {
      const data = {}
      if (value) {
        Object.assign(data, { name: value })
      }
      this.$store.dispatch('FoodAptitudeItem/getTestItem', data).then(() => {
        this.testItemData = this.$store.state.FoodAptitudeItem.list
      })
    },

    // 检测项目结果
    _testItemChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.name = data
          this.$refs.formObj.validateField('name')
          this._changeMicrobed()
          break
        case 'query':
          this.formObj.name = data
          this._getItemList(data)
          break
        case 'blur':
          this._changeMicrobed()
          break
      }
    },
    _changeMicrobed() {
      // 新增或者导入新增资质时 如果检测项目名称带‘菌’字，则是否微生物默认为‘是’
      if (this.id === '') {
        if (this.formObj.name.indexOf('菌') > -1) {
          this.$set(this.formObj, 'microbed', 1)
        } else {
          this.$set(this.formObj, 'microbed', 0)
        }
      }
    },
    _clearMarks() {
      this.marksList = []
    },
    _getMarks() {
      if (this.formObj.computeMark) {
        this.$store
          .dispatch('AptitudeFood/getMarks', {
            computeMark: this.formObj.computeMark
          })
          .then(() => {
            if (this.$store.state.AptitudeFood.model) {
              this.marksList = this.$store.state.AptitudeFood.model
            }
          })
      }
    },
    _getType() {
      this.$store
        .dispatch('LmsEnum/getByType', 'ItemCalculateType')
        .then(() => {
          this.computeTypeList = this.$store.state.LmsEnum.model
        })
    },
    // 资质
    _getAptitudeList() {
      this.$store.dispatch('LmsBaseDict/getItem', '所需资质').then(() => {
        this.aptitudeData = this.$store.state.LmsBaseDict.item
      })
    },
    // 主检人&检测科室
    _groupChange(data) {
      if (data) {
        this.groupName = data.label
        this.formObj.groupId = data.value
        this.formObj.tester = ''
        this.formObj.testerId = ''
        this._getUserList(data.value)
      }
    },
    _userChange(data) {
      if (data) {
        this.formObj.testerId = data.value
        this.formObj.tester = data.label
      }
    },
    _getUserList(groupId) {
      const data = { rows: 500 }
      if (groupId) {
        Object.assign(data, { groupId: groupId })
      }
      this.$store.dispatch('LmsUserGroup/getUserByGroupId', data).then(() => {
        this.userData = this.$store.state.LmsUserGroup.userPage.records
      })
    },
    _getUserGroup() {
      this.$store.dispatch('LmsUserGroup/list').then(() => {
        this.groupData = this.$store.state.LmsUserGroup.list
      })
    },
    // 单位
    _unitChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.unit = data
          break
        case 'query':
          this._getUnitList(data)
          break
      }
    },
    _getUnitList(data) {
      this.unitData = []
      if (data) {
        this.$store
          .dispatch('AptitudeFood/historyUnit', { unit: data })
          .then(() => {
            this.unitData = this.$store.state.AptitudeFood.list
          })
      } else {
        this.$store.dispatch('AptitudeFood/historyUnit').then(() => {
          this.unitData = this.$store.state.AptitudeFood.list
        })
      }
    },
    // 检测依据
    _getTestBasisList(data) {
      const obj = { classify: 1 }
      if (data) {
        obj.stdNum = data.stdNum
      }
      this.$store.dispatch('StandardInfo/page', obj).then(() => {
        this.testBasisData = this.$store.state.StandardInfo.page.records
      })
    },

    // 获取表数据
    _resultChange(msg) {
      this.showModal = false
      this.$refs.footerModal._hideLoading()
      this.$Message.success(msg)
      this.$emit('on-result-change')
      this.$refs.footerModal._hideLoading()
    },
    _ok() {
      this.$refs.formObj.validate(valid => {
        if (valid) {
          const data = this.$serialize('edit-form')
          if (this.$string(this.id).isEmpty()) {
            data.expId = this.expId
            // 添加
            this._save(data)
          } else {
            // 编辑
            this._edit({ id: this.formObj.id, obj: data })
          }
        } else {
          this.$refs.footerModal._hideLoading()
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      console.log(data)
      const result = await soilAptitude.expItemSave(data)
      console.log(result)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.expItemEdit(data)
      if (result) {
        this._resultChange('编辑成功!')
      }
    },
    _cancel() {
      this.showModal = false
      this._hideLoading()
    },
    // 切换tab
    _changeTabs(tab, event) {
      if (tab.name === 'testStep') {
        this.$refs.testStepByAptitude._open(
          this.formObj.stepId,
          this.formObj.id
        )
      }
    },
    _visibleChange(data) {
      if (!data && this.$showBtn('food-aptitude-item-step-edit')) {
        this.$emit('on-result-change')
        this.$refs.testStepByAptitude._clear()
      }
    }
  }
}
</script>
