<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" @click="toOrderFood" hover-class="hover-active">确定</view>

    <!-- 未登录时弹出：手机号授权一键登录 -->
    <LoginModal v-model:open="showLoginModal" @success="handleLoginSuccess" @cancel="handleLoginCancel" />
  </view>
</template>

<script setup>
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";
import { go } from "@/utils/utils.js";
import { useUserStore } from "@/store/user.js";
import LoginModal from "@/pages/components/LoginModal.vue";
import * as diningApi from "@/api/dining.js";

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

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

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

// 手机号授权登录成功：再跳转点餐页
const handleLoginSuccess = () => {
  if (pendingNavigate.value) {
    const { tableid, dinersVal } = pendingNavigate.value;
    pendingNavigate.value = null;
    go(`/pages/orderFood/index?tableid=${tableid}&diners=${dinersVal}`);
  }
};

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));

  if (!userStore.getToken) {
    // 未登录：弹出手机号授权一键登录弹窗，登录成功后再跳转
    pendingNavigate.value = { tableid, dinersVal };
    showLoginModal.value = true;
    return;
  }
uni.reLaunch({
  url: `/pages/orderFood/index?tableid=${tableid}&diners=${dinersVal}`
});
}

onLoad(async (e) => {
  const tableid = uni.getStorageSync('currentTableId') || '';
  if (!tableid) return;
  try {
    const res = await diningApi.GetTableDiningStatus(tableid);
    const raw = (res && typeof res === 'object') ? res : {};
    const inDining = raw?.inDining === true;
    const dinerCount = raw?.dinerCount ?? raw?.diner ?? uni.getStorageSync('currentDiners') ?? 1;
    if (inDining) {
      uni.setStorageSync('currentDiners', dinerCount);
      uni.reLaunch({
        url: `/pages/orderFood/index?tableid=${encodeURIComponent(tableid)}&diners=${encodeURIComponent(dinerCount)}`
      });
      return;
    }
  } catch (err) {
    console.warn('查询桌位就餐状态失败，按正常流程', err);
  }
});
</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;
}
</style>
