<template>
  <div class="SalesReportRejectedNew">
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      :size="'mini'"
      style="width: 100%"
      :row-style="({row}) => row.depart === '合计' ? {background:'#f5f7fa'} : {}"
    >
      <el-table-column label="部门" width="80" prop="depart" align="center" fixed="left">
        <template slot-scope="scope">{{mapResponseCompletionCompanyName[scope.row.depart]|| scope.row.depart}}</template>
      </el-table-column>
      <el-table-column label="本日销售净额" prop="pure_day_sale">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_day_sale)}}</template>
      </el-table-column>
      <el-table-column label="本日毛利润" prop="pure_day_profit">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_day_profit)}}</template>
      </el-table-column>
      <el-table-column label="本日毛利率" prop="pure_day_interest_rate">
       <template slot-scope="scope">{{Number(scope.row.pure_day_interest_rate || 0).toFixed(2) + '%'}}</template>
      </el-table-column>
      <el-table-column label="本月销售指标" prop="total_tips">
        <template slot-scope="scope">{{ unit2TenThousand(scope.row.total_tips) }}</template>
      </el-table-column>
      <el-table-column label="本月销售净额" prop="pure_month_sale">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_month_sale)}}</template>
      </el-table-column>
      <el-table-column label="本月指标达成率" width="105px" prop="depart">
        <template slot-scope="scope">{{Number(scope.row.month_completion_rate).toFixed(2) + '%'}}</template>
      </el-table-column>
      <el-table-column label="本月毛利润" prop="pure_month_profit">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_month_profit)}}</template>
      </el-table-column>
      <el-table-column label="本月毛利率" prop="pure_month_interest_rate">
        <template slot-scope="scope">{{Number(scope.row.pure_month_interest_rate || 0).toFixed(2) + '%'}}</template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import asyncRequest from "@/api/report";
import setHeight from "@/mixins/index";
import { 
  unit2TenThousand,
  getCompanyLabel,
  multiplication,
  subtraction,
  division,
  addition,
  createTotalItem,
  mapResponseCompletionCompanyName
} from "./_utils";

export default {
  name: "CompanyGrossMargin",
  props: ["daytime"],
  mixins: [setHeight],
  watch: {
    daytime: function(val) {
      this.dataTime = val;
      if (val) {
        this.searchList();
      }
    }
  },
  data() {
    return {
      mapResponseCompletionCompanyName,
      loading: false,
      maxHeight: "0",
      tableData: []
    };
  },
  async created() {
    window.onresize = () => {
      this.getHeight();
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getHeight();
    });
    this.searchList();
  },
  methods: {
    getCompanyLabel,
    unit2TenThousand,
    division,
    async searchList() {
      this.loading = true;
      const result = await asyncRequest.departDay({daytime:this.daytime, companyNo: "GS2203161855277894"})
      const res = await asyncRequest.companyEveryMonth({ daytime: this.date, companyNo: "GS2203161855277894" });

      // 拿到普润、百辰、锦兴、泓源的供应商端月净销售
      const mapCompanySupplierSale = res.data.reduce((prev, current) => ({
          ...prev,
          [current.companyName]: {
            supplierSaleDay: 0,
            supplierSaleMonth: 0,
            supplierPurchDay: 0,
            supplierPurchMonth: 0
          }
      }), {})

      if(Number(res.code) === 0 && Number(result.code) === 0){
        const tableData= (result.data || []).map(tableItem => {
          const supplierSalekey = Object.keys(mapCompanySupplierSale).find(key => key.includes(tableItem.depart))

            /* 本日净销售额 = 日销售额 - 日销售退货 **/ 
            let pure_day_sale = subtraction(tableItem.sale_total,tableItem.th_total)
            /* 本日净采购额 = 日采购额 - 日采购退货 **/ 
            let pure_day_purch = subtraction(tableItem.cgd_total,tableItem.cgd_th_total)


            if (supplierSalekey) { // 百辰、普润、泓源 需要加上供应商端采购的数据
              pure_day_purch = addition(pure_day_purch, mapCompanySupplierSale[supplierSalekey].supplierPurchDay)
              pure_day_sale = addition(pure_day_sale, mapCompanySupplierSale[supplierSalekey].supplierSaleDay)
            }

            if (supplierSalekey === '北京普润心堂商贸有限公司') {  //普润的数据需要再加上锦兴的
              pure_day_purch = addition(pure_day_purch, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierPurchDay)
              pure_day_sale = addition(pure_day_sale, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierSaleDay)
            }

            /* 本日毛利润 = (本日净销售额 - 本日净采购额) **/
            let pure_day_profit = subtraction(pure_day_sale,pure_day_purch)
            /* 本日毛利率 = (本日毛利润 / 本日销售净额) * 100**/
            let pure_day_interest_rate = multiplication(division(pure_day_profit,pure_day_sale),100)
            
            /* 本月净销售额 = 月销售额 - 月销售退货额 **/
            let pure_month_sale = subtraction(tableItem.msale_total,tableItem.mth_total)
            /* 本月净采购额 = 月采购额 - 月采购退货额 **/
            let pure_month_purch = subtraction(tableItem.mcgd_total,tableItem.mcgd_th_total)

            if (supplierSalekey) { // 百辰、普润、泓源 需要加上供应商端的数据
              pure_month_sale = addition(pure_month_sale, mapCompanySupplierSale[supplierSalekey].supplierSaleMonth )
              pure_month_purch = addition(pure_month_purch, mapCompanySupplierSale[supplierSalekey].supplierPurchMonth)
            }

            if (supplierSalekey === '北京普润心堂商贸有限公司') { //普润的数据需要再加上锦兴的
              pure_month_sale = addition(pure_month_sale, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierSaleMonth)
              pure_month_purch = addition(pure_month_purch, mapCompanySupplierSale['北京锦兴弘昌科技有限公司'].supplierPurchMonth)
            }
            /* 本月指标达成率 = (本月销售净额 / 本月销售指标) * 100 **/
            let month_completion_rate = multiplication(division(pure_month_sale,tableItem.total_tips),100)
            /* 本月毛利润 = (本月销售净额 - 本月采购净额) **/
            let pure_month_profit = subtraction(pure_month_sale,pure_month_purch)
            /* 本月毛利率 = (本月毛利润 / 本月销售净额) * 100**/
            let pure_month_interest_rate = multiplication(division(pure_month_profit,pure_month_sale),100)

            return {
              total_tips:tableItem.total_tips,
              depart:tableItem.depart,
              pure_day_interest_rate,
              pure_day_sale,
              pure_day_profit,
              pure_month_sale,
              month_completion_rate,
              pure_month_profit,
              pure_month_interest_rate
            }
          })

          const totalItem = createTotalItem(tableData)
          totalItem.depart = "合计"
          totalItem.month_completion_rate = multiplication(division(totalItem.pure_month_sale,Number(totalItem.total_tips).toFixed(2)),100)
          totalItem.pure_month_interest_rate = multiplication(division(Number(totalItem.pure_month_profit).toFixed(2),Number(totalItem.pure_month_sale).toFixed(2)),100)
          totalItem.pure_day_interest_rate = multiplication(division(totalItem.pure_day_profit,Number(totalItem.pure_day_sale).toFixed(2)),100)
          this.tableData = [...tableData,totalItem]  
      }
      this.getHeight();
      this.loading = false;
    }
  }
};
</script>

<style lang="scss" scoped>
</style>

