<template>
  <ContainerQuery>
    <div slot="left">
      <el-button size="small" type="primary" @click="salesManRank(1)">
        导出
      </el-button>
    </div>
    <div class="order_echarts">
      <div class="order_txt">
        <span>客户数：</span>
        <span class="num">
          {{ $_common.formatNub(totalObj.dealCustomerNum) }}
        </span>
        <span>订单数：</span>
        <span class="num">{{ $_common.formatNub(totalObj.orderNum) }}</span>
        <span>订单金额：</span>
        <span class="num">{{ $_common.formatNub(totalObj.dealAmount) }}</span>
      </div>
      <vab-chart
        style="width: 100%; height: 450px"
        :autoresize="true"
        :options="chart_options"
        theme="vab-echarts-theme"
      />
    </div>
    <el-form slot="more" :inline="true" size="small">
      <!--      <el-form-item>-->
      <!--        <el-select-->
      <!--          v-model="provinceCode"-->
      <!--          placeholder="地区"-->
      <!--          clearable-->
      <!--          @change="pageChange(1)"-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="item in area_options"-->
      <!--            :key="item.value"-->
      <!--            :label="item.label"-->
      <!--            :value="item.value"-->
      <!--          ></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-select
          v-model="orderStatus"
          :multiple="true"
          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>
    <el-table
      :data="tableData"
      style="width: 100%"
      size="mini"
      :default-sort="{ prop: 'orderNum' }"
      show-summary
      :summary-method="getSummaries"
      @sort-change="sortChange"
    >
      <el-table-column
        type="index"
        label="排名"
        width="100px"
      ></el-table-column>
      <el-table-column prop="salesManName" label="销售员"></el-table-column>
      <el-table-column
        prop="dealCustomerNum"
        label="成交客户"
        sortable="custom"
      ></el-table-column>
      <el-table-column
        prop="skuNum"
        label="SKU数"
        sortable="custom"
      ></el-table-column>
      <el-table-column
        prop="orderNum"
        label="订单数"
        sortable="custom"
      ></el-table-column>
      <el-table-column label="销售数量" prop="buyNum">
        <template slot="header">
          销售数量
          <el-tooltip placement="top">
            <div slot="content">
              标准商品：统计是销售数量
              <br />
              称重商品：统计的销售公斤数
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          {{ $_common.formatNub(scope.row.buyNum) }}
        </template>
      </el-table-column>
      <el-table-column prop="dealAmount" label="成交金额" sortable="custom">
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.dealAmount) }}
        </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 { salesManRank, exportsalesManRank } from "@/api/Commission";
  import VabChart from "@/extra/vabCharts";
  export default {
    components: { VabChart },
    data() {
      return {
        chart_options: {},
        sortField: "orderNum",
        sort: "ASC",
        time: [],
        start: "",
        end: "",
        page: 1,
        pageTotal: 0,
        pageSize: 10,
        tableData: [],
        type_options: [
          {
            label: "待审核",
            value: 2,
          },
          {
            label: "待发货",
            value: 3,
          },
          {
            label: "待收货",
            value: 4,
          },
          {
            label: "已完成",
            value: 5,
          },
          {
            label: "已关闭",
            value: 6,
          },
        ],
        orderStatus: [],
        order_num: [],
        cu_num: [],
        order_money: [],
        order_name: [],
      };
    },
    computed: {
      totalObj() {
        const totalObj = {
          orderNum: 0,
          dealAmount: 0,
          dealCustomerNum: 0,
        };
        this.tableData.forEach((item) => {
          totalObj.dealCustomerNum = this.$NP.plus(
            totalObj.dealCustomerNum,
            item.dealCustomerNum
          );
          totalObj.orderNum = this.$NP.plus(totalObj.orderNum, item.orderNum);
          totalObj.dealAmount = this.$NP.plus(
            totalObj.dealAmount,
            item.dealAmount
          );
        });
        return totalObj;
      },
    },
    created() {
      this.salesManRank();
      this.time = [new Date().getTime(), new Date().getTime()];
    },
    activated() {
      this.salesManRank();
    },
    methods: {
      // echarts() {},
      sortChange({ prop, order }) {
        this.sortField = prop;
        if (order === "ascending") {
          this.sort = "ASC";
        } else {
          this.sort = "DESC";
        }
        this.salesManRank();
      },
      async salesManRank(exports) {
        let params = {
          page: this.page,
          pageSize: this.pageSize,
          sortField: this.sortField,
          startTime: this.start,
          endTime: this.end,
          sort: this.sort,
          orderStatus: this.orderStatus,
        };
        if (exports) {
          params.export = 1;
          const target = await exportsalesManRank({
            ...params,
          });
        } else {
          const res = await salesManRank({
            ...params,
          });
          this.tableData = res.data;
          this.pageTotal = res.pageTotal;
          const orderNum = [];
          const dealCustomerNum = [];
          const dealAmount = [];
          const salesManName = [];
          res.data.forEach((item) => {
            orderNum.push(item.orderNum);
            dealCustomerNum.push(item.dealCustomerNum);
            dealAmount.push(item.dealAmount);
            salesManName.push(item.salesManName);
          });
          this.chart_options = {
            tooltip: {
              trigger: "axis",
              axisPointer: {
                type: "cross",
                crossStyle: {
                  color: "#999",
                },
              },
            },
            color: ["#3ba272", "#409EFF", "#fac858"],
            legend: {
              data: ["订单数", "客户数", "订单金额"],
            },
            xAxis: {
              type: "category",
              data: salesManName,
            },
            yAxis: [
              {
                type: "value",
                name: "订单（客户）数量",
                axisLabel: {
                  formatter: "{value} 个",
                },
              },
              {
                type: "value",
                name: "订单金额",

                axisLabel: {
                  formatter: "{value} 元",
                },
              },
            ],
            series: [
              {
                type: "bar",
                name: "订单数",
                data: orderNum,
              },
              {
                type: "bar",
                name: "客户数",
                data: dealCustomerNum,
              },
              {
                type: "line",
                smooth: true,
                name: "订单金额",
                yAxisIndex: 1,
                data: dealAmount,
              },
            ],
          };
        }
      },
      // 时间筛选
      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.salesManRank();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      // 合计
      getSummaries(param) {
        return this.$_common.getSummaries(param, [
          "成交客户",
          "SKU数",
          "订单数",
          "销售数量",
          "成交金额",
        ]);
      },
    },
  };
</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>
