<template>
  <div>
    <Form id="pie-search-form">
      <input name="beginDate" type="hidden">
      <input name="endDate" type="hidden">
      <input name="warningDate" type="hidden">
      <input name="type" type="hidden">
    </Form>
    <PTVXETable id="reportTimelyTable" ref="pageTable" :page-columns="pageColumns"
                :table-height="tableHeight" :get-page="getPage" select-data @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>{{scope.row[item.key]}}</span>
        </template>
      </vxe-table-column>
    </PTVXETable>
  </div>
</template>

<script>
import { meterSample } from '../../../api'
export default {
  data() {
    return {
      loading: true,
      formObj: {
        client: undefined,
        edateBegin: undefined,
        edateEnd: undefined
      },
      pageColumns: [
        { title: '样品编号', key: 'code', width: 180 },
        { title: '委托单位', key: 'client' },
        { title: '委托编号', key: 'entrustCode', width: 180 },
        { title: '应出证书日期', key: 'odate', width: 120, date: true },
        { title: '签发日期', key: 'reportIssueTime', width: 120, date: true }
      ],
      getPage: {},
      selectData: []
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('', 230)
    }
  },
  mounted() {
    // this._page()
  },
  methods: {
    // 日期变化时，将时间赋值，导出根据查询条件
    _changeDate(data) {
      console.log(data)
      $('input[name=beginDate]').val(data.beginDate)
      $('input[name=endDate]').val(data.endDate)
      $('input[name=type]').val('')
    },
    _openTable(data) {
      $('input[name=beginDate]').val(data.beginDate)
      $('input[name=endDate]').val(data.endDate)
      $('input[name=warningDate]').val(data.warningDate)
      $('input[name=type]').val('')
      // this._page()
    },
    _searchTable(data) {
      $('input[name=beginDate]').val(data.beginDate)
      $('input[name=endDate]').val(data.endDate)
      $('input[name=type]').val('')
      this.formObj.edateBegin = data.beginDate
      this.formObj.edateEnd = data.endDate
      this._page()
    },
    _pieOpen(res) {
      $('input[name=type]').val(res)

      // this._page()
    },
    // _page() {
    //   this.$refs.pageTable._page('pie-search-form', 'FoodSample/reportTimely')
    // },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      console.log('要传的参数', this.formObj)
      const result = await meterSample.pageCertificateRateSample(this.formObj)
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
        console.log('请求结果为', result)
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.getPage = this.$store.state.FoodSample.page
          break
        case 'selectData':
          this.selectData = data
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    /** *********************************导出-begin*********************************************/
    _exportSampleForm() {
      let result = []
      if (this.selectData.length === 0) {
        // 导出全部数据
        this.$Modal.confirm({
          title: '提示',
          content:
            this.selectData.length === 0
              ? '确定导出全部数据？'
              : '确定导出这' + result.length + ' 条数据？',
          onOk: () => {
            this.$store
              .dispatch('FoodSample/reportTimely', this._searchParams())
              .then(() => {
                result = this.$store.state.FoodSample.page.records
                this.$exportExcel(
                  'reportTimelyTable',
                  '报告及时率统计表',
                  this.pageColumns,
                  result
                )
              })
          }
        })
      } else {
        // 只导出选中的数据
        result = this.selectData
        this.$Modal.confirm({
          title: '提示',
          content:
            this.selectData.length === 0
              ? '确定导出全部数据？'
              : '确定导出这' + result.length + ' 条数据？',
          onOk: () => {
            this.$exportExcel(
              'reportTimelyTable',
              '报告及时率统计表',
              this.pageColumns,
              result
            )
          }
        })
      }
    },
    // 参数
    _searchParams() {
      const data = this.$serialize('pie-search-form')
      return this.$extend(data, { page: 1, rows: 5000 })
    }
    /** *********************************导出样品台账-end*********************************************/
  }
}
</script>
