<template>
  <div class="go-examine">
    <div class="panel">
      <div class="store-name">
        <el-image :src="homeIcon" class="homeIcon" />
        <div>{{ storeName }}</div>
      </div>
      <div class="expire">店铺到期时间：{{ expireDate }}</div>
    </div>

    <!-- <div class="verify-row">
      <el-input outline="none" v-model="voucherCode" placeholder="请输入劵码" class="verify-input" maxlength="30" clearable />
      <el-button type="primary" class="verify-btn" @click="handleVerify"> 验 券 </el-button>
    </div> -->

    <!-- <div class="stats">
      <div class="wallet">
        <div class="stat-card">
          <div class="stat-title">店铺钱包（元）</div>
          <div class="stat-value">
            {{ walletAmount }}
          </div>
        </div>
        <el-image :src="homeWallet" class="walletImg" />
      </div>
      <div class="wallet">
        <div class="stat-card">
          <div class="stat-title">今日订单量（单）</div>
          <div class="stat-value">
            {{ todayOrders }}
          </div>
        </div>
        <el-image :src="homeOrder" class="walletImg" />
      </div>
      <div class="wallet">
        <div class="stat-card">
          <div class="stat-title">今日收益（元）</div>
          <div class="stat-value">
            {{ todayRevenue }}
          </div>
        </div>
        <el-image :src="homeIncome" class="walletImg" />
      </div>
    </div> -->
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import homeIcon from "../../../assets/images/home-icon.png";
import homeWallet from "../../../assets/images/home-wallet.png";
import homeOrder from "../../../assets/images/home-order.png";
import homeIncome from "../../../assets/images/home-income.png";
import {
  verifyOrder,
  verifyCoupon,
  getTodayOrderCount,
  getTodayIncome,
  getAccountBalance,
  getDetail
} from "@/api/modules/homeEntry";

import { localGet, localSet } from "@/utils/index";
const userInfo = localGet("geeker-user").userInfo;
// 数据（可根据实际接口替换）
const storeName = ref("");
const expireDate = ref("2027/10/29");
const voucherCode = ref("");

const walletAmount = ref(0);
const todayOrders = ref(0);
const todayRevenue = ref(0);
onMounted(() => {
  storeName.value = localGet("storeName") || "店铺名称";
  getOrder();
  getInCome();
  // getMyMoney();
  // getStoreDetail();
});

// const getStoreDetail = async () => {
//   let param = {
//     id: userInfo.storeId
//   };
//   const res: any = await getDetail(param as any);
//   if (res.code == 200) {
//     console.log(res.data)
//     storeName.value = res.data.storeName;
//   }
// };
//可提现金额
// const getMyMoney = async () => {
//   let param = {
//     storeId: userInfo.storeId
//   };
//   const res: any = await getAccountBalance(param as any);
//   if (res.code == 200) {
//     walletAmount.value = res.data.cashOutMoney; //cashOutMoney  可提现金额减未审核通过的金额
//   }
// };
// const getMyMoney = async () => {
//   const res: any = await getMerchantByPhone({ phone: userInfo.phone });
//   if (res.code == 200) {
//     // walletAmount.value = res.data.moneyStr;
//     // 更新缓存里的moneyStr
//     try {
//       const geekerUser = localGet("geeker-user");
//       if (geekerUser && geekerUser.userInfo) {
//         geekerUser.userInfo.moneyStr = res.data.moneyStr;
//         localSet("geeker-user", geekerUser);
//       }
//     } catch (error) {
//       console.error("更新缓存失败:", error);
//     }
//   }
// };
const getOrder = async () => {
  const res: any = await getTodayOrderCount({ storeId: localGet("geeker-user").storeId });
  if (res.code == 200) {
    todayOrders.value = res.data;
  }
};
const getInCome = async () => {
  const res: any = await getTodayIncome({ storeId: localGet("geeker-user").storeId });
  if (res.success) {
    todayRevenue.value = res.data;
  }
};
const handleVerify = async () => {
  // 调用验券接口
  if (!voucherCode.value) return;

  try {
    const res: any = await verifyOrder({ orderCode: voucherCode.value });

    // 如果第一个接口返回错误，响应拦截器已经显示了错误信息，这里只需要清理状态
    if (res.code !== 200) {
      voucherCode.value = "";
      return;
    }

    // 第一个接口成功，调用第二个接口
    let i = {
      quanCode: voucherCode.value,
      storeId: userInfo.storeId
    };
    const res1: any = await verifyCoupon(i);

    // 检查第二个接口的返回结果
    // 响应拦截器成功时返回的是 { code: 200, msg: "...", data: {...} }
    if (res1 && res1.code === 200) {
      ElMessage.success(res.msg || res1.msg || "验券成功");
      voucherCode.value = "";
      getOrder();
      getInCome();
    } else {
      // 第二个接口返回错误，响应拦截器已经显示了错误信息，这里只需要清理状态
      voucherCode.value = "";
    }
  } catch (error: any) {
    // 捕获异常情况
    // 注意：响应拦截器已经处理了业务错误的显示（code !== 200）
    // 这里只处理网络错误等非业务错误，或者响应拦截器未处理的错误
    console.error("验券失败:", error);
    // 如果是业务错误（有 response.data.code），响应拦截器已经显示了，不需要再次显示
    // 只处理网络错误、超时等异常情况
    // if (!error?.response?.data?.code) {
    //   const errorMsg = error?.message || "验券失败，请重试";
    //   ElMessage.error(errorMsg);
    // }
    voucherCode.value = "";
  }
};
</script>

<style scoped lang="scss">
.go-examine {
  box-sizing: border-box;
  width: 100%;
  height: calc(100vh - 105px);
  overflow: hidden;
  background: url("../../../assets/images/home-bg.png") center center no-repeat;
  background-size: cover;
  .panel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 105px;
    padding: 16px;
    margin-bottom: 16px;
    background: #ffffff;
    background: linear-gradient(90deg, #6c8ff8 0%, rgb(255 255 255 / 0%) 100%);
    border: 1px solid #e4e7ed;
    border-radius: 10px;
    .store-name {
      display: flex;
      align-items: center;
      font-size: 24px;
      font-weight: 600;
      color: #ffffff;
      .homeIcon {
        width: 76px;
        height: 76px;
        margin-right: 8px;
        margin-left: 25px;
      }
    }
    .expire {
      font-size: 13px;
      color: #606266;
    }
  }
  .verify-row {
    display: flex;
    align-items: center;
    height: 228px;
    padding: 0 50px;
    background: linear-gradient(90deg, #6c8ff8 0%, rgb(255 255 255 / 0%) 100%);
    border-radius: 10px;
    .verify-input {
      height: 132px;
      font-size: 30px;

      // 去除输入框获得焦点时的默认蓝框
      :deep(.el-input__wrapper) {
        outline: none;
        box-shadow: none;
      }
    }
    .verify-btn {
      width: 356px;
      height: 132px;
      font-size: 30px;
      background: #6c8ff8;
      border: none;
    }
  }
  .stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    margin-top: 30px;
    .wallet {
      display: flex;
      align-items: center;
      justify-content: center;
      background: #ffffff;
      border: 1px solid #e4e7ed;
      border-radius: 6px;
      .stat-card {
        padding: 26px 12px;
        text-align: center;
        .stat-title {
          margin-bottom: 14px;
          font-size: 14px;
          color: #606266;
        }
        .stat-value {
          font-size: 22px;
          font-weight: 600;
          color: #303133;
        }
      }
      .walletImg {
        width: 211px;
        height: 211px;
      }
    }
  }
}
</style>
