<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { formatMoney } from '../../utils/localeDisplay';

export interface CsConfirmLine {
  scoreDisplay: string;
  odds: string;
  stake: number;
}

const props = defineProps<{
  open: boolean;
  marketLabel: string;
  homeTeamName: string;
  awayTeamName: string;
  lines: CsConfirmLine[];
  loading?: boolean;
}>();

const emit = defineEmits<{ confirm: []; close: [] }>();

const { t, locale } = useI18n();

const totalStake = computed(() =>
  props.lines.reduce((sum, line) => sum + line.stake, 0),
);

const totalReturn = computed(() =>
  props.lines.reduce((sum, line) => {
    const odds = parseFloat(line.odds);
    return sum + line.stake * (Number.isFinite(odds) ? odds : 0);
  }, 0),
);

function formatOdds(odds: string) {
  const n = parseFloat(odds);
  return Number.isFinite(n) ? n.toFixed(2) : odds;
}
</script>

<template>
  <div v-if="open && lines.length" class="overlay" @click.self="emit('close')">
    <div class="drawer" role="dialog" aria-modal="true">
      <div class="drawer-header">
        <h3>
          {{ t('bet.cs_confirm_title') }}
          <span class="count">({{ lines.length }})</span>
        </h3>
        <button type="button" class="close-btn" :aria-label="t('bet.cancel')" @click="emit('close')">
          ✕
        </button>
      </div>

      <div class="drawer-body">
        <div class="slip-item">
          <div class="item-name">{{ homeTeamName }} vs {{ awayTeamName }}</div>
          <div class="item-sel">{{ marketLabel }}</div>
        </div>

        <div v-for="(line, idx) in lines" :key="idx" class="slip-item">
          <div class="item-name">{{ line.scoreDisplay }}</div>
          <div class="item-sel">
            @ <span class="odds">{{ formatOdds(line.odds) }}</span>
            · {{ formatMoney(line.stake, locale) }}
          </div>
        </div>
      </div>

      <div class="drawer-foot">
        <div class="stake-area">
          <div class="summary-row">
            <span>{{ t('bet.cs_confirm_total_stake') }}</span>
            <strong>{{ formatMoney(totalStake, locale) }}</strong>
          </div>
          <div class="return">
            {{ t('bet.slip_est_return') }}:
            <strong>{{ formatMoney(totalReturn, locale) }}</strong>
          </div>
        </div>

        <button
          type="button"
          class="btn-primary"
          :disabled="loading"
          @click="emit('confirm')"
        >
          {{ loading ? t('bet.placing') : t('bet.place_bet') }}
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 210;
  display: flex;
  align-items: flex-end;
}

.drawer {
  background: linear-gradient(180deg, #222 0%, #141414 100%);
  width: 100%;
  max-height: 80vh;
  border-radius: 16px 16px 0 0;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 10px 14px 0;
  -webkit-overflow-scrolling: touch;
}

.drawer-foot {
  flex-shrink: 0;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px));
  background: rgba(14, 14, 14, 0.98);
  border-top: 1px solid var(--border-gold-soft);
  box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.45);
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 14px 12px;
  flex-shrink: 0;
  background: rgba(14, 14, 14, 0.98);
  border-bottom: 1px solid #2a2a2a;
  z-index: 2;
}

.drawer-header h3 {
  font-size: 16px;
  font-weight: 800;
}

.count {
  color: var(--primary-light);
}

.close-btn {
  background: none;
  color: var(--text-muted);
  font-size: 20px;
  padding: 4px;
}

.slip-item {
  padding: 10px 12px;
  background: #111;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 8px;
}

.item-name {
  font-size: 13px;
  font-weight: 800;
}

.item-sel {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

.odds {
  color: var(--primary-light);
  font-weight: 800;
}

.stake-area {
  margin: 12px 0;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 8px;
}

.summary-row strong {
  color: var(--primary-light);
  font-size: 15px;
  font-weight: 800;
}

.return {
  font-size: 13px;
  color: var(--text-muted);
}

.return strong {
  color: var(--primary-light);
  font-size: 18px;
  font-weight: 800;
}
</style>
