<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="900" @on-visible-change="_visibleChange">
      <p slot="header">{{modalTitle}}</p>
      <div>
        <Form :id="formId" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90" inline>
              <div>
                <Form-item label="高级样品位置" prop="receiveLocation" style="width: 80%">
                  <Input v-model="formObj.receiveLocation" name="receiveLocation" placeholder="请输入高级样品位置"/>
                </Form-item>
                <Form-item label="高级样品分类：" prop="seniorType" style="width: 80%">
                  <el-select v-model="formObj.seniorType" placeholder="请选择" name="seniorType" 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" style="width: 80%">
                  <Date-picker v-model="formObj.receiveTime" name="receiveTime" type="date" split-panels style="width:100%;" placeholder="请选择委托日期"
                               @on-change="_tabulateChange"></Date-picker>
                </Form-item>
                <Form-item label="接收人" prop="receiver" class="width-48" @click.native="_selectPerson">
                  <Input v-model="formObj.receiver" name="receiver" placeholder="请输入名称"/>
                </Form-item>
              </div>
        </Form>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></modal-footer>
      </div>
    </Modal>
    <assignPerson ref="userModal" @on-result-change="_userResult"></assignPerson>
  </div>
</template>
<script>
/**
 * 添加编辑环境资质项目表
 */

import { soilAptitude, soilEntrust } from '../../../../api'
import assignPerson from '../../../../components/user-info-single/assignPerson'
export default {
  components: { assignPerson },
  data() {
    return {
      activeName: '',
      hideUserSel: true,
      id: '',
      modalTitle: '',
      formObj: {
        receiveLocation: undefined,
        seniorType: undefined,
        receiveTime: undefined,
        receiver: undefined,
        receiverId: undefined
      },
      sampleIds: [],
      ruleValidate: {
        name: []
      },
      options: [],
      groupoptions: [],
      showModal: false,
      footerList: [
        { name: '取消', type: '' },
        { name: '保存', type: 'primary' }
      ],
      aptitude: '',
      // testBasisName: '',
      formId: ''
    }
  },
  mounted() {
    // 检测科室
    // this._getUserGroup()
    // 检测依据
    // this._getTestBasisList()
    // 资质
    // this._getAptitudeList()
    // this._getType()
  },
  methods: {
    groupSelect(data) {
      console.log(data)
      // this.formObj.groupId = da
    },
    _selGroup(data) {
      console.log(data)
      this.formObj.groupId = data
    },
    _selectPerson() {
      this.$refs.userModal._openOrg('选择制表人', 'itemTree')
    },
    _userResult(data, msg) {
      this.$forceUpdate()
      this.formObj.receiver = data.realname
      this.formObj.receiverId = data.id
    },
    _tabulateChange(data) {
      this.formObj.receiveTime = data
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _open(sampleIds) {
      this.formObj.receiveLocation = undefined
      this.formObj.seniorType = undefined
      this.formObj.receiveTime = undefined
      this.formObj.receiver = undefined
      this.formObj.receiverId = undefined
      this.formId = 'index-edit-form'
      this.sampleIds = sampleIds
      this._hideLoading()
      this._getTestType()
      this.$refs.formObj.resetFields()
      this.hideUserSel = true
      this.modalTitle = '编辑'
      this.showModal = true
    },
    // 获取检测依据
    _getStandardList: async function() {
      const result = await soilAptitude.getStandardList()
      if (result) {
        this.options = result
      }
    },

    _getTestType: async function() {
      const result = await soilEntrust.testType('高级分类')
      console.log(result)
      if (result) {
        this.groupoptions = result
      }
    },
    // 检测项目结果
    _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
      }
    },
    _clearMarks() {
      this.marksList = []
    },
    // 获取表数据
    _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.receiverId = this.formObj.receiverId
          console.log('index-edit-form', data)
          this._save({ ids: this.sampleIds, obj: data })
        } else {
          this.$refs.footerModal._hideLoading()
          this.$Message.error('表单验证失败!')
        }
      })
    },
    _save: async function(data) {
      console.log(data)
      const result = await soilAptitude.editSeniorSample(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()
    },
    _visibleChange(data) {
      this.$emit('on-result-change')
    }
  }
}
</script>
