<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24">
        <Form id="task-assign-item-left" :label-width="70" inline onsubmit="return false">
        <label class="label-sign"></label>
        <Form-item class="search-item" label="检测项目:">
          <Input v-model="formObj.name" @on-enter="_formSearch" placeholder="请输入检测项目" clearable></Input>
        </Form-item>
        <Form-item class="search-btn">
          <Button @click="_formSearch" type="primary">搜索</Button>
        </Form-item>
      </Form>
      </Col>
      <Col span="24">
      <btn-list @on-result-change="_btnClick" :msg="btn" class="contHide" style="margin-bottom: 6px;"></btn-list>
      </Col>
      <!-- 表格 -->
      <Col span="24">
      <PTVXETable ref="pageTable" :page-columns="pageColumns"  :table-name="tableName"
                  :table-height="tableHeight"
                    :get-page="getPage" :icon-msg="iconMsg" @on-result-change="_tableResultChange"  select-data>
        <vxe-table-column
          v-for="item in userColumns.length > 0 ?userColumns:pageColumns"
          :key="item.key"
          :field="item.key"
          :title="item.title"
          :min-width="item.width"
          :fixed="item.fixed?item.fixed:undefined"
          sortable>
          <template slot-scope="scope">
            <a v-if="item.detail" @click.stop="_detailModal(scope.row)">{{scope.row[item.key]}}</a>
            <span v-else-if="item.date">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):''}}</span>
            <span v-else>{{scope.row[item.key]}}</span>
          </template>
        </vxe-table-column>
        <VXESettingCol slot="setting" :page-columns="pageColumns" :user-columns="userColumns"
                       :table-name="tableName" @on-result-change="_resetColumn"></VXESettingCol>
      </PTVXETable>
      </Col>
    </Row>
    <CreateItemReport ref="createModal" @on-result-change="_certificateSelectExcelBack"></CreateItemReport>
    <CreateBatchItemReport ref="createBatchModal" @on-result-change="_templateBack"></CreateBatchItemReport>
  </div>
