<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24">
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="90"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="search-item" label="检测依据:">
          <Input v-model="formObj.code" placeholder="请输入检测依据" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-item" label="检测依据名称:">
          <Input v-model="formObj.name" placeholder="请输入检测依据名称" clearable @on-enter="_formSearch" />
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24">
      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
      </Col>
      <!--table-->
      <Col span="24">
      <element-table
        id="testBasisTable"
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="140"
        :icon-msg="iconMsg"
        show-check-box
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          show-overflow-tooltip
          sortable
          :prop="item.key"
          :label="item.title"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <StandardEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { standard } from '../../../api'
import StandardEdit from './StandardEdit'

export default {
  components: { StandardEdit },
  data() {
    return {
      searchOpen: true,
      formObj: {
        code: '',
        name: ''
      },
      selectIds: [],
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: 'error', id: '', name: '删除' }
        // { type: 'primary', id: '', name: '导入' },
        // { type: 'primary', id: '', name: '导出' }
      ],
      pageColumns: [
        { title: '检测依据', key: 'code', width: 220, fixed: 'left' },
        { title: '检测依据名称', key: 'name', width: 550 },
        { title: '备注', key: 'remark' }
      ],
      getPage: {},
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await standard.pageStandard(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case '删除':
          this._deleteByIds()
          break
        case '导出':
          if (this.getPage.records.length === 0) {
            this.$Message.warning('暂无数据，不可导出！')
          } else {
            this._export()
          }
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'selectIds':
          this.selectIds = data
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
        case '编辑':
          this._editModal(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该数据？',
        onOk: () => {
          this._deleteOk(id)
        }
      })
    },
    _deleteByIds() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定删除 ' + ids.length + ' 条记录？',
          onOk: () => {
            this._deleteOk(ids)
          }
        })
      }
    },
    _deleteOk: async function(id) {
      const result = await standard.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await standard.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    _export: async function() {
      const result = await standard.page(this._searchParamsTemp())
      // 导出全部数据
      if (!result) {
        return
      }
      this.$Modal.confirm({
        title: '提示',
        content: '确定导出全部数据（最多导出5000条）？',
        onOk: () => {
          this.$exportExcel(
            'testBasisTable',
            '检测依据',
            this.pageColumns,
            result.records
          )
        }
      })
    },
    // 参数
    _searchParamsTemp: function() {
      const data = this.$serializeForm(this.formObj)
      return this.$extend(data, { page: 1, rows: 5000 })
    }
  }
}
</script>
