<template>
  <div>
    <Modal v-model="showModal" v-drag width="100" class="modal-footer-none full-screen">
      <div slot="header">{{modalTitle}}</div>
      <div>
        <Form id="index-right-form" :label-width="80" inline onsubmit="return false">
          <label class="label-sign"></label>
          <Form-item class="search-item" label="指标名称:">
            <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>
          <btn-list :msg="btn" @on-result-change="_btnClick" class="contHide" style="margin-bottom: 10px;"/>
        <PTVXETable ref="pageTable" :tableHeight="tableHeight"
                    @on-result-change="_tableResultChange" :getPage="getPage" :iconMsg="iconMsg">
          <vxe-table-column
            :field="item.key"
            :title="item.title"
            :min-width="item.width?item.width:200"
            :fixed="item.fixed?item.fixed:undefined"
            v-for="item in pageColumns" :key="item.key">
            <template slot-scope="scope">
              <span v-if="item.key==='defaulted'">{{scope.row[item.key]?'是':'否'}}</span>
              <span v-else>{{scope.row[item.key]}}</span>
            </template>
          </vxe-table-column>
        </PTVXETable>
      </div>
    </Modal>
    <IndexManageEdit ref="indexEdit" @on-result-change="_page"></IndexManageEdit>
  </div>
</template>

<script>
import { soilTest } from '../../../api'
import IndexManageEdit from './IndexManageEdit'

export default {
  components: { IndexManageEdit },
  data() {
    return {
      id: '',
      modalTitle: '',
      showModal: false,
      btn: [
        { type: 'success', id: '', name: '添加指标' },
        { type: 'error', id: '', name: '删除' }
      ],
      tableHeight: document.documentElement.clientHeight - 180,
      pageColumns: [
        { title: '试验指标名称', key: 'name' },
        { title: '试验指标大类', key: 'mainType', width: 120 },
        { title: '试验指标小类', key: 'smallType', width: 120 },
        { title: '状态', key: 'status', width: 120 },
        { title: '检测值', key: 'testValue', width: 120 },
        { title: '单位', key: 'unit', width: 120 }
      ],
      getPage: {},
      iconMsg: [{ type: 'md-create', id: '', name: '编辑' }],
      selectIds: [],
      formObj: {
        name: ''
      },
      // 资质信息
      aptitudeItemInfo: {}
    }
  },
  methods: {
    _open(data) {
      console.log(data)
      this.aptitudeItemInfo = data
      this.formObj = this.$resetFields(this.formObj)
      this.id = data.id
      this.modalTitle = data.name + ' 指标管理'
      this.showModal = true
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      this.formObj.experimentId = this.id
      const result = await soilTest.itemPage(this.$serializeForm(this.formObj))
      if (result) {
        console.log(result)
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(true, data)
          break
      }
    },
    _editModal(edit, data) {
      if (edit) {
        this._getById(data)
      } else {
        this.$refs.indexEdit._open(this.id, '')
      }
    },
    _getById: async function(data) {
      const result = await soilTest.itemGetById(data.id)
      console.log(result)
      if (result) {
        this.$refs.indexEdit._open('', result)
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加指标':
          this._editModal(false)
          break
        case '删除':
          this._deleteByIds()
          break
      }
    },
    _deleteByIds() {
      if (this.selectIds.length === 0) {
        // this.$msgTip('warning')
        this.$message.warning('请至少选择一条记录')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定删除 ' + this.selectIds.length + ' 条记录？',
          onOk: () => {
            this._delOk(this.selectIds)
          }
        })
      }
    },
    _delOk: async function(ids) {
      const result = await soilTest.itemDelete(ids)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    }
  }
}
</script>
