<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import ElementPlus from "element-plus";
import { AiFillRead } from "vue-icons-plus/ai";
import axios from "axios";
import API from "../../api/index.js";
// 变量
// 充值明细表格
const tableData = ref([]);
// 搜索======================================
// 搜索detailVo
const detailVo = ref({});
// 搜索对象
const getObj = ref({
  pageNum: 1,
  pageSize: 10,
});
//分页总条目
const total = ref(100);
// 搜索对象时间
const getTime = ref([]);
// 搜索活动列表
const activity = ref([]);
// 所有信息
const allData = ref([]);
// 搜索地区列表
const area = ref([]);

//标签页默认高亮选项
const activeName = ref("all");

// 消费平台选项
const consumePlatform = [
  {
    value: "HomilyLink",
    label: "HomilyLink",
  },
  {
    value: "金币系统",
    label: "金币系统",
  },
  {
    value: "ERP系统",
    label: "ERP系统",
  },
  {
    value: "金币系统",
    label: "金币系统",
  },
];
const consumeType = [
  {
    value: "购买商品",
    label: "购买商品",
  },
];

//表格高度
const tableHeight = computed(function () {
  return (getObj.value.pageSize + 1) * 41 + "px";
});
// 方法
// 搜索==============================================================
// 搜索方法
const get = async function (val) {
  try {
    // 搜索参数页码赋值
    if (typeof val === "number") {
      getObj.value.pageNum = val;
    }
    // 搜索参数时间赋值
    if (getTime.value != null) {
      if (getTime.value.startDate != "" && getTime.value.endDate != "") {
        detailVo.value.startDate = getTime.value[0];
        detailVo.value.endDate = getTime.value[1];
      }
    } else {
      detailVo.value.startDate = "";
      detailVo.value.endDate = "";
    }
    console.log("搜索参数", getObj.value);
    // 发送POST请求
    const result = await API.post("http://192.168.8.93:10010/consume/select", {
      ...getObj.value,
      detailVo: { ...detailVo.value },
    });

    // 将响应结果存储到响应式数据中
    console.log("请求成功", result);
    // 存储表格数据
    tableData.value = result.data.list;
    console.log("tableData", tableData.value);
    // 存储分页总数
    total.value = result.data.total;
    console.log("total", total.value);
  } catch (error) {
    console.log("请求失败", error);
    // 在这里可以处理错误逻辑，比如显示错误提示等
  }
};
// 重置
const reset = function () {
  detailVo.value.productName = "";
  detailVo.value.consumePlatform = "";
  detailVo.value.consumeType = "";
  detailVo.value.startDate = "";
  detailVo.value.endDate = "";
};
// 今天
const getToday = function () {
  const today = new Date();
  const startDate = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate()
  );
  const endDate = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() + 1
  );
  getTime.value = [startDate, endDate];
  console.log("getTime", getTime.value);
  get();
};
// 昨天
const getYesterday = function () {
  const yesterday = new Date();
  yesterday.setDate(yesterday.getDate() - 1);
  const startDate = new Date(
    yesterday.getFullYear(),
    yesterday.getMonth(),
    yesterday.getDate()
  );
  const endDate = new Date(
    yesterday.getFullYear(),
    yesterday.getMonth(),
    yesterday.getDate() + 1
  );
  getTime.value = [startDate, endDate];
  console.log("getTime", getTime.value);
  get();
};
// 近7天
const get7Days = function () {
  const today = new Date();
  const startDate = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() - 6
  );
  const endDate = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() + 1
  );
  getTime.value = [startDate, endDate];
  console.log("getTime", getTime.value);
  get();
};
//点击标签页
const handleClick = function (tab, event) {
  if (tab.props.name === "all") {
    adminAll();
  } else if (tab.props.name === "wait") {
    adminWait();
  } else if (tab.props.name === "pass") {
    adminPass();
  } else if (tab.props.name === "reject") {
    adminReject();
  }
};

// 挂载
onMounted(async function () {
  await get();
  // getActivity();
  // await getArea();
});

// 验证跳转输入框的数字是否合法
const checkNumber = function () {
  if (typeof parseInt(getObj.value.pageNum) === "number") {
    console.log(
      "总共有多少页" + Math.ceil(total.value / getObj.value.pageSize)
    );
    if (
      getObj.value.pageNum > 0 &&
      getObj.value.pageNum <= Math.ceil(total.value / getObj.value.pageSize)
    ) {
      console.log("输入的数字合法");
      get();
    } else {
      //提示
      ElMessage({
        type: "error",
        message: "请检查输入内容",
      });
    }
  } else {
    //提示
    ElMessage({
      type: "error",
      message: "请检查输入内容",
    });
  }
};
// 查询商品的接口
const goods = ref([]);
const getGoods = async function () {
  try {
    // 发送POST请求
    const result = await API.post("http://192.168.8.93:10010/product", {});
    // 将响应结果存储到响应式数据中
    console.log("请求成功", result);
    // 存储全部数据
    goods.value = result.data;
    console.log("allData", allData.value);
    console.log("地区", area.value);
  } catch (error) {
    console.log("请求失败", error);
    // 在这里可以处理错误逻辑，比如显示错误提示等
  }
};
getGoods();
</script>

