<template>
  <div class="pkg-show">
    <el-form inline>
      <el-form-item label="日期">
        <el-select
          v-model="dataDate"
        >
          <el-option
            v-for="(item,index) in dateList"
            :key="index"
            :label="item"
            :value="item"
          > 

          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="请选择代理商">
        <el-select
          v-model="agentId"
          clearable
          placeholder="请选择代理商"
          filterable
        >
          <el-option
            v-for="item of agentList"
            :key="item.id"
            :label="item.agentName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-button type="primary" @click="fetchData()">查询</el-button>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column align="center" label="代理商名称" prop="agent_name"></el-table-column>
      <el-table-column align="center" label="总消耗" prop="total_cost" :formatter="toFixed"></el-table-column>
      <el-table-column align="center" label="现金消耗" prop="cash_cost" :formatter="tofix"></el-table-column>
      <el-table-column align="center" label="现金消耗（含返点）" prop="rebate_cash_cost" :formatter="tofixed"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { putonAgent } from "@/api/agent";
import { getBusinIncome } from "@/api/cloud";
export default {
  data() {
    return {
      agentId: "全部", //代理商
      agentList: [], // 代理商列表
      tableData: [],
      dataDate: '',
      dateList: []
    }
  },
  created() {
    this.putonAgent();
  },
  mounted() {
    this.getYear();
  },
  methods: {
    //获取代理商列表
    putonAgent: function () {
      putonAgent("get", {}).then((res) => {
        this.listLoading = false;
        if (res.status === 200) {
          this.agentList = res.result.data;
          // this.agentList.unshift('全部');
          this.agentList.unshift(
            {
              agentName: "全部",
              id: ""
            }
          )
        } else {
          alert(res.msg);
        }
      });
    },
    fetchData() {
      console.log(this.dataDate.slice(5))
      if(this.dataDate.slice(5) == '04' || this.dataDate.slice(5) == '06' || this.dataDate.slice(5) == '09' || this.dataDate.slice(5) == '11' ){
        let end = this.dataDate.concat('-30')
        let params = {
          startDate: this.dataDate.concat('-01'),
          endDate: end,
          sqlId: 1349,
          agentId: this.agentId === "全部" ? '' : this.agentId
        };
        getBusinIncome(params).then((res) => {
          this.tableData = res.result.data;
        })
      }else if(this.dataDate.slice(5) == '01' || this.dataDate.slice(5) == '03' || this.dataDate.slice(5) == '05' || this.dataDate.slice(5) == '07' || this.dataDate.slice(5) == '08' || this.dataDate.slice(5) == '10' || this.dataDate.slice(5) == '12'){
        let end = this.dataDate.concat('-31')
        let params = {
          startDate: this.dataDate.concat('-01'),
          endDate: end,
          sqlId: 1349,
          agentId: this.agentId === "全部" ? '' : this.agentId
        };
        getBusinIncome(params).then((res) => {
          this.tableData = res.result.data;
        })
      }else if(this.dataDate.slice(5) == '02' && Number(this.dataDate.slice(0,4)%100)!=0 &&  Number(this.dataDate.slice(0,4)%4)==0 ||  Number(this.dataDate.slice(0,4)%400)==0){
        let end = this.dataDate.concat('-29')
        let params = {
          startDate: this.dataDate.concat('-01'),
          endDate: end,
          sqlId: 1349,
          agentId: this.agentId === "全部" ? '' : this.agentId
        };
        getBusinIncome(params).then((res) => {
          this.tableData = res.result.data;
        })
      }else{
        let end = this.dataDate.concat('-28')
        let params = {
          startDate: this.dataDate.concat('-01'),
          endDate: end,
          sqlId: 1349,
          agentId: this.agentId === "全部" ? '' : this.agentId
        };
        getBusinIncome(params).then((res) => {
          this.tableData = res.result.data;
        })
      }
    },
    getYear() {
      var dataArr = [];
      var data = new Date();
      var year = data.getFullYear();
      data.setMonth(data.getMonth()+1, 1)//获取到当前月份,设置月份
      for (var i = 0; i < 12; i++) {
        data.setMonth(data.getMonth() - 1);//每次循环一次 月份值减1
          var m = data.getMonth() + 1;
          m = m < 10 ? "0" + m : m;
          dataArr.push(data.getFullYear() + "-" + (m))
      }
      // console.log(dataArr)
      this.dateList = dataArr;
    },
    toFixed(row, column, cellValue) {
      var newData = row.total_cost.toFixed(2);
      return newData
    },
    tofix(row,column) {
      var newData = row.cash_cost.toFixed(2);
      return newData
    },
    tofixed(row, column) {
      var newData = row.rebate_cash_cost.toFixed(2);
      return newData
    }
  }
}
</script>

<style scoped>
  .pkg-show{
    padding:  25px;
  }
</style>