<template>
  <basic-container>
    <avue-crud :option="option"
               v-model:search="search"
               v-model:page="page"
               v-model="form"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @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"
                   icon="el-icon-position"
                   @click="handleConfirm">确认低效
        </el-button>
        <el-button type="warning"
                   plain
                   icon="el-icon-delete"
                   @click="handleDelete">删 除
        </el-button>
        <el-button type="warning"
                   plain
                   icon="el-icon-download"
                   @click="handleExport">导 出
        </el-button>
      </template>

      <template #guardRate="{ row }">
        {{ (row.guardRate*100).toFixed(2) }}%
      </template>
      <template #finishRate="{ row }">
        {{ (row.finishRate*100).toFixed(2) }}%
      </template>

      <template #during="{ row }">
        {{ row.start}}至{{ row.end}}
      </template>


      <template #guardRate-search>
        <el-input v-model="guardRateMin" style="width: 40%" placeholder="最小值" />
        -
        <el-input v-model="guardRateMax" style="width: 40%" placeholder="最大值" />
      </template>

    </avue-crud>

    

    <el-dialog v-model="confirmDialog" title="确认提醒" width="500" center>
      <span>
        该电站是否为低效电站？
      </span>
      <p><el-input v-model="remarks" type="textarea" placeholder="请输入备注信息"/></p>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="submitConfirm(2)">确认非低效</el-button>
          <el-button type="primary" @click="submitConfirm(1)">
            确认低效
          </el-button>
        </div>
      </template>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, confirmInefficient} from "@/api/nh/powerStationInefficient";
  import option from "@/option/nh/powerStationInefficientConfirm";
  import {mapGetters} from "vuex";
  import {exportBlob} from "@/api/common";
  import {getToken} from '@/utils/auth';
  import {downloadXls} from "@/utils/util";
  import {dateNow} from "@/utils/date";
  import NProgress from 'nprogress';
  import 'nprogress/nprogress.css';

  export default {
    props: ["activeCapital"],
    watch: {
      activeCapital(newVal) {
        this.onLoad(this.page);
      }
    },
    data() {
      return {
        form: {},
        query: {},
        confirmDialog: false,
        remarks:"",
        guardRateMin:"",
        guardRateMax:"",
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: option,
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.powerStationInefficient_add, false),
          viewBtn: this.validData(this.permission.powerStationInefficient_view, false),
          delBtn: this.validData(this.permission.powerStationInefficient_delete, false),
          editBtn: this.validData(this.permission.powerStationInefficient_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleTransfer(){

      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleConfirm(){
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.confirmDialog = true;
      },
      submitConfirm(isConfirm){
        if(this.remarks.trim()==""){
          this.$message.warning("请输入备注");
          return;
        }
        
        this.$confirm("确认处理选择数据?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return confirmInefficient(this.ids,isConfirm,this.remarks);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.remarks="";
            this.confirmDialog = false;
            this.$refs.crud.toggleSelection();
          });

      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleExport() {
        let downloadUrl = `/nh/powerStationInefficient/export-powerStationInefficient?${this.website.tokenHeader}=${getToken()}`;
        let values = {
          isConfirm_notequal: 1,
          status_equal: 0
        };
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          NProgress.start();
          exportBlob(downloadUrl, values).then(res => {
            downloadXls(res.data, `低效电站${dateNow()}.xlsx`);
            NProgress.done();
          })
        });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.guardRateMin="";
        this.guardRateMax="";
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;

        const {
          status,stationCode,capitalName,installAddress
        } = this.query;

        let guardRateMin=this.guardRateMin,
            guardRateMax=this.guardRateMax;
          if(guardRateMin){
            guardRateMin = guardRateMin/100;
          }
          if(guardRateMax){
            guardRateMax = guardRateMax/100;
          }

        let values = {
          capitalName_equal: this.activeCapital,
          isConfirm_notequal: 1,
          status_equal: 0,
          stationCode,
          capitalName,
          installAddress,
          "guardRate_gt":guardRateMin,
          "guardRate_lt":guardRateMax
        };

        console.log(values)

        getList(page.currentPage, page.pageSize, values).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
