<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useAdminLocale } from '../composables/useAdminLocale';
import api from '../api';
import AdminTableEmpty from '../components/AdminTableEmpty.vue';

const { t } = useAdminLocale();

interface PaymentMethod {
  id: string;
  methodType: string;
  bankName: string | null;
  accountHolder: string | null;
  accountNumber: string | null;
  usdtAddress: string | null;
  qrCodeUrl: string | null;
  displayName: string | null;
  sortOrder: number;
  isActive: boolean;
  createdAt: string;
  translations?: {
    displayName?: Record<string, string>;
    bankName?: Record<string, string>;
  };
}

const items = ref<PaymentMethod[]>([]);
const loading = ref(false);
const typeFilter = ref<'' | 'BANK' | 'USDT'>('');

// Dialog
const dialogVisible = ref(false);
const editingId = ref<string | null>(null);
const form = ref({
  methodType: 'BANK' as 'BANK' | 'USDT',
  bankName: '',
  accountHolder: '',
  accountNumber: '',
  usdtAddress: '',
  qrCodeUrl: '',
  displayName: '',
  sortOrder: 0,
  isActive: true,
  translations: {
    displayName: { 'zh-CN': '', 'en-US': '', 'ms-MY': '' },
    bankName: { 'zh-CN': '', 'en-US': '', 'ms-MY': '' },
  },
});

const filteredItems = computed(() => {
  if (!typeFilter.value) return items.value;
  return items.value.filter((m) => m.methodType === typeFilter.value);
});

async function fetchList() {
  loading.value = true;
  try {
    const { data } = await api.get('/admin/payment-methods');
    items.value = (data.data ?? []).map((m: any) => ({ ...m, id: String(m.id) }));
  } catch { /* */ } finally {
    loading.value = false;
  }
}

function openCreate() {
  editingId.value = null;
  form.value = {
    methodType: 'BANK',
    bankName: '',
    accountHolder: '',
    accountNumber: '',
    usdtAddress: '',
    qrCodeUrl: '',
    displayName: '',
    sortOrder: 0,
    isActive: true,
    translations: {
      displayName: { 'zh-CN': '', 'en-US': '', 'ms-MY': '' },
      bankName: { 'zh-CN': '', 'en-US': '', 'ms-MY': '' },
    },
  };
  dialogVisible.value = true;
}

function openEdit(row: PaymentMethod) {
  editingId.value = row.id;
  const t = row.translations ?? {};
  form.value = {
    methodType: row.methodType as 'BANK' | 'USDT',
    bankName: row.bankName ?? '',
    accountHolder: row.accountHolder ?? '',
    accountNumber: row.accountNumber ?? '',
    usdtAddress: row.usdtAddress ?? '',
    qrCodeUrl: row.qrCodeUrl ?? '',
    displayName: row.displayName ?? '',
    sortOrder: row.sortOrder,
    isActive: row.isActive,
    translations: {
      displayName: {
        'zh-CN': t.displayName?.['zh-CN'] ?? '',
        'en-US': t.displayName?.['en-US'] ?? '',
        'ms-MY': t.displayName?.['ms-MY'] ?? '',
      },
      bankName: {
        'zh-CN': t.bankName?.['zh-CN'] ?? '',
        'en-US': t.bankName?.['en-US'] ?? '',
        'ms-MY': t.bankName?.['ms-MY'] ?? '',
      },
    },
  };
  dialogVisible.value = true;
}

async function handleSave() {
  const { translations, ...rest } = form.value;
  // Only send non-empty translations
  const cleanTranslations = {
    displayName: Object.fromEntries(
      Object.entries(translations.displayName).filter(([, v]) => v.trim()),
    ),
    bankName: Object.fromEntries(
      Object.entries(translations.bankName).filter(([, v]) => v.trim()),
    ),
  };
  const payload: any = {
    ...rest,
    translations: (Object.keys(cleanTranslations.displayName).length || Object.keys(cleanTranslations.bankName).length)
      ? cleanTranslations
      : undefined,
  };
  try {
    if (editingId.value) {
      await api.put(`/admin/payment-methods/${editingId.value}`, payload);
    } else {
      await api.post('/admin/payment-methods', payload);
    }
    dialogVisible.value = false;
    await fetchList();
  } catch (e: any) {
    alert(e.response?.data?.message || 'Error');
  }
}

async function handleDelete(row: PaymentMethod) {
  if (!confirm(t('deposit.confirm_deactivate'))) return;
  try {
    await api.delete(`/admin/payment-methods/${row.id}`);
    await fetchList();
  } catch { /* */ }
}

async function toggleActive(row: PaymentMethod) {
  try {
    await api.put(`/admin/payment-methods/${row.id}`, { isActive: !row.isActive });
    await fetchList();
  } catch { /* */ }
}

