<?php
require_once 'header.php';

try {
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS nid_front VARCHAR(255) NULL");
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS nid_back VARCHAR(255) NULL");
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS guarantor_name VARCHAR(255) NULL");
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS guarantor_nid VARCHAR(255) NULL");
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS guarantor_photo VARCHAR(255) NULL");
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS g_nid_front VARCHAR(255) NULL");
    $pdo->exec("ALTER TABLE " . table('users') . " ADD COLUMN IF NOT EXISTS g_nid_back VARCHAR(255) NULL");
} catch (Exception $e) {
}

if (isset($_SESSION['user_id'])) {
    header("Location: " . SITE_URL . "dashboard.php");
    exit;
}

$lang = $_SESSION['lang'] ?? 'bn';
$error_msg = '';

$reg_txt = [
    'bn' => [
        'title' => 'সুদমুক্ত অর্থ সহায়তার আবেদন ও নিবন্ধন',
        'desc' => 'বিশ্বস্ত, পরিচ্ছন্ন এবং নিরাপদভাবে আবেদন সম্পন্ন করতে নিচের তথ্যগুলো সতর্কভাবে পূরণ করুন।',
        'section1' => '১. আবেদনকারীর তথ্য',
        'section2' => '২. আবেদনকারীর ছবি ও NID',
        'section3' => '৩. জামিনদারের তথ্য ও NID',
        'lbl_name' => 'আবেদনকারীর পূর্ণ নাম *',
        'lbl_phone' => 'মোবাইল নম্বর *',
        'lbl_email' => 'ইমেইল এড্রেস (ঐচ্ছিক)',
        'lbl_nid' => 'আবেদনকারীর NID নম্বর *',
        'lbl_addr' => 'সম্পূর্ণ ঠিকানা *',
        'lbl_amount' => 'সহায়তার পরিমাণ *',
        'lbl_purpose' => 'আবেদনের উদ্দেশ্য *',
        'lbl_pass' => 'অ্যাকাউন্ট পাসওয়ার্ড *',
        'lbl_own_photo' => 'আবেদনকারীর ছবি *',
        'lbl_nid_front' => 'আবেদনকারীর NID সামনের অংশ *',
        'lbl_nid_back' => 'আবেদনকারীর NID পিছনের অংশ *',
        'lbl_g_name' => 'জামিনদারের পূর্ণ নাম *',
        'lbl_g_nid' => 'জামিনদারের NID নম্বর *',
        'lbl_g_photo' => 'জামিনদারের ছবি *',
        'lbl_g_front' => 'জামিনদারের NID সামনের অংশ *',
        'lbl_g_back' => 'জামিনদারের NID পিছনের অংশ *',
        'btn_submit' => 'আবেদন ও নিবন্ধন সম্পন্ন করুন',
        'has_account' => 'ইতিমধ্যে আবেদন করেছেন?',
        'login_link' => 'লগইন করুন',
        'err_missing' => 'অনুগ্রহ করে সকল আবশ্যকীয় ঘর পূরণ করুন এবং প্রয়োজনীয় সব ফাইল আপলোড করুন।',
        'err_file_type' => 'শুধুমাত্র JPG, JPEG এবং PNG ফাইল আপলোড করা যাবে।',
        'err_email' => 'এই ইমেইলটি ইতিমধ্যে নিবন্ধিত রয়েছে!',
        'err_phone' => 'এই মোবাইল নম্বর দিয়ে ইতিমধ্যে আবেদন করা হয়েছে!',
        'upload_idle' => 'ফাইল বাছাই করুন',
        'upload_hint' => 'PNG, JPG বা JPEG',
        'trust_1' => 'সুরক্ষিত তথ্য সংগ্রহ',
        'trust_2' => 'মোবাইল ও ডেস্কটপ উপযোগী',
        'trust_3' => 'সহজ ডকুমেন্ট আপলোড'
    ],
    'en' => [
        'title' => 'Interest-Free Aid Application & Registration',
        'desc' => 'Complete your application in a clean, secure, and professional experience by filling the form carefully.',
        'section1' => '1. Applicant Information',
        'section2' => '2. Applicant Photo & NID',
        'section3' => '3. Guarantor Information & NID',
        'lbl_name' => 'Applicant Full Name *',
        'lbl_phone' => 'Mobile Number *',
        'lbl_email' => 'Email Address (Optional)',
        'lbl_nid' => 'Applicant NID Number *',
        'lbl_addr' => 'Full Address *',
        'lbl_amount' => 'Requested Amount *',
        'lbl_purpose' => 'Application Purpose *',
        'lbl_pass' => 'Account Password *',
        'lbl_own_photo' => 'Applicant Photo *',
        'lbl_nid_front' => 'Applicant NID Front *',
        'lbl_nid_back' => 'Applicant NID Back *',
        'lbl_g_name' => 'Guarantor Full Name *',
        'lbl_g_nid' => 'Guarantor NID Number *',
        'lbl_g_photo' => 'Guarantor Photo *',
        'lbl_g_front' => 'Guarantor NID Front *',
        'lbl_g_back' => 'Guarantor NID Back *',
        'btn_submit' => 'Submit Application & Register',
        'has_account' => 'Already applied?',
        'login_link' => 'Login here',
        'err_missing' => 'Please fill all required fields and upload all required files.',
        'err_file_type' => 'Only JPG, JPEG, and PNG files are allowed.',
        'err_email' => 'This email is already registered!',
        'err_phone' => 'This mobile number is already registered!',
        'upload_idle' => 'Choose file',
        'upload_hint' => 'PNG, JPG or JPEG',
        'trust_1' => 'Secure information collection',
        'trust_2' => 'Optimized for mobile and desktop',
        'trust_3' => 'Simple document upload'
    ]
];

