<?php
require __DIR__ . '/includes/functions.php';

$currentPage = 'cart';
$pageTitle = 'Secure Checkout & Payment | Furnify';
$token = csrf_token();
$payments = payment_settings();
$manualGcash = array_get($payments, 'manual_gcash', array());
$onlineCheckout = array_get($payments, 'online_checkout', array());
$gcashEnabled = is_array($manualGcash) && array_get($manualGcash, 'enabled', false);
$onlineEnabled = is_array($onlineCheckout) && array_get($onlineCheckout, 'enabled', false);
$paymentMethods = payment_method_options();
$customerAccount = customer_current();

require __DIR__ . '/includes/header.php';
?>

<section class="page-section checkout-page">
    <div class="container">
        <div class="section-heading split-heading">
            <div>
                <p class="eyebrow">Secure checkout</p>
                <h1>Request Quote or Pay Downpayment</h1>
            </div>
            <div class="secure-note big"><i class="bi bi-shield-lock" aria-hidden="true"></i> CSRF protected form</div>
        </div>
        <div class="checkout-layout">
            <form class="checkout-form premium-panel" action="submit-order.php" method="post" enctype="multipart/form-data" id="checkout-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= e($token) ?>">
                <input type="hidden" name="order_payload" id="order-payload">


                <?php if ($customerAccount): ?>
                    <div class="alert alert-success d-flex align-items-center gap-2">
                        <i class="bi bi-person-check" aria-hidden="true"></i>
                        <div>Logged in as <strong><?= e(array_get($customerAccount, 'full_name', 'Customer')) ?></strong>. This order will be linked to your account.</div>
                    </div>
                    <?php if (!customer_is_email_verified($customerAccount)): ?>
                        <div class="alert alert-warning">Your email is not yet verified. You can still submit, but please verify your email from your customer dashboard.</div>
                    <?php endif; ?>
                <?php else: ?>
                    <div class="alert alert-info d-flex flex-wrap align-items-center justify-content-between gap-2">
                        <div><strong>Want order tracking?</strong> Create an account or log in before checkout.</div>
                        <div class="d-flex gap-2"><a class="btn btn-sm btn-outline-primary" href="login.php">Login</a><a class="btn btn-sm btn-primary" href="register.php">Create Account</a></div>
                    </div>
                <?php endif; ?>
                <div class="form-section-title">
                    <span>1</span>
                    <div>
                        <h2>Customer information</h2>
                        <p>We will use these details for quotation, payment verification, and delivery follow-up.</p>
                    </div>
                </div>

                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="name" class="form-label">Full name</label>
                        <input type="text" class="form-control" id="name" name="name" required maxlength="120" autocomplete="name" value="<?= e(customer_form_value('name', '')) ?>">
                    </div>
                    <div class="col-md-6">
                        <label for="phone" class="form-label">Mobile number</label>
                        <input type="tel" class="form-control" id="phone" name="phone" required maxlength="30" autocomplete="tel" placeholder="09XXXXXXXXX" value="<?= e(customer_form_value('phone', '')) ?>">
                    </div>
                    <div class="col-md-6">
                        <label for="email" class="form-label">Email</label>
                        <input type="email" class="form-control" id="email" name="email" required maxlength="160" autocomplete="email" value="<?= e(customer_form_value('email', '')) ?>">
                    </div>
                    <div class="col-md-6">
                        <label for="city" class="form-label">City / Municipality</label>
                        <input type="text" class="form-control" id="city" name="city" required maxlength="120" autocomplete="address-level2" value="<?= e(customer_form_value('city', '')) ?>">
                    </div>
                    <div class="col-12">
                        <label for="address" class="form-label">Delivery address</label>
                        <input type="text" class="form-control" id="address" name="address" required maxlength="220" autocomplete="street-address" placeholder="House no., street, barangay" value="<?= e(customer_form_value('address', '')) ?>">
                    </div>
                </div>

                <div class="form-section-title mt-4">
                    <span>2</span>
                    <div>
                        <h2>Payment and delivery</h2>
                        <p>For custom and Re-Up jobs, downpayment is recommended after admin confirms the final quote.</p>
                    </div>
                </div>

                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="payment_method" class="form-label">Preferred payment</label>
                        <select class="form-select" id="payment_method" name="payment_method" required data-payment-method>
                            <option value="">Select option</option>
                            <?php foreach ($paymentMethods as $value => $label): ?>
                                <option value="<?= e($value) ?>"><?= e($label) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label for="delivery_method" class="form-label">Delivery preference</label>
                        <select class="form-select" id="delivery_method" name="delivery_method" required>
                            <option value="">Select option</option>
                            <option value="Home Delivery">Home Delivery</option>
                            <option value="Store Pickup">Store Pickup</option>
                            <option value="Branch Delivery">Branch Delivery</option>
                        </select>
                    </div>
                </div>

                <div class="payment-helper-card mt-3" data-payment-info>
                    <div class="payment-info-default">
                        <i class="bi bi-credit-card-2-front" aria-hidden="true"></i>
                        <div>
                            <strong>Select a payment option</strong>
                            <p>Choose GCash, card payment link, bank transfer, COD, or downpayment inquiry.</p>
                        </div>
                    </div>
                    <div class="payment-info-method d-none" data-payment-copy="GCash Manual">
                        <i class="bi bi-wallet2" aria-hidden="true"></i>
                        <div>
                            <strong>Manual GCash payment</strong>
                            <?php if ($gcashEnabled): ?>
                                <p><?= e(array_get($manualGcash, 'instructions', 'Send payment via GCash and upload the receipt.')) ?></p>
                                <div class="payment-account">
                                    <span>GCash Name</span><strong><?= e(array_get($manualGcash, 'account_name', 'Furnify')) ?></strong>
                                    <span>GCash Number</span><strong><?= e(array_get($manualGcash, 'mobile_number', 'Set in data/settings.php')) ?></strong>
                                </div>
                                <?php if (trim((string) array_get($manualGcash, 'qr_image', '')) !== ''): ?>
                                    <img class="payment-qr" src="<?= e(array_get($manualGcash, 'qr_image', '')) ?>" alt="GCash QR code">
                                <?php endif; ?>
                            <?php else: ?>
                                <p>GCash is currently not configured. Admin will confirm payment instructions.</p>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="payment-info-method d-none" data-payment-copy="Credit/Debit Card Payment Link">
                        <i class="bi bi-shield-check" aria-hidden="true"></i>
                        <div>
                            <strong>Secure card payment link</strong>
                            <p><?= e(is_array($onlineCheckout) ? array_get($onlineCheckout, 'instructions', 'Admin will send a secure payment link after quote confirmation.') : 'Admin will send a secure payment link after quote confirmation.') ?></p>
                            <?php if ($onlineEnabled && trim((string) array_get($onlineCheckout, 'payment_link', '')) !== ''): ?>
                                <a class="btn btn-sm btn-outline-primary" href="<?= e(array_get($onlineCheckout, 'payment_link', '')) ?>" target="_blank" rel="noopener">Open payment link</a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="payment-info-method d-none" data-payment-copy="Bank Transfer">
                        <i class="bi bi-bank" aria-hidden="true"></i>
                        <div><strong>Bank transfer</strong><p>Admin will confirm bank details. Upload proof here if you already paid or paid a downpayment.</p></div>
                    </div>
                    <div class="payment-info-method d-none" data-payment-copy="Cash on Delivery">
                        <i class="bi bi-truck" aria-hidden="true"></i>
                        <div><strong>Cash on Delivery</strong><p>No payment proof needed. Admin will still confirm availability and delivery fee.</p></div>
                    </div>
                    <div class="payment-info-method d-none" data-payment-copy="Downpayment Inquiry">
                        <i class="bi bi-percent" aria-hidden="true"></i>
                        <div><strong>Downpayment inquiry</strong><p>Recommended for custom sofa and Re-Up. Admin confirms final quote, downpayment, and remaining balance.</p></div>
                    </div>
                </div>

                <div class="payment-proof-panel mt-3 d-none" data-payment-proof-panel>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label for="payment_reference" class="form-label">Payment reference number <span class="text-muted">optional</span></label>
                            <input type="text" class="form-control" id="payment_reference" name="payment_reference" maxlength="140" placeholder="GCash / bank reference no.">
                        </div>
                        <div class="col-md-6">
                            <label for="payment_proof" class="form-label">Upload proof of payment <span class="text-muted">optional</span></label>
                            <input type="file" class="form-control" id="payment_proof" name="payment_proof" accept="image/jpeg,image/png,image/webp,application/pdf">
                            <div class="form-text">Accepted: JPG, PNG, WebP, PDF. Max 6MB.</div>
                        </div>
                    </div>
                </div>

                <div class="row g-3 mt-1">
                    <div class="col-12">
                        <label for="message" class="form-label">Notes</label>
                        <textarea class="form-control" id="message" name="message" rows="5" maxlength="1000" placeholder="Preferred color, delivery notes, room measurements, payment note, or target delivery date"></textarea>
                    </div>
                </div>

                <button type="submit" class="btn btn-primary btn-lg mt-4 w-100">
                    <i class="bi bi-send-check" aria-hidden="true"></i>
                    Submit Secure Request
                </button>
                <p class="form-footnote">Prices are recalculated on the server before saving. Card payments need a secure gateway link; do not enter card details on this form.</p>
            </form>
            <aside class="checkout-summary premium-panel">
                <h2>Selected Sofas</h2>
                <div id="checkout-items"></div>
                <div class="checkout-total">
                    <span>Subtotal</span>
                    <strong id="checkout-total">&#8369;0.00</strong>
                </div>
                <div class="checkout-assurance">
                    <div><i class="bi bi-check2-circle" aria-hidden="true"></i> Server recalculated totals</div>
                    <div><i class="bi bi-check2-circle" aria-hidden="true"></i> GCash proof upload ready</div>
                    <div><i class="bi bi-check2-circle" aria-hidden="true"></i> Card payment link ready after admin confirmation</div>
                    <div><i class="bi bi-check2-circle" aria-hidden="true"></i> Installable mobile app experience</div>
                </div>
            </aside>
        </div>
    </div>
</section>

<?php require __DIR__ . '/includes/footer.php'; ?>
