<template>
  <div class="app-container">
    <div class="header" style="width: 100%">
      <el-row class="row" :gutter="12">
        <el-col :span="4">
          <el-card shadow="always">
            <div class="item">
              <div style="padding: 10px">
                <span>全部上架商品(个)</span>
              </div>
              <div class="count">
                {{ Statistics ? Statistics.totalPutOnGoodsCount : 0 }}
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card shadow="always">
            <div class="item">
              <div style="padding: 10px">
                <span>单日销售额(元)</span>

                <el-popover
                  placement="top-start"
                  width="300"
                  trigger="click"
                  v-model="popover"
                >
                  <el-date-picker
                    @change="pickerChange"
                    v-model="timeDate"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                  <span
                    slot="reference"
                    :style="
                      timeDate
                        ? 'position: relative; left: 30px'
                        : 'position: relative; left: 100px'
                    "
                  >
                    {{ timeDate }}
                    <i class="el-icon-date" style="color: #000"></i
                  ></span>
                </el-popover>
              </div>
              <div class="count">
                ￥{{ Statistics ? Statistics.yseTotalSalePrice : 0 }}
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card shadow="always">
            <div class="item">
              <div style="padding: 10px">
                <span>退款总金额(元)</span>
              </div>
              <div class="count">
                ￥{{ Statistics ? Statistics.totalRefundPrice : 0 }}
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="4">
          <el-card shadow="always">
            <div class="item">
              <div style="padding: 10px">
                <span>总销售额(元)</span>
              </div>
              <div class="count">{{ Statistics ? Statistics.totalSalePrice : 0 }}</div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="请输入商品名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-cascader
          v-model="queryParams.categoryId"
          ref="formCascader"
          placeholder="请选择分类"
          :props="categoryprops"
          @change="cascaderChange"
        />
      </el-form-item>
      <el-form-item label="状态" prop="spuStatus">
        <el-select v-model="queryParams.spuStatus" placeholder="请选择状态" clearable>
          <el-option label="上架" value="0" />
          <el-option label="下架" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="configList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="商品图片" align="center" prop="configId">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.img"
            style="width: 60px; height: 60px"
            :src="scope.row.img"
            :preview-src-list="[scope.row.img]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="spuName" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="价格(元)" align="center" prop="price">
        <template slot-scope="scope">
          {{ scope.row.price }}/  {{ scope.row.unit }}
        </template>
      </el-table-column>

      <el-table-column label="预计库存" align="center" prop="totalPredictStocks">
        <template slot-scope="scope">
          {{ scope.row.totalPredictStocks }}{{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="实际库存" align="center" prop="stock">
        <template slot-scope="scope">
          {{ scope.row.stock }}{{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="销量" align="center" prop="saleCount">
        <template slot-scope="scope">
          {{ scope.row.saleCount }}{{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="销售额(元)" align="center" prop="salePrice" />
      <el-table-column label="状态" align="center" prop="spuStatus">
        <template slot-scope="scope">
          {{
            scope.row.spuStatus == 2
              ? "售罄"
              : scope.row.spuStatus == 0
              ? "上架"
              : scope.row.spuStatus == 1
              ? "下架"
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:config:remove']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-descriptions title="" column="1">
        <el-descriptions-item label="商品图片">
          <el-image
            v-if="form.img"
            style="width: 60px; height: 60px"
            :src="form.img"
            :preview-src-list="[form.img]"
          >
          </el-image>
        </el-descriptions-item>
        <el-descriptions-item label="商品名称"> {{ form.spuName }}</el-descriptions-item>
        <el-descriptions-item label="商品分类">
          {{ form.categoryName }}</el-descriptions-item
        >
        <el-descriptions-item label="商品价格(元)">
          {{ form.price }}
        </el-descriptions-item>
        <el-descriptions-item label="商品库存"> {{ form.stock }}</el-descriptions-item>

        <el-descriptions-item label="商品销量">
          {{ form.saleCount }}</el-descriptions-item
        >
        <el-descriptions-item label="商品销售额(元)">
          {{ form.salePrice }}</el-descriptions-item
        >
        <el-descriptions-item label="商品状态">
          {{
            form.spuStatus == 2
              ? "售罄"
              : form.spuStatus == 0
              ? "上架"
              : form.spuStatus == 1
              ? "下架"
              : ""
          }}</el-descriptions-item
        >
        <el-descriptions-item label="商品创建时间">
          {{ form.createDate }}</el-descriptions-item
        >
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { getSalesStatisticsPage, getSalesStatistics } from "@/api/manage/sales";
import { getClassificationListPage } from "@/api/common/index";
export default {
  name: "Config",
  //   dicts: ["sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "商品详情",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        spuStatus: "",
        spuName: "",
        startTime: "",
        endTime: "",
      },
      popover: false,
      // 表单参数
      form: {},
      Statistics: {},
      timeDate: "",
      categoryprops: {
        checkStrictly: true,
        lazy: true,
        lazyLoad: this.categoryLazyLoad,
      },
    };
  },
  created() {
    this.getList();
    this.getStatistics();
  },
  methods: {
    pickerChange() {
      console.log(this.timeDate);
      let params = {
        startTime: this.timeDate,
      };
      this.getStatistics(params);
      this.popover = false;
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getSalesStatisticsPage(this.queryParams).then((response) => {
        this.configList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getStatistics(params) {
      let query = {};
      if (params) {
        query = params;
      }
      getSalesStatistics(query).then((response) => {
        this.Statistics = response.data;
      });
    },
    cascaderChange(val) {
      if (val.length == 1) {
        this.queryParams.categoryId = val[0];
      } else if (val.length == 2) {
        this.queryParams.categoryId = val[1];
      } else if (val.length == 3) {
        this.queryParams.categoryId = val[2];
      }
      // this.getList();
    },
    categoryLazyLoad(node, resolve) {
      let that = this;
      let level = node.level;
      console.log(node);
      if (!node.data) {
        getClassificationListPage({ parentId: 0 }).then((res) => {
          //接口
          const nodes = Array.from(res.data).map((item, index) => ({
            value: item.id,
            label: `${item.className}`,
            leaf: level >= 2,
          }));
          console.log("111", node);
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else if (level == 1) {
        getClassificationListPage({ parentId: node.data.value }).then((res) => {
          const nodes = Array.from(res.data).map((item) => ({
            value: item.id,
            label: `${item.className}`,
            leaf: level >= 2,
            // level: 2,
          }));
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else if (level == 2) {
        getClassificationListPage({ parentId: node.data.value }).then((res) => {
          const nodes = Array.from(res.data).map((item) => ({
            value: item.id,
            label: `${item.className}`,
            leaf: level >= 1,
            level: 1,
          }));
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else {
        resolve({});
      }
    },

    handleDetail(record) {
      this.form = record;
      this.title = "详情";
      this.open = true;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.queryParams.startTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style>
/* .el-input__inner {
  border: none;
  padding: 0px;
} */
</style>
<style scoped lang="scss">
.header {
  margin-bottom: 30px;
  .item {
    height: 100px;
  }
  .row {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    .count {
      padding: 10px;
      font-size: 23px;
      font-weight: 500;
    }
    .zz {
      font-size: 14px;
      color: red;
      padding: 0px 10px;
    }
  }
}
</style>
