<?php
require_once 'config.php';

if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: adminlogin.php");
    exit();
}

$username = $_SESSION['admin_username'];
if (isset($_SESSION['success_msg'])) {
    $msg = $_SESSION['success_msg'];
    unset($_SESSION['success_msg']);
    $showMsg = true;
} else {
    $showMsg = false;
}

$errors = [];
$success = "";

$event_name = "";
$full_name = "";
$location = "";
$institute = "";
$id_card_number = "";
$country_state = "";
$cell_number = "";
$whatsapp_number = "";
$email = "";
$address = "";
$ahpc_number = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    function clean($data) {
        return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
    }

    $event_name      = clean($_POST['event_name'] ?? '');
    $full_name       = clean($_POST['full_name'] ?? '');
    $location        = clean($_POST['location'] ?? '');
    $institute       = clean($_POST['institute'] ?? '');
    $id_card_number  = clean($_POST['id_card_number'] ?? '');
    $country_state   = clean($_POST['country_state'] ?? '');
    $cell_number     = clean($_POST['cell_number'] ?? '');
    $whatsapp_number = clean($_POST['whatsapp_number'] ?? '');
    $email           = clean($_POST['email'] ?? '');
    $address         = clean($_POST['address'] ?? '');
    $ahpc_number     = clean($_POST['ahpc_number'] ?? '');

    // Event
    if (empty($event_name)) {
        $errors['event_name'] = "Event Name is required";
    }

    // Name
    if (empty($full_name)) {
        $errors['full_name'] = "Name is required";
    } elseif (!preg_match("/^[a-zA-Z\s.'-]{3,100}$/", $full_name)) {
        $errors['full_name'] = "Invalid name";
    }

    // Location
    if (empty($location)) {
        $errors['location'] = "Location is required";
    }

    // Institute
    if (empty($institute)) {
        $errors['institute'] = "Institute is required";
    }

    // ID Card
