<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="160" 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-statistics-add ref="energyStatistics" @successSubmit="successSubmit" />
    </base-dialog>
    <!-- 参数绑定/查看 -->
    <energy-statistics-det ref="plcParam" @closeDrawer="closeDrawer"
      :energyTypeList="energyTypeList"></energy-statistics-det>
  </div>
</template>

<script>
import { getEnergyStatisticsPage, deleteEnergyStatistics } from "@/api/monitoring/energyStatistics";
import { publicFormatter } from '@/utils/dict'
import { getEnergyTypeListAll } from '@/api/base/energyType'
import EnergyStatisticsAdd from './components/energyStatisticsAdd'
import EnergyStatisticsDet from './components/energyStatisticsDet'
const tableProps = [
  {
    prop: 'name',
    label: '方案名称'
  },
  {
    prop: 'code',
    label: '方案编码',
    minWidth: 120,
    showOverflowtooltip: true
  },
  {
    prop: 'type',
    label: '统计类型',
    filter: publicFormatter('statistic_type')
  },
  {
    prop: 'energyType',
    label: '能源类型'
  },
  {
    prop: 'paramNum',
    label: '统计参数数量'
  },
  {
    prop: 'remark',
    label: '备注'
  }
]
export default {
  name: "EnergyStatistics",
  components: { EnergyStatisticsAdd, EnergyStatisticsDet },
  data() {
    return {
      formConfig: [
        {
          type: 'input',
          label: '方案名称',
          placeholder: '方案名称',
          param: 'name'
        },
        {
          type: 'button',
          btnName: '查询',
          name: 'search',
          color: 'primary'
        },
        {
          type: 'separate'
        },
        {
          type: this.$auth.hasPermi('monitoring:energy-statistics:create') ? 'button' : '',
          btnName: '新增',
          name: 'add',
          color: 'success',
          plain: true
        }
      ],
      tableProps,
      tableBtn: [
        this.$auth.hasPermi('monitoring:energy-statistics:bind')
          ? {
            type: 'connect',
            btnName: '绑定'
          }
          : undefined,
        {
          type: 'detail',
          btnName: '详情'
        },
        this.$auth.hasPermi('monitoring:energy-statistics:update')
          ? {
            type: 'edit',
            btnName: '编辑'
          }
          : undefined,
        this.$auth.hasPermi('monitoring:energy-statistics:delete')
          ? {
            type: 'delete',
            btnName: '删除'
          }
          : undefined
      ].filter((v) => v),
      tableH: this.tableHeight(260),
      // 总条数
      total: 0,
      // 班次基础信息列表
      list: [],
      // 弹出层标题
      addOrEditTitle: "",
      // 是否显示弹出层
      centervisible: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        name: null
      },
      energyTypeList: []
    };
  },
  created() {
    window.addEventListener('resize', () => {
      this.tableH = this.tableHeight(260)
    })
    this.getList();
  },
  mounted() {
    // 获取能源列表
    this.getEnergyTypeList()
  },
  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.energyStatistics.init()
          })
      }
    },
    /** 查询列表 */
    getList() {
      getEnergyStatisticsPage(this.queryParams).then(response => {
        let arr = response.data.list || [];
        this.list = arr
        this.total = response.data.total;
      });
    },
    handleClick(val) {
      console.log(val)
      switch (val.type) {
        case 'edit':
          this.addOrEditTitle = '编辑'
          this.$nextTick(() => {
            this.$refs.energyStatistics.init(val.data.id)
          })
          this.centervisible = true
          break
        case 'delete':
          this.handleDelete(val.data)
          break
        case 'detail':
          this.$nextTick(() => {
            this.$refs.plcParam.init(val.data, 'detail')
          })
          break
        default:
          this.$nextTick(() => {
            this.$refs.plcParam.init(val.data, 'connect')
          })
      }
    },
    handleCancel() {
      this.$refs.energyStatistics.formClear()
      this.centervisible = false
      this.addOrEditTitle = ''
    },
    handleConfirm() {
      this.$refs.energyStatistics.submitForm()
    },
    successSubmit() {
      this.handleCancel()
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除方案名称为"' + row.name + '"的数据项?').then(function () {
        return deleteEnergyStatistics(row.id);
      }).then(() => {
        this.queryParams.pageNo = 1;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    closeDrawer() {
      this.getList()
    },
    getEnergyTypeList() {
      getEnergyTypeListAll().then((res) => {
        this.energyTypeList = res.data || []
      })
    }
  }
};
</script>
