<template>
  <div class="SalesReportRejectedNew" v-loading="loading">
    <el-table :data="tableData" size="mini" border>
      <el-table-column prop="name" width="120px" />
      <el-table-column label="库存商品">
        <template slot-scope="scope">
          {{ getResultWithType(scope.row,'1') }}
        </template>
      </el-table-column>
      <el-table-column label="非库存商品">
        <template slot-scope="scope">
        {{ getResultWithType(scope.row, '2') }}
        </template>
      </el-table-column>
      <el-table-column label="反馈商品">
        <template slot-scope="scope">
          {{ getResultWithType(scope.row, '3') }}
        </template>
      </el-table-column>
      <el-table-column label="总计">
        <template slot-scope="scope">
          {{ getRowTotal(scope.row)}}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import asyncRequest from "@/api/newReport";
import setHeight from "@/mixins/index";
import { addition } from "./_utils";
export default {
  name: "Consult",
  mixins: [setHeight],
  props: ['date'],
  watch: {
    date: function(val) {
      this.dataTime = val;
      if (val && val.length === 2) {
        this.searchList();
      }
    }
  },
  data() {
    return {
      loading:false,
      tableData:[]
    };
  },
  async created() {
    window.onresize = () => {
      this.getHeight();
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getHeight();
    });
    this.searchList();
  },
  methods: {
    getRowTotal(row){
      if(Array.isArray(row.result)) return '0'
      const result = Object.keys(row.result).map((key) => row.result[key])
      const total = (result || []).reduce((prev, curr = {}) => {
        return addition(prev,curr.order_type === '4' ? 0 : curr.num || 0)
      },0)
      return total
    },
    getResultWithType(row, type){
      if(Array.isArray(row.result)) return '0'
      const result = Object.keys(row.result).map((key) => row.result[key])
      const item = result.find(({order_type}) => order_type === type) || {};
      return  item.num || 0
    },
    async searchList() {
      this.loading = true;
      this.tableData = [];

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

      if (res.code === 0 && res.data && res.data.length > 0) {
        this.tableData = res.data


        const keys = ['1','2','3']

        const defaultData = keys.reduce((prev, order_type) => ({
          ...prev,
          [order_type]:{ num:0, order_type }
        }),{})


        const totalItem = {
          name:'总计',
          result:this.tableData.reduce((prev,current) => {
            const { result } = current;
            const row =  keys.reduce((pre,order_type) => {
              const { num } = result[order_type] || { num: 0 }
              const newNum = addition(prev[order_type].num, num);
              return {...pre, [order_type]:{ num:newNum,  order_type }}
            },{})
            return row
        },{...defaultData})}


        this.tableData.push(totalItem)

      } else {
        this.tableData = [];
      }

      this.getHeight();
      this.loading = false;
    }
  }
};
</script>
