<template>
  <div class="SalesReportRejectedNew" v-loading="loading">
    <el-table :data="tableData" size="mini" border>
      <el-table-column label="计数项:订单编号">
        <el-table-column width="120px" label="申请人所属部门" prop="name" />
      </el-table-column>
      <el-table-column label="订单状态" width="110px">
        <template slot-scope="scope">
          {{ getStatus(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="下单月份" align="center">
        <el-table-column label="01">
          <template slot-scope="scope">
            {{getField('1',scope.row)}}
          </template>
        </el-table-column>
        <el-table-column label="02">
          <template slot-scope="scope">
            {{getField('2',scope.row)}}
          </template>
        </el-table-column>
        <el-table-column label="03">
          <template slot-scope="scope">
            {{getField('3',scope.row)}}
          </template>
        </el-table-column>

        <el-table-column label="04">
          <template slot-scope="scope">
            {{getField('4',scope.row)}}
          </template>
        </el-table-column>

        <el-table-column label="05">
          <template slot-scope="scope">
            {{getField('5',scope.row)}}
          </template>
        </el-table-column>
        
        <el-table-column label="06">
          <template slot-scope="scope">
            {{getField('6',scope.row)}}
          </template>
        </el-table-column>

        <el-table-column label="07">
          <template slot-scope="scope">
            {{getField('7',scope.row)}}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="总计">
        <template slot-scope="scope">
          {{getTotal(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 {
      tableData:[],
      loading:false,
      statusOptions: [
        { id: "0", label: "待发货" },
        { id: "1", label: "待发货完成" },
        { id: "2", label: "发货已完成" },
        { id: "3", label: "订单已取消" }
      ]
    };
  },
  async created() {
    window.onresize = () => {
      this.getHeight();
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getHeight();
    });
    this.searchList();
  },
  methods: {
    getStatus(row){
      const key = Object.keys(row.result).find(month => row.result[month] && row.result[month]['1'] && row.result[month]['1'].send_status)
      if(!key) return '--'
      return (this.statusOptions.find(({id}) => {
        return id === row.result[key]['1'].send_status
      }) || {}).label || '--'
    },
    getTotal(row){
      const keys = ['1','2','3','4','5','6','7'];
      return keys.reduce((prev, key) => {
        return addition(prev, (row.result[key]['1'] || {}).num || 0)
      },0)
    },
    getField(month,row){
      if(Array.isArray(row.result[month])) return 0
      return row.result[month]['1'].num || 0
    },
    async searchList() {
      this.loading = true;
      this.tableData = [];

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

      if (res.code === 0 && res.data && res.data.result.length > 0) {
        this.tableData = res.data.result
      } else {
        this.tableData = [];
      }

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