// QR code upload for USDT
const uploading = ref(false);
async function handleQrUpload(event: Event) {
  const input = event.target as HTMLInputElement;
  const file = input.files?.[0];
  if (!file) return;
  uploading.value = true;
  try {
    const fd = new FormData();
    fd.append('file', file);
    const { data } = await api.post('/admin/uploads?category=payments', fd);
    form.value.qrCodeUrl = data.data?.url ?? '';
  } catch { /* */ } finally {
    uploading.value = false;
    input.value = '';
  }
}

onMounted(fetchList);
</script>

<template>
  <div class="page-payment-methods">
    <div class="toolbar">
      <h2>{{ t('deposit.payment_methods_title') }}</h2>
      <div class="actions">
        <select v-model="typeFilter" class="filter-select">
          <option value="">{{ t('common.all') }}</option>
          <option value="BANK">Bank</option>
          <option value="USDT">USDT</option>
        </select>
        <button class="btn-primary" @click="openCreate">{{ t('deposit.add_method') }}</button>
      </div>
    </div>

    <AdminTableEmpty v-if="!loading && !filteredItems.length" />

    <table v-if="filteredItems.length" class="data-table">
      <thead>
        <tr>
          <th style="text-align: center; width: 60px;">{{ t('common.seq') }}</th>
          <th>{{ t('common.type') }}</th>
          <th>{{ t('deposit.display_name') }}</th>
          <th>{{ t('deposit.details') }}</th>
          <th>{{ t('deposit.sort') }}</th>
          <th>{{ t('deposit.active') }}</th>
          <th>{{ t('common.actions') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, index) in filteredItems" :key="row.id">
          <td style="text-align: center;">{{ index + 1 }}</td>
          <td><span :class="['badge', row.methodType === 'BANK' ? 'badge-blue' : 'badge-green']">{{ row.methodType }}</span></td>
          <td>{{ row.displayName || '-' }}</td>
          <td class="details-cell">
            <template v-if="row.methodType === 'BANK'">
              <div>{{ row.bankName }}</div>
              <div class="sub">{{ row.accountHolder }} · {{ row.accountNumber }}</div>
            </template>
            <template v-else>
              <div>{{ row.usdtAddress }}</div>
              <img v-if="row.qrCodeUrl" :src="row.qrCodeUrl" class="qr-thumb" />
            </template>
          </td>
          <td>{{ row.sortOrder }}</td>
          <td>
            <span :class="row.isActive ? 'status-on' : 'status-off'">{{ row.isActive ? 'ON' : 'OFF' }}</span>
          </td>
          <td class="actions-cell">
            <button class="btn-sm" @click="openEdit(row)">{{ t('common.edit') }}</button>
            <button class="btn-sm btn-toggle" @click="toggleActive(row)">
              {{ row.isActive ? t('common.disable') : t('common.enable') }}
            </button>
            <button class="btn-sm btn-danger" @click="handleDelete(row)">{{ t('common.delete') }}</button>
          </td>
        </tr>
      </tbody>
    </table>

    <!-- Create/Edit Dialog -->
    <div v-if="dialogVisible" class="dialog-overlay">
      <div class="dialog-box">
        <h3>{{ editingId ? t('deposit.edit_method') : t('deposit.create_method') }}</h3>
        <div class="form-group" v-if="!editingId">
          <label>{{ t('common.type') }}</label>
          <select v-model="form.methodType">
            <option value="BANK">Bank</option>
            <option value="USDT">USDT</option>
          </select>
        </div>
        <div class="form-group">
          <label>{{ t('deposit.display_name') }}</label>
          <input v-model="form.displayName" :placeholder="t('deposit.display_name')" />
          <div class="lang-section">
            <div class="lang-hint">{{ t('deposit.multilingual_hint') }}</div>
            <div class="lang-row">
              <span class="lang-tag">{{ t('deposit.lang_zh') }}</span>
              <input v-model="form.translations.displayName['zh-CN']" placeholder="中文名称" />
            </div>
            <div class="lang-row">
              <span class="lang-tag">{{ t('deposit.lang_en') }}</span>
              <input v-model="form.translations.displayName['en-US']" placeholder="English name" />
            </div>
            <div class="lang-row">
              <span class="lang-tag">{{ t('deposit.lang_ms') }}</span>
              <input v-model="form.translations.displayName['ms-MY']" placeholder="Nama Bahasa Melayu" />
            </div>
          </div>
        </div>
        <template v-if="form.methodType === 'BANK'">
          <div class="form-group">
            <label>{{ t('deposit.bank_name') }}</label>
            <input v-model="form.bankName" :placeholder="t('deposit.bank_name')" />
            <div class="lang-section">
              <div class="lang-row">
                <span class="lang-tag">{{ t('deposit.lang_zh') }}</span>
                <input v-model="form.translations.bankName['zh-CN']" placeholder="银行名称（中文）" />
              </div>
              <div class="lang-row">
                <span class="lang-tag">{{ t('deposit.lang_en') }}</span>
                <input v-model="form.translations.bankName['en-US']" placeholder="Bank name (English)" />
              </div>
              <div class="lang-row">
                <span class="lang-tag">{{ t('deposit.lang_ms') }}</span>
                <input v-model="form.translations.bankName['ms-MY']" placeholder="Nama bank (Melayu)" />
              </div>
            </div>
          </div>
          <div class="form-group">
            <label>{{ t('deposit.account_holder') }}</label>
            <input v-model="form.accountHolder" />
          </div>
          <div class="form-group">
            <label>{{ t('deposit.account_number') }}</label>
            <input v-model="form.accountNumber" />
          </div>
        </template>
        <template v-else>
          <div class="form-group">
            <label>{{ t('deposit.usdt_address') }}</label>
            <input v-model="form.usdtAddress" />
          </div>
          <div class="form-group">
            <label>{{ t('deposit.qr_code') }}</label>
            <div class="qr-upload-row">
              <img v-if="form.qrCodeUrl" :src="form.qrCodeUrl" class="qr-preview" />
              <input type="file" accept="image/*" @change="handleQrUpload" :disabled="uploading" />
            </div>
          </div>
        </template>
        <div class="form-group">
          <label>{{ t('deposit.sort_order') }}</label>
          <input v-model.number="form.sortOrder" type="number" />
        </div>
        <div class="form-group row-checks">
          <label class="check-option">
            <input type="checkbox" v-model="form.isActive" />
            <span class="check-option__box" aria-hidden="true"></span>
            <span>{{ t('deposit.active') }}</span>
          </label>
        </div>
        <div class="dialog-actions">
          <button class="btn-cancel" @click="dialogVisible = false">{{ t('common.cancel') }}</button>
          <button class="btn-primary" @click="handleSave">{{ t('deposit.save') }}</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.page-payment-methods { padding: 16px; }
.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.toolbar h2 { margin: 0; font-size: 18px; }
.actions { display: flex; gap: 8px; align-items: center; }
.filter-select { padding: 6px 10px; border-radius: 4px; border: 1px solid #444; background: #1e1e1e; color: #eee; }
.btn-primary { background: #409eff; color: #fff; border: none; border-radius: 4px; padding: 8px 16px; cursor: pointer; font-weight: 600; }
.btn-primary:hover { background: #337ecc; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 10px 8px; border-bottom: 1px solid #333; text-align: left; }
.data-table th { font-weight: 700; color: #aaa; font-size: 12px; text-transform: uppercase; }
.details-cell .sub { font-size: 11px; color: #888; margin-top: 2px; }
.qr-thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; margin-top: 4px; }
.badge { padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 700; }
.badge-blue { background: #1e3a5f; color: #66b1ff; }
.badge-green { background: rgba(201, 162, 39, 0.12); color: var(--gold-text); }
.status-on { font-size: 12px; font-weight: 700; color: var(--success-text); }
.status-off { font-size: 12px; font-weight: 700; color: #666; }
.actions-cell { white-space: nowrap; }
.btn-sm { border: none; border-radius: 4px; padding: 4px 10px; font-size: 11px; cursor: pointer; background: #2d2d2d; color: #ddd; margin-right: 4px; }
.btn-sm:hover { background: #444; }
.btn-toggle { background: #1e2a3a; color: #66b1ff; }
.btn-toggle:hover { background: #253a4f; }
.btn-danger { color: #f56c6c; }
.btn-danger:hover { background: #3a1a1a; }

.dialog-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 999; }
.dialog-box { background: #1e1e1e; border-radius: 8px; padding: 24px; min-width: 420px; max-width: 560px; max-height: 85vh; overflow-y: auto; }
.dialog-box h3 { margin: 0 0 16px; font-size: 16px; }
.form-group { margin-bottom: 12px; }
.form-group label { display: block; font-size: 12px; color: #aaa; margin-bottom: 4px; font-weight: 600; }
.form-group input, .form-group select { width: 100%; padding: 8px; border: 1px solid #444; border-radius: 4px; background: #111; color: #eee; box-sizing: border-box; }
.row-checks { display: flex; gap: 16px; }
.row-checks label { font-size: 13px; color: #ddd; display: flex; align-items: center; gap: 4px; }
.qr-upload-row { display: flex; align-items: center; gap: 12px; }
.qr-preview { width: 80px; height: 80px; object-fit: cover; border-radius: 4px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.btn-cancel { background: #333; color: #ccc; border: none; border-radius: 4px; padding: 8px 16px; cursor: pointer; }

.lang-section { margin-top: 6px; padding: 8px 10px; background: #161616; border-radius: 4px; border: 1px solid #2a2a2a; }
.lang-hint { font-size: 10px; color: #666; margin-bottom: 6px; line-height: 1.4; }
.lang-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.lang-row:last-child { margin-bottom: 0; }
.lang-tag { flex-shrink: 0; font-size: 10px; font-weight: 700; color: #aaa; background: #252525; padding: 2px 6px; border-radius: 3px; min-width: 48px; text-align: center; }
.lang-row input { flex: 1; padding: 5px 8px; border: 1px solid #333; border-radius: 3px; background: #0e0e0e; color: #ddd; font-size: 12px; box-sizing: border-box; }

.page-payment-methods {
  padding: 0;
  color: var(--text);
}

.toolbar {
  margin-bottom: 14px;
}

.toolbar h2 {
  color: var(--text);
  font-size: 20px;
  font-weight: 800;
}

.actions {
  flex-wrap: wrap;
}

.filter-select,
.form-group input,
.form-group select,
.lang-row input {
  min-height: 34px;
  border-color: var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
}

.btn-primary {
  border: 1px solid var(--primary);
  border-radius: 7px;
  background: var(--primary);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--primary-light);
}

.data-table {
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.data-table th,
.data-table td {
  border-bottom-color: var(--border-soft);
  color: var(--text);
}

.data-table th {
  background: #fbfaf7;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: 0;
}

.data-table tbody tr:hover {
  background: var(--accent-hover);
}

.details-cell .sub,
.form-group label,
.lang-hint {
  color: var(--text-muted);
}

.qr-thumb,
.qr-preview {
  border: 1px solid var(--border);
  background: #fbfaf7;
}

.badge {
  border-radius: 999px;
}

.badge-blue {
  background: var(--info-bg);
  color: var(--info-text);
}

.badge-green {
  background: var(--success-bg);
  color: var(--success-text);
}

.status-on {
  color: var(--success-text);
}

.status-off {
  color: var(--text-muted);
}

.btn-sm {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
  font-weight: 700;
}

.btn-sm:hover {
  background: var(--accent-hover);
}

.btn-toggle {
  border-color: var(--info-border);
  background: var(--info-bg);
  color: var(--info-text);
}

.btn-danger {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger-text);
}

.dialog-overlay {
  background: rgba(31, 35, 32, 0.28);
  backdrop-filter: blur(3px);
}

.dialog-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  color: var(--text);
  box-shadow: 0 24px 64px rgba(56, 49, 37, 0.14);
}

.dialog-box h3 {
  color: var(--text);
}

.row-checks label {
  color: var(--text);
}

.row-checks .check-option {
  position: relative;
  display: inline-flex;
  width: fit-content;
  min-height: 34px;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fbfaf7;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.row-checks .check-option:hover {
  border-color: #d5cfc3;
  background: var(--accent-hover);
}

.row-checks .check-option input[type='checkbox'] {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}

.check-option__box {
  position: relative;
  display: inline-flex;
  width: 16px;
  height: 16px;
  flex: 0 0 16px;
  align-items: center;
  justify-content: center;
  border: 1px solid #cfc7b9;
  border-radius: 5px;
  background: #ffffff;
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.check-option__box::after {
  content: '';
  width: 5px;
  height: 9px;
  border: solid #ffffff;
  border-width: 0 2px 2px 0;
  opacity: 0;
  transform: rotate(45deg) translateY(-1px);
  transition: opacity 0.12s ease;
}

.row-checks .check-option input[type='checkbox']:checked + .check-option__box {
  border-color: var(--primary);
  background: var(--primary);
  box-shadow: 0 0 0 3px rgba(31, 35, 32, 0.08);
}

.row-checks .check-option input[type='checkbox']:checked + .check-option__box::after {
  opacity: 1;
}

.row-checks .check-option input[type='checkbox']:focus-visible + .check-option__box {
  box-shadow: 0 0 0 3px rgba(31, 35, 32, 0.14);
}

.btn-cancel {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
}

.lang-section {
  border-color: var(--border-soft);
  background: #fbfaf7;
}

.lang-tag {
  background: #f0ede6;
  color: var(--text-muted);
}

@media (max-width: 700px) {
  .toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .actions,
  .filter-select,
  .actions .btn-primary {
    width: 100%;
  }

  .data-table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }

  .dialog-box {
    min-width: 0;
    width: calc(100vw - 32px);
    max-height: calc(100dvh - 32px);
  }
}
</style>
