<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24">
            <Form v-show="searchOpen" id="search-form" :label-width="80" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="search-item" label="委托编号:">
                <Input v-model="formObj.entrustCode" name="entrustCode" placeholder="请输入样品编号" clearable @on-enter="_formSearch"/>
              </Form-item>
              <Form-item class="search-item" label="试样编号:">
                <Input v-model="formObj.sampleCode" name="sampleCode" placeholder="请输入样品名称" clearable @on-enter="_formSearch"/>
              </Form-item>
              <Form-item class="search-btn">
                <Button type="primary" @click="_formSearch">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" :show-search-btn="true" class="contHide"
                      @on-result-change="_btnClick">
            </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>
                  <div v-else-if="item.key==='quantity'" @click.stop="_handleIndex(scope)">
                    <div v-if="editIndex!==scope.rowIndex">
                      {{scope.row[item.key]}}
                    </div>
                    <div v-else>
                      <el-input v-model="scope.row.quantity" clearable></el-input>
                    </div>
                  </div>
                  <div v-else-if="item.key==='conditions'" @click.stop="_handleIndex(scope)">
                    <div v-if="editIndex!==scope.rowIndex">{{scope.row[item.key]}}</div>
                    <div v-else>
                      <el-select v-model="scope.row.conditionList" clearable style="width:100%" size="small">
                        <el-option v-for="item in conditionList" :value="item.name" :key="item.name">{{ item.name }}
                        </el-option>
                      </el-select>
                    </div>
                  </div>
                  <div v-else-if="item.key==='backupLocation'" @click.stop="_handleIndex(scope)">
                    <div v-if="editIndex!==scope.rowIndex">
                      {{scope.row[item.key]}}
                    </div>
                    <div v-else>
                      <el-input v-model="scope.row.backupLocation" clearable></el-input>
                    </div>
                  </div>
                  <span v-else>{{scope.row[item.key]}}</span>
                </template>
              </vxe-table-column>
            </PTVXETableHeight>
          </Col>
        </Row>
      </div>
    </div>
    <Reason ref="reasonModal" @on-result-change="_reasonResult"></Reason>
  </div>
