<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';

$productId = (string) ($_GET['id'] ?? $_POST['product_id'] ?? '');
$orderId = (string) ($_GET['order'] ?? $_POST['order_id'] ?? '');
$step = (string) ($_GET['step'] ?? 'form');
$error = '';
$order = $orderId !== '' ? store_find('orders', $orderId) : null;

// Sipariş adımları: pay / done
if (in_array($step, ['pay', 'done'], true)) {
    if (!$order) {
        flash('error', 'Sipariş bulunamadı.');
        redirect('index.php');
    }

    // Eski siparişlerde pay_deadline yoksa oluştur
    if (empty($order['pay_deadline'])) {
        $order['pay_deadline'] = order_pay_deadline_iso((string) ($order['created_at'] ?? ''));
        store_upsert('orders', $order);
    }

    // Dekontsuz tamamlanmış sayfa yok — ödeme adımına dön
    if ($step === 'done' && order_is_pending_payment($order)) {
        redirect(url('odeme.php?order=' . rawurlencode((string) $order['id']) . '&step=pay'));
    }

    // Dekont varsa pay yerine done
    if ($step === 'pay' && !order_is_pending_payment($order)) {
        redirect(url('odeme.php?order=' . rawurlencode((string) $order['id']) . '&step=done'));
    }

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $action = (string) ($_POST['action'] ?? '');

        if ($action === 'upload_receipt') {
            $file = $_FILES['receipt'] ?? null;
            if (!$file || ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) {
                $error = 'Siparişi tamamlamak için dekont (fotoğraf veya PDF) yüklemelisin.';
            } elseif (order_pay_expired($order)) {
                $error = 'Süre doldu. Bu sipariş için dekont kabul edilmiyor.';
            } else {
                $saved = save_receipt_upload($file);
                if ($saved === false) {
                    $error = 'Dekont yüklenemedi. JPG, PNG, WEBP veya PDF dene.';
                } else {
                    $abs = RECEIPT_PATH . '/' . ltrim($saved, '/');
                    // OCR: tarayıcı Tesseract.js metni (öncelik) veya sunucu Tesseract — Gemini yok
                    $clientOcr = trim((string) ($_POST['ocr_text'] ?? ''));
                    if (mb_strlen($clientOcr) > 8000) {
                        $clientOcr = mb_substr($clientOcr, 0, 8000);
                    }
                    $check = receipt_validate_image($abs, $order, $clientOcr);
                    $verdict = (string) ($check['verdict'] ?? 'defer');

                    if ($verdict === 'reject') {
                        @unlink($abs);
                        $error = (string) ($check['message'] ?? 'Bu görsel banka dekontu gibi görünmüyor. Havale/EFT dekontu yükle.');
                    } else {
                        $order['receipt'] = $saved;
                        $order['receipt_at'] = date('c');
                        $order['payment_status'] = 'dekont_bekliyor';
                        $order['receipt_verified'] = !empty($check['verified']);
                        $order['receipt_check'] = $verdict; // accept | defer
                        $order['receipt_method'] = (string) ($check['method'] ?? 'tesseract');
                        store_upsert('orders', $order);
                        checkout_clear_pending();
                        redirect(url('odeme.php?order=' . rawurlencode((string) $order['id']) . '&step=done'));
                    }
                }
            }
        }
    }

    $pageTitle = $step === 'done' ? 'Sipariş Tamamlandı' : 'Ödeme';
    $pageClass = 'page-address';
    $productTitle = (string) ($order['items'][0]['title'] ?? 'Ürün');
    $productImg = order_product_image_url($order);
    $publicNo = order_public_no($order);
    $showOrderNoInIban = true;
    $orderProduct = store_find('products', (string) ($order['items'][0]['product_id'] ?? ''));
    if ($orderProduct && isset($orderProduct['order_no_in_iban']) && empty($orderProduct['order_no_in_iban'])) {
        $showOrderNoInIban = false;
    }
    $payDeadlineTs = order_pay_deadline_ts($order);
    $payExpired = order_pay_expired($order);
    $payTotalSec = max(1, order_pay_deadline_minutes() * 60);

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

    <?php if ($step === 'pay'): ?>
    <section class="address-page order-flow pay-wait">
        <?= checkout_steps_html('pay') ?>
        <h1 class="address-title"><?= e(theme_t('pay_title')) ?></h1>
        <?= checkout_pay_substeps_html('transfer') ?>

        <div class="pay-timer"
             data-pay-timer
             data-pay-deadline="<?= (int) $payDeadlineTs ?>"
             data-pay-total="<?= (int) $payTotalSec ?>"
             data-pay-label-active="<?= e(theme_t('pay_timer_active')) ?>"
             data-pay-label-expired="<?= e(theme_t('pay_timer_expired')) ?>"
             aria-live="polite">
            <div class="pay-timer-ring" aria-hidden="true">
                <svg viewBox="0 0 120 120" width="120" height="120">
                    <circle class="pay-timer-track" cx="60" cy="60" r="52"></circle>
                    <circle class="pay-timer-progress" data-pay-progress cx="60" cy="60" r="52"></circle>
                </svg>
                <div class="pay-timer-center">
                    <strong data-pay-clock><?= $payExpired ? '0:00' : '' ?></strong>
                </div>
            </div>
            <div class="pay-timer-copy">
                <p class="pay-timer-label" data-pay-label>
                    <?= e($payExpired ? theme_t('pay_timer_expired') : theme_t('pay_timer_active')) ?>
                </p>
                <p class="hint"><?= e(theme_t('pay_timer_hint')) ?></p>
            </div>
        </div>

        <div class="order-product-card">
            <?php if ($productImg !== ''): ?>
                <img src="<?= e($productImg) ?>" alt="<?= e($productTitle) ?>" width="72" height="72">
            <?php else: ?>
                <div class="order-product-ph" aria-hidden="true">📦</div>
            <?php endif; ?>
            <div>
                <strong><?= e($productTitle) ?></strong>
                <span class="order-product-price"><?= e(money($order['total'] ?? 0)) ?></span>
                <span class="hint"><?= e(theme_t('pay_order_no_label')) ?> <?= e($publicNo) ?></span>
            </div>
        </div>

        <?php if ($error): ?>
            <div class="flash flash-error"><?= e($error) ?></div>
        <?php endif; ?>

        <div class="iban-box pay-card" data-allow-copy>
            <?php
            $payLogoIban = iban_site();
            if (!$payLogoIban && !empty($order['iban_id'])) {
                $payLogoIban = iban_find((string) $order['iban_id']);
            }
            if (!$payLogoIban) {
                $payLogoIban = [
                    'bank' => (string) ($order['bank'] ?? ''),
                    'bank_key' => (string) ($order['bank_key'] ?? ''),
                    'logo' => (string) ($order['logo'] ?? ''),
                ];
            }
            ?>
            <div class="pay-card-head">
                <div>
                    <p class="pay-card-kicker"><?= e(theme_t('pay_method_kicker')) ?></p>
                    <h2 class="pay-card-title"><?= e(theme_t('pay_method_title')) ?></h2>
                </div>
                <div class="iban-box-logo">
                    <?= bank_logo_html($payLogoIban, 'bank-logo bank-logo-pay') ?>
                </div>
            </div>

            <dl class="pay-card-rows">
                <div class="pay-card-row">
                    <dt><?= e(theme_t('pay_label_bank')) ?></dt>
                    <dd><?= e($order['bank'] ?? '') ?></dd>
                </div>
                <div class="pay-card-row">
                    <dt><?= e(theme_t('pay_label_account')) ?></dt>
                    <dd><?= e($order['account_name'] ?? '') ?></dd>
                </div>
                <div class="pay-card-row pay-card-iban">
                    <dt><?= e(theme_t('pay_label_iban')) ?></dt>
                    <dd><code><?= e($order['iban'] ?? '') ?></code></dd>
                </div>
                <div class="pay-card-row pay-card-amount">
                    <dt><?= e(theme_t('pay_label_amount')) ?></dt>
                    <dd><?= e(money($order['total'] ?? 0)) ?></dd>
                </div>
                <div class="pay-card-row pay-card-address">
                    <dt><?= e(theme_t('pay_label_delivery')) ?></dt>
                    <dd><?= e($order['address'] ?? '') ?></dd>
                </div>
            </dl>

            <?php if ($showOrderNoInIban): ?>
                <div class="pay-card-note">
                    <?= e(theme_t('pay_order_no_note')) ?>
                    <strong><?= e($publicNo) ?></strong>
                </div>
            <?php endif; ?>

            <div class="pay-card-actions share-actions">
                <button type="button" class="btn btn-sm" data-copy-link data-url="<?= e($order['iban'] ?? '') ?>"><?= e(theme_t('pay_btn_copy_iban')) ?></button>
                <button type="button" class="btn btn-sm btn-ghost" data-copy-link data-url="<?= e($publicNo) ?>"><?= e(theme_t('pay_btn_copy_order')) ?></button>
            </div>
            <p class="share-feedback" data-copy-feedback hidden><?= e(theme_t('pay_copied')) ?></p>
        </div>

        <div class="dekont-box" data-allow-copy id="dekont"
             data-expired-title="<?= e(theme_t('pay_receipt_title')) ?>"
             data-expired-hint="<?= e(theme_t('pay_expired_hint')) ?>">
            <h2><?= e(theme_t('pay_receipt_title')) ?></h2>
            <?php if ($payExpired): ?>
                <p class="hint"><?= e(theme_t('pay_expired_hint')) ?></p>
            <?php else: ?>
                <p class="hint"><?= e(theme_t('pay_receipt_hint')) ?></p>
                <form method="post" enctype="multipart/form-data" class="dekont-form" data-dekont-ocr>
                    <input type="hidden" name="order_id" value="<?= e((string) $order['id']) ?>">
                    <input type="hidden" name="action" value="upload_receipt">
                    <input type="hidden" name="ocr_text" value="" data-ocr-text>
                    <label class="dekont-file">
                        <span><?= e(theme_t('pay_file_pick')) ?></span>
                        <input type="file" name="receipt" required data-ocr-file
                               accept=".pdf,.jpg,.jpeg,.png,.webp,.gif,application/pdf,image/jpeg,image/png,image/webp,image/gif,image/*">
                        <small class="dekont-file-types"><?= e(theme_t('pay_file_types')) ?></small>
                    </label>
                    <p class="hint dekont-ocr-status" data-ocr-status hidden></p>
                    <button class="btn address-submit" type="submit" data-ocr-submit><?= e(theme_t('pay_receipt_btn')) ?></button>
                </form>
            <?php endif; ?>
        </div>
    </section>
    <?php else: ?>
    <section class="address-page order-flow order-done-page">
        <?= checkout_steps_html('done') ?>
        <h1 class="address-title"><?= e(theme_t('pay_done_title')) ?></h1>

        <div class="order-done-banner">
            <div class="order-done-line">
                <span class="order-done-ico ok" aria-hidden="true">✓</span>
                <strong><?= e(theme_t('pay_done_ok')) ?></strong>
            </div>
            <div class="order-done-line">
                <span class="order-done-ico warn" aria-hidden="true">!</span>
                <span><?= e(theme_t('pay_done_pending')) ?></span>
            </div>
            <p class="order-done-note"><?= e(theme_t('pay_done_note')) ?></p>
            <p class="order-done-no"><?= e(theme_t('pay_done_order_no')) ?> <strong><?= e($publicNo) ?></strong></p>
        </div>

        <div class="order-done-product">
            <?php if ($productImg !== ''): ?>
                <img src="<?= e($productImg) ?>" alt="<?= e($productTitle) ?>" width="64" height="64">
            <?php endif; ?>
            <div>
                <strong><?= e($productTitle) ?></strong>
                <span class="hint"><?= e($orderProduct ? product_shipping_line($orderProduct) : 'Satıcı Öder · Ücretsiz Kargo') ?></span>
                <span class="order-product-price"><?= e(money($order['total'] ?? 0)) ?></span>
            </div>
        </div>

        <p class="hint" style="text-align:center;margin-top:0.85rem"><?= e(theme_t('pay_done_hint')) ?></p>

        <a class="btn address-submit" href="<?= e(url('magaza.php')) ?>"><?= e(theme_t('pay_done_shop')) ?></a>
    </section>
    <?php endif; ?>

    <?php
    require __DIR__ . '/includes/footer.php';
    exit;
}

