<template>
  <ContainerQuery>
    <div slot="more">
      <el-form size="small" :inline="true">
        <el-form-item>
          <SelectCustomer
            v-model="customer_name"
            @clear="customerClear"
            @change="customerSel"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="customer_type_name"
            class="float_left"
            style="width: 200px"
            size="small"
            placeholder="请选择客户类型"
          >
            <i
              slot="suffix"
              class="el-input__icon el-icon-search"
              @click="customer_type_show = true"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="time"
            clearable
            type="daterange"
            value-format="timestamp"
            range-separator="-"
            start-placeholder="下单开始日期"
            end-placeholder="下单结束日期"
            @change="orderDate"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="brand"
            style="width: 150px"
            placeholder="商品品牌"
            clearable
            @clear="delBrand"
          >
            <i
              slot="suffix"
              class="el-input__icon el-icon-search"
              @click="sel_brand = true"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <GoodsCategory
            v-model="categoryId"
            :width="160"
            check-strictly
            clearable
            size="small"
            @change="goodsChane"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="type"
            style="width: 150px"
            placeholder="统计方式"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in customerStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="order_echarts">
      <div class="order_txt">
        <span>商品数:</span>
        <span class="num">
          {{ $_common.formatNub(totalObj.goodsNum || 0) }}
        </span>
        <span>商品金额:</span>
        <span class="num">
          {{ $_common.formatNub(totalObj.goodsAmount || 0) }}
        </span>
      </div>
    </div>
    <el-divider></el-divider>
    <div v-if="type === 1">
      <MerchandiseSalesChartLine
        v-if="type === 1"
        :goods-name="goodsName"
        :goods-num="goodsNum"
        :goods-money="goodsMoney"
      ></MerchandiseSalesChartLine>
    </div>
    <div v-if="type === 2">
      <MerchandiseSalesChartPie
        v-if="type === 2"
        :goods-value="goodsValue"
        :goods-name2="goodsName2"
      ></MerchandiseSalesChartPie>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label=" "></el-table-column>
      <el-table-column
        v-if="type === 1"
        prop="title"
        label="商品"
      ></el-table-column>
      <el-table-column
        v-if="type === 2"
        prop="title"
        label="商品分类"
      ></el-table-column>
      <el-table-column prop="goodsNum" label="商品数">
        <template slot-scope="scope">
          {{ $_common.formatNub(scope.row.goodsNum) }}
          <!--          <span v-if="scope.row.isEq === 5">-->
          <!--            ({{ $_common.formatNub(scope.row.otherNum) }})-->
          <!--          </span>-->
        </template>
      </el-table-column>
      <el-table-column prop="goodsAmount" label="商品金额">
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.goodsAmount) }}
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
    <BrandSelModel
      v-if="sel_brand"
      :is-check="false"
      :dialog-visible="sel_brand"
      @close="sel_brand = false"
      @confirm="brandConfirm"
    />
    <CustomerTypeModel
      v-if="customer_type_show"
      :is-show-add="true"
      :is-check="false"
      :is-show="customer_type_show"
      @confirm="customerTypeConfirm"
      @cancel="customer_type_show = false"
    ></CustomerTypeModel>
  </ContainerQuery>
