<template>
  <div>
      <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.sampleCode" 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">
            <PTVXETableHeight ref="pageTable" :table-height="tableHeight" :form-id="formId" :loading="true"
                        :get-page="getPage" :icon-msg="iconMsg" is-edit 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"
                :edit-render="item.editCell?{autofocus: 'input'}:null" sortable>
                <template v-slot:edit="scope">
                  <div v-if="item.key==='describeDetail'" @click.stop="_handleRow(scope)">
<!--                    <el-input v-model="scope.row.describeDetail" @change="_inputChange(scope.row)" placeholder="请输入土质描述详情">-->
<!--                    </el-input>-->
                    <Input :value="scope.row.describeDetail" @change.native="_inputChange($event.target.value,scope.row)"></Input>
                  </div>
                  <div v-else-if="item.key==='remark'" @click.stop="_handleRow(scope)">
                    <Input :value="scope.row.remark" @change.native="_remarkChange($event.target.value,scope.row)"></Input>
<!--                    <Input v-model="scope.row.remark"  @change="_remarkChange(scope.row)"></Input>-->
                  </div>
                </template>
              <template slot-scope="scope">
                <div v-if="item.detail">
                  <a @click.stop="_detailModal(scope.row)">{{ scope.row[item.key] }}</a>
                </div>
                <div v-else-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>
                  {{ scope.row[item.key] }}
                </div>
              </template>
            </vxe-table-column>
          </PTVXETableHeight>
          </Col>
        </Row>
      </div>
    <SoilSampleItemManage ref="sampleItemManage" @on-result-change="_page"></SoilSampleItemManage>
    <DescribeDetailModal ref="writeDetailModal" @on-result-change="_page"></DescribeDetailModal>
    <PreparationModal ref="preModal" @on-result-change="_page"></PreparationModal>
  </div>
</template>
<script>
import { soilEntrust } from '../../api'
import SoilSampleItemManage from './SoilSampleItemManage'
import DescribeDetailModal from './sample-preparation/DescribeDetailModal'
import PreparationModal from './sample-preparation/Preparation'
export default {
  components: { SoilSampleItemManage, DescribeDetailModal, PreparationModal },
  data() {
    return {
      formId: 'soilSampleManage',
      currentComponent: '',
      btn: [
        {
          type: 'success',
          id: '',
          name: '试样制备'
        },
        {
          type: 'success',
          id: 'ZBC',
          name: '打印标签'
        },
        {
          type: 'success',
          id: '',
          name: '批量填写土质描述详情'
        }
      ],
      itemList: [],
      indexList: [],
      currentIndex: -1,
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '确定', type: 'primary' }
      ],
      iconMsg: [
        {
          type: 'ios-beaker',
          id: '',
          name: '管理检测项目'
        }
      ],
      contractId: '', // 委托id
      showModal: false,
      recordHis: false,
      searchOpen: true,
      modalTitle: '管理样品',
      selectIds: [],
      selectData: {},
      getPage: {},
      pageColumns: [
        { title: '试样编号', key: 'sampleCode', width: 100, fixed: 'left' },
        { title: '试样深度', key: 'sampleDepth', width: 100 },
        { title: '现场编号', key: 'siteNo', width: 100 },
        { title: '试验项目', key: 'experimentNames', width: 110 },
        { title: '试验项目简写', key: 'experimentShortNames', width: 120 },
        { title: '土质描述', key: 'sampleDescribe', width: 160 },
        {
          title: '土质描述详情',
          key: 'describeDetail',
          width: 200,
          editCell: true
        },
        {
          title: '备注',
          key: 'remark',
          width: 200,
          editCell: true
        },
        { title: '样品包装类型', key: 'samplePack', width: 120 }
      ],
      sampleId: '',
      dateList: [],
      formObj: {
        name: undefined,
        sampleCode: undefined
      }
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this.$refs.pageTable._showLoading()
  },
  methods: {
    _inputChange: async function(info, data) {
      this.getPage.records[data.index].describeDetail = info
      const result = await soilEntrust.sampleEdit({
        id: data.id,
        describeDetail: info
      })
      if (result) {
        this.$Message.success('修改成功')
      }
    },

    _remarkChange: async function(info, data) {
      this.getPage.records[data.index].remark = info
      const result = await soilEntrust.sampleRemarkEdit({
        id: data.id,
        remark: info
      })
      if (result) {
        // this._resultChange('修改成功')
      }
    },

    _handleRow(data) {
      this.currentRow = data.row
      this.currentIndex = data.rowIndex
    },
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '确定':
          this._ok()
          break
      }
    },
    _visibleChange(data) {
      if (data === false) {
        this.$emit('on-result-change')
      }
    },
    _cancel() {
      this.showModal = false
    },
    _ok() {
      this.showModal = false
      this.$refs.footerModal._hideLoading()
      console.log(this.getPage)
    },
    _itemImportBack(data) {
      if (this.selectIds === undefined || this.selectIds.length === 0) {
        return false
      }
      if (data === undefined || data.length === 0) {
        return false
      }
      this._importItemOk({ sampleIds: this.selectIds, drugItemList: data })
    },
    _modalResult(data1, data2) {
      switch (this.currentComponent) {
        case 'CopyModal':
          if (data1 === 0) {
            this._copySample(data2)
          } else {
            this._copyAll(data2)
          }
          break
        default:
          this._search()
      }
    },
    _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._samplePre()
            break
          case '批量填写土质描述详情':
            this._writeDetail(this.selectIds)
            break
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _samplePre() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('至少选择一条数据')
      } else {
        this.$refs.preModal._open(this.selectIds.join(','))
      }
    },
    _writeDetail(id) {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        const ids = id.join(',')
        this.$refs.writeDetailModal._open(ids)
      }
    },
    _iconClick(res, data, componentName, index) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (res) {
          case '管理检测项目':
            this._itemManage(data.id)
            break
        }
      })
    },
    _itemManage(data) {
      // 管理检测项目
      if (this.recordHis) {
        this.$refs.sampleItemManage._openRecord(data)
      } else {
        this.$refs.sampleItemManage._open(data)
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'selectData':
          const selectIds = []
          for (let i = 0; i < data.length; i++) {
            selectIds.push(data[i].id)
          }
          this.selectIds = selectIds
          this.selectData = data
          break
        case 'allSelect':
          console.log('123465798', data)
          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.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.pagePrepare(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _search() {
      this._page()
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this._page()
    }
  }
}
</script>
