<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-form-report-tab" :label-width="70" v-show="searchOpen" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="search-item" label="委托单位:">
                <Input @on-enter="_formSearch" name="client" placeholder="请输入委托单位" clearable/>
              </Form-item>
              <Form-item class="search-item" label="委托编号:">
                <Input @on-enter="_formSearch" 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" :showSearchBtn="true" @on-result-change="_btnClick" class="contHide">
            </btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable
              ref="pageTable"
              :getPage="getPage"
              :iconMsg="iconMsg"
              @on-result-change="_tableResultChange"
              >
              <vxe-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :field="item.key"
                :title="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
                sortable
              >
                <template slot-scope="scope">
                  <div v-if="item.detail">
                  </div>
                  <div v-else-if="item.key==='reportDueDate'">
                    <div v-if="scope.row.progress === '结束'">
                      {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):''}}
                    </div>
                  </div>
                  <div v-else-if="item.status">
                    {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
                  </div>
                  <div v-else-if="item.date">
                    {{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'): ''}}
                  </div>
                  <div v-else-if="item.type">{{scope.row[item.key] === 0 ? '企业':scope.row[item.key] ===
                    1?'政府':scope.row[item.key] === 2?'食品类抽样单': ''}}
                  </div>
                  <div v-else>{{scope.row[item.key]}}</div>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </div>
    <!--组件加载-->
    <Operation ref="recordModal"></Operation>
    <keep-alive>
      <!-- eslint-disable-next-line vue/require-component-is -->
      <component ref="refModal" :is="currentComponent"
                 :exportSendRecords="exportSendRecords"
                 :reportStatus="reportStatus"
                 :formObj="formObj"
                 @on-result-change="_modalChange"></component>
    </keep-alive>
  </div>
</template>
<script>
import { soilReport } from '../../../../api'
import Operation from '../../../../components/operation/Operation'
import SampleByMakeModal from '../../report-manage/SampleByMakeModal'
import OriginalRecordView from '../OriginalRecordView'
export default {
  // eslint-disable-next-line vue/no-unused-components
  components: { Operation, SampleByMakeModal, OriginalRecordView },
  props: {
    reportStatus: null
  },
  data() {
    return {
      currentComponent: '',
      typeList: [{ name: '企业', value: 0 }, { name: '政府', value: 1 }],
      getPage: {},
      pageColumns: [
        {
          title: '委托商',
          key: 'client',
          width: 100,
          fixed: 'left'
        },
        { title: '委托编号', key: 'entrustCode', width: 100 },
        { title: '报告编号', key: 'reportCode', width: 100 },
        { title: '进度', key: 'progress', width: 110, status: true },
        { title: '状态', key: 'status', width: 100, status: true },
        { title: '委托日期', key: 'entrustDate', width: 100, date: true },
        { title: '钻孔位置', key: 'boreholeLocation', width: 100 },
        { title: '钻孔名称', key: 'boreholeName', width: 100 },
        { title: '水深', key: 'waterDepth', width: 100 },
        { title: '制表人', key: 'tabulater', width: 110 },
        { title: '制表日期', key: 'tabulateDate', width: 110, date: true }
      ],
      iconMsg: [
        // {
        //   type: 'ios-list',
        //   id: '',
        //   name: '资料管理',
        //   componentName: 'ErecordIndex'
        // },
        {
          type: 'ios-flask-outline',
          id: '',
          name: '管理样品和报告'
        },
        {
          type: 'ios-beaker',
          id: '',
          name: '查看原始记录'
        },
        { type: 'ios-cloud', id: '', name: '附件' },
        {
          type: 'ios-clock',
          id: '',
          name: '操作记录'
        }
      ],
      btn: [
        {
          type: 'primary',
          id: 'food-report-manage-contract-push-to-gc',
          name: '推送至国抽平台'
        },
        {
          type: '',
          id: 'food-report-manage-export-send-record',
          name: '导出发放记录'
        },
        {
          type: '',
          id: 'food-report-manage-export-send-form',
          name: '导出发放记录表'
        },
        {
          type: '',
          id: 'food-report-manage-export-report-collect',
          name: '导出报告汇总统计表'
        }
      ],
      searchOpen: false,
      selectIds: [],
      formObj: {
        reportSn: '',
        num: ''
      },
      // 导出发放记录与统计表区分，false--统计表，true--统计表
      exportSendRecords: false
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 150)
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _modalChange(data) {
      switch (this.currentComponent) {
        case 'SelectReportExportModal':
          if (this.exportSendRecords) {
            // 导出发放记录
            this._exportSendRecordsForm(data)
          } else {
            // 导出报告汇总统计表
            this._exportReportCollect(data)
          }
          break
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '导出发放记录':
          this.currentComponent = 'GrantRecordModal'
          this.$nextTick(() => {
            this.$refs.refModal._open()
          })
          break
        case '推送至国抽平台':
          this._pushToGC()
          break
        case '导出发放记录表':
          this.exportSendRecords = true
          this.currentComponent = 'SelectReportExportModal'
          this._exportSendForm()
          break
        case '导出报告汇总统计表':
          this.exportSendRecords = false
          this.currentComponent = 'SelectReportExportModal'
          this._selectTempExport()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    // 导出发放记录表
    _exportSendForm() {
      this.$nextTick(() => {
        this.$refs.refModal._open('food-report-manage-export-send-form')
      })
    },
    // 导出发放记录表
    _exportSendRecordsForm(result) {
      const obj = {
        param: 'ids',
        ids: this.selectIds,
        url: '',
        queryObj: this.$serialize('search-form-report-tab')
      }
      obj.url = this.$judgeUrlChar(result) + '_n=' + '导出发放记录表'
      // position，标识当前导出菜单位置用
      this.$exportByQuery(obj, { position: 3 })
    },
    // 一个按钮对应多个报表
    _selectTempExport() {
      this.$nextTick(() => {
        this.$refs.refModal._open('food-report-manage-export-report-collect')
      })
    },
    // 导出报告汇总统计表
    _exportReportCollect(url) {
      const obj = {
        param: 'ids',
        ids: this.selectIds,
        url: url,
        queryObj: this.$serialize('search-form-report-tab')
      }
      this.$exportByQuery(obj)
    },
    // 推送至国抽平台
    _pushToGC() {
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请至少选择一条数据')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定要推送这' + ids.length + '条数据至国抽平台？',
          onOk: () => {
            this.$store
              .dispatch('FoodSample/pushDataToGCContract', ids)
              .then(() => {
                this._resultChange('信息推送成功')
              })
          }
        })
      }
    },
    _resultChange(msg) {
      if (this.$store.state.FoodSample.success) {
        this.$Message.success(msg)
        this._page()
        this.selectIds = []
      }
    },
    _iconClick(res, data, componentName) {
      this.currentComponent = componentName
      this.$nextTick(() => {
        switch (res) {
          case '管理样品和报告':
            this._sampleManage(data.id)
            break
          case '查看原始记录':
            this._viewRecord(data.id)
            break
          case '附件':
            this._upload(data.id)
            break
          case '操作记录':
            this._record(data.id)
            break
        }
      })
    },
    _upload(id) {
      this.currentComponent = 'FileManage'
      this.$nextTick(() => {
        this.$refs.refModal._open(id, 'entrustId')
      })
      // 上传文件
      // this.$refs.FileManage._open(id, 'entrustId')
    },
    _viewRecord(id) {
      this.currentComponent = 'OriginalRecordView'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
    },
    _sampleManage(id) {
      this.currentComponent = 'SampleByMakeModal'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
    },
    _record(id) {
      this.$refs.recordModal._open(id)
    },
    _resManage(id) {
      this.$refs.refModal._open(id)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.getPage = this.$store.state.FoodContract.page
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData, data.componentName)
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    // FoodContract/page
    async _page() {
      const data = this.$serialize('search-form-report-tab')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await soilReport.pageReportIssueHis(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _detailModal(id, type) {
      // 查看
      this.$store.dispatch('FoodContract/getById', id).then(() => {
        if (type === 0) {
          // 企业委托 ’0‘
          this.currentComponent = 'FoodContractCompanyDetail'
        } else {
          // 政府委托 ’1‘
          this.currentComponent = 'FoodContractGovernDetail'
        }
        this.$nextTick(() => {
          this.$refs.refModal._open(this.$store.state.FoodContract.model)
        })
      })
    },
    _search() {
      this._page()
    }
  }
}
</script>
