<template>
  <div class="app-container" :class="{ 'mobile-layout': isMobile }">
    <!-- US-519: Mobile Material Design Top Bar for Bank -->
    <MobileTopBar
      v-if="isMobile"
      title="Bancă"
      :show-menu="true"
      :actions="mobileTopBarActions"
      @menu-click="showDrawer = true"
      @action-click="handleTopBarAction"
    />

    <!-- US-501: Export Menu (for mobile export dropdown) -->
    <Menu ref="exportMenu" :model="exportMenuItems" :popup="true" />

    <!-- Mobile Drawer Menu (replaces old Sidebar) -->
    <MobileDrawerMenu
      v-model="showDrawer"
      :user="authStore.user"
      :companies-store="companyStore"
      :period-store="periodStore"
      :available-servers="authStore.availableServers"
      :current-server-id="authStore.selectedServerId"
      :auth-store="authStore"
      @logout="handleLogout"
      @server-switched="handleServerSwitched"
    />

    <!-- US-109: Filter BottomSheet for mobile -->
    <BottomSheet v-model="showFilters">
      <h3 class="bottom-sheet-title">Filtre</h3>
      <div class="bottom-sheet-filters">
        <!-- Register Type (only Bank options) -->
        <div class="form-group">
          <label class="form-label">Tip Registru</label>
          <Dropdown
            v-model="filters.registerType"
            :options="registerTypeOptions"
            option-label="label"
            option-value="value"
            placeholder="Selectați tipul"
            class="w-full"
            @change="handleFilterChange"
          />
        </div>

        <!-- Bank Account -->
        <div class="form-group">
          <label class="form-label">Banca</label>
          <Dropdown
            v-model="filters.bankAccount"
            :options="bankAccountOptions"
            placeholder="Toate băncile"
            :showClear="true"
            class="w-full"
            @change="handleFilterChange"
            :disabled="!filters.registerType || bankAccountOptions.length === 0"
          />
        </div>

        <!-- Partner Search -->
        <div class="form-group">
          <label class="form-label">Căutare Partener</label>
          <InputText
            v-model="filters.partnerName"
            placeholder="Nume partener..."
            class="w-full"
            @input="handleSearchChange"
          />
        </div>

        <!-- Bottom sheet actions -->
        <div class="bottom-sheet-actions">
          <Button
            icon="pi pi-filter-slash"
            label="Resetează"
            severity="secondary"
            outlined
            @click="resetFilters(); showFilters = false"
          />
          <Button
            icon="pi pi-check"
            label="Aplică"
            @click="showFilters = false"
          />
        </div>
      </div>
    </BottomSheet>

    <div class="register-view">
      <!-- Header - Desktop only -->
      <div class="page-header" v-if="!isMobile">
        <h1 class="page-title">
          <i class="pi pi-building"></i>
          Registru Bancă
        </h1>
      </div>

      <!-- Company Selection (when no company selected) -->
      <Card v-if="!companyStore.selectedCompany" class="company-selection-card">
        <template #content>
          <div class="company-selection">
            <p class="text-color-secondary mb-3">
              Selectați o companie pentru a vizualiza registrul de bancă:
            </p>
            <Dropdown
              v-model="selectedCompanyId"
              :options="companyStore.companyListFormatted"
              option-label="displayName"
              option-value="id_firma"
              placeholder="Alegeți compania"
              class="w-full"
              @change="handleCompanyChange"
            />
          </div>
        </template>
      </Card>

      <!-- US-109: Mobile Totals Bar -->
      <div v-if="isMobile && companyStore.selectedCompany && hasData" class="mobile-totals-bar">
        <div class="mobile-totals-grid">
          <div class="total-item">
            <span class="total-label">Sold Prec:</span>
            <span class="total-value">{{ formatCompact(treasuryStore.totals.sold_precedent_all) }}</span>
          </div>
          <div class="total-item">
            <span class="total-label">Încasări:</span>
            <span class="total-value">{{ formatCompact(treasuryStore.totals.total_incasari_all) }}</span>
          </div>
          <div class="total-item">
            <span class="total-label">Plăți:</span>
            <span class="total-value">{{ formatCompact(treasuryStore.totals.total_plati_all) }}</span>
          </div>
          <div class="total-item">
            <span class="total-label">Sold Final:</span>
            <span class="total-value">{{ formatCompact(treasuryStore.totals.sold_final_all) }}</span>
          </div>
        </div>
      </div>

      <!-- Filters - Desktop only (mobile uses BottomSheet) -->
      <Card v-if="companyStore.selectedCompany && !isMobile" class="filters-card">
        <template #content>
          <div class="form">
            <div class="form-row">
              <div class="form-col">
                <div class="form-group">
                  <label class="form-label">Tip Registru</label>
                  <Dropdown
                    v-model="filters.registerType"
                    :options="registerTypeOptions"
                    option-label="label"
                    option-value="value"
                    placeholder="Selectați tipul"
                    class="w-full"
                    @change="handleFilterChange"
                  />
                </div>
              </div>
              <div class="form-col">
                <div class="form-group">
                  <label class="form-label">Banca</label>
                  <Dropdown
                    v-model="filters.bankAccount"
                    :options="bankAccountOptions"
                    placeholder="Toate băncile"
                    :showClear="true"
                    class="w-full"
                    @change="handleFilterChange"
                    :disabled="
                      !filters.registerType || bankAccountOptions.length === 0
                    "
                  />
                </div>
              </div>
              <div class="form-col">
                <div class="form-group">
                  <label class="form-label">Căutare Partener</label>
                  <InputText
                    v-model="filters.partnerName"
                    placeholder="Nume partener..."
                    class="w-full"
                    @input="handleSearchChange"
                  />
                </div>
              </div>
            </div>

            <!-- Desktop Action buttons with Export dropdown -->
            <div class="form-actions">
              <Button
                icon="pi pi-filter-slash"
                label="Resetează Filtre"
                severity="secondary"
                outlined
                @click="resetFilters"
              />
              <SplitButton
                label="Export"
                icon="pi pi-download"
                :model="desktopExportItems"
                @click="exportPDF"
                outlined
                :disabled="!hasData"
              />
              <Button
                icon="pi pi-refresh"
                label="Actualizează"
                :loading="treasuryStore.isLoading"
                @click="refreshData"
              />
            </div>
          </div>
        </template>
      </Card>

      <!-- Summary Stats - Desktop only -->
      <div v-if="!isMobile && companyStore.selectedCompany" class="summary-stats-inline">
        <div class="stat-item">
          <span class="stat-label">Sold Precedent:</span>
          <span class="stat-value">{{ formatCurrency(treasuryStore.totals.sold_precedent_all) }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">Încasări:</span>
          <span class="stat-value">{{ formatCurrency(treasuryStore.totals.total_incasari_all) }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">Plăți:</span>
          <span class="stat-value">{{ formatCurrency(treasuryStore.totals.total_plati_all) }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-label">Sold Final:</span>
          <span class="stat-value">{{ formatCurrency(treasuryStore.totals.sold_final_all) }}</span>
        </div>
      </div>

      <!-- Data Table -->
      <Card v-if="companyStore.selectedCompany" class="data-card">
        <template #content>
          <!-- Mobile: Compact Rows with Collapsible Meta -->
          <div v-if="isMobile" class="mobile-card-list">
            <div
              v-for="(reg, index) in treasuryStore.registers"
              :key="`${reg.dataact}-${reg.nract}`"
              class="mobile-compact-row"
            >
              <div class="compact-main" @click="toggleMeta(index)">
                <span class="compact-date">{{ formatDateShort(reg.dataact) }}</span>
                <span class="compact-partner">{{ reg.nume || 'Fără partener' }}</span>
                <span
                  class="compact-amount"
                >
                  <template v-if="reg.incasari > 0">+{{ formatNumber(reg.incasari) }}</template>
                  <template v-else-if="reg.plati > 0">-{{ formatNumber(reg.plati) }}</template>
                  <template v-else>0</template>
                </span>
              </div>
              <div v-if="expandedMeta.has(index)" class="compact-meta">
                <span v-if="reg.nume_cont_bancar" class="meta-item">{{ reg.nume_cont_bancar }}</span>
                <span v-if="reg.nract" class="meta-item">Nr: {{ reg.nract }}</span>
                <span v-if="reg.explicatia" class="meta-item">{{ truncateText(reg.explicatia, 80) }}</span>
              </div>
            </div>
            <div v-if="treasuryStore.registers.length === 0" class="mobile-empty">
              <i class="pi pi-info-circle"></i>
              <p>Nu au fost găsite înregistrări</p>
            </div>
          </div>

          <!-- Desktop: DataTable -->
          <DataTable
            v-if="!isMobile"
            :value="treasuryStore.registers"
            :loading="treasuryStore.isLoading"
            :paginator="true"
            :rows="pagination.rows"
            :total-records="treasuryStore.pagination.totalRecords"
            :lazy="true"
            :striped-rows="true"
            paginator-template="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown"
            :rows-per-page-options="[25, 50, 100]"
            current-page-report-template="Afișare {first} - {last} din {totalRecords} înregistrări"
            responsive-layout="scroll"
            @page="onPage"
            class="p-datatable-sm"
            :rowClass="getRowClass"
          >
            <template #empty>
              <div class="table-empty">
                <i class="pi pi-info-circle table-empty-icon"></i>
                <p class="table-empty-message">
                  Nu au fost găsite înregistrări
                </p>
              </div>
            </template>

            <template #loading>
              <div class="loading-state">
                <ProgressSpinner />
                <p>Se încarcă registrul...</p>
              </div>
            </template>

            <Column field="dataact" header="Data" sortable class="col-data">
              <template #body="slotProps">
                {{ formatDate(slotProps.data.dataact) }}
              </template>
            </Column>
            <Column field="nract" header="Nr." sortable class="col-nr" />
            <Column
              field="nume_cont_bancar"
              header="Banca"
              sortable
              class="col-cont"
            />
            <Column
              field="nume"
              header="Partener"
              sortable
              class="col-partener"
            />
            <Column
              v-if="isValutaType"
              field="valuta"
              header="Valuta"
              sortable
              class="col-valuta"
            />
            <Column
              field="incasari"
              header="Încasări"
              sortable
              class="col-numeric"
            >
              <template #body="slotProps">
                <span class="numeric-value" v-if="slotProps.data.incasari > 0">
                  {{ formatNumber(slotProps.data.incasari) }}
                </span>
                <span class="numeric-value zero" v-else>0,00</span>
              </template>
            </Column>
            <Column field="plati" header="Plăți" sortable class="col-numeric">
              <template #body="slotProps">
                <span class="numeric-value" v-if="slotProps.data.plati > 0">
                  {{ formatNumber(slotProps.data.plati) }}
                </span>
                <span class="numeric-value zero" v-else>0,00</span>
              </template>
            </Column>
            <Column
              field="sold"
              header="Sold Cumulat"
              sortable
              class="col-numeric col-sold"
            >
              <template #body="slotProps">
                <span
                  class="numeric-value"
                  :class="{ negative: slotProps.data.sold < 0 }"
                >
                  {{ formatNumber(slotProps.data.sold) }}
                </span>
              </template>
            </Column>
            <Column
              field="explicatia"
              header="Explicație"
              class="col-explicatie"
            >
              <template #body="slotProps">
                {{ truncateText(slotProps.data.explicatia, 100) }}
              </template>
            </Column>
          </DataTable>
        </template>
      </Card>
    </div>

    <!-- Mobile Bottom Navigation -->
    <MobileBottomNav v-if="isMobile" />
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from "vue";
import { useToast } from "primevue/usetoast";
import { useTreasuryStore } from "@reports/stores/treasury";
import { useCompanyStore, useAuthStore } from "@reports/stores/sharedStores";
import { useAccountingPeriodStore } from "@reports/stores/sharedStores";
import { format } from "date-fns";
import { exportToExcel, exportBankCashRegisterPDF } from "@reports/utils/exportUtils";

// Mobile Material Design components
import MobileTopBar from "@shared/components/mobile/MobileTopBar.vue";
import MobileBottomNav from "@shared/components/mobile/MobileBottomNav.vue";
import BottomSheet from "@shared/components/mobile/BottomSheet.vue";
import MobileDrawerMenu from "@shared/components/mobile/MobileDrawerMenu.vue";
import Menu from "primevue/menu";
import SplitButton from "primevue/splitbutton";
import { useRouter } from "vue-router";

const toast = useToast();
const router = useRouter();
const treasuryStore = useTreasuryStore();
const companyStore = useCompanyStore();
const periodStore = useAccountingPeriodStore();
const authStore = useAuthStore();

// State for company selection
const selectedCompanyId = ref(companyStore.selectedCompany?.id_firma || null);

// Mobile state
const isMobile = ref(window.innerWidth <= 768);
const showFilters = ref(false);
const showDrawer = ref(false);
const expandedMeta = ref(new Set());

const toggleMeta = (index) => {
  const s = new Set(expandedMeta.value);
  if (s.has(index)) s.delete(index);
  else s.add(index);
  expandedMeta.value = s;
};

// Handle logout from drawer menu
const handleLogout = async () => {
  await authStore.logout();
  router.push('/login');
};

// Handle server switched event from drawer menu
const handleServerSwitched = async (newServerId) => {
  // Clear selected company and period FIRST to prevent stale data from old server
  companyStore.clearSelectedCompany();
  periodStore.reset();

  // Reload data for the new server
  await companyStore.loadCompanies();
  if (companyStore.selectedCompany?.id_firma) {
    await periodStore.loadPeriods(companyStore.selectedCompany.id_firma);
  }
};

// Mobile TopBar actions
const mobileTopBarActions = computed(() => [
  {
    icon: "pi pi-filter",
    label: "Filtre",
    tooltip: "Filtre",
    active: hasActiveFilters.value
  },
  {
    icon: "pi pi-filter-slash",
    label: "Resetează",
    tooltip: "Resetează Filtrele"
  },
  {
    icon: "pi pi-download",
    label: "Export",
    tooltip: "Export"
  }
]);

// Export menu ref for mobile
const exportMenu = ref(null);
const exportMenuItems = ref([
  {
    label: "Export PDF",
    icon: "pi pi-file-pdf",
    command: () => exportPDF()
  },
  {
    label: "Export XLSX",
    icon: "pi pi-file-excel",
    command: () => exportExcel()
  }
]);

// Desktop export dropdown items (SplitButton)
const desktopExportItems = ref([
  {
    label: "Export PDF",
    icon: "pi pi-file-pdf",
    command: () => exportPDF()
  },
  {
    label: "Export XLSX",
    icon: "pi pi-file-excel",
    command: () => exportExcel()
  }
]);

// Handle top bar action clicks
const handleTopBarAction = (action, event) => {
  if (action.icon === "pi pi-filter") {
    showFilters.value = !showFilters.value;
  } else if (action.icon === "pi pi-filter-slash") {
    resetFilters();
  } else if (action.icon === "pi pi-download") {
    exportMenu.value.toggle(event);
  }
};

// Handle window resize
const handleResize = () => {
  isMobile.value = window.innerWidth <= 768;
  if (!isMobile.value) {
    showFilters.value = false;
  }
};

// US-519: Register type options - ONLY BANK types
const registerTypeOptions = [
  { label: "Bancă LEI", value: "BANCA_LEI" },
  { label: "Bancă Valută", value: "BANCA_VALUTA" },
];

const filters = ref({
  registerType: "BANCA_LEI", // Default: Registrul de Banca Lei
  partnerName: "",
  bankAccount: null,
});

// Bank account options for dropdown
const bankAccountOptions = ref([]);

const pagination = ref({
  page: 0,
  rows: 50,
});

const formatCurrency = (amount) => {
  if (!amount || amount === 0) return "0,00";
  return new Intl.NumberFormat("ro-RO", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount);
};

const formatNumber = (amount) => {
  if (amount === null || amount === undefined) return "";
  return new Intl.NumberFormat("ro-RO", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount);
};

const formatDate = (dateString) => {
  if (!dateString) return "";
  return format(new Date(dateString), "dd.MM.yyyy");
};

// Short date format for mobile cards (DD/MM/YY)
const formatDateShort = (dateString) => {
  if (!dateString) return "";
  const date = new Date(dateString);
  const year = String(date.getFullYear()).slice(-2);
  return `${String(date.getDate()).padStart(2, "0")}/${String(date.getMonth() + 1).padStart(2, "0")}/${year}`;
};

// Compact number format
const formatCompact = (amount) => {
  if (!amount) return "0";
  if (Math.abs(amount) >= 10000) {
    return new Intl.NumberFormat("ro-RO", {
      maximumFractionDigits: 0,
    }).format(amount);
  }
  return new Intl.NumberFormat("ro-RO", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount);
};

// Truncate text to maxLength characters
const truncateText = (text, maxLength = 100) => {
  if (!text) return "";
  if (text.length <= maxLength) return text;
  return text.substring(0, maxLength) + "...";
};

// Check if current filter is a VALUTA type
const isValutaType = computed(() => {
  return filters.value.registerType === "BANCA_VALUTA";
});

// Helper to remove diacritics from text
const removeDiacritics = (text) => {
  if (!text) return "";
  return text
    .replace(/[ăâ]/gi, (match) => (match === match.toLowerCase() ? "a" : "A"))
    .replace(/[îâ]/gi, (match) => (match === match.toLowerCase() ? "i" : "I"))
    .replace(/[ș]/gi, (match) => (match === match.toLowerCase() ? "s" : "S"))
    .replace(/[ț]/gi, (match) => (match === match.toLowerCase() ? "t" : "T"))
    .replace(/[Ă]/g, "A")
    .replace(/[Â]/g, "A")
    .replace(/[Î]/g, "I")
    .replace(/[Ș]/g, "S")
    .replace(/[Ț]/g, "T");
};

// Get PDF title based on register type
const getPdfTitle = (type) => {
  const titles = {
    BANCA_LEI: "Registrul de Banca LEI",
    BANCA_VALUTA: "Registrul de Banca Valuta",
  };
  return titles[type] || "Registrul de Banca";
};

// Load bank accounts for dropdown when register type changes
const loadBankAccounts = async () => {
  if (!companyStore.selectedCompany || !filters.value.registerType) {
    bankAccountOptions.value = [];
    return;
  }

  try {
    const apiService = (await import("../services/api")).default;
    const response = await apiService.get("/treasury/bank-cash-accounts", {
      params: {
        company: companyStore.selectedCompany.id_firma,
        register_type: filters.value.registerType,
      },
    });
    bankAccountOptions.value = response.data || [];
  } catch (error) {
    console.error("Failed to load bank accounts:", error);
    bankAccountOptions.value = [];
  }
};

// Watch for register type changes to reload accounts
watch(
  () => filters.value.registerType,
  async () => {
    filters.value.bankAccount = null;
    await loadBankAccounts();
  },
);

const getRowClass = (data) => {
  return "bank-row";
};

const onPage = async (event) => {
  pagination.value.page = event.page;
  pagination.value.rows = event.rows;
  await loadData();
};

const resetFilters = async () => {
  filters.value = {
    registerType: "BANCA_LEI",
    partnerName: "",
    bankAccount: null,
  };
  pagination.value.page = 0;
  await loadBankAccounts();
  await loadData();
};

// Computed
const hasData = computed(() => treasuryStore.registers.length > 0);

// Check if any filter is active (non-default value)
const hasActiveFilters = computed(() => {
  return (
    filters.value.registerType !== "BANCA_LEI" ||
    filters.value.partnerName !== "" ||
    filters.value.bankAccount !== null
  );
});

// Handle company change from dropdown
const handleCompanyChange = async () => {
  if (!selectedCompanyId.value) return;
  const company = companyStore.getCompanyById(selectedCompanyId.value);
  if (company) {
    companyStore.setSelectedCompany(company);
    await loadData();
  }
};

// Handle filter change
const handleFilterChange = async () => {
  pagination.value.page = 0;
  await loadData();
};

// Debounced search handler
const handleSearchChange = (() => {
  let timeout;
  return () => {
    clearTimeout(timeout);
    timeout = setTimeout(async () => {
      pagination.value.page = 0;
      await loadData();
    }, 500);
  };
})();

// Refresh data with toast notification
const refreshData = async () => {
  await loadData();
  toast.add({
    severity: "success",
    summary: "Actualizare reușită",
    detail: "Registrul a fost actualizat cu succes",
    life: 3000,
  });
};

// Fetch ALL data for export (not just current page)
const fetchAllData = async () => {
  if (!companyStore.selectedCompany) return [];
  if (!periodStore.selectedPeriod) return [];

  try {
    const { luna, an } = periodStore.selectedPeriod;

    const params = {
      company: companyStore.selectedCompany.id_firma,
      page: 1,
      page_size: 999999,
      luna: luna,
      an: an,
    };

    if (filters.value.registerType) {
      params.register_type = filters.value.registerType;
    }

    if (filters.value.partnerName) {
      params.partner_name = filters.value.partnerName;
    }
    if (filters.value.bankAccount) {
      params.bank_account = filters.value.bankAccount;
    }

    const apiService = (await import("../services/api")).default;
    const response = await apiService.get("/treasury/bank-cash-register", {
      params,
    });

    return response.data.registers || [];
  } catch (error) {
    console.error("Failed to fetch all data:", error);
    return [];
  }
};

// Export to Excel
const exportExcel = async () => {
  if (!hasData.value) {
    toast.add({
      severity: "warn",
      summary: "Nu există date",
      detail: "Nu există înregistrări de exportat",
      life: 3000,
    });
    return;
  }

  toast.add({
    severity: "info",
    summary: "Se pregătește exportul",
    detail: "Se încarcă toate datele...",
    life: 2000,
  });

  const allData = await fetchAllData();

  if (allData.length === 0) {
    toast.add({
      severity: "error",
      summary: "Eroare",
      detail: "Nu s-au putut prelua datele pentru export",
      life: 3000,
    });
    return;
  }

  const exportData = allData.map((row) => {
    const baseData = {
      Data: row.dataact ? formatDate(row.dataact) : "",
      "Nr.": row.nract || "",
    };

    baseData["Banca"] = row.nume_cont_bancar || "";
    baseData["Partener"] = row.nume || "";

    if (isValutaType.value) {
      baseData["Valuta"] = row.valuta || "";
    }

    baseData["Încasări"] = parseFloat(row.incasari) || 0;
    baseData["Plăți"] = parseFloat(row.plati) || 0;
    baseData["Sold Cumulat"] = parseFloat(row.sold) || 0;
    baseData["Explicație"] = truncateText(row.explicatia, 100);

    return baseData;
  });

  const result = exportToExcel(
    exportData,
    `registru_banca_${companyStore.selectedCompany.name.replace(/\s+/g, "_")}`,
    "Registru Bancă",
  );

  if (result.success) {
    toast.add({
      severity: "success",
      summary: "Export reușit",
      detail: `${allData.length} înregistrări exportate cu succes`,
      life: 3000,
    });
  } else {
    toast.add({
      severity: "error",
      summary: "Eroare la export",
      detail: "Nu s-a putut genera fișierul Excel",
      life: 3000,
    });
  }
};

// Export to PDF
const exportPDF = async () => {
  if (!hasData.value) {
    toast.add({
      severity: "warn",
      summary: "Nu există date",
      detail: "Nu există înregistrări de exportat",
      life: 3000,
    });
    return;
  }

  toast.add({
    severity: "info",
    summary: "Se pregătește exportul",
    detail: "Se încarcă toate datele...",
    life: 2000,
  });

  const allData = await fetchAllData();

  if (allData.length === 0) {
    toast.add({
      severity: "error",
      summary: "Eroare",
      detail: "Nu s-au putut prelua datele pentru export",
      life: 3000,
    });
    return;
  }

  const pdfTitle = getPdfTitle(filters.value.registerType);

  const result = exportBankCashRegisterPDF(
    allData,
    {
      companyName: removeDiacritics(companyStore.selectedCompany?.name || ""),
      title: pdfTitle,
      luna: treasuryStore.accountingPeriod.luna,
      an: treasuryStore.accountingPeriod.an,
      isBanca: true, // This is Bank view
    },
    `registru-banca-${companyStore.selectedCompany.name.replace(/\s+/g, "-")}`,
  );

  if (result.success) {
    toast.add({
      severity: "success",
      summary: "Export reușit",
      detail: `${allData.length} înregistrări exportate cu succes`,
      life: 3000,
    });
  } else {
    toast.add({
      severity: "error",
      summary: "Eroare la export",
      detail: "Nu s-a putut genera fișierul PDF",
      life: 3000,
    });
  }
};

const loadData = async () => {
  if (!companyStore.selectedCompany) return;
  if (!periodStore.selectedPeriod) return;

  treasuryStore.setPagination(pagination.value);

  const { luna, an } = periodStore.selectedPeriod;

  const filterParams = {
    partner_name: filters.value.partnerName || undefined,
    register_type: filters.value.registerType || undefined,
    bank_account: filters.value.bankAccount || undefined,
    luna: luna,
    an: an,
  };

  await treasuryStore.loadBankCashRegister(
    companyStore.selectedCompany.id_firma,
    filterParams,
  );
};

onMounted(async () => {
  window.addEventListener("resize", handleResize);

  if (!companyStore.hasCompanies) {
    await companyStore.loadCompanies();
  }

  if (companyStore.selectedCompany) {
    await loadBankAccounts();
  }
});

onUnmounted(() => {
  window.removeEventListener("resize", handleResize);
});

// Watch for company changes
watch(
  () => companyStore.selectedCompany,
  async (newCompany) => {
    if (newCompany && periodStore.selectedPeriod) {
      await loadBankAccounts();
      await loadData();
    }
  },
);

// Watch for period changes
watch(
  () => periodStore.selectedPeriod,
  async (newPeriod) => {
    if (newPeriod && companyStore.selectedCompany) {
      await loadData();
    }
  },
  { immediate: true },
);

// Reset expanded meta when data changes
watch(
  () => treasuryStore.registers,
  () => { expandedMeta.value = new Set(); }
);
</script>

<style scoped>
/* ================================================
   US-519: BankView Mobile Material Design Styles
   ================================================ */

/* Page Container */
.register-view {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--space-xl);
}

/* Mobile layout adjustments for top/bottom bars */
.mobile-layout .register-view {
  padding-top: calc(56px + var(--space-md));
  padding-bottom: calc(56px + var(--space-md));
  padding-left: 0;
  padding-right: 0;
}

/* Card Spacing */
/* US-610: Reduced margin-bottom from var(--space-xl) to var(--space-md) to eliminate excessive gap between filters and table */
.company-selection-card,
.filters-card,
.data-card {
  margin-bottom: var(--space-md);
}

/* Numeric Values */
.numeric-value {
  display: block;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono, "Roboto Mono", "Consolas", monospace);
}

.numeric-value.zero {
  color: var(--color-text-muted);
}

.numeric-value.negative {
  color: var(--color-error);
}

/* ================================================
   Mobile Totals Bar
   ================================================ */

.mobile-totals-bar {
  background: var(--surface-card);
  border-bottom: 1px solid var(--surface-border);
  padding: var(--space-sm) var(--space-xs);
  margin-bottom: var(--space-sm);
}

.mobile-totals-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-xs) var(--space-md);
}

