<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-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" @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>
              <vxe-table-column
                slot="col"
                :width="80"
                title="操作"
                align="center"
                fixed="right">
                <template slot-scope="scope">
                  <VXEIconList :msg="editIndex===scope.rowIndex?iconMsgSave:iconMsg" @on-result-change="_iconClick" :rowData="scope.row" :rowIndex="scope.rowIndex"></VXEIconList>
                </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: {
        client: '',
        entrustCode: '',
        sampleCode: ''
      },
      iconMsg: [
        {
          type: 'ios-clock',
          id: '',
          name: '操作日志'
        }
      ],
      btn: [
        // { type: '', id: '', name: '导出' },
      ],
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 150 },
        { title: '状态', key: 'status', status: true, width: 100 },
        { title: '包装类型', key: 'samplePack', width: 100 },
        { title: '保存容器', key: 'keepContainer', width: 100 },
        { title: '制备数量', key: 'quantity', width: 100 },
        { title: '单位', key: 'unit', width: 70 },
        { title: '制备人', key: 'preparer', width: 130 },
        { title: '制备时间', key: 'prepareDate', date: true, width: 130 }
      ],
      searchOpen: false,
      getPage: {},
      selectData: [],
      endDateBegin: '',
      endDateEnd: ''
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 255)
      } else {
        return this.$tableHeight('', 220)
      }
    }
  },
  methods: {
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      console.log('this.formObj', this.formObj)
      const result = await soilSample.pageSurplusHis(
        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._openSurplus(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>