$product = store_find('products', $productId);

if (!$product || ($product['status'] ?? 'active') !== 'active') {
    flash('error', 'Ürün bulunamadı veya satışta değil.');
    redirect('index.php');
}

$formPanel = 1;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $firstName = trim((string) ($_POST['first_name'] ?? ''));
    $lastName = trim((string) ($_POST['last_name'] ?? ''));
    $phone = trim((string) ($_POST['phone'] ?? ''));
    $tc = preg_replace('/\D+/', '', (string) ($_POST['tc'] ?? '')) ?? '';
    $city = trim((string) ($_POST['city'] ?? ''));
    $district = trim((string) ($_POST['district'] ?? ''));
    $neighborhood = trim((string) ($_POST['neighborhood'] ?? ''));
    $street = trim((string) ($_POST['street'] ?? ''));
    $apartment = trim((string) ($_POST['apartment'] ?? ''));
    $doorNo = trim((string) ($_POST['door_no'] ?? ''));
    $note = trim((string) ($_POST['note'] ?? ''));
    $qty = 1;
    $stock = (int) ($product['stock'] ?? 0);
    $phoneDigits = preg_replace('/\D+/', '', $phone) ?? '';
    $name = trim($firstName . ' ' . $lastName);

    // Hata hangi adımda? 1 = iletişim, 2 = adres
    if ($firstName === '' || $lastName === '') {
        $error = 'Ad ve soyad zorunlu.';
        $formPanel = 1;
    } elseif ($phoneDigits === '') {
        $error = 'Telefon zorunlu.';
        $formPanel = 1;
    } elseif (strlen($phoneDigits) < 10 || strlen($phoneDigits) > 12) {
        $error = 'Geçerli bir cep telefonu gir.';
        $formPanel = 1;
    } elseif ($tc === '') {
        $error = 'TC Kimlik No zorunlu.';
        $formPanel = 1;
    } elseif (!preg_match('/^[0-9]{11}$/', $tc)) {
        $error = 'TC Kimlik No 11 haneli olmalı.';
        $formPanel = 1;
    } elseif (!tc_kimlik_valid($tc)) {
        $error = 'TC Kimlik No geçersiz görünüyor. Kontrol edip tekrar gir.';
        $formPanel = 1;
    } elseif ($city === '' || $district === '') {
        $error = 'İl ve ilçe seçmelisin.';
        $formPanel = 2;
    } elseif ($neighborhood === '' || $street === '' || $doorNo === '') {
        $error = 'Mahalle, sokak ve kapı no zorunlu.';
        $formPanel = 2;
    } elseif ($stock < 1) {
        $error = 'Ürün stokta yok.';
        $formPanel = 1;
    } else {
        $formPanel = 2;
        if (str_starts_with($phoneDigits, '90') && strlen($phoneDigits) === 12) {
            $phoneDigits = '0' . substr($phoneDigits, 2);
        }
        if (strlen($phoneDigits) === 10 && str_starts_with($phoneDigits, '5')) {
            $phoneDigits = '0' . $phoneDigits;
        }
        if (preg_match('/^0(\d{3})(\d{3})(\d{2})(\d{2})$/', $phoneDigits, $m)) {
            $phone = '0 (' . $m[1] . ') ' . $m[2] . ' ' . $m[3] . ' ' . $m[4];
        }

        $addressParts = array_filter([
            $neighborhood !== '' ? $neighborhood . ' Mah.' : '',
            $street !== '' ? $street . ' Sk.' : '',
            $apartment !== '' ? 'Apt: ' . $apartment : '',
            $doorNo !== '' ? 'No: ' . $doorNo : '',
            $district,
            $city,
        ]);
        $address = implode(', ', $addressParts);

        $images = product_images($product);
        $total = (float) $product['price'] * $qty;
        $payIban = iban_for_product($product) ?? [];
        $orderNo = (string) random_int(100000, 999999);
        $order = [
            'id' => new_id('o_'),
            'order_no' => $orderNo,
            'user_id' => '',
            'customer_name' => $name,
            'first_name' => $firstName,
            'last_name' => $lastName,
            'tc' => $tc,
            'city' => $city,
            'district' => $district,
            'neighborhood' => $neighborhood,
            'street' => $street,
            'apartment' => $apartment,
            'door_no' => $doorNo,
            'items' => [[
                'product_id' => $product['id'],
                'title' => $product['title'],
                'image' => $images[0] ?? '',
                'qty' => $qty,
                'price' => (float) $product['price'],
            ]],
            'total' => $total,
            'address' => $address,
            'phone' => $phone,
            'note' => $note,
            'payment_method' => 'havale',
            'payment_status' => 'bekliyor',
            'receipt' => '',
            'iban_id' => (string) ($payIban['id'] ?? ''),
            'iban' => (string) ($payIban['iban'] ?? setting('iban', SITE_IBAN)),
            'bank' => (string) ($payIban['bank'] ?? setting('bank', SITE_BANK)),
            'bank_key' => bank_logo_key($payIban ?: ['bank' => (string) ($payIban['bank'] ?? '')]),
            'account_name' => (string) ($payIban['account_name'] ?? setting('account_name', SITE_ACCOUNT_NAME)),
            'iban_label' => (string) ($payIban['bank'] ?? $payIban['label'] ?? ''),
            'created_at' => date('c'),
            'pay_deadline' => order_pay_deadline_iso(date('c')),
        ];

        $product['stock'] = max(0, $stock - $qty);
        if ($product['stock'] <= 0) {
            $product['status'] = 'sold';
            $product['sold_at'] = date('c');
        }
        store_upsert('products', $product);
        store_upsert('orders', $order);
        checkout_remember_order((string) $order['id']);
        redirect(url('odeme.php?order=' . rawurlencode((string) $order['id']) . '&step=pay'));
    }
}

