<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24" style="margin-top:10px">
            <Form :d="formId" :label-width="90" v-show="searchOpen" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="search-item" label="判定依据:" style="margin-left: -20px">
                <Input @on-enter="_formSearch" v-model="formObj.code" name="judgeBasis" placeholder="请输入判定依据" clearable/>
              </Form-item>
              <Form-item class="search-item" label="判定依据名称:">
                <Input @on-enter="_formSearch"  v-model="formObj.name" name="judgeBasisName" placeholder="请输入判定依据名称" clearable/>
              </Form-item>
              <Form-item class="search-btn">
                <Button @click="_formSearch" type="primary">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <!--操作-->
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" :showSearchBtn="true" @on-result-change="_btnClick"
                     class="contHide"></btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable
              ref="pageTable"
              :form-id="formId"
              :tableHeight="tableHeight"
              :getPage="getPage"
              :iconMsg="iconMsg"
              @on-result-change="_tableResultChange">
              <vxe-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :field="item.key"
                :title="item.title"
                :width="item.width"
                :min-width="200"
                :fixed="item.fixed?item.fixed:undefined"
                sortable
              >
                <template slot-scope="scope">
                  <span v-if="item.dateTime">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):''}}</span>
                  <span v-else>{{scope.row[item.key]}}</span>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </div>
    <!-- 添加、编辑 -->
    <MeterAptitudeStandardEdit ref="editModal" @on-result-change="_page"></MeterAptitudeStandardEdit>
    <!--导入资质项目-->
<!--    <DownloadTemplateImport ref="sampleModal" @on-result-change="_page"></DownloadTemplateImport>-->
  </div>
</template>
<script>
import { meterAptitude } from '../../../api'
import MeterAptitudeStandardEdit from './MeterAptitudeStandardEdit'
export default {
  name: 'MeterAptitudeStandard',
  components: { MeterAptitudeStandardEdit },
  data() {
    return {
      formId: 'meterAptitudeFormId',
      pageColumns: [
        { title: '检测依据', key: 'code', width: 160 },
        { title: '检测依据名称', key: 'name' },
        // {title: '限制范围或说明', key: 'limitDescription'},
        { title: '登记时间', key: 'ctime', dateTime: true, width: 140 },
        { title: '备注', key: 'remark' }
      ],
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: 'error', id: '', name: '删除' },
        { type: '', id: '', name: '导入' },
        { type: '', id: '', name: '导出' }
      ],
      iconMsg: [
        { type: 'ios-clock', id: '', name: '编辑' },
        { type: 'ios-clock', id: '', name: '删除' }
      ],
      formObj: {
        name: undefined,
        code: undefined
      },
      searchOpen: true,
      selectIds: [],
      getPage: {},
      typeData: []
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this._editModal(false)
          break
        case '删除':
          this._deleteSelected()
          break
        case '导入':
          this._import()
          break
        case '导出':
          this._export()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(true, data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
        case '操作日志':
          this._record(data.id)
          break
      }
    },
    _page: async function() {
      console.log(9522123)
      // this.$refs.pageTable._page('search-form-package', 'FoodJudgeBasis/page')
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      console.log('this.formObj', this.formObj)
      const result = await meterAptitude.page(this.formObj)
      if (result) {
        console.log(55555)
        console.log(result)
        this.getPage = result
        console.log(123456789)
        console.log(this.getPage)
        this.$refs.pageTable._hideLoading()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.getPage = this.$store.state.FoodJudgeBasis.page
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    // 删除
    _deleteByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._delete(ids)
        }
      })
    },
    _delete: async function(ids) {
      const result = await meterAptitude.deleteById(ids)
      if (result) {
        this._formSearch()
        this.$Message.success('删除成功！')
      }
    },
    _deleteById(id) {
      // 删除一条记录
      this._deleteByIds([id])
    },
    _deleteSelected() {
      // 批量删除
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请选择一条或多条数据！')
      } else {
        this._deleteByIds(ids, '确定删除这 ' + ids.length + ' 条记录？')
      }
    },
    // 编辑&添加
    _editModal(edit, id) {
      if (edit) {
        this._getById(id)
        // 编辑
        // this.$store.dispatch('FoodJudgeBasis/getById', id).then(() => {
        //   this.$refs.editModal._open(this.$store.state.FoodJudgeBasis.model)
        // })
      } else {
        // 添加
        this.$refs.editModal._open()
      }
    },

    _getById: async function(id) {
      const result = await meterAptitude.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    // 导入
    _import() {
      const data = {
        importUrl:
          '/food/v1/food_aptitude_judge_basis/food_aptitude_judge_import',
        downloadUrl: '/food/v1/excel/template/FoodAptitudeJudgeBasis',
        title: '导入检验方法管理'
      }
      this.$refs.sampleModal._open(data)
    },
    // 导出
    _export() {
      console.log('.this.selectIds', this.selectIds)
      if (this.selectIds.length === 0) {
        this.$Message.warning('请选择需要导出的数据')
      } else {
        const content = '确定导出 ' + this.selectIds.length + ' 条记录？'
        this.$Modal.confirm({
          title: '提示',
          content: content,
          onOk: () => {
            // eslint-disable-next-line no-undef
            http.open(
              '/food/v1/food_aptitude_judge_basis/food_aptitude_judge_export?ids=' +
                this.selectIds
            )
          }
        })
      }
    }
  }
}
</script>

<style scoped>
</style>
