<template>
  <div>
    <Modal v-model="showModal" v-drag width="1030">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <!--内容-->
        <Row>
          <!--查询-->
          <Col span="24">
          <Form id="search-sample-company" v-show="searchOpen" :label-width="80" inline onsubmit="return false">
            <label class="label-sign"></label>
            <Form-item label="试样编号:" class="search-item">
              <Input v-model="formObj.sampleCode" @on-enter="_formSearch" placeholder="请输入试样编号" clearable />
            </Form-item>
            <Form-item label="现场编号:" class="search-item">
              <Input v-model="formObj.siteNo" @on-enter="_formSearch" placeholder="请输入现场编号" clearable />
            </Form-item>
            <Form-item class="search-btn">
              <Button @click="_page" type="primary">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <!--操作-->
          <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" :icon-msg="iconMsg" @on-result-change="_tableResultChange"  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>
                <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, soilSample } from '../../api'
import SoilSampleItemManage from './SoilSampleItemManage'
export default {
  components: { SoilSampleItemManage },
  data() {
    return {
      formId: 'soilSampleManage',
      currentComponent: '',
      btn: [],
      itemList: [],
      indexList: [],
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ],
      iconMsg: [
        {
          type: 'ios-beaker',
          id: '',
          name: '管理检测项目'
        },
        { type: 'md-trash', id: '', name: '删除' }
      ],
      contractId: '', // 委托id
      showModal: false,
      recordHis: false,
      searchOpen: true,
      modalTitle: '管理样品',
      selectIds: [],
      selectData: {},
      getPage: {},
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 120, fixed: 'left' },
        { title: '试样深度', key: 'sampleDepth', width: 100 },
        { title: '现场编号', key: 'siteNo', width: 100 },
        { title: '试验项目', key: 'experimentNames' },
        { title: '土质描述', key: 'sampleDescribe', width: 150 },
        { title: '样品包装类型', key: 'samplePack', width: 120 }
      ],
      sampleId: '',
      dateList: [],
      formObj: {
        siteNo: undefined,
        sampleCode: undefined
      }
    }
  },
  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)
    },
    _btnClick(msg, componentName) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (msg) {
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _iconClick(res, data, componentName, index) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (res) {
          case '管理检测项目':
            this._itemManage(data.id)
            break
          case '删除':
            this._deleteById(data.id)
            break
        }
      })
    },
    _itemManage(data) {
      // 管理检测项目
      if (this.recordHis) {
        this.$refs.sampleItemManage._openRecord(data)
      } else {
        this.$refs.sampleItemManage._open(data)
      }
    },
    _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
      }
    },
    allSelect(data) {
      this.indexList = []
      for (let i = 0; i < data.length; i++) {
        this.indexList.push(data[i].index)
      }
      console.log('索引', this.indexList)
    },
    _open(id) {
      this.formObj = this.$resetFields(this.formObj)
      this.dateList = []
      this.showModal = true
      this.contractId = id // 委托id
      console.log(this.contractId)
      this.$refs.pageTable._hideLoading()
      this._page()
    },
    _openHis(id) {
      this.formObj = this.$resetFields(this.formObj)
      this.dateList = []
      this.showModal = true
      this.contractId = id // 委托id
      console.log(this.contractId)
      this.$refs.pageTable._hideLoading()
      this.recordHis = true
      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.samplePage(
        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(ids) {
      const result = await soilSample.sampleDeleteById(ids)
      if (result) {
        this._resultChange('删除成功')
      }
    },

    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _search() {
      this._page()
    },
    _resultChange(msg) {
      this._page()
      this.$Message.success(msg)
    }
  }
}
</script>
