<template>
  <!-- 扫码点餐-选择就餐人数 -->
  <view class="content">
    <view class="title">请选择就餐人数</view>
    <view class="number-list">
      <view class="number-item" :class="{ 'current': currentDiners === item }" v-for="item in diners" :key="item"
        @click="selectDiners(item)" hover-class="hover-active">
        <view class="number-item-content">
          <view class="number-item-content-title">{{ item }}</view>
        </view>
      </view>
    </view>

    <view class="more-number" @click="addDiners" v-if="diners < 16">查看更多</view>

    <view class="confirm-button" @tap.stop="toOrderFood" hover-class="hover-active">确定</view>

    <!-- 未登录时弹出：手机号授权一键登录（需高 z-index 盖住其他内容） -->
    <view class="login-modal-wrapper">
      <LoginModal v-model:open="showLoginModal" @success="handleLoginSuccess" @cancel="handleLoginCancel" />
    </view>
  </view>
</template>

<script setup>
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";
import { useUserStore } from "@/store/user.js";
import LoginModal from "@/pages/components/LoginModal.vue";
import { TOKEN } from "@/settings/enums.js";

const userStore = useUserStore();
const diners = ref(12);
const currentDiners = ref(1);
const showLoginModal = ref(false);
// 登录成功后进入点餐页所需参数（手机号授权完成后再 navigate）
const pendingNavigate = ref(null);

const addDiners = () => {
  diners.value = 16
};

const selectDiners = (item) => {
  currentDiners.value = item;
};

/** 本版本不上预约：选完人数直接进入点餐页 */
function navigateToOrderFood(tableid, dinersVal) {
  const q = [];
  if (tableid) q.push(`tableid=${encodeURIComponent(String(tableid))}`);
  q.push(`diners=${encodeURIComponent(String(dinersVal ?? currentDiners.value))}`);
  uni.navigateTo({
    url: `/pages/orderFood/index?${q.join('&')}`
  });
}

// 手机号授权登录成功：本版本不上预约，原「就餐信息页 diningInfo」流程已注释
const handleLoginSuccess = () => {
  const pending = pendingNavigate.value;
  pendingNavigate.value = null;
  if (!pending) return;
  const { tableid, dinersVal } = pending;
  navigateToOrderFood(tableid, dinersVal);
  // go('/pages/diningInfo/index');
};

const handleLoginCancel = () => {
  pendingNavigate.value = null;
};

const toOrderFood = () => {
  uni.setStorageSync('currentDiners', currentDiners.value);
  // 桌号来自二维码缓存（App 启动时 s=店铺id t=桌号id 已写入 currentTableId）
  const tableid = uni.getStorageSync('currentTableId') || '';
  const dinersVal = currentDiners.value;
  if (tableid) uni.setStorageSync('currentTableId', String(tableid));

  const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  if (!token) {
    // 未登录：授权成功后与已登录一致，直接进入点餐页（不上预约 / diningInfo）
    pendingNavigate.value = { tableid, dinersVal };
    showLoginModal.value = true;
    return;
  }
  // 本版本不上预约：不到 diningInfo，选毕人数直接去点餐
  // uni.navigateTo({ url: '/pages/diningInfo/index' });
  navigateToOrderFood(tableid, dinersVal);
}

// 页面加载时仅同步参数到 storage，不自动弹出登录框；未登录时只能在点击「确定」时弹出登录框
onLoad((options) => {
  const tableid = options?.tableid || uni.getStorageSync('currentTableId') || '';
  const dinersVal = options?.diners || uni.getStorageSync('currentDiners') || 1;
  if (tableid) uni.setStorageSync('currentTableId', tableid);
  if (dinersVal) {
    uni.setStorageSync('currentDiners', dinersVal);
    currentDiners.value = Number(dinersVal) || 1;
  }
});
</script>

<style lang="scss" scoped>
.title {
  font-size: 32rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20rpx;
  margin-top: 60rpx;
}

.number-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 20rpx;
  margin-top: 60rpx;

  .number-item {
    width: 160rpx;
    height: 160rpx;
    background-color: #f0f0f0;
    border-radius: 8rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 32rpx;
    transition: all 0.3s ease;

    &.current {
      background: linear-gradient(180deg, #FCB73F 0%, #FC743D 100%);
      color: #fff;
      box-shadow: 0 8rpx 24rpx rgba(252, 183, 63, 0.4);
    }

    &.hover-active {
      opacity: 0.8;
    }
  }
}

.more-number {
  font-size: 28rpx;
  color: #aaa;
  text-align: center;
  margin-top: 20rpx;
  margin-top: 30rpx;
}

.confirm-button {
  width: 90%;
  height: 80rpx;
  background: linear-gradient(180deg, #FCB73F 0%, #FC743D 100%);
  color: #fff;
  text-align: center;
  line-height: 80rpx;
  border-radius: 24rpx;
  position: fixed;
  bottom: 80rpx;
  left: 50%;
  transform: translateX(-50%);
  font-size: 32rpx;
  z-index: 100;
}

/* 登录弹窗层级（与 index 一致，确保弹窗可显示） */
.login-modal-wrapper {
  position: relative;
  z-index: 99999;

  :deep(.uni-popup) {
    z-index: 99999 !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
  }
  :deep(.uni-popup__wrapper) {
    z-index: 99999 !important;
  }
}
</style>
