<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24">
        <Form id="task-assign-item-right" :label-width="70" inline onsubmit="return false">
          <label class="label-sign"></label>
          <Form-item class="search-item" label="试样编号:">
            <Input v-model="formObj.specimenNum" @on-enter="_formSearch" name="specimenNum" 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 :msg="btn" @on-result-change="_btnClick" class="contHide"></btn-list>
      </Col>
      <!-- 表格 -->
      <Col span="24">
        <PTVXETable ref="pageTable" :page-columns="pageColumns" :table-height="tableHeight"
                    :get-page="getPage" :icon-msg="iconMsg" :table-name="tableName" @on-result-change="_tableResultChange"  is-task 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">
              <div v-if="item.key === 'name'" :style="{color:colorComputed(scope.row.planEndDate)}">
                {{scope.row[item.key]}}
              </div>
              <a v-else-if="item.key==='code'" @click.stop="_detailModal(scope.row)">{{scope.row[item.key]}}</a>
              <a v-else-if="item.key==='num'" @click.stop="_sampleDetail(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-if="item.datetime">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM'):''}}</span>
              <span v-else-if="item.key==='progress'">{{scope.row[item.key].display}}</span>
              <span v-else>{{scope.row[item.key]}}</span>
            </template>
          </vxe-table-column>
        </PTVXETable>
      </Col>
    </Row>
  </div>
</template>
<script>
import { soilReport, soilTest } from '../../../api'
export default {
  components: {},
  data() {
    return {
      // 定义表格名称----英文
      tableName: 'food-task-assign-item-right',
      formObj: {
        entrustId: '',
        name: '',
        testMethod: '',
        testBasis: '',
        specimenNum: undefined,
        obj: {
          entrustId: '',
          nameList: [],
          sampleCode: undefined
        }
      },
      entrustId: '',
      envPro: false,
      // 用户自己选中的列
      userColumns: [],
      currentComponent: '',
      btn: [],
      iconMsg: [
        {
          type: 'ios-book',
          id: '',
          name: '查看原始记录'
        }
      ],
      getPage: {},
      pageColumns: [
        { title: '试验项目', key: 'name', width: 120, fixed: 'left' },
        { title: '缩写', key: 'shortName', width: 90 },
        { title: '钻孔编号', key: 'boreholeName', width: 120 },
        { title: '现场编号', key: 'siteNo', width: 110 },
        { title: '试样编号', key: 'sampleCode', width: 140 },
        { title: '原始记录填写', key: 'recorded', width: 120 },
        { title: '试样深度', key: 'sampleDepth', width: 110 },
        { title: '土质描述', key: 'describeDetail', width: 110 },
        { title: '含水率', key: 'haveWater', width: 110 },
        { title: '密度', key: 'density', width: 110 },
        { title: '土粒比重', key: 'bulkDensity', width: 110 },
        { title: '检测方法', key: 'testMethod', width: 140 },
        { title: '检测依据', key: 'testBasis', width: 140 },
        { title: '试样包装类型', key: 'samplePack', width: 160 },
        { title: '检测科室', key: 'groupName', width: 150 },
        { title: '状态', key: 'progress', width: 180 },
        { title: '大类', key: 'mainType', width: 140 },
        { title: '小类', key: 'smallType', width: 140 }
      ],
      leftSelectData: [],
      selectIds: [], // 检测项目id
      selectData: [], // 检测项目data
      selectSampleIds: [], // 样品ids
      result: [],
      sampleNames: '', // 没有计划完成时间的样品名
      defaultPlanDate: null, // 是否有字典
      warningValue: ''
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tabSearch')
    },
    colorComputed() {
      return function(val) {
        return val - new Date().getTime() > this.warningValue * 86400000
          ? '#606266'
          : val - new Date().getTime() <= 0
            ? '#eb6877'
            : '#f90'
      }
    }
  },
  mounted() {
    // this._dicSearch()
    if (process.env.NODE_ENV === 'production') {
      this.envPro = true
    }
  },
  methods: {
    _iconClick(res, data, currentComponent) {
      this.$nextTick(() => {
        switch (res) {
          case '查看原始记录':
            this._reportView(data)
            break
        }
      })
    },
    _reportView(data) {
      if (data.recorded === '是') {
        if (data.objectKey) {
          this._reportMakeLook(data)
        } else {
          this.$refs.editModal._openWithType(data.recordId, 'ENVTESTMAKEEDIT')
        }
      } else {
        this.$Message.warning('未填写原始记录')
      }
    },
    _reportMakeLook: async function(data) {
      const result = await soilReport.getExcelOriginalRecord(data.id)
      if (result) {
        this._viewReport(result)
      }
    },
    _viewReport(data) {
      this.$refs.pageTable._hideLoading()
      if (data) {
        this.$openWindowModeless({
          objectKey: data.objectKey,
          idType: 10,
          id: data.id,
          isReport: 4
        })
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '填写原始记录':
          this._addRecord()
          break
      }
    },
    _getEntrustId(id) {
      this.entrustId = id
    },
    _open(id, data) {
      this.leftSelectData = data
      this.entrustId = id
      if (data.length === 0) {
        this.$set(this.getPage, 'records', [])
        this.$set(this.getPage, 'total', 0)
        this.$set(this.getPage, 'current', 1)
        this.selectSampleIds = []
        this.selectIds = []
        this.selectData = []
        this.result = []
      } else {
        this._formSearch()
      }
    },
    _refresh() {
      this.$emit('on-result-change')
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      this.formObj.obj.nameList = []
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      this.formObj.obj.entrustId = this.entrustId
      this.formObj.obj.nameList = this.leftSelectData
      this.formObj.obj.sampleCode = this.formObj.specimenNum
      // this.formObj.entrustId = this.entrustId
      // this.formObj.nameList = this.leftSelectData.join(',')
      const result = await soilTest.pageDocumentTestBath(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.$refs.pageTable._checkAll()
        this.getPage = result
      }
    },
    _tableResultChange(msg, data) {
      const selectIds = []
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData, data.currentComponent)
          break
        case 'selectData':
          for (let i = 0; i < data.length; i++) {
            selectIds.push(data[i].id)
          }
          this.selectIds = selectIds
          this.selectData = data
          this.$emit('child-data', data)
          break
        case 'table-col':
          // 用户选中的表格列
          this.userColumns = data
          break
        case 'changeSize':
          this._page()
          // this.$refs.pageTable._checkAll()
          break
      }
    },
    _clearAll() {
      this.getPage.records = []
      this.selectIds = []
      this.formObj.specimenNum = undefined
    }
  }
}
</script>
