<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"
    >
      <energy-plc-add ref="energyPlc" @successSubmit="successSubmit" />
    </base-dialog>
  </div>
</template>

<script>
import { getEnergyPlcPage, deleteEnergyPlc } from "@/api/base/energyPlc";
// import { publicFormatter } from '@/utils/dict'
import EnergyPlcAdd from './components/energyPlcAdd.vue'
const tableProps = [
  {
    prop: 'plcTableName',
    label: '关联表名'
  },
  {
    prop: 'code',
    label: '关联表编码',
    minWidth: 150,
    showOverflowtooltip: true
  },
  {
    prop: 'name',
    label: '标识名'
  },
  {
    prop: 'enName',
    label: '英文标识名'
  },
  {
    prop: 'collection',
    label: '是否采集'
  },
  {
    prop: 'description',
    label: '描述',
    showOverflowtooltip: true
  }
]
export default {
  name: "EnergyPlc",
  components: { EnergyPlcAdd },
  data() {
    return {
      formConfig: [
        {
          type: 'input',
          label: '标识名',
          placeholder: '标识名',
          param: 'name'
        },
        {
          type: 'button',
          btnName: '查询',
          name: 'search',
          color: 'primary'
        },
        {
          type: 'separate'
        },
        {
          type: this.$auth.hasPermi('base:energy-plc:create') ? 'button' : '',
          btnName: '新增',
          name: 'add',
          color: 'success',
          plain: true
        }
      ],
      tableProps,
      tableBtn: [
        this.$auth.hasPermi('base:energy-plc:update')
          ? {
              type: 'edit',
              btnName: '编辑'
            }
          : undefined,
        this.$auth.hasPermi('base:energy-plc:delete')
          ? {
              type: 'delete',
              btnName: '删除'
            }
          : undefined
      ].filter((v) => v),
      tableH: this.tableHeight(260),
      collectionList: [
        {value: 0,label: '否'},
        {value: 1,label: '是'}
      ],
      // 总条数
      total: 0,
      // 班次基础信息列表
      list: [],
      // 弹出层标题
      addOrEditTitle: "",
      // 是否显示弹出层
      centervisible: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        name: null
      }
    };
  },
  created() {
    window.addEventListener('resize', () => {
      this.tableH = this.tableHeight(260)
    })
    this.getList();
  },
  methods: {
    buttonClick(val) {
      switch (val.btnName) {
        case 'search':
          this.queryParams.pageNo = 1;
          this.queryParams.name = val.name
          this.getList()
          break
        default:
          this.addOrEditTitle = '新增'
          this.centervisible = true
          this.$nextTick(() => {
            this.$refs.energyPlc.init()
          })
      }
    },
    /** 查询列表 */
    getList() {
      getEnergyPlcPage(this.queryParams).then(response => {
        let arr = response.data.list || [];
        arr&&arr.map(item => {
          this.collectionList.map(i => {
            if (item.collection === i.value) {
              item.collection = i.label
            }
          })
        })
        this.list = arr
        this.total = response.data.total;
      });
    },
    handleClick(val) {
      switch (val.type) {
        case 'edit':
          this.addOrEditTitle = '编辑'
          this.$nextTick(() => {
            this.$refs.energyPlc.init(val.data.id)
          })
          this.centervisible = true
          break
        default:
        this.handleDelete(val.data)
      }
    },
    handleCancel() {
      this.$refs.energyPlc.formClear()
      this.centervisible = false
      this.addOrEditTitle = ''
    },
    handleConfirm() {
      this.$refs.energyPlc.submitForm()
    },
    successSubmit() {
      this.handleCancel()
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除关联表名为"' + row.name + '"的数据项?').then(function() {
          return deleteEnergyPlc(row.id);
        }).then(() => {
          this.queryParams.pageNo = 1;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    }
  }
};
</script>
