<?php
require_once 'config.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$error = "";
$success = "";

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

    $author_name = trim($_POST['author_name']);
    $email = trim($_POST['email']);
	$phone = trim($_POST['phone']);
	$specialties = $_POST['specialty'] ?? [];
	$specialty = implode(', ', $specialties);
	$category = trim($_POST['category']);

    // File Upload
    $pdf_file = $_FILES['pdf_file']['name'];
    $tmp_name = $_FILES['pdf_file']['tmp_name'];
	
	// Check if email already exists
	$checkStmt = $pdo->prepare("SELECT id FROM papers WHERE email = ? AND status != 'Revision'");
	$checkStmt->execute([$email]);

    if (empty($author_name) || empty($email) || empty($pdf_file) || empty($phone) || empty($specialty) || empty($category)) {

        $error = "All fields are required.";

    } elseif (!preg_match("/^[a-zA-Z\s]+$/", $author_name)) {

    $error = "Author name must contain only letters and spaces.";

	}elseif (!preg_match("/^\d{11}$/", $phone)) {

    $error = "Phone number must contain 11 digits.";
	}
	elseif (!in_array(strtolower(pathinfo($pdf_file, PATHINFO_EXTENSION)), ['pdf', 'doc', 'docx'])) {

    $error = "Only PDF, DOC, and DOCX files are allowed.";

    } elseif ($_FILES['pdf_file']['size'] > 1024 * 1024) {
    $error = "File size must not exceed 1 MB.";
	}
	elseif ($checkStmt->fetch()) {
		
		$error = "This email address has already submitted a paper.";
	}

	else {

        $upload_dir = "/home/fuiedu5/conferenceUploads/abstract/";

        if (!is_dir($upload_dir)) {
            mkdir($upload_dir, 0777, true);
        }

        $file_name = time() . "_" . basename($pdf_file);
        $file_path = $upload_dir . $file_name;
		

        if (move_uploaded_file($tmp_name, $file_path)) {

            $sql = "INSERT INTO papers (author_name, email, phone, specialty, category, pdf_file)
                    VALUES (?, ?, ?, ?, ?, ?)";

            $stmt = $pdo->prepare($sql);

            if ($stmt->execute([$author_name, $email, $phone, $specialty, $category, $file_name, ])) {

    // SEND EMAIL
    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';   // change if needed
        $mail->SMTPAuth = true;
        $mail->Username = 'abstract.arc@fui.edu.pk';   // your email
        $mail->Password = 'xnhipmroathkzmxt';     // Gmail App Password
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->setFrom('abstract.arc@fui.edu.pk', 'ARC Conference');
        $mail->addAddress($email, $author_name);

        $mail->isHTML(true);
        $mail->Subject = "Abstract Submission Received - ARC 2026";

        $mail->Body = "
            <h3>Dear $author_name,</h3>
            <p>Thank you for submitting your abstract to ARC 2026.</p>
            <p><strong>Status:</strong> Successfully Received</p>
            <p>We will review your submission and contact you soon.</p>
            <br>
            <p>Regards,<br>ARC Conference Team</p>
        ";

        $mail->send();

        $success = "Paper submitted successfully! Confirmation email sent to the registered email.";

    } catch (Exception $e) {
        $success = "Paper submitted successfully but email failed: " . $mail->ErrorInfo;
    }
} else {

                $error = "Submission failed. Please try again.";
            }

        } else {

            $error = "Failed to upload PDF file.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>Abstract Submission | ARC 2026</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
	<link href="assets/img/Final logo-02.png" rel="icon">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .top-bar {
            max-width: 1100px;
            margin: 0 auto 20px auto;
            background: white;
            border-radius: 20px;
            padding: 15px 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }
        .logo-area {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .logo-wrapper {
            background: #f8f9fa;
            padding: 6px 12px;
            border-radius: 50px;
            display: flex;
            align-items: center;
        }
        .logo-area img {
            max-height: 45px;
            width: auto;
            object-fit: contain;
            border-radius: 10px;
        }
        .logo-text {
            font-size: 20px;
            font-weight: 700;
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .year-badge {
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            color: white;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 12px;
            font-weight: 600;
        }
        .user-badge {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .user-name {
            color: #1e3c72;
            font-weight: 600;
        }
		.form-group{
			margin-bottom: 10px;
		}
        .logout-btn {
            background: #dc2626;
            color: white;
            padding: 8px 20px;
            border-radius: 12px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .logout-btn:hover {
            background: #b91c1c;
            transform: translateY(-2px);
        }
        .container {
            max-width: 1100px;
            margin: 0 auto;
            background: white;
            border-radius: 32px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
            overflow: hidden;
            animation: slideUp 0.5s ease-out;
        }
        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .form-header {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            padding: 35px 35px;
            color: white;
        }
        .form-header h2 {
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 8px;
        }
        .form-header h2 i {
            margin-right: 10px;
        }
        .registration-line {
            font-size: 18px;
            font-weight: 500;
            margin-top: 12px;
            font-style: italic;
            letter-spacing: 0.5px;
        }
        .registration-line i {
            margin-right: 8px;
        }
        .event-date {
            display: inline-block;
            background: rgba(255,255,255,0.2);
            padding: 5px 15px;
            border-radius: 30px;
            font-size: 13px;
            margin-top: 12px;
        }
        .form-body {
            padding: 40px;
        }
        .checkbox-list label {
    display: flex;
    align-items: center;   /* this fixes vertical alignment */
    gap: 8px;              /* space between checkbox and text */
    margin-bottom: 8px;
    cursor: pointer;
    font-size: 14px;
}

.checkbox-list input[type="checkbox"] {
    margin: 0;             /* remove default weird spacing */
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}
        /* PERFECTLY ALIGNED GRID SYSTEM */
        .form-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 25px 30px;
        }
        label .required {
    color: #dc2626;
    margin-left: 3px;
		}
        .input-group {
            display: flex;
            flex-direction: column;
        }
        
        .input-group.full-width {
            grid-column: span 2;
        }
        
        label {
            display: block;
            margin-bottom: 3px;
            font-weight: 600;
            color: #1e3c72;
            font-size: 14px;
        }
        
		.field-hint {
    font-size: 10px;
    color: #64748b;
    margin-top: 5px;
    display: flex;
    align-items: center;
    gap: 5px;
}

        label i {
            margin-right: 8px;
            color: #2a5298;
            width: 18px;
        }
        
        input, select, textarea {
            width: 100%;
            padding: 14px 16px;
            border: 2px solid #e5e7eb;
            border-radius: 14px;
            font-size: 14px;
            font-family: 'Inter', sans-serif;
            transition: all 0.3s;
            background: #f9fafb;
        }
        
        input:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #2a5298;
            background: white;
            box-shadow: 0 0 0 4px rgba(42, 82, 152, 0.1);
        }
        
        textarea {
            resize: vertical;
            min-height: 100px;
        }
        
        /* Country input with datalist styling */
        .country-input {
            position: relative;
        }
        
        .country-icon {
            position: absolute;
            right: 16px;
            top: 45px;
            color: #9ca3af;
            pointer-events: none;
        }
        
        .btn-submit {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            color: white;
            border: none;
            border-radius: 16px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px -5px rgba(30, 60, 114, 0.4);
        }
        
        .contact-info {
    text-align: center;
    font-size: 18px;
    margin: 20px 0;
    width: 100%;
    color: #fff !important;
}

.contact-info a {
    color: #022c5e;
    text-decoration: none;
    font-weight: 600;
}
.checkbox-list {
    border: 1px solid #ddd;
    border-radius: 5px;
    padding: 10px;
    max-height: 250px;
    overflow-y: auto;
    background: #fff;
}

.checkbox-list label {
    display: block;
    margin-bottom: 8px;
    cursor: pointer;
    font-size: 14px;
}

.checkbox-list input[type="checkbox"] {
    margin-right: 8px;
}

.contact-info a:hover {
    text-decoration: underline;
}
        .alert {
            padding: 14px 16px;
            border-radius: 16px;
            margin-bottom: 25px;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .alert-error {
            background: #fee2e2;
            color: #dc2626;
            border-left: 4px solid #dc2626;
        }
        
        .alert-success {
            background: #dcfce7;
            color: #16a34a;
            border-left: 4px solid #16a34a;
        }
        
        .info-card {
            background: linear-gradient(135deg, #e6f0ff 0%, #f0f6ff 100%);
            padding: 16px 20px;
            border-radius: 16px;
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 14px;
            color: #1e3c72;
            border: 1px solid rgba(30, 60, 114, 0.1);
        }
        
        /* RESPONSIVE DESIGN */
        @media (max-width: 768px) {
            .form-grid {
                grid-template-columns: 1fr;
                gap: 20px;
            }
            
            .input-group.full-width {
                grid-column: span 1;
            }
            
            .form-body {
                padding: 25px;
            }
            
            .top-bar {
                padding: 12px 20px;
            }
            
            .logo-area img {
                max-height: 35px;
            }
            
            .logo-text {
                font-size: 14px;
            }
            
            .form-header {
                padding: 25px 25px;
            }
            
            .form-header h2 {
                font-size: 22px;
            }
            .registration-line {
                font-size: 14px;
            }
        }
        
        @media (max-width: 480px) {
            .top-bar {
                flex-direction: column;
                text-align: center;
            }
            
            .user-badge {
                width: 100%;
                justify-content: center;
            }
            
            .logo-area {
                width: 100%;
                justify-content: center;
            }
        }
		.download-template{
    text-align:center;
    margin:20px 0 30px;
}

.download-template a{
    display:inline-block;
    background:#ff9800;
    color:#fff;
    padding:12px 25px;
    border-radius:8px;
    text-decoration:none;
    font-weight:700;
    font-size:16px;
    box-shadow:0 4px 10px rgba(0,0,0,0.15);
    transition:all 0.3s ease;
}

.download-template a:hover{
    background:#e68900;
    transform:translateY(-2px);
}
    </style>
</head>
<body>
    <div class="top-bar">
        <div class="logo-area">
            <div class="logo-wrapper">
                <img src="Final logo-02.png" alt="FUI Logo" onerror="this.src='https://via.placeholder.com/120x40?text=FUCP'">
            </div>
            <span class="logo-text">Allied Health Sciences and Rehabilitation Conference - ARC</span>
            <span class="year-badge">2026</span>
        </div>
    </div>

    <div class="container">
        <div class="form-header">
            <h2><i class="fas fa-clipboard-list"></i> Abstract Submission</h2>
            <div class="registration-line">
             
            </div>
          
        </div>
        <div class="form-body">
            <?php if($error): ?>
                <div class="alert alert-error">
                    <i class="fas fa-exclamation-circle"></i>
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>
            <?php if($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i>
                    <?php echo htmlspecialchars($success); ?>
                </div>
            <?php endif; ?>
 
			<div class="download-template">
    <a href="guidelines.docx" target="_blank">
        📄 Abstract Guidelines
    </a>
</div>
 
			<div class="download-template">
    <a href="Abstractsubmissiontemplate.docx" target="_blank">
        📄 Download Abstract Template
    </a>
</div>

				<form method="POST" enctype="multipart/form-data">

					<div class="form-group">
						<label>Author Name<span class="required">*</span></label>
						<input type="text" id="author_name" name="author_name" class="form-control" placeholder="Enter your full Name" required>
						<span id="name_msg" style="font-size: 12px;"></span>
					    <div class="field-hint">
                            <i class="fas fa-info-circle"></i> Only Letters and Spaces allow
                        </div>
					</div>
					
					<br>
					<div class="form-group">
						<label>Email Address<span class="required">*</span></label>
						<input type="email" name="email"  id="email" class="form-control" placeholder="Enter your Valid Email Address" required>
						 <span id="email_msg" style="font-size: 12px;"></span>
						<div class="field-hint">
                            <i class="fas fa-info-circle"></i> (e.g example@gmail.com)
                        </div>
					</div>
					
					<br>
					<div class="form-group">
						<label>Contact Number<span class="required">*</span></label>
						<input type="tel" name="phone" class="form-control" id="phone" placeholder="Enter your Phone Number" maxlength="11" required>
						<span id="phone_msg" style="font-size: 12px;"></span>
						<div class="field-hint">
                            <i class="fas fa-info-circle"></i> 11 digits (e.g., 03001111111)
                        </div>
					</div>
					
					<br>
				<div class="form-group">
    <label>Specialty / Discipline <span class="required">*</span></label>

    <div class="checkbox-list">
        <label><input type="checkbox" name="specialty[]" value="Physical therapists"> Physical therapists</label>

        <label><input type="checkbox" name="specialty[]" value="Speech & Language Pathologists"> Speech & Language Pathologists</label>

        <label><input type="checkbox" name="specialty[]" value="Medical Laboratory Technologist"> Medical Laboratory Technologist</label>

        <label><input type="checkbox" name="specialty[]" value="Medical Imaging Technologist"> Medical Imaging Technologist</label>

        <label><input type="checkbox" name="specialty[]" value="Nutritionist and Dieticians"> Nutritionist and Dieticians</label>

        <label><input type="checkbox" name="specialty[]" value="Optometrist"> Optometrist</label>

        <label><input type="checkbox" name="specialty[]" value="Dental Technologist"> Dental Technologist</label>

        <label><input type="checkbox" name="specialty[]" value="Operation theater technologist"> Operation theater technologist</label>

        <label><input type="checkbox" name="specialty[]" value="Rehabilitation and Allied Health Professionals"> Rehabilitation and Allied Health Professionals</label>

        <label><input type="checkbox" name="specialty[]" value="Health Care Professionals"> Health Care Professionals</label>

        <label><input type="checkbox" name="specialty[]" value="Undergraduate and post graduate students of all fields"> Undergraduate and post graduate students of all fields</label>

        <label><input type="checkbox" name="specialty[]" value="Academicians, clinicians, Hospital Administrators, Public Health"> Academicians, clinicians, Hospital Administrators, Public Health</label>

        <label><input type="checkbox" name="specialty[]" value="Professional, Industry Experts."> Professional, Industry Experts.</label>
    </div>
</div>
					
					<br>
					<div class="form-group">
						<label for="category">Abstract Category Selection<span class="required">*</span></label>
						<select name="category" id="category" required>
							<option value="">Select one option</option>
							<option value="Oral Presentation">Oral Presentation</option>
							<option value="Poster Presentation">Poster Presentation</option>
						</select>
					</div>

					<br>
					<div class="form-group">
						<label>Upload File<span class="required">*</span></label>
						<input type="file" name="pdf_file" accept=".pdf,.doc,.docx" class="form-control" required>
						<div class="field-hint">
                            <i class="fas fa-info-circle"></i> Only pdf, doc, docx are allowed to upload
                        </div>
					</div>
					<br>
					
					<button type="submit" class="btn-submit">
									<i class="fas fa-paper-plane"></i>
									Submit Paper
					</button>
				</form>
        </div>
    </div>
<div class="contact-info">
    For further information, please contact at
    <a href="mailto:abstract.arc@fui.edu.pk">abstract.arc@fui.edu.pk</a>
</div>
<script>
// Phone Validation
const phoneInput = document.getElementById('phone');
const phoneMsg = document.getElementById('phone_msg');

phoneInput.addEventListener('input', function () {

    let phone = this.value.replace(/\D/g, '');
    phone = phone.substring(0, 11);

    this.value = phone;

    if (phone === '') {
        phoneMsg.textContent = "✖ Phone number is required";
        phoneMsg.style.color = "red";
        this.style.borderColor = "red";
    }
    else if (!/^03\d{9}$/.test(phone)) {
        phoneMsg.textContent = "✖ Enter a valid 11-digit number (e.g. 03001111111)";
        phoneMsg.style.color = "red";
        this.style.borderColor = "red";
    }
    else {
        phoneMsg.textContent = "✔ Valid";
        phoneMsg.style.color = "green";
        this.style.borderColor = "green";
    }
});

// Email Validation
const emailInput = document.getElementById('email');
const emailMsg = document.getElementById('email_msg');

emailInput.addEventListener('input', function () {

    const email = this.value.trim();

    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    if (email === '') {
        emailMsg.textContent = "✖ Email is required";
        emailMsg.style.color = "red";
        this.style.borderColor = "red";
    }
    else if (!emailPattern.test(email)) {
        emailMsg.textContent = "✖ Invalid email address";
        emailMsg.style.color = "red";
        this.style.borderColor = "red";
    }
    else {
        emailMsg.textContent = "✔ Valid";
        emailMsg.style.color = "green";
        this.style.borderColor = "green";
    }
});

const authorInput = document.getElementById('author_name');
const msg = document.getElementById('name_msg');

authorInput.addEventListener('input', function() {

    const name = this.value.trim();

    if (name === '') {
        msg.textContent = "✖ Name is required";
        msg.style.color = "red";
        this.style.borderColor = "red";
    }
    else if (!/^[A-Za-z\s-]+$/.test(name)) {
        msg.textContent = "✖ Only letters, spaces, and hyphens (-) allowed";
        msg.style.color = "red";
        this.style.borderColor = "red";
    }
    else {
        msg.textContent = "✔ Valid";
        msg.style.color = "green";
        this.style.borderColor = "green";
    }
});
</script>
</body>
</html>