<template>
  <div>
    <Modal v-model="showModal" v-drag width="1200">
      <p slot="header">
        {{ modalTitle }}
      </p>
      <div>
        <!--内容-->
        <Row>
          <!--查询-->
          <Col span="24">
          <Form id="search-sample-company" v-show="searchOpen" :label-width="90" inline onsubmit="return false">
            <label class="label-sign"></label>
            <Form-item label="试验名称:" class="search-item">
              <Input v-model="formObj.name" @on-enter="_formSearch" placeholder="请输入试验名称" clearable />
            </Form-item>
            <Form-item class="search-btn">
              <Button @click="_formSearch" type="primary">
                搜索
              </Button>
            </Form-item>
          </Form>
          </Col>
          <!--操作-->
          <Col span="24">
          <btn-list :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">
              <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)">{{ scope.row[item.key] }}</a>
                </div>
                <div v-else-if="item.key==='status'">
                  {{ scope.row[item.key].display }}
                </div>
                <div v-else-if="item.judged">
                  {{ scope.row[item.key]===0? '否':'是' }}
                </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>
            </PTVXETable>
          </Col>
        </Row>
      </div>
      <div slot="footer">
        <modal-footer ref="footerModal" :footer="footerList" @on-result-change="_footerResult" />
      </div>
    </Modal>
  </div>
</template>
<script>
import { soilEntrust, soilTest } from '../../../api'
export default {
  components: {},
  data() {
    return {
      formId: 'SampleItemManage',
      currentComponent: '',
      iconMsg: [{ type: 'pt-a-end', id: '', name: '删除' }],
      sampleId: '', // 样品id
      showModal: false,
      modalTitle: '管理检测项目',
      selectIds: [],
      itemId: '',
      recordId: '',
      index: '',
      showOption: true,
      searchOpen: true,
      selectData: {},
      getPage: {},
      currentRow: '',
      currentIndex: '',
      footerList: [
        { id: '', name: '取消', type: '' },
        { id: '', name: '关闭', type: 'primary' }
      ],
      pageColumns: [
        { title: '试验名称', key: 'name', width: 160 },
        { title: '试验项目英文简写', key: 'shortName', width: 140 },
        { title: '大类', key: 'mainType', width: 140 },
        { title: '小类', key: 'smallType' },
        { title: '检测依据', key: 'testBasis', width: 180 },
        { title: '试验方法', key: 'testMethod', width: 180 },
        { title: '状态', key: 'status', width: 180 }
      ],
      subOptions: [
        {
          value: '0',
          label: '否'
        },
        {
          value: '1',
          label: '是'
        }
      ],
      dateList: [],
      formObj: {
        name: undefined,
        recordId: undefined,
        code: undefined
      }
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _footerResult(name) {
      switch (name) {
        case '取消':
          this._cancel()
          break
        case '关闭':
          this._cancel()
          break
      }
    },
    _cancel() {
      this.$emit('on-result-change')
      this.$refs.pageTable._hideLoading()
      this.$refs.footerModal._hideLoading()
      this.showModal = false
    },
    _ok() {
      console.log(this.getPage.records)
    },
    _modalResult(data1, data2) {
      switch (this.currentComponent) {
        default:
          this._search()
      }
    },
    _handleRow(data) {
      this.currentRow = data.row
      this.currentIndex = data.rowIndex
    },
    _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._importItem()
            break
          case '导入检测项目包':
            this._importItemPackage()
            break
          case '删除':
            this._deleteSelected()
            break
          case 'search':
            this.searchOpen = !this.searchOpen
            break
        }
      })
    },
    _iconClick(res, data, componentName, index) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (res) {
          case '编辑':
            this._editModal(true, data)
            break
          case '复制':
            this._copy(data)
            break
          case '删除':
            console.log(index)
            this._deleteById(data.id)
            break
          case '附件':
            this.$refs.refModal._open(data.id, 'sampleId')
            break
        }
      })
    },
    _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 'iconClick':
        //   this._iconClick(data.name, data.rowData, data.componentName)
        //   break
        case 'page':
          this._page()
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _open(id) {
      this.formObj = this.$resetFields(this.formObj)
      this.dateList = []
      this.showModal = true
      this.sampleId = id // 委托id
      this.recordId = id
      this.$refs.pageTable._hideLoading()
      this._page()
    },
    _openRecord(id) {
      this.formObj = this.$resetFields(this.formObj)
      this.dateList = []
      this.showModal = true
      this.sampleId = id // 委托id
      this.$refs.pageTable._hideLoading()
      this.showOption = false
      this._page()
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      this.formObj = this.$serializeForm(this.formObj)
      this.formObj.recordId = this.recordId
      const result = await soilTest.pageOriginalExp(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
        console.log(result)
      }
    },
    _deleteByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._deleteOk(ids)
        }
      })
    },

    _deleteOk: async function(ids) {
      const result = await soilEntrust.deleteById(ids)
      if (result) {
        this._resultChange('删除成功！ ')
      }
    },
    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(ids, '确定删除 ' + ids.length + ' 条记录？')
      }
    },
    _detailModal(data) {},
    _editModal(edit, data) {
      if (edit) {
        console.log(data)
        this.$refs.sampleManageEdit._open(data)
      } else {
        // 添加
        this.$refs.refModal._open('', this.sampleId)
      }
    },
    _search() {
      this._page()
    },
    _resultChange(msg) {
      this._page()
      this.$Message.success(msg)
      this.$emit('on-result-change')
    },
    _copy(data) {
      this.$refs.copyModal._open(data.id, data.type)
    },
    // 导入检测项目
    _importItem() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$refs.refModal._open(
          this.selectIds,
          this.selectData[0],
          this.selectData
        )
      }
    },
    // 导入检测项目包
    _importItemPackage() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选中一条样品数据！')
      } else {
        this.$refs.refModal._open(this.selectIds, 'sample-rel-package-item')
      }
    },
    _operationRecord(id) {
      // 操作日志
      this.$refs.recordModal._open(id)
    }
  }
}
</script>
