<flux:main>
    <div class="flex justify-between items-center">
        <div>
            <flux:heading size="xl">{{ $pageTitle }}</flux:heading>
        </div>
        @can('manage adjustment')
            <div>
                <flux:modal.trigger name="form-modal">
                    <flux:button variant="primary" class="text-sm"
                        wire:click="$dispatch('modal:open', {method: 'create', 'modalTitle': 'Tambah Penyesuaian'})">
                        Tambah
                    </flux:button>
                </flux:modal.trigger>
            </div>
        @endcan
    </div>

    <div class="mt-6">
        <flux:callout icon="megaphone" color="blue" class="mb-6">
            <flux:callout.heading>Informasi</flux:callout.heading>
            <flux:callout.text>
                Data penggajian akan dihasilkan secara otomatis pada setiap awal bulan pad pukul 00.00 dan berakhir pada
                akhir bulan pada pukul 23.59.
            </flux:callout.text>
        </flux:callout>

        <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4 mb-4 text-sm items-start">
            @foreach ($payrolls as $payroll)
                <flux:card class="h-auto">
                    <div class="flex justify-between items-center mb-3">
                        <div>
                            <flux:heading>{{ $payroll->user->employee->full_name }}</flux:heading>
                            <flux:text>{{ formatDate($payroll->period_month, 'F Y') }}</flux:text>
                        </div>
                        <div class="text-right">
                            <flux:badge color="{{ $payroll->is_paid->color() }}">
                                {{ $payroll->is_paid->label() }}
                            </flux:badge>
                        </div>
                    </div>

                    <div class="grid grid-cols-1 gap-4 mb-2 text-sm">
                        <div>
                            <flux:text>Gaji Pokok</flux:text>
                            <div class="font-medium">{{ currency($payroll->base_salary, 'Rp') }}</div>
                        </div>
                        <div>
                            <flux:text>Bonus</flux:text>
                            <div class="font-medium text-green-600">{{ currency($payroll->bonus, 'Rp') }}</div>
                        </div>
                        <div>
                            <flux:text>Potongan</flux:text>
                            <div class="font-medium text-red-600">{{ currency($payroll->deduction, 'Rp') }}</div>
                        </div>
                    </div>

                    <flux:separator class="my-2 !bg-black dark:!bg-white" />

                    <flux:text class="mb-1">Rincian</flux:text>
                    @foreach ($payroll->adjustments->where('payroll_id', $payroll->id) as $item)
                        <div class="flex justify-between items-start text-[13px] leading-tight mb-2">
                            <div>
                                <div
                                    class="font-bold {{ $item->type->value == \App\Enums\SalaryAdjustmentType::DEDUCTION->value ? 'text-red-500' : 'text-green-500' }}">
                                    {{ currency($item->amount, 'Rp') }}
                                </div>
                                <div class="text-gray-400 text-[12px]">{{ $item->description }}</div>
                            </div>
                            <flux:modal.trigger name="delete">
                                <flux:button variant="danger" icon="trash" size="sm"
                                    wire:click="$dispatch('fn:confirmDelete', {id: '{{ $item->id }}'})">
                                </flux:button>
                            </flux:modal.trigger>
                        </div>
                    @endforeach

                    <flux:separator class="my-2 !bg-black dark:!bg-white" />

                    <div class="flex justify-between items-center mt-2">
                        <flux:text>Total Gaji</flux:text>
                        <FLux:heading>{{ currency($payroll->total_salary, 'Rp') }}</FLux:heading>
                    </div>
                </flux:card>
            @endforeach
        </div>

        <livewire:datatable.studio.finance.payrolls-table />
    </div>

    @include('components.confirmation.delete')

    <flux:modal name="form-modal" class="w-[95%] max-w-sm md:max-w-xl mx-auto" @close="closeModal('form-modal')">
        <div class="p-4 space-y-6">
            <flux:heading size="lg">{{ $modalTitle }}</flux:heading>

            <flux:field>
                <flux:label>Pegawai <span class="text-red-500 ms-1">*</span></flux:label>
                <flux:select variant="listbox" multiple searchable placeholder="Pilih Pegawai"
                    wire:model.live.debounce.500ms="form.user_ids">
                    <flux:select.option wire:click="selectAllUsers" wire:ignore>Pilih Semua
                    </flux:select.option>
                    <flux:select.option wire:click="deselectAllUsers" wire:ignore>Hapus Semua
                    </flux:select.option>
                    @foreach ($users as $key => $value)
                        <flux:select.option value="{{ $key }}">{{ $value }}
                        </flux:select.option>
                    @endforeach
                </flux:select>
                <flux:error name="form.user_ids" />
            </flux:field>

            <flux:field>
                <flux:label>Tipe <span class="text-red-500 ms-1">*</span></flux:label>
                <flux:radio.group wire:model.live="form.type" variant="buttons" class="w-full *:flex-1">
                    @foreach (\App\Enums\SalaryAdjustmentType::cases() as $type)
                        <flux:radio value="{{ $type->value }}">
                            {{ $type->label() }}
                        </flux:radio>
                    @endforeach
                </flux:radio.group>
                <flux:error name="form.type" />
            </flux:field>

            <flux:field>
                <flux:label>Jumlah <span class="text-red-500 ms-1">*</span>
                </flux:label>
                <flux:input.group>
                    <flux:input.group.prefix>Rp</flux:input.group.prefix>
                    <flux:input placeholder="1.500.000" x-mask:dynamic="$money($input, ',')"
                        wire:model.live.debounce.500ms="form.amount" autocomplete="off" autofocus />
                </flux:input.group>
                <flux:error name="form.amount" />
            </flux:field>

            <flux:field>
                <flux:label>Keterangan <span class="text-red-500 ms-1">*</span></flux:label>
                <flux:input placeholder="Gajian bulan xxx + bonus ya, semangat kerjanya"
                    wire:model.live.debounce.500ms="form.description" autocomplete="off" />
                <flux:error name="form.description" />
            </flux:field>

            <div class="flex">
                <flux:spacer />
                <flux:button variant="primary" class="sm:w-auto cursor-pointer" wire:click="{{ $method }}">
                    Simpan
                </flux:button>
            </div>
        </div>
    </flux:modal>
</flux:main>
