<template>
  <!--  订单数据报表-->
  <ContainerQuery>
    <el-form slot="more" style="margin-bottom: 0" :inline="true" size="small">
      <el-form-item>
        <el-select
          v-model="status"
          placeholder="订单状态"
          clearable
          @change="pageChange(1)"
        >
          <el-option
            v-for="(item, index) in type_options"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="block">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="timestamp"
            @change="timeChange"
          ></el-date-picker>
        </div>
      </el-form-item>
    </el-form>
    <div class="order_echarts">
      <div class="order_txt">
        <!--        <span>客户数：</span>-->
        <!--        <span class="num">{{ cus_num }}</span>-->
        <span>订单数：</span>
        <span class="num">{{ $_common.formatNub(totalObj.num || 0) }}</span>
        <span>订单金额：</span>
        <span class="num">{{ $_common.formatNub(totalObj.amount || 0) }}</span>
        <!--        <div class="radios">-->
        <!--          <template>-->
        <!--            <span>统计方式：</span>-->
        <!--            <el-radio-group v-model="radio">-->
        <!--              <el-radio :label="1">日</el-radio>-->
        <!--              <el-radio :label="2">月</el-radio>-->
        <!--            </el-radio-group>-->
        <!--          </template>-->
        <!--        </div>-->
      </div>
      <vab-chart
        style="width: 100%; height: 450px"
        :autoresize="true"
        :options="chart_options"
        theme="vab-echarts-theme"
      />
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label=" "></el-table-column>
      <el-table-column prop="day" label="时间"></el-table-column>
      <el-table-column prop="num" label="订单数"></el-table-column>
      <el-table-column prop="amount" label="订单金额">
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.amount) }}
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="pageTotal"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </ContainerQuery>
</template>
<script>
  import { order } from "@/api/Commission";
  import VabChart from "@/extra/vabCharts";
  export default {
    components: { VabChart },
    data() {
      return {
        cus_num: 0,
        orders_num: 0,
        orders_money: 0,
        radio: 1,
        type: 1,
        status: "",
        time: "",
        chart_options: {},
        type_options: [
          {
            label: "全部",
            value: "",
          },
          {
            label: "待审核",
            value: 2,
          },
          {
            label: "待发货",
            value: 3,
          },
          {
            label: "待收货",
            value: 4,
          },
          {
            label: "已完成",
            value: 5,
          },
          {
            label: "已关闭",
            value: 6,
          },
        ],
        tableData: [],
        pageTotal: 10,
        page: 1,
        pageSize: 10,
        start: "",
        end: "",
        order_money: [],
        order_num: [],
        order_time: [],
      };
    },
    computed: {
      totalObj() {
        const totalObj = {
          num: 0,
          amount: 0,
        };
        this.tableData.forEach((item) => {
          totalObj.num = this.$NP.plus(totalObj.num, item.num);
          totalObj.amount = this.$NP.plus(totalObj.amount, item.amount);
        });
        return totalObj;
      },
    },
    created() {
      this.order();
    },
    methods: {
      async order() {
        const res = await order({
          page: this.page,
          pageSize: this.pageSize,
          customerId: this.customerId,
          customerType: "",
          startTime: this.start,
          endTime: this.end,
          type: 1,
          orderStatus: this.status,
        });
        this.tableData = res.data;
        this.pageTotal = res.pageTotal;
        const orderMoney = [];
        const orderNum = [];
        const orderTime = [];
        this.tableData.forEach((item) => {
          orderMoney.push(item.amount);
          orderNum.push(Number(item.num));
          orderTime.push(item.day);
        });
        const maxNum = Math.max(...orderMoney);
        const minNum = Math.min(...orderMoney);
        // 图表
        this.chart_options = {
          tooltip: {
            trigger: "axis",
            axisPointer: {
              type: "cross",
              crossStyle: {
                color: "#999999",
              },
            },
          },
          legend: {
            data: ["订单金额", "订单数"],
          },
          xAxis: [
            {
              type: "category",
              data: orderTime,
              axisPointer: {
                type: "shadow",
              },
            },
          ],
          yAxis: [
            {
              type: "value",
              name: "订单金额",
              min: minNum,
              max: maxNum,
              axisLabel: {
                formatter: "{value} 元",
              },
            },
            {
              type: "value",
              name: "订单数",
              axisLabel: {
                formatter: "{value} 个",
              },
            },
          ],
          series: [
            {
              color: "#36B365",
              name: "订单金额",
              type: "bar",
              data: orderMoney,
            },
            {
              color: "#f1c44d",
              name: "订单数",
              type: "line",
              smooth: true,
              yAxisIndex: 1,
              data: orderNum,
            },
          ],
        };
      },
      // 时间筛选
      timeChange(val) {
        if (val && val.length) {
          this.start = val[0] / 1000;
          this.end = val[1] / 1000 + 86399;
        } else {
          this.start = "";
          this.end = "";
        }
        this.pageChange(1);
      },
      pageChange(val) {
        this.page = val;
        this.order();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
    },
  };
</script>
<style scoped lang="scss">
  .order_echarts {
    width: 100%;
    height: 500px;
    background-color: #ffffff;
    .order_txt {
      width: 100%;
      height: 70px;
      padding: 20px 20px;
      position: relative;
      .radios {
        position: absolute;
        top: 45px;
        right: 20px;
      }
      .num {
        color: #fd7945;
        font-size: 24px;
        margin-left: 10px;
        margin-right: 40px;
      }
    }
  }
</style>
