@extends('layouts.user')

@section('title', 'Formulir Pemesanan Foto - Florist dan Self Photo Studio Nganjuk')

@section('content')
    <section class="section-formulirfoto">
        <div class="container">

            <div class="row mb-5 text-center">
                <div class="col-12">
                    <h2 class="formulirfoto-page-title">Formulir Pemesanan</h2>
                </div>
            </div>
            <form action="{{ route('transaksi.foto.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
                {{-- DATA DARI HALAMAN SEBELUMNYA (WAJIB ADA) --}}
                <input type="hidden" name="id_paket" value="{{ $foto->id_paket }}">
                <input type="hidden" name="tgl_booking" value="{{ $request->tgl_booking }}">
                <input type="hidden" name="jam_mulai" value="{{ $request->jam_mulai ?? $request->jam_awal }}">

                {{-- Loop untuk mengirim ulang data Add-ons yang dipilih --}}
                @if (!empty($addonsDetails))
                    @foreach ($addonsDetails as $add)
                        <input type="hidden" name="addons[{{ $add['id'] }}]" value="{{ $add['qty'] }}">
                    @endforeach
                @endif
                <div class="row g-4">

                    <div class="col-lg-6 mb-5 mb-lg-0">
                        <h4 class="formulirfoto-section-title mb-3">Informasi Pemesan</h4>
                        <div class="formulirfoto-card d-flex flex-column gap-2">

                            <div>
                                <label class="form-label small">Nama Lengkap</label>
                                <input type="text" name="nama"
                                    class="form-control formulirbuket-input @error('nama') is-invalid @enderror"
                                    placeholder="Masukkan Nama Lengkap" value="{{ old('nama') }}">

                                @error('nama')
                                    <div class="invalid-feedback d-block">{{ $message }}</div>
                                @enderror
                            </div>

                            <div>
                                <label class="form-label  small">Nomor WhatsApp</label>
                                <input type="number" name="no_wa"
                                    class="form-control formulirbuket-input @error('no_wa') is-invalid @enderror"
                                    placeholder="Masukkan Nomor WhatsApp" value="{{ old('no_wa') }}">
                                @error('no_wa')
                                    <div class="invalid-feedback d-block">{{ $message }}</div>
                                @enderror
                            </div>

                        </div>
                    </div>

                    <div class="col-lg-6">
                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <span class="timer-alert-text">Selesaikan pembayaran dalam</span>
                            @php
                                // Hitung format jam:menit:detik langsung di HTML agar tidak flash 58:58
                                $jam = floor($sisaWaktu / 3600);
                                $menit = floor(($sisaWaktu / 60) % 60);
                                $detik = $sisaWaktu % 60;
                                $tampilanAwal = sprintf('%02d:%02d:%02d', $jam, $menit, $detik);
                            @endphp

                            <span class="timer-badge" id="countdown-timer">{{ $tampilanAwal }}</span>
                            {{-- <span class="timer-badge" id="countdown-timer"></span> --}}
                        </div>
                        <h4 class="formulirfoto-section-title mb-3">Informasi Pembayaran</h4>

                        <div class="formulirfoto-payment-box">
                            <div class="summary-item">
                                <span class="summary-label">Jadwal Booking:</span>
                                <div class="summary-row">
                                    {{-- Menampilkan Hari, Tanggal Bulan Tahun dalam Bahasa Indonesia --}}
                                    <span class="summary-subtext">
                                        {{ \Carbon\Carbon::parse($request->tgl_booking)->translatedFormat('l, d F Y') }}
                                    </span>

                                    {{-- Menampilkan Jam di bagian kanan --}}
                                    <span class="summary-value">
                                        {{ $request->jam_mulai }} - {{ $jamSelesaiBaru }} WIB </span>
                                </div>
                            </div>
                            <div class="summary-item">
                                <span class="summary-label">Paket Foto:</span>
                                <div class="summary-row">
                                    <span class="summary-subtext">{{ $foto->nama }}</span>
                                    <span class="summary-value">Rp {{ number_format($foto->harga, 0, ',', '.') }}</span>
                                </div>
                            </div>
                            <div class="summary-item">
                                <span class="summary-label">Additional:</span>
                                @forelse($addonsDetails as $add)
                                    <div class="summary-row">
                                        <div class="row-left">
                                            <span class="summary-subtext">{{ $add['nama'] }}</span>
                                            <span class="summary-qty">x{{ $add['qty'] }}</span>
                                        </div>
                                        <span class="summary-value">Rp
                                            {{ number_format($add['subtotal'], 0, ',', '.') }}</span>
                                    </div>
                                @empty
                                    <div class="summary-row">
                                        <span class="summary-subtext text-muted">- Tidak ada tambahan -</span>
                                    </div>
                                @endforelse
                            </div>
                            <div class="d-flex justify-content-between align-items-center mb-4">
                                <span class="text-muted">Total Pembayaran</span>
                                <h5 class=" mb-0">Rp {{ number_format($grandTotal, 0, ',', '.') }}</h5>
                            </div>
                            <p class="small mb-2">Transfer ke Rekening Berikut:</p>
                            <div class="row g-2 mb-4">
                                <div class="col-6">
                                    <div class="formulirbuket-bank-card">
                                        <div class="d-flex align-items-center gap-2 mb-2">
                                            <i class="bi bi-bank text-secondary"></i> <span class="small ">BCA</span>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-end">
                                            <div>
                                                <p class="mb-0  small bank-num">6860305231</p>
                                                <p class="mb-0 x-small bank-name">a.n Deborah Febryana</p>
                                            </div>
                                            <button type="button"
                                                class="btn btn-sm btn-outline-secondary py-1 px-2 btn-copy"
                                                data-clipboard-text="6860305231" style="font-size: 12px">salin</button>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="formulirbuket-bank-card">
                                        <div class="d-flex align-items-center gap-2 mb-2">
                                            <i class="bi bi-bank text-secondary"></i> <span class="small ">BRI</span>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-end">
                                            <div>
                                                <p class="mb-0  small bank-num">005601002337563</p>
                                                <p class="mb-0 x-small bank-name">a.n Deborah Febryana</p>
                                            </div>
                                            <button type="button"
                                                class="btn btn-sm btn-outline-secondary py-1 px-2 btn-copy"
                                                data-clipboard-text="005601002337563" style="font-size: 12px">salin</button>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="formulirbuket-bank-card">
                                        <div class="d-flex align-items-center gap-2 mb-2">
                                            <i class="bi bi-bank text-secondary"></i> <span class="small ">ShopeePay</span>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-end">
                                            <div>
                                                <p class="mb-0  small bank-num">082133958469</p>
                                                <p class="mb-0 x-small bank-name">a.n Deborah Febryana</p>
                                            </div>
                                            <button type="button"
                                                class="btn btn-sm btn-outline-secondary py-1 px-2 btn-copy"
                                                data-clipboard-text="082133958469" style="font-size: 12px">salin</button>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="formulirbuket-bank-card">
                                        <div class="d-flex align-items-center gap-2 mb-2">
                                            <i class="bi bi-bank text-secondary"></i> <span class="small ">Dana</span>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-end">
                                            <div>
                                                <p class="mb-0  small bank-num">082133958469</p>
                                                <p class="mb-0 x-small bank-name">a.n Deborah Febryana</p>
                                            </div>
                                            <button type="button"
                                                class="btn btn-sm btn-outline-secondary py-1 px-2 btn-copy"
                                                data-clipboard-text="082133958469" style="font-size: 12px">salin</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <p class="small  mb-2">Upload Bukti Pembayaran</p>
                            <div class="formulirbuket-upload-area mb-2 text-center position-relative">
                                <input type="file" name="bukti_bayar" accept="image/*"
                                    class="position-absolute w-100 h-100 opacity-0 start-0 top-0 cursor-pointer @error('bukti_bayar') is-invalid @enderror"
                                    id="fileUpload" value="{{ old('bukti_bayar') }}">
                                <div class="py-4" id="uploadPlaceholder">
                                    <i class="bi bi-file-earmark-arrow-up fs-3 text-secondary"></i>
                                    <p class="mb-0 small text-muted">Upload Bukti Pembayaran</p>
                                    <p class="mb-0 x-small text-muted">Max. 2 MB</p>
                                </div>
                            </div>
                            @error('bukti_bayar')
                                <div class="invalid-feedback d-block">{{ $message }}</div>
                            @enderror
                            <p class="x-small text-muted mb-4">
                                *Note: Pastikan pembayaran sudah dilakukan sebelum mengunggah bukti, ya!
                            </p>

                            <div class="d-flex gap-3">
                                <a href="{{ route('booking.cancel') }}"
                                    class="btn formulirfoto-btn-cancel flex-fill">Batalkan</a>
                                {{-- <a href="{{ route('detail.foto', $foto->id_paket) }}"
                                    class="btn formulirfoto-btn-cancel flex-fill">Batalkan</a> --}}
                                <button type="submit" class="btn formulirfoto-btn-submit flex-fill">Kirim
                                    Pesanan</button>
                            </div>

                        </div>
                    </div>

                </div>
            </form>
        </div>
    </section>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 1. VARIABLE PENGUNCI
            let isPaymentTimeUp = false; //
            const submitBtn = document.querySelector('.formulirfoto-btn-submit');

            // 2. FITUR COPY NO REK (Tetap sama)
            document.querySelectorAll('.btn-copy').forEach(btn => {
                btn.addEventListener('click', function() {
                    navigator.clipboard.writeText(this.getAttribute('data-clipboard-text'));
                    let originalText = this.innerText;
                    this.innerText = 'Disalin!';
                    setTimeout(() => {
                        this.innerText = originalText;
                    }, 1500);
                });
            });

            // 3. FITUR PREVIEW UPLOAD (Tetap sama)
            const fileInput = document.getElementById('fileUpload');
            if (fileInput) {
                fileInput.addEventListener('change', function() {
                    const file = this.files[0];
                    const placeholder = document.getElementById('uploadPlaceholder');
                    if (file) {
                        placeholder.innerHTML = `
                        <i class="bi bi-check-circle-fill fs-3 text-success"></i>
                        <p class="mb-0 small text-success fw-bold">${file.name}</p>
                        <p class="mb-0 x-small text-muted">Klik lagi untuk ganti file</p>
                    `;
                        placeholder.parentElement.style.borderColor = "#198754";
                        placeholder.parentElement.style.backgroundColor = "#e8f5e9";
                    }
                });
            }

            // 4. FITUR TIMER MUNDUR
            let sisaDetik = {{ $sisaWaktu }};
            let display = document.querySelector('#countdown-timer');

            function startTimer(duration) {
                let timer = duration,
                    hours, minutes, seconds;

                let interval = setInterval(function() {
                    hours = parseInt(timer / 3600, 10);
                    minutes = parseInt((timer % 3600) / 60, 10);
                    seconds = parseInt(timer % 60, 10);

                    hours = hours < 10 ? "0" + hours : hours;
                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;

                    if (display) display.textContent = hours + ":" + minutes + ":" + seconds;

                    if (--timer < 0) {
                        clearInterval(interval);
                        isPaymentTimeUp = true; // Kunci status

                        if (submitBtn) {
                            submitBtn.disabled = true; // Matikan tombol secara visual
                            submitBtn.classList.add('disabled');
                        }

                        // Gunakan SweetAlert agar pasti muncul
                        Swal.fire({
                            title: 'Waktu Pembayaran Habis!',
                            text: 'Maaf, batas waktu pembayaran Anda telah berakhir, jadwal booking akan dibatalkan.',
                            confirmButtonColor: '#3B8181',
                            icon: 'error',
                            confirmButtonText: 'Kembali ke Katalog',
                            allowOutsideClick: false
                        }).then(() => {
                            window.location.href = "{{ route('booking.cancel') }}";
                        });
                    }
                }, 1000);
            }

            // Jalankan timer
            if (sisaDetik > 0) {
                startTimer(sisaDetik);
            } else {
                window.location.href = "{{ route('booking.cancel') }}";
            }

            // 5. VALIDASI TAMBAHAN SAAT SUBMIT (Mencegah submit jika waktu habis)
            const form = document.querySelector('form');
            if (form) {
                form.addEventListener('submit', function(e) {
                    if (isPaymentTimeUp) {
                        e.preventDefault(); // Stop kirim data
                        Swal.fire('Gagal!', 'Waktu pembayaran sudah habis.', 'error');
                    }
                });
            }
        });
    </script>
@endsection
