<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item :label="'商品名称'">
              <el-input v-model="queryParams.productName" clearable />
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <el-form-item :label="'商品分类'">
              <el-cascader
                style="width: 100%"
                class="theCascader"
                v-model="queryParams.theOptions"
                :options="optionList"
                :props="regionParams"
                clearable
                @visible-change="validateCascader"
                ref="cascader"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'统计时间'" prop="teamCode">
              <el-date-picker
                v-model="queryParams.statisticsDate"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="4">
            <el-form-item label="品牌名称">
              <el-input v-model="queryParams.brandName" clearable />
            </el-form-item>
          </el-col> -->
          <el-col :span="4">
            <div class="searchbox">
              <el-button
                class="my_search"
                @click="
                  () => {
                    getSearch();
                    getCzNum();
                  }
                "
                > {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="small" class="thebtn1" @click="handleExport"
            > {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="670px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
          :show-summary="true"
          :summary-method="summaryMethod"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <!-- <el-table-column align="center" :label="'排序'" type="index">
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="productName"
            :label="'商品名称'"
            width="350px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="titleCategory"
            :label="'商品分类'"
            width="200px"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="specValueNames"
            min-width="150px"
            :label="'商品规格'"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="price"
            :label="`${'商品单价'}(${getUser.currency.symbol})`"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="achieve"
            :label="`${'业绩'}(${getUser.currency.symbol})`"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="achieveDollar"
            :label="'业绩'+'($)'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="realSale"
            :label="'销量'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="saleAmount"
            :label="'销售额'+'(￥)'"
            min-width="120px"
          >
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSearch"
    />
  </div>
</template>

  <script>
import topBar from "@/components/topBar";
import * as three from "@/api/threegoods";
import { mapGetters } from "vuex";
export default {
  name: "StatisticsGoods",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "statisticsGoods",
      topList: [
        {
          name: '商品统计',
          path: "statisticsGoods",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        source: 0,
        theOptions: [], //商品分类
        statisticsDate: [],
      },
      optionList: [],
      total: 0,
      multipleSelection: [],
      optionList: [],
      regionParams: {
        label: "name",
        value: "id",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true,
      },
      totalData: {},
    };
  },
  computed: {
    ...mapGetters(["getUser"]),
  },
  watch:{
    'queryParams.theOptions'(n,o){
      this.changeValidate()
    }
  },
  created() {
    this.getSearch();
    this.getOthers();
  },
  methods: {
    changeValidate(){
      if(this.queryParams.theOptions.length>2||this.queryParams.theOptions.length>1){
        this.$refs.cascader.dropDownVisible = false;
      }
    },
    //合计行
    summaryMethod({ columns, data }) {
      const sums = [];

      // 遍历所有列
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "统计合计";
          return;
        }
        if (column.property === "realSale") {
          // sums[index] = data[lastIndex1] ? data[lastIndex1].price : "";
          sums[index] = this.totalData.realSale;
        } else if (column.property === "saleAmount") {
          sums[index] = this.totalData.saleAmount;
        } else {
          sums[index] = "";
        }
      });
      return sums;
    },
    //导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/sale/manage/sa-t-wares/statistics-export",
          { ...this.queryParams },
          `${'商品统计'}${new Date().getTime()}.xlsx`
        );
      });
    },
    validateCascader(value) {
      if (!value && this.queryParams.theOptions.length < 2) {
        this.$message({
          message: '请选择一二级分类',
          type: "warning",
        });
        this.queryParams.theOptions = [];
      }
    },
    getOthers() {
      three.getOptionList({ source: this.queryParams.source }).then((res) => {
        this.optionList = res.data;
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        source: 0,
        theOptions: [], //商品分类
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      this.queryParams.pkCategoryOne = this.queryParams.theOptions[0] || "";
      this.queryParams.pkCategoryTwo = this.queryParams.theOptions[1] || "";
      this.queryParams.pkCategoryThree = this.queryParams.theOptions[2] || "";
      three.getStatisticsList(this.queryParams).then((res) => {         
        this.loading = false
        this.tableData = res.rows;
        this.total = res.total;
      });
      three.getStatisticsTotal().then((res) => {
        this.totalData = res.data;
      });
    },
    getCzNum() {
      this.queryParams.pageNum = 1;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table__footer-wrapper {
  position: fixed;
  bottom: 60px;
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 0px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .searchtitle {
      margin-right: 10px;
    }
    .searchbtn {
      background: #08143f;
      color: #ffffff;
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #ffad41;
          width: 68px;
          height: 32px;
        }
      }
    }
  }
}
</style>
