<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'
import SampleManage from '../SampleManage'
export default {
  // eslint-disable-next-line vue/no-unused-components
  components: {
    Operation,
    // eslint-disable-next-line vue/no-unused-components
    SampleByMakeModal,
    // eslint-disable-next-line vue/no-unused-components
    OriginalRecordView,
    // eslint-disable-next-line vue/no-unused-components
    SampleManage
  },
  props: {
    reportStatus: null
  },
  data() {
    return {
      currentComponent: '',
      typeList: [{ name: '企业', value: 0 }, { name: '政府', value: 1 }],
      getPage: {},
      pageColumns: [
        {
          title: '委托商',
          key: 'client',
          width: 160,
          fixed: 'left'
        },
        { title: '项目名称', key: 'projectName', width: 150 },
        { title: '委托编号', key: 'entrustCode', width: 150 },
        // { title: '报告编号', key: 'reportCode', width: 100 },
        // { title: '进度', key: 'progress', width: 110, status: true },
        // { title: '状态', key: 'status', width: 100, status: true },
        { title: '委托日期', key: 'entrustDate', width: 130, date: true },
        { title: '钻孔位置', key: 'boreholeLocation', width: 140 },
        { title: '钻孔名称', key: 'boreholeName', width: 140 },
        { title: '水深', key: 'waterDepth', width: 150 },
        { title: '制表人', key: 'tabulater', width: 110 },
        { title: '制表日期', key: 'tabulateDate', width: 110, date: true }
      ],
      iconMsg: [
        {
          type: 'ios-flask-outline',
          id: 'report-manage-sample-item-manage',
          name: '管理样品和报告'
        },
        {
          type: 'ios-beaker',
          id: 'report-manage-view-record',
          name: '查看原始记录'
        },
        {
          type: 'ios-list-box',
          id: '',
          name: '按样品查看原始记录'
        },
        { type: 'ios-cloud', id: 'report-manage-upload', name: '附件' },
        {
          type: 'ios-clock',
          id: 'report-manage-record',
          name: '操作记录'
        }
      ],
      btn: [],
      searchOpen: false,
      selectIds: [],
      formObj: {
        reportSn: '',
        num: ''
      },
      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':
          break
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _resultChange(msg) {
      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._viewRecordBySample(data.id)
            break
          case '附件':
            this._upload(data.id)
            break
          case '操作记录':
            this._record(data.id)
            break
        }
      })
    },
    _viewRecordBySample(id) {
      this.currentComponent = 'SampleManage'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
    },
    _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 'iconClick':
          this._iconClick(data.name, data.rowData, data.componentName)
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    async _page() {
      const data = this.$serialize('search-form-report-tab')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await soilReport.pageReportEndBySummaryReport(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _search() {
      this._page()
    }
  }
}
</script>
