<x-filament-panels::page>
    <x-logistics.error-banner :message="$errorMessage" />

    {{-- Toggle Lecture / Ecriture --}}
    <div class="flex gap-2">
        <x-filament::button wire:click="$set('mode', 'read')" :color="$mode === 'read' ? 'primary' : 'gray'" icon="heroicon-o-arrow-down-tray">
            Lecture
        </x-filament::button>
        <x-filament::button wire:click="$set('mode', 'write')" :color="$mode === 'write' ? 'primary' : 'gray'" icon="heroicon-o-arrow-up-tray">
            Ecriture
        </x-filament::button>
    </div>

    @if ($mode === 'read')
        {{-- getserialnumber --}}
        <x-logistics.card>
            <x-logistics.section-header title="getserialnumber" description="Numéro de série du dossier comptable" />
            <div class="p-6">
                <div class="flex items-center gap-3">
                    <x-filament::button wire:click="getSerialNumber" icon="heroicon-o-finger-print">
                        Obtenir le numéro de série
                    </x-filament::button>
                    <div wire:loading wire:target="getSerialNumber" class="flex items-center gap-2">
                        <x-filament::loading-indicator class="h-4 w-4 text-primary-500" />
                        <span class="text-sm text-gray-500">Chargement...</span>
                    </div>
                </div>

                <x-logistics.param-table :params="[]" />

                <div wire:loading.remove wire:target="getSerialNumber" class="mt-4">
                    <x-logistics.json-block :data="$serialData" :searched="$hasSerial" />
                </div>
            </div>
        </x-logistics.card>

        {{-- codes_list --}}
        <x-logistics.card>
            <x-logistics.section-header title="codes_list" description="Données associées à un code interne (table incodes). Correspondance exacte, sensible à la casse (MAJUSCULES)." />
            <div class="p-6">
                <div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <x-logistics.form-field
                        wire:model="code"
                        label="Nom du groupe de codes (code)"
                        id="code"
                        placeholder="Ex: PAYMODE, PAYDELAY, VAT, STOCK"
                    />
                    <x-logistics.form-field
                        wire:model="codesSelect"
                        label="Colonnes (select)"
                        id="codesSelect"
                        placeholder="Sans effet -- toutes les colonnes sont retournées"
                    />
                </div>

                <div class="mt-4 flex items-center gap-3">
                    <x-filament::button wire:click="searchCodes" icon="heroicon-o-magnifying-glass">
                        Rechercher
                    </x-filament::button>
                    <div wire:loading wire:target="searchCodes" class="flex items-center gap-2">
                        <x-filament::loading-indicator class="h-4 w-4 text-primary-500" />
                        <span class="text-sm text-gray-500">Chargement...</span>
                    </div>
                </div>

                <div wire:loading wire:target="searchCodes" class="mt-4 flex items-center gap-2">
                    <x-filament::loading-indicator class="h-4 w-4 text-primary-500" />
                    <span class="text-sm text-gray-500">Chargement...</span>
                </div>

                <x-logistics.param-table :params="$this->paramTableCodesList()" />

                <div wire:loading.remove wire:target="searchCodes" class="mt-4">
                    <x-logistics.json-block :data="$codesData" :searched="$hasCodes" />
                </div>
            </div>
        </x-logistics.card>

    @else
        {{-- custom_geninv_updatestock --}}
        <x-logistics.card>
            <x-logistics.section-header title="custom_geninv_updatestock" description="Mise à jour du stock (réapprovisionnement ou inventaire)" />
            <div class="p-6">
                <div class="mb-4 rounded-lg bg-amber-50 p-3 text-sm text-amber-700 dark:bg-amber-400/10 dark:text-amber-400">
                    MODE=0 (Inventaire) necessite un journal KI configure pour le depot. MODE=1 (Restock) fonctionne et necessite un THIRDID (fournisseur).
                </div>
                <div class="grid grid-cols-1 gap-4 sm:grid-cols-3">
                    <x-logistics.form-field
                        wire:model="stkArtId"
                        label="Article (ARTID)"
                        id="stkArtId"
                        placeholder="Ex: 003R92572"
                    />
                    <x-logistics.form-field
                        wire:model="stkId"
                        label="Depot / Entrepot (STKID)"
                        id="stkId"
                        placeholder="Ex: SG, A"
                    />
                    <x-logistics.form-field
                        wire:model="stkQty"
                        label="Quantite (QTY)"
                        id="stkQty"
                        placeholder="Ex: 10 (negatif possible)"
                    />
                    <x-logistics.form-field
                        wire:model="stkMode"
                        label="Mode (MODE)"
                        id="stkMode"
                        placeholder="0=Inventaire, 1=Restock"
                    />
                    <x-logistics.form-field
                        wire:model="stkThirdId"
                        label="Tiers / Fournisseur (THIRDID)"
                        id="stkThirdId"
                        placeholder="Obligatoire si MODE=1"
                    />
                    <x-logistics.form-field
                        wire:model="stkToCheck"
                        label="Prix de controle (TOCHECK)"
                        id="stkToCheck"
                        placeholder="Ex: 12.50"
                    />
                    <x-logistics.form-field
                        wire:model="stkToCheckDetail"
                        label="Detail de controle (TOCHECKDETAIL)"
                        id="stkToCheckDetail"
                        placeholder="Remarques"
                    />
                </div>

                <div class="mt-4 flex items-center gap-3">
                    <x-filament::button wire:click="updateStock" icon="heroicon-o-arrow-path" color="warning">
                        Mettre a jour le stock
                    </x-filament::button>
                    <div wire:loading wire:target="updateStock" class="flex items-center gap-2">
                        <x-filament::loading-indicator class="h-4 w-4 text-primary-500" />
                        <span class="text-sm text-gray-500">Envoi en cours...</span>
                    </div>
                </div>

                <x-logistics.param-table :params="$this->paramTableUpdateStock()" />

                <div wire:loading.remove wire:target="updateStock" class="mt-4">
                    <x-logistics.json-block :data="$updateStockResult" :searched="$hasUpdatedStock" />
                </div>
            </div>
        </x-logistics.card>
    @endif
</x-filament-panels::page>