</template>
<script>
  import BrandSelModel from "@/component/goods/BrandSelModel.vue";
  import { goods } from "@/api/Charts";
  import GoodsCategory from "@/component/common/GoodsCategory.vue";
  import SelectCustomer from "@/component/common/SelectCustomer.vue";
  import CustomerTypeModel from "@/component/customer/CustomerTypeModel";
  import MerchandiseSalesChartLine from "@/views/statement/components/MerchandiseSalesChartLine";
  import MerchandiseSalesChartPie from "@/views/statement/components/MerchandiseSalesChartPie";
  export default {
    components: {
      BrandSelModel,
      GoodsCategory,
      SelectCustomer,
      CustomerTypeModel,
      MerchandiseSalesChartLine,
      MerchandiseSalesChartPie,
    },
    data() {
      return {
        tableData: [],
        page: 1,
        pageSize: 10,
        total: 0,
        time: "",
        start: "",
        end: "",
        type: 1,
        customerStatus: [
          { label: "商品", value: 1 },
          { label: "商品分类", value: 2 },
        ],
        sel_brand: false,
        brand: "",
        brandId: "",
        categoryId: [],
        customerId: "",
        customer_name: "",
        customerType: "",
        customer_type_show: false,
        customer_type_name: "",
        goodsName: [],
        goodsNum: [],
        goodsMoney: [],
        goodsValue: [],
        goodsName2: [],
      };
    },
    computed: {
      totalObj() {
        const totalObj = {
          goodsNum: 0,
          goodsAmount: 0,
        };
        this.tableData.forEach((item) => {
          totalObj.goodsNum = this.$NP.plus(totalObj.goodsNum, item.goodsNum);
          totalObj.goodsAmount = this.$NP.plus(
            totalObj.goodsAmount,
            item.goodsAmount
          );
        });
        return totalObj;
      },
    },
    created() {
      this.goods();
    },
    activated() {
      if (this.$_isInit()) return;
      this.goods();
    },
    methods: {
      pageChange(val) {
        if (this.type === 2) {
          this.goodsName = [];
          this.goodsNum = [];
          this.goodsMoney = [];
        }
        if (this.type === 1) {
          this.goodsValue = [];
          this.goodsName2 = [];
        }
        this.page = val;
        this.goods();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.goods();
      },
      //  订单时间
      orderDate(val) {
        if (val && val.length) {
          this.start = val[0] / 1000;
          this.end = val[1] / 1000 + 86399;
        } else {
          this.start = "";
          this.end = "";
        }
        this.pageChange(1);
      },
      async goods() {
        const goodsName = [];
        const goodsNum = [];
        const goodsMoney = [];
        const goodsValue = [];
        const goodsName2 = [];
        const { data, pageTotal } = await goods({
          page: this.page,
          pageSize: this.pageSize,
          categoryId: this.categoryId,
          brandId: this.brandId,
          startTime: this.start,
          endTime: this.end,
          type: this.type,
          customerId: this.customerId,
          customerType: this.customerType,
        });
        this.tableData = data;
        this.total = pageTotal;
        if (this.type === 1) {
          data.forEach((value) => {
            if (goodsName.indexOf(value.title) === -1) {
              goodsName.push(value.title);
              goodsNum.push(Number(value.goodsNum));
              goodsMoney.push(Number(value.goodsAmount));
            }
          });
          this.goodsName = goodsName;
          this.goodsNum = goodsNum;
          this.goodsMoney = goodsMoney;
        }
        if (this.type === 2) {
          data.forEach((value) => {
            if (goodsName2.indexOf(value.title) === -1) {
              goodsValue.push({
                value: Number(value.goodsAmount),
                name: value.title,
              });
              goodsName2.push(value.title);
            }
          });
          this.goodsName2 = goodsName2;
          this.goodsValue = goodsValue;
        }
      },
      // 品牌搜索
      brandConfirm(row) {
        this.brand = row[0].title;
        this.brandId = row[0].id;
        this.pageChange(1);
      },
      delBrand() {
        this.brand = "";
        this.brandId = "";
        this.pageChange(1);
      },
      goodsChane(val) {
        this.categoryId = val[0];
        this.pageChange(1);
      },
      // 选择客户
      customerSel(val, list) {
        this.customerId = list[0].id;
        this.pageChange(1);
      },
      customerClear() {
        this.customerId = "";
        this.customer_name = "";
        this.pageChange(1);
      },
      customerTypeConfirm(val) {
        const row = val[0];
        this.customer_type_name = row.name;
        this.customerType = row.id;
        this.pageChange(1);
      },
    },
  };
</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>