</template>
<script>
import Reason from '../../../../components/base/Reason'
// eslint-disable-next-line no-unused-vars
import http from '../../../../api/http'
import { soilSample } from '../../../../api'
export default {
  components: {
    Reason
  },
  data() {
    return {
      currentComponent: '',
      searchOpen: false,
      formObj: {},
      selectData: [],
      selectIds: [],
      selectSampleIds: [],
      getPage: {},
      btn: [
        // {
        //   type: 'primary',
        //   id: '',
        //   name: '通过'
        // },
        // {
        //   type: '',
        //   id: '',
        //   name: '驳回'
        // }
        // { type: '', id: '', name: '填写存放信息' },
        // { type: '', id: '', name: '申请处理' },
        // { type: '', id: '', name: '导出' },
      ],
      iconMsg: [
        { type: 'md-create', id: '', name: '编辑' } // food-sample-prepare-edit
      ],
      iconMsgSave: [{ type: 'ios-checkmark', id: '', name: '保存' }],
      pageColumns: [
        { title: '委托商', key: 'client', width: 180 },
        { title: '委托编号', key: 'entrustCode', width: 100 },
        { title: '试样编号', key: 'sampleCode', width: 100 },
        { title: '状态', key: 'status', width: 100, status: true },
        { title: '试样深度', key: 'sampleDepth', width: 100 },
        { title: '样品包装类型', key: 'samplePack', width: 120 },
        { title: '现场编号', key: 'siteNo', width: 100 },
        { title: '钻孔位置', key: 'boreholeLocation', width: 110 },
        { title: '钻孔名称', key: 'boreholeName', width: 160 },
        { title: '水深（米）', key: 'waterDepth', width: 100 },
        { title: '存储条件', key: 'conditions', width: 180 },
        { title: '备样位置', key: 'backupLocation', width: 180 },
        { title: '备样数量', key: 'quantity', width: 180 },
        { title: '备样人', key: 'backupUser', width: 140 }
        /*  {title: '处置方式', key: 'handleMethod', width: 120,}, */
      ],
      conditionList: [],
      locList: [],
      currentIndex: -1,
      editIndex: -1,
      endDateBegin: '',
      endDateEnd: '',
      // 只看预警和超期样品
      warnAndOver: 0,
      sampleHandleValue: '',
      issueDateBegin: '',
      issueDateEnd: '',
      isEligible: ''
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 255)
      } else {
        return this.$tableHeight('', 220)
      }
    },
    colorComputed() {
      return function(date) {
        if (this.$warningValue(date) < 0) {
          return '#eb6877' // 红色
        } else if (
          this.$warningValue(date) >= 0 &&
          this.$warningValue(date) <= this.sampleHandleValue
        ) {
          return '#f90' // 橙色
        }
      }
    }
  },
  mounted() {},
  methods: {
    // 签发日期
    _issueDateChange(date) {
      this.issueDateBegin = date[0]
      this.issueDateEnd = date[1]
    },
    // 重新请求page以及备样数量
    _relPage() {
      this._page()
      // this._getBackUpCount()
    },
    // 选择时间
    _endDateChange(data) {
      this.getPage.records[this.currentIndex].endDate = data
    },
    // 查询-接收日期
    _dateChange(data) {
      this.endDateBegin = data[0]
      this.endDateEnd = data[1]
    },
    _iconClick(res, data, componentName, index) {
      this.currentIndex = index
      switch (res) {
        case '编辑':
          this.editIndex = index
          break
        case '保存':
          console.log('保存')
          this.editIndex = -1
          break
      }
    },
    // 操作的序号
    _handleIndex(data) {
      this.currentIndex = data.rowIndex
    },
    _btnClick(msg) {
      switch (msg) {
        case '通过':
          this._dispose()
          break
        case '驳回':
          this._disposeBack()
          break
        case '制备':
          this._preMethod()
          break
        case '无需制备':
          this._noPreMethod()
          break
        case '扫码制备':
          this._scanPreMethod()
          break
        case '填写存放信息':
          this._batchEdit()
          break
        case '申请处理':
          this._handle()
          break
        case '导出':
          if (this.getPage.records.length === 0) {
            this.$Message.warning('暂无数据，不可导出')
          } else {
            this._export()
          }
          break
        case '导出备样记录':
          this._exportReceiveRecord()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    // 通过
    _dispose() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定通过这 ' + this.selectIds.length + ' 条数据？',
          onOk: () => {
            this._disposeOk(this.selectIds.join(','))
          }
        })
      }
    },
    _disposeBack() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条委托！')
      } else {
        this.$refs.reasonModal._open('退回原因')
      }
    },
    _reasonResult(data) {
      if (undefined !== data && data !== '') {
        this._reportCheckBack(data)
      }
    },
    _reportCheckBack: async function(data) {
      const result = await soilSample.disposalFail({
        ids: this.selectIds,
        remark: data
      })

      if (result) {
        this.$Message.success('退回成功！')
        console.log('退回成功')
        await this._page()
      }
    },
    _disposeOk: async function(ids) {
      const result = await soilSample.disposalOk(ids)
      if (result) {
        this._resultChange('提交成功')
      }
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this._page()
    },
    // 制备
    _preMethod() {
      if (this.selectSampleIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$refs.preModal._open(this.selectSampleIds)
      }
    },
    // 无需制备
    _noPreMethod() {
      if (this.selectSampleIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.$refs.noPreModal._open(this.selectSampleIds)
      }
    },
    _scanPreMethod() {
      this.$refs.scanPreModal._open()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'selectData':
          this.selectData = data
          this.selectIds = []
          this.selectSampleIds = []
          for (let i = 0; i < data.length; i++) {
            this.selectIds.push(data[i].id)
            this.selectSampleIds.push(data[i].sampleId)
          }
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
      // this._getBackUpCount()
    },
    _page: async function() {
      this.editIndex = -1
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilSample.pageBackupHandleCheckHis(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _batchEdit() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this.$refs.editModal._open(this.selectData)
      }
    },
    _handle() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this.$refs.applyModal._open(this.selectData)
      }
    },
    // 参数
    _searchParams() {
      const data = this.$serialize('search-form')
      return this.$extend(data, { page: 1, rows: 5000 })
    }
    /** *********************************导出样品台账-end*********************************************/
  }
}
</script>
<style scoped>
/******副样、备样卡片********/
.reimbursement-item {
  flex: 1;
  border-radius: 5px;
  border: 1px solid #e8e8e8;
  padding-bottom: 10px;
}

.reimbursement-item + .reimbursement-item {
  margin-left: 20px;
}

.reimbursement-item p {
  text-align: center;
  margin: 0;
}

.reimbursement-item > p:first-child {
  font-size: 22px;
  line-height: 40px;
}
</style>
