<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="90" inline>
              <div>
                <Form-item label="试验名称" prop="name" class="width-48">
                  <Input v-model="formObj.name" name="name" placeholder="请输入试验名称"/>
                </Form-item>
<!--                <Form-item label="检测科室" prop="groupName" class="width-48">-->
<!--                  <Input v-model="formObj.groupName" name="groupName" placeholder="请输入试验名称"/>-->
<!--                </Form-item>-->
                <Form-item label="检测科室：" prop="groupName" class="width-48">
                  <el-select @change="_selGroup" v-model="formObj.groupName" placeholder="请选择" name="groupName" style="width: 100%">
                    <el-option
                      v-for="(item,index) in groupoptions"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </Form-item>
<!--                <Form-item label="检测依据" prop="testBasis" class="width-48">-->
<!--                  <Input v-model="formObj.testBasis" name="testBasis" placeholder="请输入试验名称"/>-->
<!--                </Form-item>-->
                <Form-item label="检测依据：" prop="testBasis" class="width-48">
                  <el-select  v-model="formObj.testBasis" placeholder="请选择" name="testBasis" style="width: 100%">
                    <el-option
                      v-for="(item,index) in options"
                      :key="index"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </Form-item>
                <Form-item label="英文名称" prop="elName" class="width-48">
                  <Input v-model="formObj.elName" name="elName"
                         placeholder="请输入英文名称"/>
                </Form-item>
                <Form-item label="试验项目英文简写" prop="shortName" class="width-48">
                  <Input v-model="formObj.shortName" name="shortName" 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="testMethod" class="width-48">
                  <Input v-model="formObj.testMethod"
                         name="testMethod" placeholder="请输入试验方法"/>
                </Form-item>
                <Form-item @click.native="_selectPerson" label="主检人" prop="mainTester" class="width-48">
                  <Input v-model="formObj.mainTester"
                         name="mainTester" placeholder="请输入主检人"/>
                  <Input v-model="formObj.mainTesterId"
                         name="mainTesterId" style="display: none"/>
                </Form-item>
                <Form-item @click.native="_selectCheckPerson" label="审核人" prop="checker" class="width-48">
                  <Input v-model="formObj.checker"
                         name="checker" placeholder="请输入审核人"/>
                  <Input v-model="formObj.checkerId"
                         name="checkerId" style="display: none"/>
                </Form-item>
              </div>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" @on-result-change="_footerResult" :footer="footerList"></modal-footer>
      </div>
    </Modal>
    <assignPerson ref="userModal" @on-result-change="_userResult"></assignPerson>
    <assignPersonCheck ref="userCheckModal" @on-result-change="_userCheckResult"></assignPersonCheck>
  </div>
</template>
<script>
/**
 * 添加编辑环境资质项目表
 */

import { soilAptitude } from '../../../api'
import assignPerson from '../../../components/user-info-single/assignPerson'
import assignPersonCheck from '../../../components/user-info-single/assignPersonCheck'
export default {
  components: { assignPerson, assignPersonCheck },
  data() {
    return {
      activeName: '',
      hideUserSel: true,
      id: '',
      modalTitle: '',
      formObj: {
        name: '',
        elName: '',
        shortName: '',
        mainType: '',
        smallType: '',
        testMethod: '',
        groupName: '',
        groupId: '',
        testBasis: ''
      },
      ruleValidate: {
        name: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }]
      },
      options: [],
      groupoptions: [],
      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() {},
  methods: {
    groupSelect(data) {
      console.log(data)
      // this.formObj.groupId = da
    },
    _selectPerson() {
      this.$refs.userModal._openOrg('选择主检人', 'itemTree')
    },
    _selectCheckPerson() {
      this.$refs.userCheckModal._openOrg('选择审核人', 'itemTreeCheck')
    },
    _userResult(data, msg) {
      this.$forceUpdate()
      this.formObj.mainTester = data.realname
      this.formObj.mainTesterId = data.id
    },
    _userCheckResult(data, msg) {
      this.$forceUpdate()
      this.formObj.checker = data.realname
      this.formObj.checkerId = data.id
    },
    _selGroup(data) {
      console.log(data)
      this.formObj.groupId = data
    },
    _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
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open(formObj) {
      this.formId = 'index-edit-form'
      this._hideLoading()
      this._getStandardList()
      this._getGroupList()
      this.$refs.formObj.resetFields()
      if (this.$string(formObj).isEmpty()) {
        this.id = ''
        this.modalTitle = '添加试验项目'
      } else {
        this.hideUserSel = true
        this.id = formObj.id
        this.formObj = formObj
        this.modalTitle = '编辑试验项目'
      }
      this.showModal = true
    },
    // 获取检测依据
    _getStandardList: async function() {
      const result = await soilAptitude.getStandardList()
      if (result) {
        this.options = result
      }
    },
    _getGroupList: async function() {
      const result = await soilAptitude.getGroupList()
      if (result) {
        this.groupoptions = result
      }
    },
    _clearMarks() {
      this.marksList = []
    },
    // 主检人&检测科室
    _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
      }
    },
    // 单位
    _unitChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.unit = data
          break
        case 'query':
          this._getUnitList(data)
          break
      }
    },
    // 获取表数据
    _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('index-edit-form')
          data.groupId = this.formObj.groupId
          if (this.$string(this.id).isEmpty()) {
            // 添加
            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.save(data)
      console.log(result)
      if (result) {
        this._resultChange('添加成功!')
      }
    },
    _edit: async function(data) {
      const result = await soilAptitude.edit(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) {
      this.$emit('on-result-change')
    }
  }
}
</script>
