<template>
  <div class="SalesReportRejectedNew">
    <div class="table-header">{{index}}.{{companyName || '--'}}</div>
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      :size="'mini'"
      style="width: 100%"
      :row-style="({row}) => row.field === '合计' ? {background:'#f5f7fa'} : {}"
    >
      <el-table-column label="业绩类型" prop="depart" align="center" fixed="left">
        <template slot-scope="scope">{{scope.row.field}}</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_month_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="pure_month_sale">
        <template slot-scope="scope">{{unit2TenThousand(scope.row.pure_month_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="本月指标达成率" width="105px" prop="month_completion_rate">
        <template slot-scope="scope">{{ scope.row.month_completion_rate === '--' ? '--' : Number(scope.row.month_completion_rate || 0).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,
  createTotalItem,
  multiplication,
  subtraction,
  division,
  addition,
} from "./_utils";

export default {
  name: "CompanyGrossMargin",
  props: ["date","companyNo","index",'daytime'],
  mixins: [setHeight],
  watch: {
    daytime: function(val) {
      this.dataTime = val;
      if (val) {
        this.searchList();
      }
    }
  },
  data() {
    return {
      loading: false,
      maxHeight: "0",
      tableData: [],
      companyName:"",
    };
  },
  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.companyEveryMonth({companyNo:this.companyNo,daytime:this.daytime})

      switch(Number(result.code)){
        case 0:
          this.companyName = result.data[0].companyName
          const companies = ['百辰','普润','泓源','锦兴']
          const fields = ['自营','渠道','小计','供应商端','合计']
          let tableData = (result.data || []).filter(({companyName}) => companies.some(company => companyName.indexOf(company) !== -1))
          //合计自营、渠道、供应商端数据
          const total = tableData.reduce((prev,current) => ({
            supplierSaleDay:addition(prev.supplierSaleDay,subtraction(current.dayinfo[2].sale_total,current.dayinfo[2].th_total)),
            supplierSaleMonth:addition(prev.supplierSaleMonth,subtraction(current.monthinfo[2].msale_total,current.monthinfo[2].mth_total)),
            supplierPurchDay:addition(prev.supplierPurchDay,subtraction(current.dayinfo[2].cgd_total,current.dayinfo[2].cgd_th_total)),
            supplierPurchMonth:addition(prev.supplierPurchMonth,subtraction(current.monthinfo[2].mcgd_total,current.monthinfo[2].mcgd_th_total)),
            
            selfOperatedSaleDay:addition(prev.selfOperatedSaleDay,subtraction(current.dayinfo[0].sale_total,current.dayinfo[0].th_total)),
            selfOperatedSaleMonth:addition(prev.selfOperatedSaleMonth,subtraction(current.monthinfo[0].msale_total,current.monthinfo[0].mth_total)),
            selfOperatedPurchDay:addition(prev.selfOperatedPurchDay,subtraction(current.dayinfo[0].cgd_total,current.dayinfo[0].cgd_th_total)),
            selfOperatedPurchMonth:addition(prev.selfOperatedPurchMonth,subtraction(current.monthinfo[0].mcgd_total,current.monthinfo[0].mcgd_th_total)),
            
            channelSaleDay:addition(prev.channelSaleDay,subtraction(current.dayinfo[1].sale_total,current.dayinfo[1].th_total)),
            channelSaleMonth:addition(prev.channelSaleMonth,subtraction(current.monthinfo[1].msale_total,current.monthinfo[1].mth_total)),
            channelPurchDay:addition(prev.channelPurchDay,subtraction(current.dayinfo[1].cgd_total,current.dayinfo[1].cgd_th_total)),
            channelPurchMonth:addition(prev.channelPurchMonth,subtraction(current.monthinfo[1].mcgd_total,current.monthinfo[1].mcgd_th_total)),

            totalTips:addition(prev.totalTips,current.total_tips)
          }),{
            supplierSaleDay:0,
            supplierSaleMonth:0,
            supplierPurchDay:0,
            supplierPurchMonth:0,
            selfOperatedSaleDay:0,
            selfOperatedSaleMonth:0,
            selfOperatedPurchDay:0,
            selfOperatedPurchMonth:0,
            channelSaleDay:0,
            channelSaleMonth:0,
            channelPurchDay:0,
            channelPurchMonth:0,
            totalTips:0
          })


          const mapFieldToTotalProperties = {
            '供应商端':{
                saleDay:'supplierSaleDay',
                saleMonth:'supplierSaleMonth',
                purchDay:'supplierPurchDay',
                purchMonth:'supplierPurchMonth',
            },
            '自营':{
                 saleDay:'selfOperatedSaleDay',
                 saleMonth:'selfOperatedSaleMonth',
                 purchDay:'selfOperatedPurchDay',
                 purchMonth:'selfOperatedPurchMonth',
            },
            '渠道':{
                saleDay:'channelSaleDay',
                saleMonth:'channelSaleMonth',
                purchDay:'channelPurchDay',
                purchMonth:'channelPurchMonth'
            }
          }


          tableData = fields.map(field => {
            const properties = mapFieldToTotalProperties[field]
          
           if(!properties) {
              return {
                field,
                total_tips:'--',
                pure_day_sale:0,
                pure_day_purch:0,
                pure_day_profit:0,
                pure_month_sale:0,
                pure_month_purch:0,
                month_completion_rate:'--',
                pure_month_profit:0,
                pure_month_interest_rate:0
              }
           }

            const pure_day_sale = total[properties.saleDay]
            const pure_day_purch = total[properties.purchDay]
            /* 本日毛利润 = (本日净销售额 - 本日净采购额) **/
            const pure_day_profit = subtraction(pure_day_sale,pure_day_purch)
            let pure_day_interest_rate = multiplication(division(pure_day_profit,pure_day_sale),100)
            const pure_month_sale = total[properties.saleMonth]
            const pure_month_purch = total[properties.purchMonth]
            /* 本月毛利润 = (本月销售净额 - 本月采购净额) **/
            const 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)

            if (Number(pure_day_sale) < 0) {
              pure_day_interest_rate = 0
            }

            if (Number(pure_month_sale) < 0) {
               pure_month_interest_rate = 0
            }

            return {
              field,
              total_tips:'--',
              pure_day_sale,
              pure_day_purch,
              pure_day_profit,
              pure_month_sale,
              pure_month_purch,
              month_completion_rate:'--',
              pure_month_profit,
              pure_month_interest_rate,
              pure_day_interest_rate
            }
          })

          /* 小计 = 自营 + 渠道 **/
          const totalItem = createTotalItem([tableData[0],tableData[1]])
          totalItem.field = "小计"
          totalItem.total_tips = "--"
          totalItem.month_completion_rate = "--"
          totalItem.pure_month_interest_rate = multiplication(division(totalItem.pure_month_profit,totalItem.pure_month_sale),100)
          totalItem.pure_day_interest_rate = multiplication(division(totalItem.pure_day_profit,totalItem.pure_day_sale),100)
          if (Number(totalItem.pure_day_sale) < 0) {
            totalItem.pure_day_interest_rate = 0
          }

          if (Number(totalItem.pure_month_sale) < 0) {
            totalItem.pure_month_interest_rate = 0
          }
          tableData[2] = totalItem

          /* 合计 = 小计 + 供应商端 **/
          const lastItem = createTotalItem([tableData[2],tableData[3]])
          lastItem.field = "合计"
          lastItem.total_tips = total.totalTips
          lastItem.pure_month_interest_rate = multiplication(division(lastItem.pure_month_profit,lastItem.pure_month_sale),100)
          /* 本月指标达成率 = (本月销售净额 / 本月销售指标) * 100 **/
          lastItem.month_completion_rate = multiplication(division(lastItem.pure_month_sale,lastItem.total_tips),100)
          lastItem.pure_day_interest_rate = multiplication(division(totalItem.pure_day_profit,totalItem.pure_day_sale),100)

          if (Number(lastItem.pure_day_sale) < 0) {
              lastItem.pure_day_interest_rate = 0
          }

          if (Number(lastItem.pure_month_sale) < 0) {
            lastItem.pure_month_interest_rate = 0
          }

          tableData[4] = lastItem
          this.tableData = tableData
          break
        default:
          break
      }
      this.getHeight();
      this.loading = false;
    }
  }
};
</script>

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