<template>
  <basic-container>
    <avue-crud
      :option="option"
      v-model:search="search"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button type="danger" @click="handleDelete">删除</el-button>
      </template>
      <template #menu-right>
        <!-- <el-button type="primary"  @click="handleImport">导入</el-button> -->
      </template>
      <template #menu="scope">
        <el-button type="primary" text @click="editRow(scope.row)">修改</el-button>
      </template>
    </avue-crud>

    <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/QA/CycleTestItem/download-excel-template"
      templateName="试验项目模板.xlsx"
      importUrl="/blade-desk/QA/CycleTestItem/import-excel"
      @closeDialog="closeDialog"></basic-import>
  </basic-container>
  <add-batch-monitoring 
    v-if="showDialog" 
    :title="title"
    :showDialog="showDialog" 
    :checkRow="checkRow"
    :moldAddMore="moldAddMore"
    @closeDialog="closeDialog">
  </add-batch-monitoring>
</template>
<script>
import addBatchMonitoring from './components/addBatchMonitoring.vue'
import basicImport from '@/components/basic-import/main.vue'
import { pageList, removeItem, saveItem, exportData } from '@/api/energyManagement/bsEnergyMonitor';

export default {
  components: {
    basicImport,
    addBatchMonitoring
  },
  data() {
    return {
      loading:false,
      data:[],
      isShowImport:false,
      selectionList: [],
      query: {},
      search: {},
      showDialog:false,
      title:"",
      moldAddMore:false,
      option: {
        height: "auto",
        calcHeight: 32,
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn:false,
        editBtnText: "修改",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        viewBtnText: "详情",
        labelWidth: 120,
        menuWidth: 80,
        dialogWidth: 1040,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "center",
        gridBtn: false,
        searchMenuPosition: "right",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        align: "center",
        column: [
          {
            label: '月份',
            prop: 'month',
            type: "month",
            sortable: true,
            filter: true,
            format:'YYYY-MM',
            valueFormat:'YYYY-MM',
            span: 24,
            search: true,
            width: 120,
            searchLabelWidth: 50,
          },
          {
            label: "上年度月用水目标",
            prop: "lastWaterTarget",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 170,
          },
          {
            label: "上年度月用电目标",
            prop: "lastElectricTarget",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 170,
          },
          {
            label: "上年度月用水",
            prop: "lastWater",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 140,
          },
          {
            label: "上年度月用电",
            prop: "lastElectric",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 140,
          },
          {
            label: "上年度月产值",
            prop: "lastOutput",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 140,
          },
          {
            label: "上年度月用水实际完成",
            prop: "lastWaterRate",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 200,
          },
          {
            label: "上年度月用电实际完成",
            prop: "lastElectricRate",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 200,
          },
          {
            label: "本年度月用水目标",
            prop: "currentWaterTarget",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 170,
          },
          {
            label: "本年度月用电目标",
            prop: "currentElectricTarget",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 170,
          },
          {
            label: "本年度月用水",
            prop: "currentWater",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 140,
          },
          {
            label: "本年度月用电",
            prop: "currentElectric",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 140,
          },
          {
            label: "本年度月产值",
            prop: "currentOutput",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 140,
          },
          {
            label: "本年度月用水实际完成",
            prop: "currentWaterRate",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 200,
          },
          {
            label: "本年度月用电实际完成",
            prop: "currentElectricRate",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 200,
          },
          // {
          //   label: "创建人",
          //   prop: "userName",
          //   search: false,
          //   sortable: true,
          //   filter: true,
          //   span: 12,
          //   width: 140,
          // },
          // {
          //   label: "创建时间",
          //   prop: "uploadTime",
          //   search: false,
          //   sortable: true,
          //   filter: true,
          //   span: 12,
          //   width: 140,
          // },
        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      checkRow:{},
    };
  },
  methods: {
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    closeDialog(val){
      this.isShowImport = false
      this.showDialog = false
      this.moldAddMore = false
      if(val){
        this.onLoad()
      }
    },
    handleAdd(){
      this.title = '新增'
      this.checkRow = {}
      this.moldAddMore = true
      this.showDialog = true
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const ids = this.selectionList.map(item => item.id).join(',');
        removeItem(ids).then(() => {
          this.$message.success("删除成功");
          this.onLoad(this.page);
          this.selectionClear();
        });
      });
    },
    editRow(row){
      this.title = "修改"
      this.moldAddMore = false
      this.checkRow = row
      this.showDialog = true
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeItem(row.id).then(() => {
          this.$message.success("删除成功");
          this.onLoad(this.page);
        });
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    onLoad() {
      this.loading = true;
      pageList(
        this.page.currentPage,
        this.page.pageSize,
        {...this.query}
      ).then(res => {
        console.log('res------------',res);
        res.data.data.records.map(item =>{
          console.log('item------------',item);
          item.lastWaterTarget = item.lastWaterTarget ? item.lastWaterTarget == -1 ? '' : item.lastWaterTarget : '';
          item.lastElectricTarget = item.lastWaterTarget ? item.lastElectricTarget == -1 ? '' : item.lastElectricTarget :""
          item.lastWater = item.lastWater ? item.lastWater == -1 ? '' : item.lastWater : ''
          item.lastElectric = item.lastElectric ? item.lastElectric == -1 ? '' : item.lastElectric : ''
          item.lastOutput = item.lastOutput == -1 ? '' : item.lastOutput
          item.currentWaterTarget = item.currentWaterTarget ? item.currentWaterTarget == -1 ? '' : item.currentWaterTarget : ''
          item.currentElectricTarget = item.currentElectricTarget ? item.currentElectricTarget == -1 ? '' : item.currentElectricTarget : ''
          item.currentWater = item.currentWater ? item.currentWater == -1 ? '' : item.currentWater : ''
          item.currentElectric = item.currentElectric ? item.currentElectric == -1 ? '' : item.currentElectric : ''
          item.currentOutput = item.currentOutput ? item.currentOutput == -1 ? '' : item.currentOutput : ''
          item.lastWaterRate = item.lastWaterRate ? item.lastWaterRate == -1 ? '' : item.lastWaterRate : ''
          item.lastElectricRate = item.lastElectricRate ? item.lastElectricRate == -1 ? '' : item.lastElectricRate : ''
          item.currentWaterRate = item.currentWaterRate ? item.currentWaterRate == -1 ? '' : item.currentWaterRate : ''
          item.currentElectricRate = item.currentElectricRate ? item.currentElectricRate == -1 ? '' : item.currentElectricRate : ''
        })
        this.data = res.data.data.records;
        this.page.total = res.data.data.total;
        console.log('data-----------',this.data)
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
