<template>
  <div class="SalesReportRejectedNew" v-loading="loading">
    <el-table :data="[...tableData, {
      creater:'合计',
      name:'--',
      num: total,
      participation:'--'
    }]" size="mini" border>
      <el-table-column label="采购员" prop="creater" width="120px" />
      <!-- <el-table-column label="采购部门" prop="name" width="120px" /> -->
      <el-table-column label="反馈单数" prop="num" />
      <el-table-column label="竞价参与度" prop="participation" width="120px">
        <template slot-scope="scope">
          {{getParticipation(scope.row.num)}}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import asyncRequest from "@/api/newReport";
import setHeight from "@/mixins/index";
import { addition, division, multiplication } from "./_utils";

export default {
  name: "Consult",
  mixins: [setHeight],
  props: ['date'],
  watch: {
    date: function(val) {
      this.dataTime = val;
      if (val && val.length === 2) {
        this.searchList();
      }
    }
  },
  computed:{
    total(){
      if(!this.tableData || this.tableData.length === 0) return 0
      const total =  this.tableData.reduce((prev,curr) => (
        addition(prev, curr.num || 0)
      ),0)

      return total
    }
  },
  data() {
    return {
      loading:false,
      tableData:[],
      zxTotal:0
    };
  },
  async created() {
    window.onresize = () => {
      this.getHeight();
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getHeight();
    });
    this.searchList();
  },
  methods: {
    getParticipation(num){
      if(Number(num) === 0 && Number(this.zxTotal) === 0) {
        return "0%"
      }

      return multiplication(division(num, this.zxTotal), 100).toFixed(2) + "%"
    },
    async searchList() {
      this.loading = true;
      this.tableData = [];

      const res = await asyncRequest.bidsOrder({
        start:this.date[0] || "",
        end:this.date[1] || ""
      });


      if (res.code === 0 && res.data && res.data.length > 0) {
        this.tableData = res.data.reduce((prev,curr = { name:"", result: {}}) => {
          return [
            ...prev,
            ...Object.keys(curr.result).map(key => {
              return {  name:curr.name,  ...curr.result[key] }
            })]
        },[])

        const { num } = this.tableData.pop()
        this.$emit('getZXTotal', num)
        this.zxTotal = num;
        // console.log(this.tableData)
      } else {
        this.tableData = [];
      }
      this.getHeight();
      this.loading = false;
    }
  }
};
</script>