$r = $reg_txt[$lang];
$old = function ($key) {
    return htmlspecialchars($_POST[$key] ?? '', ENT_QUOTES, 'UTF-8');
};

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = sanitize_input($_POST['name'] ?? '');
    $phone = sanitize_input($_POST['phone'] ?? '');
    $email = sanitize_input($_POST['email'] ?? '');
    $nid_number = sanitize_input($_POST['nid_number'] ?? '');
    $present_address = sanitize_input($_POST['present_address'] ?? '');
    $amount = sanitize_input($_POST['amount'] ?? '');
    $purpose = sanitize_input($_POST['purpose'] ?? '');
    $password = $_POST['password'] ?? '';
    $g_name = sanitize_input($_POST['g_name'] ?? '');
    $g_nid = sanitize_input($_POST['g_nid'] ?? '');

    if (empty($email)) {
        $email = $phone . '@araihazargeneralhospital.com';
    }

    $upload_dir = 'uploads/';
    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0777, true);
    }

    $own_photo = $_FILES['photo'] ?? null;
    $nid_front = $_FILES['nid_front'] ?? null;
    $nid_back = $_FILES['nid_back'] ?? null;
    $g_photo = $_FILES['g_photo'] ?? null;
    $g_nid_front = $_FILES['g_nid_front'] ?? null;
    $g_nid_back = $_FILES['g_nid_back'] ?? null;

    if (
        empty($name) || empty($phone) || empty($nid_number) || empty($present_address) || empty($amount) || empty($purpose) || empty($password) || empty($g_name) || empty($g_nid) ||
        empty($own_photo['name']) || empty($nid_front['name']) || empty($nid_back['name']) || empty($g_photo['name']) || empty($g_nid_front['name']) || empty($g_nid_back['name'])
    ) {
        $error_msg = $r['err_missing'];
    } else {
        $allowed_ext = ['jpg', 'jpeg', 'png'];
        $ext1 = strtolower(pathinfo($own_photo['name'], PATHINFO_EXTENSION));
        $ext2 = strtolower(pathinfo($nid_front['name'], PATHINFO_EXTENSION));
        $ext3 = strtolower(pathinfo($nid_back['name'], PATHINFO_EXTENSION));
        $ext4 = strtolower(pathinfo($g_photo['name'], PATHINFO_EXTENSION));
        $ext5 = strtolower(pathinfo($g_nid_front['name'], PATHINFO_EXTENSION));
        $ext6 = strtolower(pathinfo($g_nid_back['name'], PATHINFO_EXTENSION));

        if (!in_array($ext1, $allowed_ext) || !in_array($ext2, $allowed_ext) || !in_array($ext3, $allowed_ext) || !in_array($ext4, $allowed_ext) || !in_array($ext5, $allowed_ext) || !in_array($ext6, $allowed_ext)) {
            $error_msg = $r['err_file_type'];
        } else {
            try {
                $stmt_ph = $pdo->prepare("SELECT COUNT(*) FROM " . table('users') . " WHERE phone = ?");
                $stmt_ph->execute([$phone]);

                if ($stmt_ph->fetchColumn() > 0) {
                    $error_msg = $r['err_phone'];
                } else {
                    $file_own = uniqid() . '_own.' . $ext1;
                    $file_front = uniqid() . '_front.' . $ext2;
                    $file_back = uniqid() . '_back.' . $ext3;
                    $file_g = uniqid() . '_g.' . $ext4;
                    $file_g_front = uniqid() . '_g_front.' . $ext5;
                    $file_g_back = uniqid() . '_g_back.' . $ext6;

                    move_uploaded_file($own_photo['tmp_name'], $upload_dir . $file_own);
                    move_uploaded_file($nid_front['tmp_name'], $upload_dir . $file_front);
                    move_uploaded_file($nid_back['tmp_name'], $upload_dir . $file_back);
                    move_uploaded_file($g_photo['tmp_name'], $upload_dir . $file_g);
                    move_uploaded_file($g_nid_front['tmp_name'], $upload_dir . $file_g_front);
                    move_uploaded_file($g_nid_back['tmp_name'], $upload_dir . $file_g_back);

                    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                    $stmt_u = $pdo->prepare("INSERT INTO " . table('users') . "
                        (name, email, phone, password, nid_number, present_address, photo, nid_front, nid_back, guarantor_name, guarantor_nid, guarantor_photo, g_nid_front, g_nid_back)
                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

                    $stmt_u->execute([
                        $name, $email, $phone, $hashed_password, $nid_number, $present_address,
                        $file_own, $file_front, $file_back, $g_name, $g_nid, $file_g, $file_g_front, $file_g_back
                    ]);

                    $user_id = $pdo->lastInsertId();
                    $tracking_id = 'BNP-' . date('Y') . '-' . rand(1000, 9999);

                    $stmt_app = $pdo->prepare("INSERT INTO " . table('applications') . "
                        (user_id, tracking_id, amount_requested, purpose, status) VALUES (?, ?, ?, ?, 'Pending')");
                    $stmt_app->execute([$user_id, $tracking_id, $amount, $purpose]);
                    $app_id = $pdo->lastInsertId();

                    $stmt_gua_rep = $pdo->prepare("INSERT INTO " . table('guarantors') . "
                        (application_id, name, nid_number, phone, address, relation, photo) VALUES (?, ?, ?, ?, ?, ?, ?)");
                    $stmt_gua_rep->execute([$app_id, $g_name, $g_nid, $phone, $present_address, 'গ্যারান্টার', $file_g]);

                    $welcome_msg = "আসসালামু আলাইকুম {$name}! কর্জে হাসানা ফান্ডে আপনার সুদমুক্ত সহায়তার আবেদনটি সফলভাবে রেজিস্টার করা হয়েছে। আপনার ট্র্যাকিং আইডি হলো: {$tracking_id}।";
                    $stmt_not = $pdo->prepare("INSERT INTO " . table('notifications') . " (user_id, title, message) VALUES (?, ?, ?)");
                    $stmt_not->execute([$user_id, 'আবেদন ও স্বাগতম নোটিফিকেশন', $welcome_msg]);

                    $_SESSION['user_id'] = $user_id;
                    $_SESSION['user_name'] = $name;
                    $_SESSION['user_email'] = $email;

                    header("Location: " . SITE_URL . "dashboard.php");
                    exit;
                }
            } catch (Exception $e) {
                $error_msg = 'সিস্টেম ত্রুটি: ' . $e->getMessage();
            }
        }
    }
}
?>

<style>
    :root {
        --reg-bg-1: #f8fafc;
        --reg-bg-2: #eef2ff;
        --reg-card: rgba(255, 255, 255, 0.92);
        --reg-stroke: rgba(148, 163, 184, 0.22);
        --reg-text: #0f172a;
        --reg-muted: #475569;
        --reg-primary: #0f766e;
        --reg-primary-dark: #115e59;
        --reg-accent: #1d4ed8;
        --reg-danger-bg: #fef2f2;
        --reg-danger-border: #ef4444;
        --reg-danger-text: #991b1b;
        --reg-success-bg: #ecfdf5;
        --reg-success-border: #10b981;
        --reg-shadow: 0 24px 60px rgba(15, 23, 42, 0.12);
        --reg-radius-xl: 28px;
        --reg-radius-lg: 20px;
        --reg-radius-md: 14px;
    }

    .reg-shell {
        position: relative;
        padding: 36px 0 70px;
        background: radial-gradient(circle at top left, #dbeafe 0%, transparent 28%), radial-gradient(circle at top right, #ccfbf1 0%, transparent 24%), linear-gradient(180deg, var(--reg-bg-2) 0%, var(--reg-bg-1) 46%, #ffffff 100%);
    }

    .reg-shell::before,
    .reg-shell::after {
        content: '';
        position: absolute;
        border-radius: 999px;
        filter: blur(24px);
        opacity: 0.45;
        pointer-events: none;
    }

    .reg-shell::before {
        width: 220px;
        height: 220px;
        background: #93c5fd;
        left: -70px;
        top: 30px;
    }

    .reg-shell::after {
        width: 260px;
        height: 260px;
        background: #99f6e4;
        right: -80px;
        top: 120px;
    }

    .reg-layout {
        position: relative;
        z-index: 1;
        display: grid;
        grid-template-columns: 360px minmax(0, 1fr);
        gap: 24px;
        align-items: start;
    }

    .reg-info-card,
    .reg-form-card {
        background: var(--reg-card);
        backdrop-filter: blur(14px);
        border: 1px solid var(--reg-stroke);
        border-radius: var(--reg-radius-xl);
        box-shadow: var(--reg-shadow);
    }

    .reg-info-card {
        padding: 28px;
        position: sticky;
        top: 24px;
    }

    .reg-eyebrow {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 8px 14px;
        border-radius: 999px;
        background: rgba(15, 118, 110, 0.1);
        color: var(--reg-primary-dark);
        font-size: 13px;
        font-weight: 800;
        letter-spacing: 0.02em;
        margin-bottom: 18px;
    }

    .reg-info-card h2 {
        margin: 0 0 10px;
        font-size: 32px;
        line-height: 1.18;
        color: var(--reg-text);
        font-weight: 900;
    }

    .reg-info-card p {
        margin: 0;
        color: var(--reg-muted);
        font-size: 15px;
        line-height: 1.75;
    }

    .reg-trust-list {
        display: grid;
        gap: 12px;
        margin-top: 24px;
    }

    .reg-trust-item,
    .reg-mini-note {
        display: flex;
        gap: 12px;
        align-items: flex-start;
        padding: 14px 16px;
        border-radius: 16px;
        background: rgba(255, 255, 255, 0.9);
        border: 1px solid rgba(148, 163, 184, 0.18);
    }

    .reg-icon-dot {
        width: 34px;
        height: 34px;
        min-width: 34px;
        border-radius: 50%;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, var(--reg-primary) 0%, var(--reg-accent) 100%);
        color: #fff;
        font-size: 15px;
        font-weight: 800;
    }

    .reg-mini-note {
        margin-top: 22px;
        background: linear-gradient(135deg, rgba(29, 78, 216, 0.08), rgba(15, 118, 110, 0.08));
    }

    .reg-form-card {
        overflow: hidden;
    }

    .reg-form-head {
        padding: 28px 30px;
        background: linear-gradient(135deg, #0f172a 0%, #0f766e 60%, #1d4ed8 100%);
        color: #fff;
    }

    .reg-form-head h1 {
        margin: 0 0 8px;
        font-size: 30px;
        font-weight: 900;
        line-height: 1.2;
    }

    .reg-form-head p {
        margin: 0;
        color: rgba(255, 255, 255, 0.82);
        font-size: 14px;
        line-height: 1.7;
        max-width: 760px;
    }

    .reg-stepbar {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 12px;
        margin-top: 22px;
    }

    .reg-step {
        padding: 12px 14px;
        border-radius: 16px;
        background: rgba(255, 255, 255, 0.14);
        border: 1px solid rgba(255, 255, 255, 0.18);
        font-size: 13px;
        font-weight: 700;
    }

    .reg-form-body {
        padding: 30px;
    }

    .reg-alert {
        display: flex;
        gap: 12px;
        align-items: flex-start;
        margin-bottom: 22px;
        padding: 16px 18px;
        border-radius: 16px;
        background: var(--reg-danger-bg);
        border: 1px solid rgba(239, 68, 68, 0.24);
        border-left: 5px solid var(--reg-danger-border);
        color: var(--reg-danger-text);
        font-weight: 700;
    }

    .reg-section {
        margin-bottom: 22px;
        padding: 24px;
        border-radius: 24px;
        background: linear-gradient(180deg, rgba(248, 250, 252, 0.96) 0%, rgba(255, 255, 255, 0.96) 100%);
        border: 1px solid rgba(148, 163, 184, 0.18);
    }

    .reg-section-title {
        margin: 0 0 18px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        color: var(--reg-text);
        font-size: 20px;
        font-weight: 900;
    }

    .reg-section-badge {
        padding: 7px 12px;
        border-radius: 999px;
        background: rgba(15, 118, 110, 0.1);
        color: var(--reg-primary-dark);
        font-size: 12px;
        font-weight: 800;
    }

    .reg-grid-2,
    .reg-upload-grid {
        display: grid;
        gap: 18px;
    }

    .reg-grid-2 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .reg-upload-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .reg-col-full {
        grid-column: 1 / -1;
    }

    .reg-field label,
    .reg-upload-label {
        display: block;
        margin-bottom: 9px;
        color: var(--reg-text);
        font-size: 14px;
        font-weight: 800;
    }

    .reg-input,
    .reg-textarea {
        width: 100%;
        border: 1px solid rgba(148, 163, 184, 0.35);
        background: #fff;
        color: var(--reg-text);
        border-radius: 16px;
        padding: 15px 16px;
        font-size: 15px;
        outline: none;
        transition: 0.2s ease;
        box-sizing: border-box;
    }

    .reg-input:focus,
    .reg-textarea:focus {
        border-color: rgba(29, 78, 216, 0.45);
        box-shadow: 0 0 0 4px rgba(29, 78, 216, 0.09);
    }

    .reg-textarea {
        min-height: 132px;
        resize: vertical;
    }

    .reg-upload-box {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 10px;
        min-height: 168px;
        padding: 20px;
        text-align: center;
        border-radius: 20px;
        border: 1.5px dashed rgba(148, 163, 184, 0.5);
        background: #fff;
        cursor: pointer;
        transition: 0.2s ease;
        box-sizing: border-box;
    }

    .reg-upload-box:hover {
        transform: translateY(-2px);
        border-color: rgba(15, 118, 110, 0.55);
        box-shadow: 0 16px 34px rgba(15, 23, 42, 0.08);
    }

    .reg-upload-box input[type="file"] {
        display: none;
    }

    .reg-upload-icon {
        width: 56px;
        height: 56px;
        border-radius: 18px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, rgba(15, 118, 110, 0.12), rgba(29, 78, 216, 0.12));
        color: var(--reg-primary-dark);
        font-size: 24px;
        font-weight: 900;
    }

    .reg-upload-title {
        font-size: 14px;
        font-weight: 800;
        color: var(--reg-text);
        line-height: 1.5;
        word-break: break-word;
    }

    .reg-upload-sub {
        font-size: 12px;
        color: var(--reg-muted);
        line-height: 1.5;
    }

    .is-selected {
        background: var(--reg-success-bg);
        border-color: rgba(16, 185, 129, 0.6);
        box-shadow: 0 16px 34px rgba(16, 185, 129, 0.12);
    }

    .is-selected .reg-upload-icon {
        background: linear-gradient(135deg, rgba(16, 185, 129, 0.16), rgba(5, 150, 105, 0.18));
        color: #047857;
    }

    .reg-submit-wrap {
        margin-top: 26px;
    }

    .reg-submit-btn {
        width: 100%;
        border: none;
        border-radius: 18px;
        padding: 18px 20px;
        background: linear-gradient(135deg, var(--reg-primary) 0%, var(--reg-accent) 100%);
        color: #fff;
        font-size: 16px;
        font-weight: 900;
        letter-spacing: 0.01em;
        cursor: pointer;
        box-shadow: 0 22px 36px rgba(29, 78, 216, 0.18);
        transition: 0.2s ease;
    }

    .reg-submit-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 26px 44px rgba(29, 78, 216, 0.22);
    }

    .reg-login-text {
        margin: 16px 0 0;
        text-align: center;
        color: var(--reg-muted);
        font-size: 14px;
        font-weight: 600;
    }

    .reg-login-text a {
        color: var(--reg-accent);
        font-weight: 800;
        text-decoration: none;
    }

    .reg-login-text a:hover {
        text-decoration: underline;
    }

    @media (max-width: 1199px) {
        .reg-layout {
            grid-template-columns: 320px minmax(0, 1fr);
        }
    }

    @media (max-width: 991px) {
        .reg-layout {
            grid-template-columns: 1fr;
        }

        .reg-info-card {
            position: static;
        }

        .reg-upload-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }
    }

    @media (max-width: 767px) {
        .reg-shell {
            padding: 20px 0 40px;
        }

        .reg-info-card,
        .reg-form-body,
        .reg-form-head,
        .reg-section {
            padding-left: 18px;
            padding-right: 18px;
        }

        .reg-info-card h2,
        .reg-form-head h1 {
            font-size: 24px;
        }

        .reg-stepbar,
        .reg-grid-2,
        .reg-upload-grid {
            grid-template-columns: 1fr;
        }

        .reg-section-title {
            align-items: flex-start;
            flex-direction: column;
        }

        .reg-upload-box {
            min-height: 148px;
        }
    }

    @media (max-width: 480px) {
        .reg-shell .container {
            padding-left: 12px;
            padding-right: 12px;
        }

        .reg-info-card,
        .reg-form-card {
            border-radius: 22px;
        }

        .reg-submit-btn {
            font-size: 15px;
            padding: 16px 18px;
        }
    }