if (empty($id_card_number)) {

    $errors['id_card_number'] = "This field is required";

} else {

    if ($country_state == 'Pakistan') {

        // CNIC Format: 42101-1234567-1
        if (!preg_match('/^\d{5}-\d{7}-\d{1}$/', $id_card_number)) {

            $errors['id_card_number'] =
                "CNIC format should be 42101-1234567-1";
        }

    } else {

        // Passport validation
        if (!preg_match('/^[A-Za-z0-9]{5,20}$/', $id_card_number)) {

            $errors['id_card_number'] =
                "Invalid Passport Number";
        }
    }
}

    // Country / State
    if (empty($country_state)) {
        $errors['country_state'] = "Country/State is required";
    }

    // Cell Number
    if (empty($cell_number)) {
        $errors['cell_number'] = "Cell Number is required";
    } elseif (!preg_match('/^[0-9]{10,15}$/', $cell_number)) {
        $errors['cell_number'] = "Invalid Cell Number";
    }

    // WhatsApp
    if (empty($whatsapp_number)) {
        $errors['whatsapp_number'] = "WhatsApp Number is required";
    } elseif (!preg_match('/^[0-9]{10,15}$/', $whatsapp_number)) {
        $errors['whatsapp_number'] = "Invalid WhatsApp Number";
    }

    // Email
    if (empty($email)) {
        $errors['email'] = "Email is required";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = "Invalid Email Address";
    }

    // Address
    if (empty($address)) {
        $errors['address'] = "Address is required";
    } elseif (strlen($address) < 10) {
        $errors['address'] = "Address too short";
    }


    if (empty($errors)) {

        $stmt = $pdo->prepare("
            INSERT INTO  sponsorship
            (
                event_name,
                full_name,
                location,
                institute,
                id_card_number,
                country_state,
                cell_number,
                whatsapp_number,
                email,
                address,
                ahpc_number
            )
            VALUES
            (
                :event_name,
                :full_name,
                :location,
                :institute,
                :id_card_number,
                :country_state,
                :cell_number,
                :whatsapp_number,
                :email,
                :address,
                :ahpc_number
            )
        ");

        $stmt->execute([
            ':event_name'      => $event_name,
            ':full_name'       => $full_name,
            ':location'        => $location,
            ':institute'       => $institute,
            ':id_card_number'  => $id_card_number,
            ':country_state'   => $country_state,
            ':cell_number'     => $cell_number,
            ':whatsapp_number' => $whatsapp_number,
            ':email'           => $email,
            ':address'         => $address,
            ':ahpc_number'     => $ahpc_number
        ]);

        $success = "Registration submitted successfully.";

        $event_name = "";
        $full_name = "";
        $location = "";
        $institute = "";
        $id_card_number = "";
        $country_state = "";
        $cell_number = "";
        $whatsapp_number = "";
        $email = "";
        $address = "";
        $ahpc_number = "";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sponsorship Registration Form</title>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">

<style>
*{
    margin:0;
    padding:0;
    box-sizing:border-box;
}

body{
    font-family:'Segoe UI',sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height:100vh;
    padding:40px 20px;
}
.form-header {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    padding: 22px 30px;
    color: white;
}
.header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:25px;
}

.header h2{
    color:#fff;
    font-size:28px;
}
.field-hint {
    font-size: 10px;
    color: #64748b;
    margin-top: 5px;
    display: flex;
    align-items: center;
    gap: 5px;
}
h2{
    text-align:center;
    color:#fff;
    margin-bottom:25px;
    font-size:32px;
    font-weight:700;
}

form{
padding: 35px;
}

label{
    display:block;
    margin-top:25px;
    margin-bottom:0px;
    color:#374151;
    font-weight:600;
}

input,
select,
textarea{
    width:100%;
    padding:14px 16px;
    border:1px solid #d1d5db;
    border-radius:12px;
    font-size:15px;
    transition:all .3s ease;
    background:#f9fafb;
}

input:focus,
select:focus,
textarea:focus{
    outline:none;
    border-color:#4f46e5;
    background:#fff;
    box-shadow:0 0 0 4px rgba(79,70,229,.15);
}

textarea{
    min-height:120px;
    resize:vertical;
}

.error{
    color:#dc2626;
    margin-top:5px;
    font-size:13px;
}

.success{
    max-width:900px;
    margin:0 auto 20px;
    background:#ecfdf5;
    border-left:5px solid #10b981;
    color:#065f46;
    padding:15px;
    border-radius:10px;
    font-weight:600;
}

.validation-msg{
    margin-top:5px;
    font-size:13px;
}

.valid{
    color:#10b981;
    font-weight:600;
}

.invalid{
    color:#dc2626;
    font-weight:600;
}

button{
    width:100%;
    border:none;
    margin-top:25px;
    padding:16px;
    border-radius:12px;
    background:linear-gradient(
        135deg,
        #4f46e5,
        #7c3aed
    );
    color:#fff;
    font-size:16px;
    font-weight:600;
    cursor:pointer;
    transition:.3s;
}

button:hover{
    transform:translateY(-2px);
    box-shadow:0 10px 20px rgba(79,70,229,.25);
}

button:active{
    transform:translateY(0);
}

#identityField{
    margin-top:15px;
    padding:15px;
    background:#f8fafc;
    border:1px solid #e5e7eb;
    border-radius:12px;
}

select{
    appearance:none;
    cursor:pointer;
}

@media(max-width:768px){

    body{
        padding:20px 10px;
    }

    form{
        padding:20px;
    }

    h2{
        font-size:26px;
    }

}
input,textarea{
    width:100%;
    padding:10px;
    margin-top:5px;
}
label{
    font-weight:bold;
}
.error{
    color:red;
    font-size:14px;
}
.success{
    color:green;
    margin-bottom:15px;
}
button{
    padding:12px 20px;
    cursor:pointer;
}
.validation-msg{
    font-size:14px;
    margin-top:3px;
}

.valid{
    color:green;
}

.invalid{
    color:red;
}
.container {
    max-width: 1000px;
    margin: 0 auto;
    background: white;
    border-radius: 24px;
    box-shadow: 0 20px 40px -12px rgba(0, 0, 0, 0.2);
    overflow: hidden;
}
.topbar {
    background: #fff; /* Dark background */
    color: #2a5298;
    padding: 15px 25px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    box-sizing: border-box;
}

.topbar h2 {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
}

.logout-btn {
    background: #dc3545;
    color: #fff;
    text-decoration: none;
    padding: 10px 18px;
    border-radius: 5px;
    transition: 0.3s;
}

.logout-btn:hover {
    background: #bb2d3b;
}

/* Mobile Responsive */
@media (max-width: 768px) {
    .topbar {
        flex-direction: column;
        gap: 10px;
        text-align: center;
    }

    .topbar h2 {
        font-size: 20px;
    }
}
</style>

