<template>
  <div>
    <Row>
      <Form
        id="lab-sample-form-his"
        v-show="searchOpen"
        :label-width="70"
        inline
        onsubmit="return false"
        class="search-form"
      >
        <label class="label-sign" />
        <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-item" label="报检时间:">-->
<!--          <Date-picker-->
<!--            ref="sampleTime"-->
<!--            type="datetimerange"-->
<!--            placeholder="报检时间"-->
<!--            style="width: 100%;"-->
<!--            :editable="false"-->
<!--            transfer-->
<!--            @on-change="_labSampleTime"-->
<!--          />-->
<!--          <input type="hidden" name="inspectionDateBegin">-->
<!--          <input type="hidden" name="inspectionDateEnd">-->
<!--        </Form-item>-->
        <Form-item class="search-btn">
          <Button @click="_formSearch" type="primary">
            搜索
          </Button>
        </Form-item>
      </Form>
      <Col span="24">
      <btn-list :msg="btn" :open="searchOpen" @on-result-change="_btnClick" show-search-btn="true" />
      </Col>
      <Col />
      <Button @click="_downloadByIds">
        下载
      </Button>
      </Col>
      <Col />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        @on-result-change="_tableResultChange"
        show-check-box
        select-data
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          :prop="item.key"
          :label="item.title"
          :align="item.align"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
          show-overflow-tooltip
          sortable
        >
          <template slot-scope="scope" @click.stop="_handleIndex(scope)">
            <span v-if="item.datetime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </span>
            <div v-else-if="item.status">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </div>
            <span v-else>
              {{ scope.row[item.key] }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          slot="col"
          :width="120"
          label="操作"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <icon-list
              :msg="iconMsg"
              :row-data="scope.row"
              :row-index="scope.$index"
              @on-result-change="_iconClick"
            />
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <fileManage ref="FileManage" @on-result-change="_fileResult" />
    <OperationModal ref="operationModal" />
  </div>
</template>

<script>
// eslint-disable-next-line import/named
import { soilStatistics } from '../../../../api'
import global from '../../../../api/config'
import OperationModal from '../../../../components/operation/Operation'
export default {
  name: 'PreparationManage',
  components: { OperationModal },
  data() {
    return {
      name: '',
      pageColumns: [
        { title: '委托方', key: 'client', width: 200 },
        { title: '联系人', key: 'person', width: 120 },
        { title: '委托编号', key: 'entrustCode', width: 120 },
        { title: '报告是否生成', key: 'reported', width: 120 },
        { title: '状态', key: 'progress', width: 120, status: true },
        { title: '电话/手机', key: 'tel', width: 120 },
        { title: '传真', key: 'fax', width: 120 },
        { title: '详细地址', key: 'address', width: 250 },
        { title: '邮箱', key: 'email' },
        { title: '费用合计', key: 'fee', width: 120 },
        { title: '备注', key: 'remark' }
      ],
      btn: [
        // { id: 'env-report-make-submit', type: 'primary', name: '提交' },
        // { id: '', type: 'primary', name: '生成' },
        // { id: '', type: 'primary', name: '下载' }
      ],
      getPage: {},
      searchOpen: false,
      currentIndex: '',
      selectIds: [],
      selectData: [],
      iconMsg: [
        { id: '', type: 'pt-a-view', name: '预览报告' },
        { id: '', type: 'pt-a-cloud', name: '附件' },
        { id: '', type: 'pt-a-clock', name: '操作日志' }
      ]
    }
  },
  computed: {
    tableHeight() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearch')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  methods: {
    async _page() {
      const data = this.$serialize('lab-sample-form-his')
      Object.assign(data, this.$refs.pageTable._searchParams())
      const result = await soilStatistics.pageReportMake(data)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _clearPage() {
      $('#lab-sample-form-his')
        .find('input')
        .each(function() {
          $(this).val('')
        })
      // this.$refs.sampleTime.handleClear()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '扫码接收':
          this._scanReceive()
          break
        case '手动接收':
          this._sampleReceive()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _scanReceive() {
      this.$refs.receiveModal._open()
    },
    _sampleReceive() {
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
      } else {
        this._receiveByIds(ids, `确认要接收 ${ids.length} 条数据吗?`)
      }
    },
    _receiveByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || `确定要处理该条数据吗?`,
        onOk: () => {
          this._receiveIds(ids)
        }
      })
    },
    async _receiveIds(ids) {
      const result = await soilStatistics.submitReportCheck(ids)
      if (result) {
        this._resultChange('提交成功!')
      }
    },
    _submitReportCheck() {
      const data = this.selectData
      if (data.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
        return false
      }
      this._submitByIds(
        this.selectIds,
        `确定要提交 ${this.selectIds.length} 条数据吗?`
      )
    },

    _submitByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || `确定要提交该条数据吗?`,
        onOk: () => {
          this._submit(ids)
        }
      })
    },

    // 接口异步
    _submit: async function(ids) {
      if (ids.length === 0) return false
      const result = await soilStatistics.submitEntrustReportCheck(ids)
      if (result) {
        this.$Message.success('提交成功!')
        this._formSearch()
      }
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this._formSearch()
    },

    _createReport() {
      console.log(this.currentComponent)
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
      } else {
        this.$refs.createReportModal._open(this.selectData)
      }
    },

    _downloadByIds() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
      } else {
        this._download(
          this.selectIds,
          `确定要下载 ${this.selectIds.length} 条数据吗?`
        )
      }
    },

    _download(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定要下载报告?',
        onOk: () => {
          window.open(
            global.baseURL + '/v1/sample_report/download_report/' + ids,
            '_blank'
          )
        }
      })
    },
    _iconClick(name, data) {
      switch (name) {
        case '提交':
          this._receiveByIds(data.id)
          break
        case '附件':
          this._uploadByIds(data.id)
          break
        case '预览报告':
          this._viewReport(data)
          break
        case '操作日志':
          this._record(data.id)
          break
      }
    },
    _record(id) {
      this.$refs.operationModal._open(id)
    },
    _viewReport(data) {
      this._getObjectKey(data)
    },

    _getObjectKey: async function(data) {
      const result = await soilStatistics.getObjectKey(data.id)
      if (result) {
        this.$openWindowModeless({
          objectKey: result,
          id: data.id,
          isReport: 0
        })
      }
    },

    _uploadByIds(id) {
      const tempData = {
        id: id,
        uploadFileUrl: '/v1/entrust_attachment/upload/',
        downloadFileUrl: '/v1/entrust_attachment/download/'
      }
      this.$refs.FileManage._open(tempData, 'entrustId')
    },
    // 文件返回的数据
    _fileResult(msg, data) {
      switch (msg) {
        case 'file-page':
          this.$refs.FileManage._openFile(data)
          break
        case 'delete-page':
          this.$refs.FileManage._delEntrustPage('', data)
          break
        case 'success':
          this.$Message.success('删除成功！')
          this.$refs.FileManage._pageEntrust()
          break
        default:
          this.$refs.FileManage._pageEntrust()
      }
    },
    _handleIndex(data) {
      this.currentIndex = data.$index
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'selectData':
          this.selectData = data
          this.selectIds = []
          for (let i = 0; i < this.selectData.length; i++) {
            this.selectIds.push(this.selectData[i].id)
          }
          break
        default:
          this._page()
      }
    },
    _labSampleTime(data) {
      $('input[name="inspectionDateBegin"]').val(data[0])
      $('input[name="inspectionDateEnd"]').val(data[1])
      console.log(data)
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    }
  }
}
</script>

<style scoped>
</style>
