<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">{{ pageTotal }}</span>
        <span>订单数:</span>
        <span class="num">{{ $_common.formatNub(totalObj.num || 0) }}</span>
        <span>订单金额:</span>
        <span class="num">{{ $_common.formatNub(totalObj.amount || 0) }}</span>
      </div>
    </div>
    <el-divider></el-divider>
    <div
      style="
        width: 300px;
        height: 200px;
        position: absolute;
        top: 220px;
        right: 200px;
      "
    >
      <CustomersOrderChartPie
        :goods-value="goodsValue"
        :goods-name2="goodsName2"
      ></CustomersOrderChartPie>
    </div>
    <div>
      <CustomerOrderChart
        :goods-name="goodsName"
        :goods-num="goodsNum"
        :goods-money="goodsMoney"
      ></CustomerOrderChart>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label=" "></el-table-column>
      <el-table-column prop="customerName" label="客户"></el-table-column>
      <el-table-column prop="customerMobile" 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 { customerOrder } from "@/api/Commission";
  import CustomerOrderChart from "@/views/statement/components/CustomerOrderChart";
  import CustomersOrderChartPie from "@/views/statement/components/CustomersOrderChartPie";
  export default {
    components: {
      CustomerOrderChart,
      CustomersOrderChartPie,
    },
    data() {
      return {
        status: "",
        type: "",
        time: "",
        type_options: [
          {
            label: "等待审核",
            value: 1,
          },
          {
            label: "等待支付",
            value: 2,
          },
          {
            label: "待发货",
            value: 3,
          },
          {
            label: "待收货",
            value: 4,
          },
          {
            label: "已完成",
            value: 5,
          },
          {
            label: "已关闭",
            value: 6,
          },
        ],
        tableData: [],
        pageTotal: 10,
        page: 1,
        pageSize: 10,
        start: "",
        end: "",
        goodsName: [],
        goodsNum: [],
        goodsMoney: [],
        goodsValue: [],
        goodsName2: [],
      };
    },
    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.customerOrder();
    },
    activated() {
      if (this.$_isInit()) return;
      this.customerOrder();
    },
    methods: {
      async customerOrder() {
        const goodsName = [];
        const goodsNum = [];
        const goodsMoney = [];
        const goodsValue = [];
        const goodsName2 = [];
        const res = await customerOrder({
          page: this.page,
          pageSize: this.pageSize,
          customerId: "",
          customerType: "",
          startTime: this.start,
          endTime: this.end,
          type: 1,
          orderStatus: this.status,
        });
        this.tableData = res.data;
        this.pageTotal = res.pageTotal;
        res.data.forEach((value) => {
          if (this.goodsName.indexOf(value.customerName) === -1) {
            goodsName.push(value.customerName);
            goodsNum.push(Number(value.num));
            goodsMoney.push(Number(value.amount));
          }
        });
        res.data.forEach((value) => {
          if (goodsName2.indexOf(value.customerName) === -1) {
            goodsValue.push({
              value: Number(value.amount),
              name: value.customerName,
            });
            goodsName2.push(value.customerName);
          }
        });
        this.goodsName = goodsName;
        this.goodsNum = goodsNum;
        this.goodsMoney = goodsMoney;
        this.goodsValue = goodsValue;
        this.goodsName2 = goodsName2;
      },
      // 时间筛选
      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.customerOrder();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.customerOrder();
      },
    },
  };
</script>
<style scoped lang="scss">
  .order_echarts {
    width: 100%;
    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>