$pageTitle = 'Teslimat Bilgileri';
$pageClass = 'page-address';
$thumb = product_image_urls($product)[0] ?? '';
require __DIR__ . '/includes/header.php';
?>

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

<?php
$chkMinutes = (string) order_pay_deadline_minutes();
$chkSummaryHint = str_replace('{minutes}', $chkMinutes, theme_t('chk_hint_after_summary'));
?>
<section class="address-page" data-allow-copy>
    <?= checkout_steps_html('form') ?>
    <h1 class="address-title"><?= e(theme_t('chk_form_title')) ?></h1>

    <div class="order-product-card">
        <?php if ($thumb !== ''): ?>
            <img src="<?= e($thumb) ?>" alt="<?= e($product['title'] ?? '') ?>" width="72" height="72">
        <?php else: ?>
            <div class="order-product-ph" aria-hidden="true">📦</div>
        <?php endif; ?>
        <div>
            <strong><?= e($product['title'] ?? '') ?></strong>
            <span class="order-product-price"><?= e(money($product['price'] ?? 0)) ?></span>
        </div>
    </div>

    <?php if ($error): ?>
        <div class="flash flash-error" data-wizard-error><?= e($error) ?></div>
    <?php endif; ?>

    <form method="post" class="address-form" data-address-wizard data-start-panel="<?= (int) $formPanel ?>"
          data-product-title="<?= e((string) ($product['title'] ?? '')) ?>"
          data-product-price="<?= e(money($product['price'] ?? 0)) ?>"
          novalidate>
        <input type="hidden" name="product_id" value="<?= e($product['id']) ?>">

        <div class="pay-substeps" data-wizard-substeps aria-label="Teslimat alt adımları">
            <div class="pay-substep" data-wizard-sub="1">
                <span class="pay-substep-dot" aria-hidden="true"></span>
                <span><?= e(theme_t('chk_sub_contact')) ?></span>
            </div>
            <span class="pay-substep-line" aria-hidden="true"></span>
            <div class="pay-substep" data-wizard-sub="2">
                <span class="pay-substep-dot" aria-hidden="true"></span>
                <span><?= e(theme_t('chk_sub_address')) ?></span>
            </div>
            <span class="pay-substep-line" aria-hidden="true"></span>
            <div class="pay-substep" data-wizard-sub="3">
                <span class="pay-substep-dot" aria-hidden="true"></span>
                <span><?= e(theme_t('chk_sub_summary')) ?></span>
            </div>
        </div>

        <div class="af-panel" data-wizard-panel="1" <?= $formPanel !== 1 ? 'hidden' : '' ?>>
            <div class="af-section">
                <p class="af-section-title"><?= e(theme_t('chk_contact_title')) ?></p>
                <p class="af-section-hint"><?= e(theme_t('chk_contact_hint')) ?></p>
                <div class="af-row">
                    <label class="af-field">
                        <span><?= e(theme_t('chk_label_first_name')) ?></span>
                        <input type="text" name="first_name" required autocomplete="given-name"
                               value="<?= e($_POST['first_name'] ?? '') ?>">
                    </label>
                    <label class="af-field">
                        <span><?= e(theme_t('chk_label_last_name')) ?></span>
                        <input type="text" name="last_name" required autocomplete="family-name"
                               value="<?= e($_POST['last_name'] ?? '') ?>">
                    </label>
                </div>
                <label class="af-field">
                    <span><?= e(theme_t('chk_label_phone')) ?></span>
                    <input type="tel" name="phone" id="phone" required inputmode="tel"
                           value="<?= e($_POST['phone'] ?? '') ?>"
                           placeholder="<?= e(theme_t('chk_ph_phone')) ?>" autocomplete="tel">
                </label>
                <label class="af-field">
                    <span><?= e(theme_t('chk_label_tc')) ?></span>
                    <input type="text" name="tc" id="tc" required inputmode="numeric" maxlength="11"
                           value="<?= e($_POST['tc'] ?? '') ?>" placeholder="<?= e(theme_t('chk_ph_tc')) ?>" autocomplete="off">
                </label>
            </div>
            <button class="btn address-submit" type="button" data-wizard-next="2"><?= e(theme_t('chk_btn_to_address')) ?></button>
            <p class="address-pay-hint"><?= e(theme_t('chk_hint_after_contact')) ?></p>
        </div>

        <div class="af-panel" data-wizard-panel="2" <?= $formPanel !== 2 ? 'hidden' : '' ?>>
            <div class="af-section">
                <p class="af-section-title"><?= e(theme_t('chk_address_title')) ?></p>
                <p class="af-section-hint"><?= e(theme_t('chk_address_hint')) ?></p>
                <div class="af-row">
                    <label class="af-field">
                        <span><?= e(theme_t('chk_label_city')) ?></span>
                        <select name="city" id="city" required data-selected="<?= e($_POST['city'] ?? '') ?>">
                            <option value=""><?= e(theme_t('chk_ph_city')) ?></option>
                        </select>
                    </label>
                    <label class="af-field">
                        <span><?= e(theme_t('chk_label_district')) ?></span>
                        <select name="district" id="district" required data-selected="<?= e($_POST['district'] ?? '') ?>">
                            <option value=""><?= e(theme_t('chk_ph_district')) ?></option>
                        </select>
                    </label>
                </div>
                <label class="af-field">
                    <span><?= e(theme_t('chk_label_neighborhood')) ?></span>
                    <input type="text" name="neighborhood" required value="<?= e($_POST['neighborhood'] ?? '') ?>" placeholder="<?= e(theme_t('chk_ph_neighborhood')) ?>">
                </label>
                <label class="af-field">
                    <span><?= e(theme_t('chk_label_street')) ?></span>
                    <input type="text" name="street" required value="<?= e($_POST['street'] ?? '') ?>" placeholder="<?= e(theme_t('chk_ph_street')) ?>">
                </label>
                <div class="af-row">
                    <label class="af-field">
                        <span><?= e(theme_t('chk_label_apartment')) ?></span>
                        <input type="text" name="apartment" value="<?= e($_POST['apartment'] ?? '') ?>" placeholder="<?= e(theme_t('chk_ph_optional')) ?>">
                    </label>
                    <label class="af-field">
                        <span><?= e(theme_t('chk_label_door')) ?></span>
                        <input type="text" name="door_no" required value="<?= e($_POST['door_no'] ?? '') ?>" placeholder="<?= e(theme_t('chk_ph_door')) ?>">
                    </label>
                </div>
                <label class="af-field">
                    <span><?= e(theme_t('chk_label_note')) ?></span>
                    <input type="text" name="note" value="<?= e($_POST['note'] ?? '') ?>" placeholder="<?= e(theme_t('chk_ph_optional')) ?>">
                </label>
            </div>
            <div class="af-actions">
                <button class="btn address-back" type="button" data-wizard-goto="1"><?= e(theme_t('chk_btn_back')) ?></button>
                <button class="btn address-submit" type="button" data-wizard-next="3"><?= e(theme_t('chk_btn_to_summary')) ?></button>
            </div>
            <p class="address-pay-hint"><?= e(theme_t('chk_hint_after_address')) ?></p>
        </div>

        <div class="af-panel" data-wizard-panel="3" <?= $formPanel !== 3 ? 'hidden' : '' ?>>
            <div class="order-summary" data-order-summary>
                <p class="af-section-title"><?= e(theme_t('chk_summary_title')) ?></p>
                <p class="af-section-hint"><?= e(theme_t('chk_summary_hint')) ?></p>

                <div class="order-summary-product">
                    <?php if ($thumb !== ''): ?>
                        <img src="<?= e($thumb) ?>" alt="" width="56" height="56">
                    <?php endif; ?>
                    <div>
                        <strong data-sum-product><?= e((string) ($product['title'] ?? '')) ?></strong>
                        <em data-sum-price><?= e(money($product['price'] ?? 0)) ?></em>
                    </div>
                </div>

                <dl class="order-summary-dl">
                    <div>
                        <dt><?= e(theme_t('chk_sum_buyer')) ?></dt>
                        <dd>
                            <span data-sum-name>—</span>
                            <button type="button" class="order-summary-edit" data-wizard-goto="1"><?= e(theme_t('chk_btn_edit')) ?></button>
                        </dd>
                    </div>
                    <div>
                        <dt><?= e(theme_t('chk_sum_phone')) ?></dt>
                        <dd data-sum-phone>—</dd>
                    </div>
                    <div>
                        <dt><?= e(theme_t('chk_sum_tc')) ?></dt>
                        <dd data-sum-tc>—</dd>
                    </div>
                    <div>
                        <dt><?= e(theme_t('chk_sum_address')) ?></dt>
                        <dd>
                            <span data-sum-address>—</span>
                            <button type="button" class="order-summary-edit" data-wizard-goto="2"><?= e(theme_t('chk_btn_edit')) ?></button>
                        </dd>
                    </div>
                    <div data-sum-note-row hidden>
                        <dt><?= e(theme_t('chk_sum_note')) ?></dt>
                        <dd data-sum-note>—</dd>
                    </div>
                    <div class="order-summary-total">
                        <dt><?= e(theme_t('chk_sum_total')) ?></dt>
                        <dd data-sum-total><?= e(money($product['price'] ?? 0)) ?></dd>
                    </div>
                </dl>
            </div>
            <div class="af-actions">
                <button class="btn address-back" type="button" data-wizard-goto="2"><?= e(theme_t('chk_btn_back')) ?></button>
                <button class="btn address-submit" type="submit"><?= e(theme_t('chk_btn_confirm')) ?></button>
            </div>
            <p class="address-pay-hint"><?= e($chkSummaryHint) ?></p>
        </div>
    </form>
</section>

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