<template>
  <view class="page">
    <view class="header-hero" :style="{ paddingTop: statusBarHeight + 'px' }">
      <view class="top-bar">
        <view class="top-left" />
        <view class="top-center">
          <text class="page-title">{{ t('login.selectStore') }}</text>
          <text class="page-sub">{{ t('login.welcomeUser') }} {{ userName }}</text>
        </view>
        <view class="top-right" />
      </view>
    </view>

    <view class="list">
      <view
        v-for="store in stores"
        :key="store.id"
        class="card"
        :class="{ selected: selectedStore === store.id }"
        @click="selectedStore = store.id"
      >
        <view class="card-icon" :class="{ selected: selectedStore === store.id }">
          <AppIcon name="mapPin" size="md" :color="selectedStore === store.id ? 'white' : 'gray'" />
        </view>
        <view class="card-content">
          <text class="card-title">{{ t(store.nameKey) }}</text>
          <view class="info-row">
            <AppIcon name="mapPin" size="sm" color="gray" />
            <text class="info">{{ store.address }}</text>
          </view>
          <text class="info">{{ t('location.storeManager') }}: {{ store.manager }}</text>
          <text class="info">{{ t('location.storePhone') }}: {{ store.phone }}</text>
        </view>
        <view class="card-check">
          <AppIcon
            v-if="selectedStore === store.id"
            name="check"
            size="sm"
            color="white"
          />
          <AppIcon
            v-else
            name="chevronRight"
            size="sm"
            color="gray"
          />
        </view>
      </view>
    </view>

    <view class="bottom-bar" :style="{ paddingBottom: (bottomSafeArea + 20) + 'px' }">
      <view
        class="confirm-btn"
        :class="{ disabled: !selectedStore }"
        @click="handleConfirm"
      >
        <text class="confirm-btn-text">{{ t('common.confirm') }}</text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import AppIcon from '../../components/AppIcon.vue'
import { getStatusBarHeight, getBottomSafeArea } from '../../utils/statusBar'

const { t } = useI18n()
const statusBarHeight = getStatusBarHeight()
const bottomSafeArea = getBottomSafeArea()
const userName = uni.getStorageSync('userName') || 'Employee'
const selectedStore = ref('')

const stores = [
  { id: '1', nameKey: 'login.store1', address: '123 Main St, New York, NY 10001', manager: 'Sarah Johnson', phone: '(212) 555-0101' },
  { id: '2', nameKey: 'login.store2', address: '456 Oak Ave, Brooklyn, NY 11201', manager: 'Michael Chen', phone: '(718) 555-0102' },
  { id: '3', nameKey: 'login.store3', address: '789 Pine Rd, Queens, NY 11354', manager: 'Emily Rodriguez', phone: '(718) 555-0103' },
  { id: '4', nameKey: 'login.store4', address: '321 Elm St, Manhattan, NY 10002', manager: 'David Kim', phone: '(212) 555-0104' },
]

const handleConfirm = () => {
  if (!selectedStore.value) {
    uni.showToast({ title: t('login.selectStoreError'), icon: 'none' })
    return
  }
  const store = stores.find((s) => s.id === selectedStore.value)!
  uni.setStorageSync('storeId', selectedStore.value)
  uni.setStorageSync('storeName', t(store.nameKey))
  uni.showToast({ title: t('login.storeSelected'), icon: 'success' })
  uni.redirectTo({ url: '/pages/index/index' })
}
</script>

<style scoped>
.page {
  min-height: 100vh;
  background: #f9fafb;
  padding-bottom: 200rpx;
}

.header-hero {
  background: linear-gradient(135deg, var(--theme-primary), var(--theme-primary-dark));
  padding: 16rpx 32rpx 32rpx;
}

.top-bar {
  height: 96rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.top-left,
.top-right {
  width: 64rpx;
  height: 64rpx;
}

.top-center {
  flex: 1;
  text-align: center;
}

.page-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #fff;
  display: block;
  margin-bottom: 4rpx;
}

.page-sub {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.85);
}

.list {
  padding: 32rpx;
}

.card {
  padding: 32rpx;
  background: #ffffff;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  display: flex;
  align-items: flex-start;
  gap: 24rpx;
  border: 3rpx solid #e5e7eb;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.card.selected {
  border-color: var(--theme-primary);
  background: var(--theme-primary-light);
}

.card-icon {
  width: 80rpx;
  height: 80rpx;
  background: #f3f4f6;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.card-icon.selected {
  background: var(--theme-primary);
}

.card-content {
  flex: 1;
  min-width: 0;
}

.card-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #111827;
  display: block;
  margin-bottom: 12rpx;
}

.info-row {
  display: flex;
  align-items: flex-start;
  gap: 12rpx;
  margin-bottom: 6rpx;
}

.info {
  font-size: 26rpx;
  color: #6b7280;
  display: block;
  margin-bottom: 6rpx;
}

.card-check {
  width: 44rpx;
  height: 44rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: transparent;
}

.card.selected .card-check {
  background: var(--theme-primary);
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 32rpx 48rpx;
  padding-bottom: 48rpx;
  background: #ffffff;
  border-top: 1rpx solid #e5e7eb;
}

.confirm-btn {
  width: 100%;
  height: 96rpx;
  background: var(--theme-primary);
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.confirm-btn.disabled {
  opacity: 0.5;
}

.confirm-btn-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #fff;
  line-height: 1;
}
</style>