.mobile-totals-bar .total-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mobile-totals-bar .total-label {
  font-size: var(--text-xs);
  color: var(--text-color-secondary);
  font-weight: var(--font-medium);
}

.mobile-totals-bar .total-value {
  font-size: var(--text-sm);
  font-weight: var(--font-bold);
  color: var(--text-color);
}

/* Colors removed - totals use neutral --text-color like TrialBalance */

/* ================================================
   Mobile Compact Rows with Collapsible Meta
   ================================================ */

.mobile-card-list {
  display: flex;
  flex-direction: column;
}

.mobile-compact-row {
  border-bottom: 1px solid var(--surface-border);
}

.compact-main {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: var(--space-sm) 0;
  gap: var(--space-sm);
  cursor: pointer;
}

.compact-main:active {
  background: var(--surface-hover);
}

.compact-date {
  font-size: var(--text-sm);
  color: var(--text-color-secondary);
  min-width: 54px;
  flex-shrink: 0;
}

.compact-partner {
  flex: 1;
  font-size: var(--text-sm);
  color: var(--text-color);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.compact-amount {
  font-weight: var(--font-semibold);
  font-variant-numeric: tabular-nums;
  font-size: var(--text-sm);
  color: var(--text-color);
  flex-shrink: 0;
}

.compact-meta {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  padding: var(--space-xs) var(--space-xs) var(--space-sm) calc(54px + var(--space-sm));
  background: var(--surface-hover);
  font-size: var(--text-sm);
  color: var(--text-color-secondary);
}

.meta-item {
  display: block;
}

.mobile-empty {
  text-align: center;
  padding: var(--space-xl);
  color: var(--text-color-secondary);
}

.mobile-empty i {
  font-size: var(--text-3xl);
  margin-bottom: var(--space-sm);
  display: block;
}

/* ================================================
   Bottom Sheet Styles
   ================================================ */

.bottom-sheet-title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--text-color);
  margin: 0 0 var(--space-md) 0;
}

.bottom-sheet-filters {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.bottom-sheet-actions {
  display: flex;
  gap: var(--space-sm);
  justify-content: flex-end;
  margin-top: var(--space-md);
  padding-top: var(--space-md);
  border-top: 1px solid var(--surface-border);
}

/* ================================================
   Dark Mode Support
   ================================================ */

/* Auto dark mode */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .numeric-value.negative {
    color: var(--red-400);
  }
}

[data-theme="dark"] .numeric-value.negative {
  color: var(--red-400);
}

/* ================================================
   Responsive Design
   ================================================ */

@media (max-width: 768px) {
  .register-view {
    padding: var(--space-xs);
  }
}
</style>