</style>

<div class="reg-shell">
    <div class="container">
        <div class="reg-layout">
            <aside class="reg-info-card">
                <div class="reg-eyebrow">Secure Application Portal</div>
                <h2><?php echo $r['title']; ?></h2>
                <p><?php echo $r['desc']; ?></p>

                <div class="reg-trust-list">
                    <div class="reg-trust-item">
                        <span class="reg-icon-dot">✓</span>
                        <div>
                            <strong><?php echo $r['trust_1']; ?></strong><br>
                            <span style="color:#64748b;font-size:13px;">আপনার তথ্য ধাপে ধাপে সংগ্রহ করা হবে, যাতে ফর্ম পূরণ করা সহজ হয়।</span>
                        </div>
                    </div>
                    <div class="reg-trust-item">
                        <span class="reg-icon-dot">📱</span>
                        <div>
                            <strong><?php echo $r['trust_2']; ?></strong><br>
                            <span style="color:#64748b;font-size:13px;">মোবাইলে স্ট্যাকড লেআউট, ডেস্কটপে বিস্তৃত দুই-কলামের উপস্থাপন।</span>
                        </div>
                    </div>
                    <div class="reg-trust-item">
                        <span class="reg-icon-dot">⬆</span>
                        <div>
                            <strong><?php echo $r['trust_3']; ?></strong><br>
                            <span style="color:#64748b;font-size:13px;">প্রতিটি ছবি/ডকুমেন্ট আলাদা প্রিমিয়াম আপলোড বক্সে যুক্ত করা যাবে।</span>
                        </div>
                    </div>
                </div>

                <div class="reg-mini-note">
                    <span class="reg-icon-dot">i</span>
                    <div>
                        <strong style="color:#0f172a;">নোট</strong><br>
                        <span style="color:#475569;font-size:13px;line-height:1.7;">ডিজাইন রিডিজাইনের সময় মূল ফিল্ড নাম, ফাইল ইনপুট নাম এবং ব্যাকএন্ড প্রসেসিং স্ট্রাকচার অপরিবর্তিত রাখা হয়েছে, যাতে সরাসরি ইন্টিগ্রেশন সহজ হয়।</span>
                    </div>
                </div>
            </aside>

            <section class="reg-form-card">
                <div class="reg-form-head">
                    <h1><?php echo $r['title']; ?></h1>
                    <p><?php echo $r['desc']; ?></p>

                    <div class="reg-stepbar">
                        <div class="reg-step"><?php echo $r['section1']; ?></div>
                        <div class="reg-step"><?php echo $r['section2']; ?></div>
                        <div class="reg-step"><?php echo $r['section3']; ?></div>
                    </div>
                </div>

                <div class="reg-form-body">
                    <form action="" method="POST" enctype="multipart/form-data">
                        <?php if (!empty($error_msg)): ?>
                            <div class="reg-alert">
                                <div>⚠</div>
                                <div><?php echo $error_msg; ?></div>
                            </div>
                        <?php endif; ?>

                        <div class="reg-section">
                            <h3 class="reg-section-title">
                                <span><?php echo $r['section1']; ?></span>
                                <span class="reg-section-badge">Applicant</span>
                            </h3>

                            <div class="reg-grid-2">
                                <div class="reg-field">
                                    <label><?php echo $r['lbl_name']; ?></label>
                                    <input class="reg-input" type="text" name="name" required value="<?php echo $old('name'); ?>" placeholder="মোঃ রফিকুল ইসলাম">
                                </div>

                                <div class="reg-field">
                                    <label><?php echo $r['lbl_phone']; ?></label>
                                    <input class="reg-input" type="tel" name="phone" required value="<?php echo $old('phone'); ?>" placeholder="017XXXXXXXX">
                                </div>

                                <div class="reg-field">
                                    <label><?php echo $r['lbl_email']; ?></label>
                                    <input class="reg-input" type="email" name="email" value="<?php echo $old('email'); ?>" placeholder="example@gmail.com">
                                </div>

                                <div class="reg-field">
                                    <label><?php echo $r['lbl_nid']; ?></label>
                                    <input class="reg-input" type="number" name="nid_number" required value="<?php echo $old('nid_number'); ?>" placeholder="NID নম্বর লিখুন">
                                </div>

                                <div class="reg-field">
                                    <label><?php echo $r['lbl_amount']; ?></label>
                                    <input class="reg-input" type="number" name="amount" required value="<?php echo $old('amount'); ?>" placeholder="যেমন: 20000">
                                </div>

                                <div class="reg-field">
                                    <label><?php echo $r['lbl_pass']; ?></label>
                                    <input class="reg-input" type="password" name="password" required placeholder="••••••••">
                                </div>

                                <div class="reg-field reg-col-full">
                                    <label><?php echo $r['lbl_purpose']; ?></label>
                                    <input class="reg-input" type="text" name="purpose" required value="<?php echo $old('purpose'); ?>" placeholder="যেমন: ব্যবসা সম্প্রসারণ, চিকিৎসা সহায়তা, দোকান সংস্কার">
                                </div>

                                <div class="reg-field reg-col-full">
                                    <label><?php echo $r['lbl_addr']; ?></label>
                                    <textarea class="reg-textarea" name="present_address" required placeholder="গ্রাম, ডাকঘর, ইউনিয়ন, উপজেলা, জেলা ও সম্পূর্ণ ঠিকানা লিখুন"><?php echo $old('present_address'); ?></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="reg-section">
                            <h3 class="reg-section-title">
                                <span><?php echo $r['section2']; ?></span>
                                <span class="reg-section-badge">Documents</span>
                            </h3>

                            <div class="reg-upload-grid">
                                <div>
                                    <label class="reg-upload-label"><?php echo $r['lbl_own_photo']; ?></label>
                                    <label class="reg-upload-box" id="box_photo">
                                        <input type="file" name="photo" accept="image/*" required onchange="updateFileCard(this, 'box_photo', 'photo_name')">
                                        <span class="reg-upload-icon">👤</span>
                                        <span class="reg-upload-title" id="photo_name"><?php echo $r['upload_idle']; ?></span>
                                        <span class="reg-upload-sub"><?php echo $r['upload_hint']; ?></span>
                                    </label>
                                </div>

                                <div>
                                    <label class="reg-upload-label"><?php echo $r['lbl_nid_front']; ?></label>
                                    <label class="reg-upload-box" id="box_nid_front">
                                        <input type="file" name="nid_front" accept="image/*" required onchange="updateFileCard(this, 'box_nid_front', 'nid_front_name')">
                                        <span class="reg-upload-icon">🪪</span>
                                        <span class="reg-upload-title" id="nid_front_name"><?php echo $r['upload_idle']; ?></span>
                                        <span class="reg-upload-sub"><?php echo $r['upload_hint']; ?></span>
                                    </label>
                                </div>

                                <div>
                                    <label class="reg-upload-label"><?php echo $r['lbl_nid_back']; ?></label>
                                    <label class="reg-upload-box" id="box_nid_back">
                                        <input type="file" name="nid_back" accept="image/*" required onchange="updateFileCard(this, 'box_nid_back', 'nid_back_name')">
                                        <span class="reg-upload-icon">📄</span>
                                        <span class="reg-upload-title" id="nid_back_name"><?php echo $r['upload_idle']; ?></span>
                                        <span class="reg-upload-sub"><?php echo $r['upload_hint']; ?></span>
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="reg-section">
                            <h3 class="reg-section-title">
                                <span><?php echo $r['section3']; ?></span>
                                <span class="reg-section-badge">Guarantor</span>
                            </h3>

                            <div class="reg-grid-2" style="margin-bottom:18px;">
                                <div class="reg-field">
                                    <label><?php echo $r['lbl_g_name']; ?></label>
                                    <input class="reg-input" type="text" name="g_name" required value="<?php echo $old('g_name'); ?>" placeholder="জামিনদারের পূর্ণ নাম">
                                </div>

                                <div class="reg-field">
                                    <label><?php echo $r['lbl_g_nid']; ?></label>
                                    <input class="reg-input" type="number" name="g_nid" required value="<?php echo $old('g_nid'); ?>" placeholder="জামিনদারের NID নম্বর">
                                </div>
                            </div>

                            <div class="reg-upload-grid">
                                <div>
                                    <label class="reg-upload-label"><?php echo $r['lbl_g_photo']; ?></label>
                                    <label class="reg-upload-box" id="box_g_photo">
                                        <input type="file" name="g_photo" accept="image/*" required onchange="updateFileCard(this, 'box_g_photo', 'g_photo_name')">
                                        <span class="reg-upload-icon">🧑</span>
                                        <span class="reg-upload-title" id="g_photo_name"><?php echo $r['upload_idle']; ?></span>
                                        <span class="reg-upload-sub"><?php echo $r['upload_hint']; ?></span>
                                    </label>
                                </div>

                                <div>
                                    <label class="reg-upload-label"><?php echo $r['lbl_g_front']; ?></label>
                                    <label class="reg-upload-box" id="box_g_nid_front">
                                        <input type="file" name="g_nid_front" accept="image/*" required onchange="updateFileCard(this, 'box_g_nid_front', 'g_nid_front_name')">
                                        <span class="reg-upload-icon">🪪</span>
                                        <span class="reg-upload-title" id="g_nid_front_name"><?php echo $r['upload_idle']; ?></span>
                                        <span class="reg-upload-sub"><?php echo $r['upload_hint']; ?></span>
                                    </label>
                                </div>

                                <div>
                                    <label class="reg-upload-label"><?php echo $r['lbl_g_back']; ?></label>
                                    <label class="reg-upload-box" id="box_g_nid_back">
                                        <input type="file" name="g_nid_back" accept="image/*" required onchange="updateFileCard(this, 'box_g_nid_back', 'g_nid_back_name')">
                                        <span class="reg-upload-icon">📄</span>
                                        <span class="reg-upload-title" id="g_nid_back_name"><?php echo $r['upload_idle']; ?></span>
                                        <span class="reg-upload-sub"><?php echo $r['upload_hint']; ?></span>
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="reg-submit-wrap">
                            <button type="submit" class="reg-submit-btn"><?php echo $r['btn_submit']; ?></button>
                            <p class="reg-login-text">
                                <?php echo $r['has_account']; ?>
                                <a href="<?php echo SITE_URL; ?>login.php"><?php echo $r['login_link']; ?></a>
                            </p>
                        </div>
                    </form>
                </div>
            </section>
        </div>
    </div>
</div>

<script>
    function updateFileCard(input, boxId, textId) {
        const box = document.getElementById(boxId);
        const text = document.getElementById(textId);

        if (input.files && input.files.length > 0) {
            text.textContent = '✓ ' + input.files[0].name;
            box.classList.add('is-selected');
        } else {
            text.textContent = '<?php echo addslashes($r['upload_idle']); ?>';
            box.classList.remove('is-selected');
        }
    }
</script>

<?php require_once 'footer.php'; ?>