</template>
<script>
import { soilSample, soilTest } from '../../../../api'
import CreateItemReport from './CreateItemReport'
import CreateBatchItemReport from './CreateBatchItemReport'
export default {
  components: { CreateItemReport, CreateBatchItemReport },
  props: {
    rightSelectData: null
  },
  data() {
    return {
      currentComponent: '',
      formObj: {
        name: undefined
      },
      formObjSample: {
        name: undefined,
        entrustId: undefined
      },
      formObjBatchSample: {
        obj: {
          entrustId: '',
          nameList: []
        }
      },
      sampleIds: [],
      batchSampleIds: [],
      itemName: '',
      tableName: 'food-task-assign-item-left',
      // 用户自己选中的列
      userColumns: [],
      selectDataName: [],
      optionList: [
        { key: 'name', name: '检测项目', placeholder: '请输入检测项目' },
        { key: 'testBasis', name: '检测依据', placeholder: '请输入检测依据' }
      ],
      getPage: {},
      iconMsg: [{ type: 'ios-book', id: '', name: '生成项目报告' }],
      btn: [
        {
          type: 'success',
          id: '',
          name: '生成项目报告'
        }
      ],
      id: '',
      selectId: '',
      pageColumns: [{ title: '检测项目', key: 'name', width: 120 }]
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tabSearch')
    }
  },
  methods: {
    // 重置column
    _resetColumn(colList) {
      this.userColumns = colList
      this.$refs.pageTable._loadColumn(colList)
    },
    _modalResult() {
      if (this.currentComponent === 'AutoAssignModal') {
        this._formSearch()
      }
    },
    _selInputResult1(msg, data) {
      switch (msg) {
        case 'search':
          this._formSearch()
          break
      }
    },
    _open(id) {
      this.id = id
      this.selectId = ''
      this._page()
    },
    _searchParams() {
      const obj = {}
      const obj1 = this.$refs.selInput1._getFormObj()
      Object.assign(obj, obj1)
      return obj
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      this.formObj.entrustId = this.id
      const result = await soilTest.pageTestByExp(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _tableResultChange(msg, data) {
      console.log(msg, data)
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectData':
          this.selectDataName = []
          for (let i = 0; i < data.length; i++) {
            this.selectDataName.push(data[i].name)
          }
          console.log('selectDataName', this.selectDataName)
          this.$emit('on-result-change', this.selectDataName)
          break
        case 'iconClick':
          const selcetName = []
          selcetName.push(data.rowData.name)
          if (this.rightSelectData.length === 0) {
            this.$emit('on-result-change', selcetName)
          }
          this._iconClick(data.name, data.rowData, data.componentName)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _btnClick(msg) {
      switch (msg) {
        case '生成项目报告':
          // this.$emit('on-result-change', data)
          this._itemReportMakeBatch()
          break
        case '自动分配':
          this._autoAssign()
          break
      }
    },
    _iconClick(res, data, currentComponent) {
      this.$nextTick(() => {
        switch (res) {
          case '生成项目报告':
            // this.$emit('on-result-change', data)
            this._itemReportMake(data)
            break
        }
      })
    },
    _itemReportMakeBatch() {
      if (this.selectDataName.length > 0) {
        if (this.rightSelectData.length > 0) {
          // this.itemName = data.name
          this.batchSampleIds = this.rightSelectData
        } else {
          // console.log('12345767')
          // this.itemName = data.name
          this._searchBatchSampleId(this.id, this.selectDataName)
        }
        // this.itemName = data.name
        // this.selectId = data.id
        this.$refs.createBatchModal._open()
      } else {
        this.$Message.warning('请至少选择一条数据')
      }
    },
    _itemReportMake(data) {
      if (this.rightSelectData.length > 0) {
        this.itemName = data.name
        this.sampleIds = this.rightSelectData
        console.log('点击生成报告', this.rightSelectData)
      } else {
        // console.log('12345767')
        this.itemName = data.name
        this._searchSampleId(this.id, data.name)
      }
      // this.selectId = data.id
      this.$refs.createModal._open()
    },
    _searchBatchSampleId: async function(id, name) {
      Object.assign(
        this.formObjBatchSample,
        this.$refs.pageTable._searchParams()
      )
      this.formObjBatchSample.obj.entrustId = id
      this.formObjBatchSample.obj.nameList = name
      const result = await soilTest.pageExperimentTestBath(
        this.$serializeForm(this.formObjBatchSample)
      )
      if (result) {
        this.batchSampleIds = []
        for (let i = 0; i < result.records.length; i++) {
          this.batchSampleIds.push(result.records[i].id)
        }
        console.log('右侧样品id', this.batchSampleIds)
        this.$refs.pageTable._hideLoading()
      } else {
        this.$refs.pageTable._hideLoading()
      }
    },
    _searchSampleId: async function(id, name) {
      Object.assign(this.formObjSample, this.$refs.pageTable._searchParams())
      this.formObjSample.entrustId = id
      this.formObjSample.name = name
      this.$refs.pageTable._hideLoading()
      const result = await soilTest.pageExperimentTest(
        this.$serializeForm(this.formObjSample)
      )
      if (result) {
        console.log(result)
        this.sampleIds = []
        for (let i = 0; i < result.records.length; i++) {
          this.sampleIds.push(result.records[i].id)
        }
        console.log('右侧样品id', this.sampleIds)
      }
    },
    _templateBack(data) {
      if (data) {
        this.$refs.pageTable._showLoading()
        this._makeBatchCodeExcel(data)
      } else {
        this.$refs.pageTable._hideLoading()
      }
    },
    _certificateSelectExcelBack(data) {
      if (data) {
        this.$refs.pageTable._showLoading()
        this._makeCodeExcel(data)
      } else {
        this.$refs.pageTable._hideLoading()
      }
    },
    _makeBatchCodeExcel: async function(data) {
      const result = await soilSample.saveExcelExpReport({
        entrustId: this.id,
        expName: this.selectDataName.join(','),
        ids: this.batchSampleIds.join(','),
        templateId: data.id
      })
      if (result) {
        // await this._page()
        if (result === null || result === undefined) {
          this.$Message.warning('证书编制失败，请联系管理员！')
          this.$refs.pageTable._hideLoading()
          return false
        } else {
          this.$refs.pageTable._hideLoading()
          this._viewReport(result)
        }
      } else {
        this.$refs.pageTable._hideLoading()
      }
    },
    _makeCodeExcel: async function(data) {
      const result = await soilSample.saveExcelExpReport({
        entrustId: this.id,
        expName: this.itemName,
        ids: this.sampleIds.join(','),
        templateId: data.id
      })
      if (result) {
        // await this._page()
        if (result === null || result === undefined) {
          this.$Message.warning('证书编制失败，请联系管理员！')
          this.$refs.pageTable._hideLoading()
          return false
        } else {
          this.$refs.pageTable._hideLoading()
          this._viewReport(result)
        }
      } else {
        this.$refs.pageTable._hideLoading()
      }
    },
    _viewReport(data) {
      if (data) {
        this.$openWindowModeless({
          objectKey: data.objectKey,
          idType: 10,
          id: data.id,
          isReport: 4
        })
      }
    },
    _autoAssign() {
      console.log('自动分配')
    }
  }
}
</script>
