<template>
  <ContainerQuery>
    <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>
        <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">
          {{ $_common.formatNub(totalObj.dealCustomerNum || 0) }}
        </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 id="echarts"></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="title" label="地区"></el-table-column>
      <el-table-column prop="dealCustomerNum" 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 { areaOrder } from "@/api/Commission";
  import VabChart from "@/extra/vabCharts";
  import Region from "@/assets/area.json";
  export default {
    components: { VabChart },
    data() {
      return {
        chart_options: {},
        cus_num: 15,
        orders_num: 15,
        orders_money: 15,
        value: "",
        cityCode: "",
        districtCode: "",
        provinceCode: "",
        area_options: Region,
        time: "",
        tableData: [],
        page: 1,
        pageSize: 10,
        start: "",
        end: "",
        pageTotal: 10,
        a: [],
      };
    },
    computed: {
      totalObj() {
        const totalObj = {
          num: 0,
          amount: 0,
          dealCustomerNum: 0,
        };
        this.tableData.forEach((item) => {
          totalObj.dealCustomerNum = this.$NP.plus(
            totalObj.dealCustomerNum,
            item.dealCustomerNum
          );
          totalObj.num = this.$NP.plus(totalObj.num, item.num);
          totalObj.amount = this.$NP.plus(totalObj.amount, item.amount);
        });
        return totalObj;
      },
    },
    created() {
      this.areaOrder();
    },
    activated() {
      this.areaOrder();
    },
    methods: {
      async areaOrder() {
        const res = await areaOrder({
          page: this.page,
          pageSize: this.pageSize,
          startTime: this.start,
          endTime: this.end,
          provinceCode: this.provinceCode,
        });
        this.tableData = res.data;
        this.pageTotal = res.pageTotal;
        res.data = res.data.map((v) => {
          return { name: v.title, value: v.amount };
        });
        this.chart_options = {
          tooltip: {
            color: "#2D405E",
            trigger: "item",
            formatter: "{b} <br/>订单金额: {c}",
          },
          color: [
            "#3ba272",
            "#fac858",
            "#ee6666",
            "#73c0de",
            "#fc8452",
            "#9a60b4",
            "#ea7ccc",
            "#5470c6",
            "#91cc75",
          ],
          legend: {
            orient: "vertical",
            show: true,
            left: 40,
            top: 20,
            bottom: 20,
            data: this.a.forEach((item) => {
              return item.name;
            }),
          },
          series: [
            {
              type: "pie",
              // radius: ["50%", "76%"],
              labelLine: {
                normal: {
                  length: 35, // 改变标示线的长度
                },
              },
              label: {
                formatter: "{a|{a}-} ",
                borderWidth: 1,
                borderRadius: 4,
                normal: {
                  textStyle: {
                    color: "#2D405E", // 改变标示文字的颜色
                  },
                },
                rich: {
                  a: {
                    color: "#6E7079",
                    lineHeight: 22,
                    align: "center",
                  },
                  hr: {
                    borderColor: "#8C8D8E",
                    width: "100%",
                    borderWidth: 1,
                    height: 0,
                  },
                  b: {
                    color: "#4C5058",
                    fontSize: 14,
                    fontWeight: "bold",
                    lineHeight: 33,
                  },
                  per: {
                    color: "#fff",
                    backgroundColor: "#4C5058",
                    padding: [3, 4],
                    borderRadius: 4,
                  },
                },
              },
              data: res.data,
            },
          ],
        };
      },
      // 时间筛选
      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);
      },
      //  区域
      regionChange(val) {
        this.provinceCode = val[0];
        this.cityCode = val[1];
        this.districtCode = val[2];
        this.areaOrder();
      },
      pageChange(val) {
        this.page = val;
        this.areaOrder();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.areaOrder();
      },
    },
  };
</script>
<style scoped lang="scss">
  .order_echarts {
    width: 100%;
    height: 600px;
    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>
