<template>
  <div>
    <Modal v-model="showModal" v-drag width="1030" class="modal-footer-none">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <!--内容-->
        <Row>
          <!--查询-->
          <Col span="24">
            <Form v-show="searchOpen" id="search-sample-company" :label-width="80" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item label="钻孔名称:" class="search-item">
                <Input v-model="formObj.boreholeName" placeholder="请输入钻孔名称" clearable @on-enter="_formSearch" />
              </Form-item>
              <Form-item class="search-btn">
                <Button type="primary" @click="_page">
                  搜索
                </Button>
              </Form-item>
            </Form>
          </Col>
          <!--操作-->
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" class="contHide" show-search-btn="true" @on-result-change="_btnClick"></btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable ref="pageTable" :table-height="tableHeight" :form-id="formId" :loading="true"
                        :get-page="getPage" select-data  @on-result-change="_tableResultChange">
              <vxe-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :field="item.key"
                :title="item.title"
                :width="item.width?item.width:200"
                :fixed="item.fixed?item.fixed:undefined" sortable>
                <template slot-scope="scope">
                  <div v-if="item.status">
                    {{ 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>
                  <span v-else>
                  {{ scope.row[item.key] }}
                </span>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
    <SoilSampleItemManage ref="sampleItemManage" @on-result-change="_page"></SoilSampleItemManage>
  </div>
</template>
<script>
import { soilEntrust } from '../../../api'
import SoilSampleItemManage from './ViewSample'
export default {
  components: { SoilSampleItemManage },
  data() {
    return {
      formId: 'soilSampleManage',
      currentComponent: '',
      btn: [
        // {
        //   type: 'success',
        //   id: '',
        //   name: '导入检测项目'
        // }
        // {
        //   type: 'success',
        //   id: '',
        //   name: '导入检测项目包'
        // },
        // {
        //   type: 'success',
        //   id: '',
        //   name: '打印标签'
        // }
      ],
      itemList: [],
      indexList: [],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ],
      iconMsg: [
        {
          type: 'ios-beaker',
          id: '',
          name: '查看试样'
        }
      ],
      contractId: '', // 委托id
      showModal: false,
      searchOpen: true,
      modalTitle: '管理样品',
      selectIds: [],
      selectData: {},
      getPage: {},
      pageColumns: [
        { title: '项目名称', key: 'projectName', width: 140 },
        { title: '钻孔名称', key: 'boreholeName', width: 140 },
        { title: '数量', key: 'casesNum', width: 120 },
        { title: '收样时间', key: 'receiveTime', width: 120, date: true },
        { title: '收样人', key: 'receiver', width: 120 },
        { title: '登记时间', key: 'registTime', width: 120, date: true },
        { title: '登记人', key: 'registrant', width: 120 },
        { title: '入库日期', key: 'instockTime', width: 120, date: true },
        { title: '留存日期', key: 'keepLimitTime', width: 120, date: true },
        { title: '项目负责人', key: 'projectLeader', width: 120 }
      ],
      formObj: {
        receiveLocation: undefined,
        entrustCode: undefined,
        client: undefined,
        warehouse: undefined
      },
      receiveLocation: ''
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _cancel() {
      this.showModal = false
    },
    _ok() {
      this.showModal = false
      this.$refs.footerModal._hideLoading()
      console.log(this.getPage)
    },
    _dateChange(data) {
      this.formObj.ctimeBegin = data[0]
      this.formObj.ctimeEnd = data[1]
    },
    _btnClick(msg, componentName) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (msg) {
          case '添加':
            this._editModal(false)
            break
          case '删除':
            this._deleteSelected()
            break
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _copyHisSample() {
      this.$refs.refModal._open(this.contractId)
    },

    _iconClick(res, data, componentName, index) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (res) {
          case '查看试样':
            this._itemManage(data.id)
            break
          case '编辑':
            this._editModal(true, data)
            break
          case '复制':
            this._copy(data)
            break
          case '删除':
            console.log(index)
            this._deleteById(data.id)
            break
        }
      })
    },
    _itemManage(data) {
      this.$refs.sampleItemManage._open(data, this.receiveLocation)
    },
    _tableResultChange(msg, data) {
      const selectIds = []
      switch (msg) {
        case 'selectData':
          for (let i = 0; i < data.length; i++) {
            selectIds.push(data[i].id)
          }
          this.selectIds = selectIds
          this.selectData = data
          break
        case 'allSelect':
          this.allSelect(data)
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _open(data) {
      console.log(data)
      this.formObj = this.$resetFields(this.formObj)
      // this.formObj.receiveLocation = name
      // this.receiveLocation = name
      this.formObj.storehouse = data.warehouse
      this.formObj.shelfCode = data.name
      this.showModal = true
      this.$refs.pageTable._hideLoading()
      this._page()
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      // this.formObj.entrustId = this.contractId
      const result = await soilEntrust.pageSampleOfStorehouse(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _deleteByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._deleteOk(ids)
        }
      })
    },

    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(ids, '确定删除 ' + ids.length + ' 条记录？')
      }
    },
    _editModal(edit, data) {
      if (edit) {
        console.log(data)
        this.$refs.sampleManageEdit._open(data)
      } else {
        // 添加
        this.$refs.refModal._open('', this.contractId)
      }
    },
    _search() {
      this._page()
    },
    _resultChange(msg) {
      this._page()
      this.$Message.success(msg)
    },
    _operationRecord(id) {
      // 操作日志
      this.$refs.recordModal._open(id)
    }
  }
}
</script>
