<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-form-query" :label-width="80" v-show="searchOpen" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="search-item" label="试样编号:">
                <Input @on-enter="_formSearch" v-model="formObj.sampleCode" name="sampleCode" placeholder="请输入样品编号" clearable/>
              </Form-item>
              <Form-item class="search-item" label="委托单位:" style="margin-left: 8px;">
                <Input @on-enter="_formSearch" v-model="formObj.client" name="client" placeholder="请输入委托单位" clearable/>
              </Form-item>
              <Form-item class="search-item" label="委托编号:" style="margin-left: 8px;">
                <Input @on-enter="_formSearch" v-model="formObj.entrustCode" name="entrustCode" placeholder="请输入委托单位" clearable/>
              </Form-item>
              <Form-item class="search-btn">
                <Button @click="_formSearch" type="primary">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" @on-result-change="_btnClick" :showSearchBtn="true"
                     class="contHide"></btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETableHeight ref="pageTable" :table-height="tableHeight" :loading="true"
                        :get-page="getPage" :icon-msg="iconMsg" @on-result-change="_tableResultChange">
              <vxe-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :field="item.key"
                :title="item.title"
                :min-width="item.width?item.width:200"
                :fixed="item.fixed?item.fixed:undefined" sortable>
                <template slot-scope="scope">
                  <span v-if="item.date">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):''}}</span>
                  <span v-else-if="item.status">{{scope.row[item.key].display}}</span>
                  <span v-else>{{scope.row[item.key]}}</span>
                </template>
              </vxe-table-column>
            </PTVXETableHeight>
          </Col>
        </Row>
      </div>
    </div>
    <Operation ref="recordModal"></Operation>
  </div>
</template>
<script>
import { soilSample } from '../../../../api'
import Operation from '../../../../components/operation/Operation'
export default {
  components: { Operation },
  data() {
    return {
      currentComponent: '',
      selectIds: [],
      formObj: {
        entrustCode: '',
        client: '',
        sampleCode: ''
      },
      btn: [
        // { type: '', id: '', name: '导出' },
      ],
      iconMsg: [
        {
          type: 'ios-clock',
          id: '',
          name: '操作日志'
        }
      ],
      pageColumns: [
        { title: '委托商', key: 'client', width: 180 },
        { title: '委托编号', key: 'entrustCode', width: 180 },
        { title: '试样编号', key: 'sampleCode', width: 180 },
        { title: '状态', key: 'status', width: 160, status: true },
        { title: '试样深度', key: 'sampleDepth', width: 180 },
        { title: '样品包装类型', key: 'samplePack', width: 180 },
        { title: '现场编号', key: 'siteNo', width: 180 },
        { title: '存储条件', key: 'conditions', width: 180 },
        { title: '备样位置', key: 'backupLocation', width: 180 },
        { title: '备样数量', key: 'quantity', width: 180 },
        { title: '备样人', key: 'backupUser', width: 140 },
        { title: '钻孔位置', key: 'boreholeLocation', width: 110 },
        { title: '钻孔名称', key: 'boreholeName', width: 160 },
        { title: '水深（米）', key: 'waterDepth', width: 160 }
      ],
      searchOpen: false,
      getPage: {},
      selectData: [],
      endDateBegin: '',
      endDateEnd: ''
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 250)
      } else {
        return this.$tableHeight('', 210)
      }
    }
  },
  methods: {
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilSample.pageBackupHis(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.selectIds = []
          this.getPage = this.$store.state.FoodSampleBackup.page
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData, data.componentName)
          break
        case 'selectData':
          this.selectData = data
          this.selectIds = []
          for (let i = 0; i < data.length; i++) {
            this.selectIds.push(data[i].id)
          }
          break
        case 'changeSize':
          this._page()
          break
        // default :
        //   this._page();
      }
    },
    _iconClick(res, data, componentName, index) {
      this.currentIndex = index
      switch (res) {
        case '操作日志':
          this._record(data.id)
          break
        default:
          this._page()
      }
    },
    _record(id) {
      this.$refs.recordModal._openBcakup(id)
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _dateChange(data) {
      this.endDateBegin = data[0]
      this.endDateEnd = data[1]
    },
    _btnClick(msg) {
      switch (msg) {
        case '导出':
          if (this.getPage.records.length === 0) {
            this.$Message.warning('暂无数据，不可导出')
          } else {
            this._export()
          }
          break
        case '导出样品贮存表':
          this._exportSampleStore()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _exportSampleStore() {
      const obj = {
        param: 'ids',
        ids: this.selectIds,
        url: '',
        queryObj: this.$serialize('search-form-query')
      }
      this.$store
        .dispatch(
          'ReportExport/getById',
          'food-sample-handle-already-export-sample-store'
        )
        .then(() => {
          const result = this.$store.state.ReportExport.model
          obj.url = result
          this.$exportByQuery(obj)
        })
    },
    // 样品详情
    _sampleDetailModal(data) {
      if (data.type === 1) {
        // 政府
        this.currentComponent = 'FoodSampleGovernDetail'
        this.$nextTick(() => {
          this.$store
            .dispatch('FoodSample/getByGovernId', data.sampleId)
            .then(() => {
              this.$refs.refModal._open(
                this.$store.state.FoodSample.governModel
              )
            })
        })
      } else {
        this.currentComponent = 'FoodSampleCompanyDetail'
        this.$nextTick(() => {
          this.$store
            .dispatch('FoodSample/getByCompanyId', data.sampleId)
            .then(() => {
              this.$refs.refModal._open(
                this.$store.state.FoodSample.companyModel
              )
            })
        })
      }
    },
    /** *********************************导出-begin*********************************************/
    _export() {
      let result = []
      if (this.selectData.length === 0) {
        // 导出全部数据
        this.$store
          .dispatch('FoodSampleBackup/perSampleQuery', this._searchParams())
          .then(() => {
            result = this.$store.state.FoodSampleBackup.page.records
            this.$Modal.confirm({
              title: '提示',
              content:
                this.selectData.length === 0
                  ? '确定导出全部数据（最多导出5000条）？'
                  : '确定导出这' + result.length + ' 条数据？',
              onOk: () => {
                this.$exportExcel(
                  'perSampleQuery',
                  '样品处理',
                  this.pageColumns,
                  result
                )
              }
            })
          })
      } else {
        // 只导出选中的数据
        result = this.selectData
        this.$Modal.confirm({
          title: '提示',
          content:
            this.selectData.length === 0
              ? '确定导出全部数据（最多导出5000条）？'
              : '确定导出这' + result.length + ' 条数据？',
          onOk: () => {
            this.$exportExcel(
              'perSampleQuery',
              '样品处理',
              this.pageColumns,
              result
            )
          }
        })
      }
    },
    // 参数
    _searchParams() {
      const data = this.$serialize('search-form-query')
      return this.$extend(data, { page: 1, rows: 5000 })
    }
    /** *********************************导出样品台账-end*********************************************/
  }
}
</script>