<template>
  <el-row>
    <el-col>
      <el-card style="margin-bottom: 20px">
        <el-row style="margin-bottom: 10px">
          <el-col :span="8">
            <div class="head-card-element">
              <el-text class="mx-1" size="large">商品名称：</el-text>
              <el-select
                v-model="detailVo.productName"
                placeholder="请选择商品名称"
                size="large"
                style="width: 240px"
                clearable
              >
                <el-option
                  v-for="item in goods"
                  :key="item.activityId"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="head-card-element">
              <el-text class="mx-1" size="large">消费平台：</el-text>
              <el-select
                v-model="detailVo.consumePlatform"
                placeholder="请选择消费平台"
                size="large"
                style="width: 240px"
                clearable
              >
                <el-option
                  v-for="item in consumePlatform"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="head-card-element">
              <el-text class="mx-1" size="large">消费类型：</el-text>
              <el-select
                v-model="detailVo.consumeType"
                placeholder="请选择消费类型"
                size="large"
                style="width: 240px"
                clearable
              >
                <el-option
                  v-for="item in consumeType"
                  :key="item"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="21">
            <div class="head-card-element">
              <el-text class="mx-1" size="large">消费时间：</el-text>
              <el-date-picker
                v-model="getTime"
                type="datetimerange"
                range-separator="至"
                start-placeholder="起始时间"
                end-placeholder="结束时间"
              />
              <el-button style="margin-left: 10px" @click="getToday()"
                >今</el-button
              >
              <el-button @click="getYesterday()">昨</el-button>
              <el-button @click="get7Days()">近7天</el-button>
            </div>
          </el-col>
          <el-col :span="3">
            <div class="head-card-btn">
              <el-button @click="reset()">重置</el-button>
              <el-button type="primary" @click="get()">查询</el-button>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </el-col>
  </el-row>
  <el-row>
    <el-col>
      <el-card>
        <div>
          <el-table
            :data="tableData"
            :height="tableHeight"
            style="width: 100%"
            :default-sort="{ prop: 'createTime ', order: 'descending' }"
          >
            <el-table-column prop="userName" label="姓名" width="80px" />
            <el-table-column prop="jwcode" label="精网号" width="180px" />
            <el-table-column prop="area" label="所属地区" width="120px" />
            <el-table-column
              prop="productName"
              label="商品名称"
              width="200px"
            />
            <el-table-column
              prop="consumePlatform"
              label="消费平台"
              width="120px"
            />
            <el-table-column
              prop="consumeType"
              label="消费类型"
              width="120px"
            />
            <el-table-column
              prop="rechargeTotal"
              label="消费金币总数"
              width="180px"
            >
              <template #default="scope">
                {{
                  scope.row.taskCoin +
                  scope.row.freeCoin +
                  scope.row.rechargeCoin
                }}
              </template>
            </el-table-column>
            <el-table-column prop="taskCoin" label="任务金币" width="120px" />
            <el-table-column prop="freeCoin" label="免费金币" width="120px" />
            <el-table-column
              prop="rechargeCoin"
              label="充值金币"
              width="120px"
            />
            <el-table-column
              prop="remark"
              label="备注"
              width="200px"
              show-overflow-tooltip
            />
            <el-table-column prop="adminName" label="提交人" width="120px" />
            <el-table-column
              prop="createTime"
              label="消费时间"
              width="180px"
              sortable
            />
          </el-table>
        </div>

        <!-- 分页 -->
        <div class="pagination">
          <el-pagination
            background
            :page-size="getObj.pageSize"
            layout="slot"
            :total="total"
          >
            <div>共{{ total }}条,每页</div>
            <el-select
              v-model="getObj.pageSize"
              class="page-size"
              @change="get()"
              style="width: 80px"
            >
              <el-option
                v-for="item in [5, 6, 7, 8, 9, 10]"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
            <div>条</div>
          </el-pagination>
          <el-pagination
            background
            layout="prev, pager, next, slot"
            :page-size="getObj.pageSize"
            :total="total"
            :current-page="getObj.pageNum"
            @current-change="get"
          >
            <div>跳至</div>
            <el-input
              v-model="getObj.pageNum"
              style="width: 40px"
              @change="checkNumber"
            />
            <div>页</div>
          </el-pagination>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<style scoped>
.status {
  display: flex;
}

.head-card {
  display: flex;
}

.head-card-element {
  margin-right: 20px;
}

.head-card-btn {
  margin-left: auto;
}
.pagination {
  display: flex;
  margin-top: 20px;
}
</style>
