<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-form-rel-sample" :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></Input>
              </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">
            <PTVXETable
              ref="pageTable"
              :tableHeight="tableHeight"
              @on-result-change="_tableResultChange"
              :getPage="getPage"
              :icon-msg="iconMsg"
              select-data>
              <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">
                  <div v-if="item.detail"><a @click.stop="_detailModal(scope.row.contractId,scope.row.type)">{{scope.row[item.key]}}</a>
                  </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.sampleDetail"><a @click.stop="_sampleDetailModal(scope.row.id,scope.row.type)">{{scope.row[item.key]}}</a>
                  </div>
                  <div v-else>{{scope.row[item.key]}}</div>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </div>
    <!--组件加载-->
    <!--打印（可填打印数量）-->
    <!--选择领样人-->
    <UserInfo ref="userModal" @on-result-change="_userResult"></UserInfo>
    <FileManage ref="FileManage"></FileManage>
  </div>
</template>
<script>
import http from '../../../api/http'
import UserInfo from '../../../components/user-info-single/assignPerson'
import { soilEntrust } from '../../../api'

export default {
  components: {
    UserInfo
  },
  data() {
    return {
      formObj: { num: undefined, name: undefined },
      iconMsgDis: [],
      iconMsg: [
        {
          type: 'md-create',
          id: '',
          name: '编辑'
        },
        { type: 'md-cloud', id: '', name: '附件' },
        {
          type: 'ios-clock',
          id: '',
          name: '操作日志'
        }
      ],
      // 操作列
      sampleFile: {
        type: 'cloud',
        id: '',
        name: '样品附件',
        componentName: 'FileManage'
      },
      sampleFileDis: {
        type: 'cloud',
        id: '',
        name: '样品附件',
        disabled: true
      },
      edit: {
        type: 'compose',
        id: 'rel-sample-receive-edit-sample',
        name: '编辑'
      },
      del: {
        type: 'trash-a',
        id: 'rel-sample-receive-del-sample',
        name: '删除'
      },
      copy: {
        type: 'ios-copy',
        id: 'rel-sample-receive-copy-sample',
        name: '复制'
      },
      record: {
        type: 'ios-clock',
        id: '',
        name: '操作日志',
        componentName: 'FoodSampleRecord'
      },
      sampleDownloadContract: {
        type: 'share',
        id: 'food-sample-receive-sample-download-contract',
        name: '导出委托协议'
      },

      getPage: {},
      pageColumns: [
        { title: '委托商', key: 'client', width: 200 },
        { title: '试样编号', key: 'sampleCode', width: 120 },
        { title: '委托日期', key: 'entrustDate', width: 120, date: true },
        { title: '钻孔位置', key: 'boreholeLocation', width: 120 },
        { title: '水深（米）', key: 'waterDepth', width: 120 },
        { title: '钻孔名称', key: 'boreholeName', width: 120 },
        { title: '进度', key: 'progress', width: 120, status: true },
        { title: '制表日期', key: 'tabulateDate', width: 120, date: true },
        { title: '制表人', key: 'tabulater', width: 120 },
        { title: '检验类别', key: 'testType', width: 120 }
      ],
      searchOpen: false,
      btn: [
        { type: 'primary', id: 'ZBC', name: '接收并发放', componentName: '' }
      ],
      selectIds: [],
      selectData: [],
      currentComponent: '',
      printerVal: '', // 选择的打印机
      standard: '',
      stdList: [],
      acceptSelectUserValue: ''
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('', 340)
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  mounted() {
    // // 获取当前的执行标准下拉项
    // this._getStdList()
  },
  methods: {
    // 接收的返回保存
    _userResult(data, msg) {
      const sendData = {
        ids: this.selectIds.join(',')
      }
      if (data) {
        sendData.person = data.realname
        sendData.personId = data.userId
      }
      switch (msg) {
        case 'relReceiveSendScanMan':
          // 扫码接收并发放
          this.$refs.refModal._open('扫码接收发放', data)
          break
      }
    },
    // 抽样日期
    _makeDateChange(data) {
      $('input[name=samplingDateBegin]').val(data[0])
      $('input[name=samplingDateEnd]').val(data[1])
    },
    // 组件返回值
    _componentResult(data, msg) {
      switch (this.currentComponent) {
        case 'SelectPrinter':
          this._printerResult(data)
          break
        default:
          this._page()
      }
    },
    _btnClick(msg, currentComponent, formTypeObj) {
      this.currentComponent = currentComponent
      this.$nextTick(() => {
        switch (msg) {
          case 'search':
            this.searchOpen = !this.searchOpen
            break
          default:
            this._btnOption(msg, formTypeObj)
        }
      })
    },
    // btn操作
    _btnOption(msg, formTypeObj) {
      switch (msg) {
        case '扫码接收并发放':
          // 根据字典判断是否选择人员
          if (this.acceptSelectUserValue === '是') {
            this.$refs.userModal._open('relReceiveSendScanMan', '请选择领样人')
          } else {
            this.$refs.refModal._open('扫码接收发放')
          }
          break
        case '接收并发放':
          this._submitSend()
          break
        case '添加':
          if (formTypeObj.uri === 'DLTB-contract-reg') {
            this.currentComponent = 'FoodContractCompanyEditByDLTB'
          } else if (formTypeObj.uri === 'HES-company-contract-reg') {
            this.currentComponent = 'FoodContractCompanyEditByHES'
          } else {
            this.currentComponent = 'FoodContractCompanyEdit'
          }
          this.$nextTick(() => {
            this.$refs.refModal._openReceive()
          })
          break
        case '导入检测项目':
          this._importItem()
          break
        case '导入检测项目包':
          this._importItemPackage()
          break
        case '复制历史样品检测项目':
          this._copyHisItem()
          break
        case '删除':
          this._deleteSelected()
          break
        case '打印标签':
          this._selectPrinter('print-label')
          break
        case '自定义打印':
          if (this.selectData.length === 0) {
            this.$Message.warning('请至少选择一条样品信息')
          } else {
            this._cusTomePrint()
          }
          break
      }
    },
    // 单个删除
    _deleteById(id) {
      this._deleteByIds([id], '确定删除该条记录？')
    },
    _deleteByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {}
      })
    },
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(ids, '确定删除 ' + ids.length + ' 条记录？')
      }
    },
    // 导入检测项目包
    _importItemPackage() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$refs.refModal._open(this.selectIds)
      }
    },
    // 导入检测项目
    _importItem() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$refs.refModal._open(this.selectIds, this.selectData[0])
      }
    },
    _copyHisItem() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$refs.refModal._open(this.selectIds)
      }
    },
    _iconClick(res, data, currentComponent, rowIndex, obj) {
      this.currentComponent = currentComponent
      this.$nextTick(() => {
        switch (res) {
          case '删除':
            this._deleteById(data.id)
            break
          case '操作日志':
            this._operationRecord(data.id)
            break
          case '样品附件':
            this._upload(data.id)
            break
        }
      })
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        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
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      console.log(this.$refs.pageTable._searchParams())
      console.log('this.formObj', this.formObj)
      const result = await soilEntrust.pageReceive(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _editModal(id, type) {
      this.$refs.refModal._open(id, type)
    },
    _search() {
      this._page()
    },
    _operationRecord(id) {
      // 操作日志
      this.$refs.refModal._open(id)
    },
    _resultChange(msg) {
      if (this.$store.state.FoodContract.success) {
        this._page()
        this.$Message.success(msg)
        this.selectIds = []
      }
    },
    _resultSampleChange(msg) {
      if (this.$store.state.FoodSample.success) {
        this._page()
        this.$Message.success(msg)
        this.selectIds = []
      }
    },
    _upload(id) {
      // 上传文件
      this.$refs.FileManage._open(id, 'entrustId')
    },
    // 选择完打印机之后
    _printerResult(data) {
      // printModal  不能使用动态组件，否则第二次打不开，原因未知
      switch (data.type) {
        // 打印标签--直接打印
        case 'print-label':
          this.$refs.printModal._printOpen(this.selectData, data.printerVal)
          break
      }
    },
    // 选择打印机
    _selectPrinter(msg) {
      if (this.selectData.length === 0) {
        this.$Message.warning('请至少选择一条数据')
      } else {
        this.$refs.refModal._open(msg)
      }
    },
    _stdResult(msg, data) {
      switch (msg) {
        case 'select':
          this.standard = data
          break
        case 'query':
          this.standard = data
          this._getStdList(data)
          break
        case 'focus':
          this._getStdList()
          break
      }
    }
  }
}
</script>