</head>
<body>

<div class="topbar">
    <h2 style="color: #2c2cc0;">Admin Control Panel</h2>

    <a href="adminsponlogout.php" class="logout-btn">
        Logout (<?= htmlspecialchars($username) ?>)
    </a>
</div>
<br>
    <div class="container">
        <div class="form-header">
            <h2><i class="fas fa-clipboard-list"></i> Sponsorship Registration</h2>
        </div>

<?php if($success): ?>
<div class="success"><?= $success ?></div>
<?php endif; ?>

<form method="post" id="registrationForm">

<label>Event Name <span class="required" style="color: red;">*</span></label>
<select name="event_name" id="event_name">
    <option value="">Select Event</option>
    <option value="Conference" <?= ($event_name == 'Conference') ? 'selected' : '' ?>>Conference</option>
    <option value="Workshop" <?= ($event_name == 'Workshop') ? 'selected' : '' ?>>Workshop</option>
</select>
<div class="error"><?= $errors['event_name'] ?? '' ?></div>
 <div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> Select Event Type
 </div>

<label>Name <span class="required" style="color: red;">*</span></label>
<input type="text" name="full_name" id="full_name" value="<?= $full_name ?>">
<div id="full_name_msg" class="validation-msg"></div>
<div class="error"><?= $errors['full_name'] ?? '' ?></div>
 <div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> Letters only, 2-50 characters
 </div>

<label>Location <span class="required" style="color: red;">*</span></label>
<input type="text" name="location" id="location" value="<?= $location ?>">
<div class="error"><?= $errors['location'] ?? '' ?></div>
 <div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> Letters Spaces and Symbols
 </div>

<label>Institute <span class="required" style="color: red;">*</span></label>
<input type="text" name="institute" id="institute" value="<?= $institute ?>">
<div class="error"><?= $errors['institute'] ?? '' ?></div>
 <div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> Letters Spaces and Symbols
 </div>

<label>Country <span class="required" style="color: red;">*</span></label>
<select name="country_state" id="country_state">
    <option value="">Select Country</option>

    <option value="Pakistan" <?= ($country_state == 'Pakistan') ? 'selected' : '' ?>>Pakistan</option>
    <option value="Saudi Arabia" <?= ($country_state == 'Saudi Arabia') ? 'selected' : '' ?>>Saudi Arabia</option>
    <option value="United Arab Emirates" <?= ($country_state == 'United Arab Emirates') ? 'selected' : '' ?>>United Arab Emirates</option>
    <option value="Qatar" <?= ($country_state == 'Qatar') ? 'selected' : '' ?>>Qatar</option>
    <option value="Oman" <?= ($country_state == 'Oman') ? 'selected' : '' ?>>Oman</option>
    <option value="Kuwait" <?= ($country_state == 'Kuwait') ? 'selected' : '' ?>>Kuwait</option>
    <option value="Bahrain" <?= ($country_state == 'Bahrain') ? 'selected' : '' ?>>Bahrain</option>
    <option value="United Kingdom" <?= ($country_state == 'United Kingdom') ? 'selected' : '' ?>>United Kingdom</option>
    <option value="United States" <?= ($country_state == 'United States') ? 'selected' : '' ?>>United States</option>
    <option value="Canada" <?= ($country_state == 'Canada') ? 'selected' : '' ?>>Canada</option>
    <option value="Australia" <?= ($country_state == 'Australia') ? 'selected' : '' ?>>Australia</option>
    <option value="Other" <?= ($country_state == 'Other') ? 'selected' : '' ?>>Other</option>
</select>
<div class="error"><?= $errors['country_state'] ?? '' ?></div>
 <div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> Select Your Country
 </div>

<div id="identityField" style="display:none;">
    <label id="identityLabel">
        <?= ($country_state == 'Pakistan') ? 'CNIC Number' : 'Passport Number' ?>
    </label>

    <input
        type="text"
        name="id_card_number"
        id="id_card_number"
        value="<?= htmlspecialchars($id_card_number) ?>"
    >

    <div class="error"><?= $errors['id_card_number'] ?? '' ?></div>
</div>

<label>Cell Number <span class="required" style="color: red;">*</span></label>
<input type="text" name="cell_number" id="cell_number">
<div id="cell_number_msg" class="validation-msg"></div>
<div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> 10-15 digits (e.g., 030012345678)
</div>

