<template>
  <div>
    <Modal v-model="showModal" v-drag width="1030" class="modal-footer-none">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <!--内容-->
        <Row>
          <!--查询-->
          <!--操作-->
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" @on-result-change="_btnClick" class="contHide" show-search-btn="true"></btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable ref="pageTable" :table-height="tableHeight" :form-id="formId" :loading="true"
                        :get-page="getPage" @on-result-change="_tableResultChange" :icon-msg="iconMsg"  select-data>
              <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>
                  <div v-else-if="item.key==='sampleDepth' && scope.row.index > 0" @click.stop="_handleRow(scope)">
                    <Input v-model="scope.row.sampleDepth" blur placeholder="请输入试样深度"
                    ></Input>
                  </div>
                  <div v-else-if="item.key==='siteNo'&& scope.row.index > 0" @click.stop="_handleRow(scope)">
                    <Input v-model="scope.row.siteNo" blur placeholder="请输入现场编号"
                    ></Input>
                  </div>
                  <div v-else-if="item.key==='sampleCode'">
                    {{scope.row[item.key]}}{{'--'+scope.row.index}}
                  </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>
  </div>
</template>
<script>
import { soilEntrust, soilSample } from '../../api'
export default {
  components: {},
  data() {
    return {
      formId: 'soilSampleManage',
      currentComponent: '',
      btn: [
        {
          type: 'success',
          id: '',
          name: '保存'
        }
        // {
        //   type: 'success',
        //   id: '',
        //   name: '导入检测项目包'
        // },
        // {
        //   type: 'success',
        //   id: '',
        //   name: '打印标签'
        // }
      ],
      itemList: [],
      indexList: [],
      sampleId: '',
      currentIndex: -1,
      currentRow: '',
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '关闭', type: 'primary' }
      ],
      iconMsg: [
        {
          type: 'ios-beaker',
          id: '',
          name: '添加'
        }
      ],
      contractId: '', // 委托id
      showModal: false,
      searchOpen: true,
      modalTitle: '样品拆分',
      selectIds: [],
      selectData: {},
      sampleProgress: '',
      getPage: {
        records: []
      },
      pageColumns: [
        { title: '样品编号', key: 'sampleCode', width: 140 },
        { title: '试样深度', key: 'sampleDepth', width: 140 },
        { title: '现场编号', key: 'siteNo', width: 140 },
        { title: '试验项目', key: 'experimentNames', width: 140 },
        { title: '试验项目简写', key: 'experimentShortNames', width: 120 },
        { title: '土质描述', key: 'sampleDescribe', width: 160 },
        {
          title: '土质描述详情',
          key: 'describeDetail',
          width: 200
        },
        {
          title: '备注',
          key: 'remark',
          width: 200
        },
        { title: '样品包装类型', key: 'samplePack', width: 120 }
      ],
      formObj: {},
      entrustId: '',
      receiveLocation: ''
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _add(data) {
      const addData = {
        sampleCode: data.sampleCode,
        sampleDepth: '',
        siteNo: '',
        experimentNames: data.experimentNames,
        experimentShortNames: data.experimentShortNames,
        sampleDescribe: data.sampleDescribe,
        describeDetail: data.describeDetail,
        remark: data.remark,
        samplePack: data.samplePack
      }
      this.getPage.records.splice(data.index + 1, 0, addData)
    },
    _handleRow(data) {
      this.currentRow = data.row
      this.currentIndex = data.rowIndex
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '关闭':
          this._cancel()
          break
      }
    },
    _cancel() {
      this.showModal = false
    },
    _ok() {
      this.showModal = false
      this.$refs.footerModal._hideLoading()
    },
    _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 '保存':
            this._save()
            break
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _save() {
      console.log(this.getPage.records)
      for (let i = 0; i < this.getPage.records.length; i++) {
        this.getPage.records[i].id = this.sampleId
        this.getPage.records[i].sampleCode =
          this.getPage.records[i].sampleCode +
          '-' +
          this.getPage.records[i].index
        console.log(this.getPage.records[i])
        this._addSample(this.getPage.records[i])
      }
    },
    _addSample: async function(data) {
      const result = await soilSample.addSample(data)
      if (result) {
        this.showModal = false
        this._deleteOk(this.sampleId)
        this.$emit('on-result-change')
      }
    },
    _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._add(data)
            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) {
      this.sampleId = ''
      this.formObj = {}
      this.sampleId = data.id
      this.getPage.records = []
      // this.formObj.receiveLocation = name
      // this.receiveLocation = name
      // this.formObj.storehouse = data.warehouse
      // this.formObj.shelfCode = data.name
      this.formObj.sampleCode = data.sampleCode
      this.formObj.sampleDepth = data.sampleDepth
      this.formObj.siteNo = data.siteNo
      this.formObj.experimentNames = data.experimentNames
      this.formObj.experimentShortNames = data.experimentShortNames
      this.formObj.sampleDescribe = data.sampleDescribe
      this.formObj.describeDetail = data.describeDetail
      this.formObj.remark = data.remark
      this.formObj.samplePack = data.samplePack
      this.showModal = true
      this.$refs.pageTable._hideLoading()
      this.$refs.pageTable._clearSelection()
      // this._page()
      this.getPage.records.push(this.formObj)
      console.log('123456', this.getPage.records)
    },
    _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)
        }
      })
    },
    _deleteOk: async function(id) {
      const result = await soilSample.sampleDeleteById(id)
      if (result) {
        console.log(result)
      }
    },
    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _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>
