<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <search-bar
      :formConfigs="formConfig"
      ref="searchBarForm"
      @headBtnClick="buttonClick"
    />
    <!-- 列表 -->
    <base-table
      :page="queryParams.pageNo"
      :limit="queryParams.pageSize"
      :table-props="tableProps"
      :table-data="list"
      :max-height="tableH"
    >
      <method-btn
        v-if="tableBtn.length"
        slot="handleBtn"
        :width="80"
        label="操作"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
    <!-- 新增&编辑 -->
    <base-dialog
      :dialogTitle="addOrEditTitle"
      :dialogVisible="centervisible"
      @cancel="handleCancel"
      @confirm="handleConfirm"
      :before-close="handleCancel"
      width='60%'
    >
      <voc-add ref="vocAdd" @successSubmit="successSubmit" />
    </base-dialog>
  </div>
</template>
<script>
import { parseTime } from '@/utils/ruoyi'
import VocAdd from './components/vocAdd'
import { publicFormatter } from '@/utils/dict'
import { environmentalCheckPage, environmentalCheckDelete } from '@/api/safetyEnvironmental/environmental'
const tableProps = [
  {
    prop: 'name',
    label: '指标名称',
    minWidth: 120,
    showOverflowtooltip: true
  },
  {
    prop: 'code',
    label: '指标编码',
    minWidth: 120
  },
  {
    prop: 'unit',
    label: '单位',
    filter: publicFormatter('environment_check_unit')
  },
  {
    prop: 'minValue',
    label: '最小值'
  },
  {
    prop: 'maxValue',
    label: '最大值'
  },
  {
    prop: 'creator',
    label: '创建人'
  },
  {
    prop: 'createTime',
    label: '创建时间',
    filter: parseTime,
    minWidth: 160
  }
]
export default {
  name: 'VocDetectionIndication',
  data() {
    return {
      formConfig: [
        {
          type: 'input',
          label: '指标名称',
          placeholder: '指标名称',
          param: 'name'
        },
        {
          type: 'button',
          btnName: '查询',
          name: 'search',
          color: 'primary'
        },
        {
          type: this.$auth.hasPermi('base:voc:create') ? 'separate' : '',
        },
        {
          type: this.$auth.hasPermi('base:voc:create') ? 'button' : '',
          btnName: '新增',
          name: 'add',
          color: 'success',
          plain: true
        }
      ],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        checkType: 3,
        name: null
      },
      total: 0,
      tableProps,
      list: [],
      tableH: this.tableHeight(260),
      tableBtn: [
        this.$auth.hasPermi('base:voc:update')
          ? {
              type: 'edit',
              btnName: '编辑'
            }
          : undefined,
        this.$auth.hasPermi('base:voc:delete')
          ? {
              type: 'delete',
              btnName: '删除'
            }
          : undefined
      ].filter((v)=>v),
      addOrEditTitle: '',
      centervisible: false
    }
  },
  components: { VocAdd },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      environmentalCheckPage({...this.queryParams}).then(res => {
        this.list = res.data.list || []
        this.total = res.data.total || 0
      })
    },
    buttonClick(val) {
      console.log(val)
      if (val.btnName === 'search') {
        this.queryParams.name = val.name
        this.getList()
      } else {
        this.addOrEditTitle = '新增'
        this.centervisible = true
        this.$nextTick(() => {
          this.$refs.vocAdd.init()
        })
      }
    },
    handleClick(val) {
      console.log(val)
      switch (val.type) {
        case 'edit':
          this.addOrEditTitle = '编辑'
          this.centervisible = true
          this.$nextTick(() => {
            this.$refs.vocAdd.init(val.data.id)
          })
          break
        default:
          this.handleDelete(val.data)
      }
    },
    // 删除
    handleDelete(val) {
      this.$modal.confirm('是否确认删除"' + val.name + '"的数据项?').then(function() {
        return environmentalCheckDelete({ id: val.id })
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    // 新增
    handleCancel() {
      this.$refs.vocAdd.formClear()
      this.centervisible = false
      this.addOrEditTitle = ''
    },
    handleConfirm() {
      this.$refs.vocAdd.submitForm()
    },
    successSubmit() {
      this.handleCancel()
      this.getList()
    }
  }
}
</script>