<template>
  <!-- 员工工资 -->
  <div>
    <div class="staffRanking_header padding-bottom-xs">
      <div>
        <span class="demonstration padding-left-xs">选择日</span>
        <el-date-picker v-model="days" :picker-options="pickerOptions" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
        <el-button type="primary" size="mini" @click="getData">查询</el-button>
      </div>
      <div class="block">
      </div>
    </div>
    <div class="padding-left-xs">
      <Forms :lists='tableDate' :pay='payLists' ref="form"></Forms>
    </div>
  </div>
</template>

<script>
import { storeDailys, exportachi, exportachiwhole } from "@/api/statistics.js";
import { partten } from "../../../../partten/index";
import perform from "./examine/index";
import Forms from './form.vue'
export default {
  name:"storeDaily", 
  components: { perform ,Forms},
  data() {
    return {
      headOffice: sessionStorage.getItem("headOffice") * 1,
      days: "",
      payLists:[],
      tableDate: [],
      month: partten.months,
      queryForm: {},
      pickerOptions: {
        //组件限制方法
        onPick: ({ maxDate, minDate }) => {
          this.choiceDate = minDate.getTime();
          if (maxDate) {
            this.choiceDate = "";
          }
        },
        disabledDate: (time) => {
          const self = this;
          if (!!self.choiceDate) {
            const startDay =
              (new Date(self.choiceDate).getDate() - 1) * 24 * 3600 * 1000;
            const endDay =
              (new Date(
                new Date(self.choiceDate).getFullYear(),
                new Date(self.choiceDate).getMonth() + 1,
                0
              ).getDate() -
                new Date(self.choiceDate).getDate()) *
              24 *
              3600 *
              1000;
            let minTime = self.choiceDate - startDay;
            let maxTime = self.choiceDate + endDay;
            return time.getTime() < minTime || time.getTime() > maxTime;
          }
        },
      },
    };
  },
  methods: {
    //导出方法
    exit() {
      let date1 = this.formatTime(this.days[0], "YYYY-MM-DD");
      let date2 = this.formatTime(this.days[1], "YYYY-MM-DD");
      let form = { startDate: date1, endDate: date2 };
      exportachi(form).then((res) => {
        let blob = new Blob([res]);
        var a = document.createElement("a");
        var url = window.URL.createObjectURL(blob);
        console.log(url);
        a.href = url;
        a.download = "员工业绩导出表.xlsx";
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    exits() {
      let date1 = this.formatTime(this.days[0], "YYYY-MM-DD");
      let date2 = this.formatTime(this.days[1], "YYYY-MM-DD");
      let form = { startDate: date1, endDate: date2 };
      exportachiwhole(form).then((res) => {
        let blob = new Blob([res]);
        var a = document.createElement("a");
        var url = window.URL.createObjectURL(blob);
        console.log(url);
        a.href = url;
        a.download = "员工工资明细导出表.xlsx";
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    changeMonth(res) {
      if(!res){
        this.payLists = null
        this.tableDate = []
        return
      }
      this.days = res;
      this.getData();
    },
    getData() {
      this.$refs.form.loading = true
      this.queryForm.startDate = this.days[0];
      this.queryForm.endDate = this.days[1];
      this.queryForm.storeId = sessionStorage.getItem("storeId");
      storeDailys(this.queryForm).then((res) => {
        this.$refs.form.loading = false
        if (res.code == "000000") {
          console.log('这是什么----->',res.data.payLists)
          this.tableDate = res.data.lists;
          this.payLists = res.data.payLists
          if(this.payLists == null){
            this.payLists = []
          }
          console.log('this.payLists---->',this.payLists)
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //指定列求和
    //数组按对应字段合计返回方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (column.property == "baseSalary") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return (prev + curr).toFixed(2) * 1;
            } else {
              sums[index] = "--";
            }
          }, 0);
          sums[index] += " ";
        } else if (column.property == "zyj") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return (prev + curr).toFixed(2) * 1;
            } else {
              sums[index] = "--";
            }
          }, 0);
          sums[index] += " ";
        } else if (column.property == "totalWages") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return (prev + curr).toFixed(2) * 1;
            } else {
              sums[index] = "--";
            }
          }, 0);
          sums[index] += " ";
        } else if (column.property == "totaltc") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return (prev + curr).toFixed(2) * 1;
            } else {
              sums[index] = "--";
            }
          }, 0);
          sums[index] += " ";
        } else {
          sums[index] = "--";
        }
      });
      return sums;
    },
  },
  created() {
    let sDate = this.formatTime(new Date(), "YYYY-MM-DD");
    let eDate = this.formatTime(new Date(), "YYYY-MM-DD");
    this.days = [sDate, eDate];
    this.$nextTick(()=>{
      this.getData();
    })
  },
};
</script>

<style>
.staffRanking_header {
  display: flex;
  justify-content: space-between;
}
</style>
