<template>
  <div class="SalesReportRejectedNew">
    <el-table
      :data="tableData"
      v-loading="loading"
      border
      :size="'mini'"
      style="width: 100%"


    >
      <el-table-column label="月份" prop="depart">
        <template slot-scope="scope">{{scope.row.month}}</template>
      </el-table-column>
      <el-table-column label="销售净额" prop="pure_sale">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_sale)}}</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="completion_rate">
        <template slot-scope="scope">{{Number(scope.row.completion_rate || 0).toFixed(2) + '%'}}</template>
      </el-table-column>
      <el-table-column label="毛利润" prop="pure_profit">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_profit)}}</template>
      </el-table-column>
      <el-table-column label="毛利率" prop="pure_interest_rate">
        <template slot-scope="scope">{{Number(scope.row.pure_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,
  createTotalItem,
  multiplication,
  subtraction,
  division
} from "./_utils";

export default {
  name: "MonthGrossMargin",
  props: ["daytime"],
  mixins: [setHeight],
  watch: {
    daytime: function(val) {
      this.dataTime = val;
      if (val) {
        this.searchList();
      }
    }
  },
  data() {
    return {
      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.departMonth({year:(this.daytime.split('-'))[0], companyNo: "GS2203161855277894"})
      switch(Number(result.code)){
        case 0:
          const tableData = (result.data || []).map(tableItem => {
            /* 销售净额 = 销售额 - 退货额 **/
            const pure_sale = subtraction(tableItem.sale_total,tableItem.th_total)
            /* 指标达成率 = 销售净额 / 销售指标 **/
            const completion_rate = multiplication(division(pure_sale,tableItem.total_tips),100)

            /* 采购净额 = 采购额 - 采购退货额 **/
            const pure_purch = subtraction(tableItem.cgd_total,tableItem.cgd_th_total)
            /* 毛利润 = (本月销售净额 - 本月采购净额) **/
            const pure_profit = subtraction(pure_sale,pure_purch)
            /*  毛利率 = (本月毛利润 / 本月销售净额) * 100 **/
            const pure_interest_rate = multiplication(division(pure_profit,pure_sale) || 0,100)

            return {
              month:tableItem.month,
              completion_rate,
              pure_interest_rate,
              pure_profit,
              pure_sale,
              pure_purch,
              total_tips:tableItem.total_tips
            }
          })

          const totalItem = createTotalItem(tableData)
          totalItem.month = "年度合计"
          /* 年度指标达成率 = (年度销售净额 / 年度销售指标) **/
          totalItem.completion_rate = multiplication(division(totalItem.pure_sale,totalItem.total_tips),100)
          /* 年度毛利润 = 年度销售额 - 年度采购额 **/
          totalItem.pure_profit = subtraction(totalItem.pure_sale,totalItem.pure_purch)
          /* 年度毛利率 = (年度毛利润 / 年度销售额) **/
          totalItem.pure_interest_rate = multiplication(division(totalItem.pure_profit,totalItem.pure_sale),100)
          this.tableData = [...tableData,totalItem]
          break
        default:
          break
      }
      this.getHeight();
      this.loading = false;
    }
  }
};
</script>

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