<label>WhatsApp Number <span class="required" style="color: red;">*</span></label>
<input type="text" name="whatsapp_number" id="whatsapp_number">
<div id="whatsapp_number_msg" class="validation-msg"></div>
<div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> 10-15 digits (e.g., 030012345678)
</div>

<label>Email Address <span class="required" style="color: red;">*</span></label>
<input type="email" name="email" id="email">
<div id="email_msg" class="validation-msg"></div>
<div class="field-hint">
<i class="fas fa-lock"></i> example@gmail.com
</div>

<label>Address <span class="required" style="color: red;">*</span></label>
<textarea name="address" id="address"><?= $address ?></textarea>
<div class="error"><?= $errors['address'] ?? '' ?></div>
<div class="field-hint" id="hint_cell_no">
	<i class="fas fa-info-circle"></i> Complete Adress
</div>

<label>AHPC Number</label>
<input type="text" name="ahpc_number" id="ahpc_number" value="<?= $ahpc_number ?>">
<div class="error"><?= $errors['ahpc_number'] ?? '' ?></div>
  <div class="field-hint" id="hint_ahpc_reg_number">
		<i class="fas fa-info-circle"></i> Enter your AHPC (Allied Health Professional Council) registration number
	</div>

<br><br>

<button type="submit">Submit Registration</button>

</form>

<script>

function showValidation(inputId, msgId, condition, errorText, validText) {

    let input = document.getElementById(inputId);
    let msg = document.getElementById(msgId);

    if(input.value.trim() === ''){
        msg.innerHTML = errorText;
        msg.className = 'validation-msg invalid';
        input.style.borderColor = 'red';
    }
    else if(!condition){
        msg.innerHTML = errorText;
        msg.className = 'validation-msg invalid';
        input.style.borderColor = 'red';
    }
    else{
        msg.innerHTML = validText;
        msg.className = 'validation-msg valid';
        input.style.borderColor = 'green';
    }
}

document.getElementById('full_name').addEventListener('input', function () {

    let msg = document.getElementById('full_name_msg');

    if(this.value.trim() === ''){
        msg.innerHTML = 'Name is required';
        msg.className = 'validation-msg invalid';
        this.style.borderColor = 'red';
    }
    else if(!/^[a-zA-Z\s.'-]{3,100}$/.test(this.value)){
        msg.innerHTML = 'Invalid name';
        msg.className = 'validation-msg invalid';
        this.style.borderColor = 'red';
    }
    else{
        msg.innerHTML = '✓ Valid Name';
        msg.className = 'validation-msg valid';
        this.style.borderColor = 'green';
    }
});

document.getElementById('full_name').addEventListener('input', function(){

    showValidation(
        'full_name',
        'full_name_msg',
        /^[a-zA-Z\s.'-]{3,100}$/.test(this.value),
        'Invalid Name',
        '✓ Valid Name'
    );

});

document.getElementById('email').addEventListener('input', function(){

    showValidation(
        'email',
        'email_msg',
        /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.value),
        'Invalid Email',
        '✓ Valid Email'
    );

});

document.getElementById('cell_number').addEventListener('input', function(){

    showValidation(
        'cell_number',
        'cell_number_msg',
        /^[0-9]{10,15}$/.test(this.value),
        'Enter 10-15 digits',
        '✓ Valid Number'
    );

});

document.getElementById('whatsapp_number').addEventListener('input', function(){

    showValidation(
        'whatsapp_number',
        'whatsapp_number_msg',
        /^[0-9]{10,15}$/.test(this.value),
        'Enter 10-15 digits',
        '✓ Valid Number'
    );

});


function toggleIdentityField() {

    let country = document.getElementById('country_state').value;

    let field = document.getElementById('identityField');
    let label = document.getElementById('identityLabel');
    let input = document.getElementById('id_card_number');

    // Hide if no country selected
    if(country === '') {

        field.style.display = 'none';
        input.value = '';
        return;
    }

    // Show field
    field.style.display = 'block';

    if(country === 'Pakistan') {

        label.innerText = 'CNIC Number';
        input.placeholder = '42101-1234567-1';

    } else {

        label.innerText = 'Passport Number';
        input.placeholder = 'Enter Passport Number';

    }
}

document.getElementById('country_state')
.addEventListener('change', toggleIdentityField);

window.onload = toggleIdentityField;

</script>
</div>
</body>
</html>