<template>
  <div>
    <Modal v-model="showModal" :mask-closable="false" width="700">
      <p slot="header">分包信息</p>
      <div>
        <Form id="batch-edit-form" ref="formObj" :model="formObj" :rules="ruleValidate" :label-width="90">
          <Form-item label="分包价格：" prop="outFee">
            <InputNumber v-model.number="formObj.outFee" :min="0" :step="1"
                         name="outFee" style="width:100%" placeholder="">
            </InputNumber>
          </Form-item>
          <Form-item label="分包实验室：" prop="subcontractor">
            <div style="display: flex">
              <AutoCompletes :value="formObj.subcontractor" :down-data="labList" name="subcontractorName"
                             show-key="name"
                             placeholder="请输入或选择分包实验室" style="width: 94%" @on-result-change="_labChange"></AutoCompletes>
              <Icon type="ios-arrow-forward" size="24" color="#00a0e9"  style="margin-top: 5px;margin-left: 5px" @click="_labSel"/>
            </div>
            <input v-model="formObj.subcontractorId" type="hidden" name="subcontractorId"/>
          </Form-item>
          <Form-item label="分包原因" prop="outReason">
            <Input v-model="formObj.outReason" :rows="3" type="textarea"
                   name="outReason"
                   placeholder="请输入分包原因"/>
          </Form-item>
        </Form>
      </div>
      <div slot="footer">
        <ModalFooter ref="footerModal" :footer="footerList" @on-result-change="_footerResult"></ModalFooter>
      </div>
    </Modal>
    <!--选择实验室-->
    <EditLabSelect ref="labModal" @on-result-change="_labResult"></EditLabSelect>
  </div>
</template>
<script>
import ModalFooter from '../../../components/base/modalFooter'
import AutoCompletes from '../../../components/base/AutoCompletes'
import { meterSample, meterSubcontractor } from '../../../api'
import EditLabSelect from './EditLabSelect'
/**
 * 添加编辑分包信息
 */
const defVal = {
  subcontractor: undefined,
  subcontractorId: undefined,
  outReason: undefined,
  outFee: undefined
}
export default {
  components: {
    EditLabSelect,
    ModalFooter,
    AutoCompletes
  },
  data() {
    return {
      ids: [],
      selectData: [],
      formObj: defVal,
      ruleValidate: {
        // price: [{required: true, message: '分包价不能为空', trigger: 'blur', type: 'number'}],
      },
      showModal: false,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '保存', type: 'primary' }
      ],
      labList: []
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '保存':
          this._ok()
          break
      }
    },
    _hideLoading() {
      this.$refs.footerModal._hideLoading()
    },
    _ok() {
      console.log('_ok----', this.formObj)
      console.log('_ok----', this.ids)
      this._subpackageInfoReg({ ids: this.ids, reg: this.formObj })
      this._hideLoading()
    },

    _subpackageInfoReg: async function(data) {
      const result = await meterSample.subpackageInfoReg(data)
      if (result) {
        this.$Message.success('登记成功！')
        this._hideLoading()
        this.$emit('on-result-change')
        this.showModal = false
      }
    },
    _cancel() {
      this.showModal = false
    },
    _open(ids, selectData) {
      this.formObj = {
        subcontractor: undefined,
        subcontractorId: undefined,
        outReason: undefined,
        outFee: undefined
      }
      this._getLab()
      this._hideLoading()
      this.$refs.formObj.resetFields()
      this.formObj.subcontractorId = ''
      this.showModal = true
      this.ids = ids
      this.selectData = selectData
    },
    _labChange(msg, data) {
      switch (msg) {
        case 'select':
          this.formObj.subcontractor = data.name
          this.formObj.subcontractorId = data.id
          break
        case 'query':
          this.formObj.subcontractor = data.name
          this._getLab(data.name)
          break
      }
    },
    _getLab: async function(name) {
      const data = {}
      if (name) {
        data.name = name
      }
      // this.$store.dispatch('FoodSubcontracter/getLab', data).then(() => {
      //   this.labList = this.$store.state.FoodSubcontracter.list
      // })
      // Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      // console.log('this.formObj', this.formObj)
      const result = await meterSubcontractor.page(data)
      console.log('实验室名称', result)
      if (result) {
        console.log(result.records)
        this.labList = result.records
        console.log(this.labList)
      }
    },
    _labSel() {
      const voList = []
      for (let i = 0; i < this.selectData.length; i++) {
        voList.push({
          itemName: this.selectData[i].itemName,
          testBasis: this.selectData[i].testBasis
        })
      }
      this.$refs.labModal._open(voList, 'batch-lab-form')
    },
    _labResult(result) {
      this.formObj.subcontractorId = result.id
      this.formObj.subcontractor = result.name
    }
  }
}
</script>
