<template>
    <div>
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>
          <a href="#">基础信息管理</a>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          收入各板块汇总表
        </el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
      <!--
        要resetFields起作用，必须配置:model和prop
      -->
      <el-form ref="queryForm" :model="queryModel" inline class="demo-form-inline">
        <el-form-item label="年份" prop="yearMonth">
          <el-date-picker
            size="mini"
            v-model="queryModel.yearMonth"
            type="month"
            placeholder="选择年月"
            value-format="yyyy-MM"
            format="yyyy-MM">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input type="text" size="mini" v-model="queryModel.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            icon="ios-search"
            @click="changePage(1)"
            :loading="loading"
          >查询</el-button>&nbsp;
          <el-button
            type="info"
            size="mini"
            style="margin-left: 8px"
            @click="handleReset('queryForm')"
          >重置</el-button>&nbsp;
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-row class="button-group">
        <el-button
          type="primary"
          size="small"
          plain
          icon="el-icon-upload2"
          @click="exportXls"
        >导出全部数据</el-button>
        <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          :disabled="multipleSelection.length==0"
          @click="selectExportXls"
        >导出所选数据</el-button>
      </el-row>
      <el-table
        :data="tableData"
        min-height="450"
        v-loading="loading"
        row-key="id"
        height="500"
        @selection-change="handleSelectionChange"
        :row-class-name="tableRowClassName"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column  prop="serialNum"  label="序号" width="250"></el-table-column>
        <el-table-column  prop="name"  label="收入子项目" width="300"></el-table-column>
        <el-table-column  prop="budgetRevenue"  label="预算收入(当前)">
            <template slot-scope="scope">
                {{formaNum(scope.row.curBudgetRevenue)}}
            </template>
        </el-table-column>
        <el-table-column  prop="curMonthReceived"  label="本月到账金额">
            <template slot-scope="scope">
            {{formaNum(scope.row.curMonthReceived)}}
          </template>
        </el-table-column>
        <el-table-column  prop="actualTotalRevenue"  label="实际总收入">
            <template slot-scope="scope">
            {{formaNum(scope.row.actualTotalRevenue)}}
          </template>
        </el-table-column>
        <el-table-column  prop="accumulatedArrears"  label="累计欠费">
            <template slot-scope="scope">
            {{formaNum(scope.row.accumulatedArrears)}}
          </template>
        </el-table-column>
        <el-table-column  prop="overIncome"  label="超额完成收入">
            <template slot-scope="scope">
            {{formaNum(scope.row.overIncome)}}
          </template>
        </el-table-column>
        <el-table-column  prop="percentage"  label="收入完成比率">
          <template slot-scope="scope">
            <span v-if="scope.row.percentage<50" style="color:#F56C6C">
              {{scope.row.percentage}}%
            </span>
            <span v-if="scope.row.percentage>=50&&scope.row.percentage<100" style="color:#E6A23C">
              {{scope.row.percentage}}%
            </span>
            <span v-if="scope.row.percentage==100" style="color:#67C23A">
              {{scope.row.percentage}}%
            </span>
          </template>
        </el-table-column>
        <el-table-column  prop="chargePerson"  label="责任人"></el-table-column>
        <!-- <el-table-column    label="累计已支出金额"></el-table-column>
        <el-table-column    label="实际收入节余"></el-table-column> -->
        <el-table-column    label="备注"></el-table-column>
      </el-table>
    </div>
  </template>
  <script>
  import Constant from "@/constant";
  import fundIncomeInfoApi from "@/api/base/fundIncomeInfo";
  
  export default {
    name: 'baseSectionsSummaryList',
    data() {
      var self = this;
  
      return {
        queryModel: {
            yearMonth: "",
            name:"",
        },
        loading: false,
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        totalPages: 0,
        totalElements: 0,
        field: "",
        direction: "",
        pageSizeList: [10, 20, 30],
        multipleSelection: [],
        modalTitle: "",
        modalTitle2: "",        
        businessKey: "",
        showModal: false,
        showModal2:false,
        dialogFormVisible:false,
        newPassword:"",
        roleResult:[],
        props: {
          // 配置项（必选）
          value: "id",
          label: "name",
          children: "children"
        },
        batchImportVisible: false,
        xlsLoading: false,
        batchImportFileList: [],
        chargingStationResult:[],
      };
    },
    computed:{
      formaNum(){
        return function(numer){
          if(numer!=null){
            return numer.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
          }
          else{
            numer;
          }
        }
      }
    },
    created(){
      var self = this;
      self.getNowTime();
    },
    methods: {
      changePage(pageIndex,exportFlag) {
        var self = this;
  
        console.log(pageIndex);
  
        self.pageIndex = pageIndex;
        var formData = new FormData();
  
        formData.append("name", self.queryModel.name);
        formData.append("yearMonth", self.queryModel.yearMonth);

        if(exportFlag!=null){
            formData.append("exportFlag", exportFlag);
        }
  
        self.loading = true;
  
        fundIncomeInfoApi.summaryList(formData).then(function(response) {
          self.loading = false;
            var jsonData = response.data;
            if(jsonData.result) {
                if(exportFlag){
                    //导出
                    self.$message({
                        showClose: true,
                        type: "success",
                        message: `报表已生成，<a href="${jsonData.data}">请点击链接下载</a>`,
                        dangerouslyUseHTMLString: true,
                        duration: 30000
                    });
                }
                else{
                    //分页查看
                    var page = jsonData.data;

                    console.log(jsonData.data)

                    self.tableData = page.data;
                    self.totalPages = page.totalPages;
                    self.totalElements = page.recordsTotal;
                }
            }
            else{
                self.$message.warning(jsonData.message);
            }
        }).catch((error)=>{
          self.loading = false;
        });
      },
      pageSizeChange(pageSize) {
        this.pageSize = pageSize;
      },
      sortChange(data) {
        this.field = data.column.field;
        this.direction = data.order == "ascending" ? "asc" : "desc";
  
        this.changePage(this.pageIndex);
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleReset(name) {
        this.$refs[name].resetFields();
      },
      handleAdd() {
        var self = this;
        
        self.modalTitle = "新增模板";
        self.businessKey = "";
        self.showModal = true;
      },
      handleEdit(record) {
        var self = this;
        
        self.modalTitle = "编辑模板";
        self.businessKey = record.id;
        self.showModal = true;
      },
      handleOption(record){
        var self = this;
        
        self.modalTitle2 = "编辑项目";
        self.businessKey = record.id;
        self.showModal2 = true;
      },
      handleDelete(record) {
        var self = this;
  
        this.$confirm("是否确认删除？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
            fundIncomeInfoApi.remove(record.id).then(function(response) {
            var jsonData = response.data;
  
            if (jsonData.result) {
              // var index = self.tableData.indexOf(record);
              // self.tableData.splice(index, 1);
              self.changePage(self.pageIndex);
  
              self.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        });
      },
      handleBatchDelete() {
        var self = this;
  
        var idList = this.multipleSelection.map(record => {
          return record.id;
        });
  
        this.$confirm("是否确认删除选中项？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
            fundIncomeInfoApi.batchRemove(idList).then(function(response) {
            var jsonData = response.data;
  
            if (jsonData.result) {
              self.changePage(self.pageIndex);
  
              self.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        });
      },
      exportXls() {
        this.changePage(1,true);
      },
      selectExportXls() {
        var self = this;
  
        fundIncomeInfoApi.selectExportXls(self.multipleSelection).then(function(response) {
          var jsonData = response.data;

          if (jsonData.result) {
            //导出
            self.$message({
                showClose: true,
                type: "success",
                message: `报表已生成，<a href="${jsonData.data}">请点击链接下载</a>`,
                dangerouslyUseHTMLString: true,
                duration: 30000
            });
          }
        });

      },
        onDetailModalClose(refreshed) {
            this.showModal = false;
    
            if(refreshed) {
            this.changePage(this.pageIndex);
            }
        },

        getNowTime() {
            var now = new Date();
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth(); //得到月份
            month = month + 1;
            month = month.toString().padStart(2, "0");
            var defaultDate = `${year}-${month}`;
            this.$set(this.queryModel, "yearMonth", defaultDate);
        },
        tableRowClassName({row, rowIndex}) {
          if (row.isSum) {
            return 'success-row';
          }
          if (row.isSum2) {
            return 'warning-row';
          }
          else{
            return ''; 
          }
        },
    },
    mounted: function() {
      // var self = this;
      // this.changePage(1);
    },
    components: {
    }
  };
  </script>
  <style lang="scss" scoped>
  .el-breadcrumb {
    margin: 10px;
    line-height: 20px;
  }
  
  .el-divider {
    margin: 5px 0;
  }
  
  .demo-form-inline {
    margin-left: 10px;
    text-align: left;
  }
  
  .button-group {
    padding: 10px;
    text-align: left;
  }
  </style>
<style>
    .el-table .success-row {
      background: #f0f9eb;
    }
    .el-table .warning-row {
    background: oldlace;
  }
</style>