<?php
// Include database configuration
include('config/database.php');

$message = '';
$message_type = '';

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Sanitize and validate input
    $full_name = trim($_POST['full_name']);
    $programme_of_study = trim($_POST['programme_of_study']);
    $level = trim($_POST['level']);
    $contact = trim($_POST['contact']);
    $email = trim($_POST['email']);
    $business_idea_title = trim($_POST['business_idea_title']);
    $brief_description = trim($_POST['brief_description']);
    $target_market = trim($_POST['target_market']);
    $competitors = trim($_POST['competitors']);
    $market_trends = trim($_POST['market_trends']);
    $initial_capital = trim($_POST['initial_capital']);
    $required_resources = trim($_POST['required_resources']);
    $skills_needed = trim($_POST['skills_needed']);
    $potential_partnerships = trim($_POST['potential_partnerships']);
    $social_impact = trim($_POST['social_impact']);
    $technological_elements = trim($_POST['technological_elements']);
    
    // Team member fields
    $member1_name = trim($_POST['member1_name'] ?? '');
    $member1_programme = trim($_POST['member1_programme'] ?? '');
    $member1_level = trim($_POST['member1_level'] ?? '');
    $member1_email = trim($_POST['member1_email'] ?? '');
    
    $member2_name = trim($_POST['member2_name'] ?? '');
    $member2_programme = trim($_POST['member2_programme'] ?? '');
    $member2_level = trim($_POST['member2_level'] ?? '');
    $member2_email = trim($_POST['member2_email'] ?? '');

    // Basic validation
    $errors = [];
    if (empty($full_name)) $errors[] = "Full name is required";
    if (empty($programme_of_study)) $errors[] = "Programme of study is required";
    if (empty($level)) $errors[] = "Level is required";
    if (empty($contact)) $errors[] = "Contact number is required";
    if (empty($email)) $errors[] = "Email is required";
    if (empty($business_idea_title)) $errors[] = "Business idea title is required";
    if (empty($brief_description)) $errors[] = "Brief description is required";

    if (empty($errors)) {
        // Insert into database
        $sql = "INSERT INTO proposals (full_name, programme_of_study, level, contact, email, 
                business_idea_title, brief_description, target_market, competitors, market_trends, 
                initial_capital, required_resources, skills_needed, potential_partnerships, 
                social_impact, technological_elements, member1_name, member1_programme, member1_level, member1_email,
                member2_name, member2_programme, member2_level, member2_email) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        if ($stmt = $conn->prepare($sql)) {
            $stmt->bind_param("ssssssssssssssssssssssss", 
                $full_name, $programme_of_study, $level, $contact, $email,
                $business_idea_title, $brief_description, $target_market, $competitors, $market_trends,
                $initial_capital, $required_resources, $skills_needed, $potential_partnerships,
                $social_impact, $technological_elements, $member1_name, $member1_programme, $member1_level, $member1_email,
                $member2_name, $member2_programme, $member2_level, $member2_email);
            
            if ($stmt->execute()) {
                $message = "Your proposal has been submitted successfully! We will review it and get back to you soon.";
                $message_type = "success";
                // Clear form data after successful submission
                $_POST = array();
            } else {
                $message = "Error submitting proposal: " . $stmt->error;
                $message_type = "danger";
            }
            $stmt->close();
        } else {
            $message = "Database error: Unable to prepare statement.";
            $message_type = "danger";
        }
    } else {
        $message = "Please correct the following errors: " . implode(", ", $errors);
        $message_type = "danger";
    }
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Submit Your Proposal - Business Pitch Challenger</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" href="assets/css/main.css?v=1.2">
    <link rel="stylesheet" href="assets/css/components.css?v=1.2">
    <style>
        body {
            background-image: url('assets/images/hero/1.JPG');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            position: relative;
        }
        
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.7);
            z-index: -1;
        }
        
        .form-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 0;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            margin: 2rem auto;
            max-width: 800px;
            margin-top: 100px;
        }
        
        .form-header {
            background: #2c2b7c;
            color: white;
            padding: 1.5rem;
            border-radius: 0;
            text-align: center;
        }
        
        .form-body {
            padding: 1.5rem;
        }
        
        .form-section {
            background: #f8f9fa;
            border-radius: 0;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            border-left: 4px solid #2c2b7c;
        }
        
        .form-section h4 {
            color: #2c2b7c;
            border-bottom: 2px solid #2c2b7c;
            padding-bottom: 0.5rem;
            margin-bottom: 1rem;
        }
        
        .btn-submit {
            background: #2c2b7c;
            border: none;
            border-radius: 0;
            padding: 8px 24px;
            font-size: 0.95rem;
            font-weight: 600;
            color: white;
            transition: all 0.3s ease;
        }
        
        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(44, 43, 124, 0.4);
            color: white;
            background: #2c2b7c;
        }
        
        /* Global button styling for consistency */
        .btn-primary {
            background: #2c2b7c !important;
            border-color: #2c2b7c !important;
            color: white !important;
        }
        
        .btn-primary:hover {
            background: #2c2b7c !important;
            border-color: #2c2b7c !important;
            color: white !important;
            box-shadow: 0 4px 15px rgba(44, 43, 124, 0.4) !important;
        }
        
        /* Team member section styling */
        .team-member-section {
            background: #f8f9fa;
            padding: 1.5rem;
            border-radius: 8px;
            border-left: 4px solid #2c2b7c;
        }
        
        .team-member-section h5 {
            color: #2c2b7c;
            margin-bottom: 1rem;
        }
        
        .navbar {
            background: rgba(44, 43, 124, 0.9);
            backdrop-filter: blur(10px);
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 9999;
        }
        
        .navbar-brand {
            font-weight: bold;
            font-size: 1.5rem;
            color: white;
        }
        
        .navbar-brand img {
            height: 50px;
            width: auto;
            margin-right: 15px;
            border-radius: 4px;
            box-shadow: 0 3px 12px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
            filter: brightness(1.1) contrast(1.1);
        }
        
        .navbar-brand img:hover {
            transform: scale(1.05);
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.3);
            filter: brightness(1.2) contrast(1.2);
        }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg">
        <div class="container">
            <a class="navbar-brand" href="index.php">
                <img src="assets/images/logos/logo.png" alt="UEW iHub Logo" class="me-2">
                UEW iHub
            </a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link text-white" href="index.php">
                    <i class="fas fa-home me-1"></i>Back to Home
                </a>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="form-container">
            <div class="form-header">
                <h2><i class="fas fa-paper-plane me-2"></i>Submit Your Business Proposal</h2>
                <p class="mb-0">Business Pitch Challenger 2025 - 2nd Edition</p>
            </div>

            <div class="form-body">
                <?php if (!empty($message)): ?>
                    <div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
                        <?php echo $message; ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                <?php endif; ?>

                <form method="POST" action="">
                    <!-- Personal Information Section -->
                    <div class="form-section">
                        <h4><i class="fas fa-user me-2"></i>Personal Information</h4>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="full_name" class="form-label">Full Name *</label>
                                <input type="text" class="form-control" id="full_name" name="full_name" 
                                       value="<?php echo htmlspecialchars($_POST['full_name'] ?? ''); ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="programme_of_study" class="form-label">Programme of Study *</label>
                                <input type="text" class="form-control" id="programme_of_study" name="programme_of_study" 
                                       value="<?php echo htmlspecialchars($_POST['programme_of_study'] ?? ''); ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="level" class="form-label">Level *</label>
                                <select class="form-control" id="level" name="level" required>
                                    <option value="">Select Level</option>
                                    <option value="100" <?php echo ($_POST['level'] ?? '') == '100' ? 'selected' : ''; ?>>Level 100</option>
                                    <option value="200" <?php echo ($_POST['level'] ?? '') == '200' ? 'selected' : ''; ?>>Level 200</option>
                                    <option value="300" <?php echo ($_POST['level'] ?? '') == '300' ? 'selected' : ''; ?>>Level 300</option>
                                    <option value="400" <?php echo ($_POST['level'] ?? '') == '400' ? 'selected' : ''; ?>>Level 400</option>
                                    <option value="Graduate" <?php echo ($_POST['level'] ?? '') == 'Graduate' ? 'selected' : ''; ?>>Graduate</option>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="contact" class="form-label">Contact Number *</label>
                                <input type="tel" class="form-control" id="contact" name="contact" 
                                       value="<?php echo htmlspecialchars($_POST['contact'] ?? ''); ?>" required>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="email" class="form-label">Email Address *</label>
                                <input type="email" class="form-control" id="email" name="email" 
                                       value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" required>
                            </div>
                        </div>
                    </div>

                    <!-- Business Idea Section -->
                    <div class="form-section">
                        <h4><i class="fas fa-lightbulb me-2"></i>Business Idea</h4>
                        <div class="mb-3">
                            <label for="business_idea_title" class="form-label">Business Idea Title *</label>
                            <input type="text" class="form-control" id="business_idea_title" name="business_idea_title" 
                                   value="<?php echo htmlspecialchars($_POST['business_idea_title'] ?? ''); ?>" required>
                        </div>
                        <div class="mb-3">
                            <label for="brief_description" class="form-label">Brief Description * (Maximum 250 words)</label>
                            <textarea class="form-control" id="brief_description" name="brief_description" rows="4" 
                                      placeholder="Describe your business idea in brief (maximum 250 words)..." 
                                      maxlength="1500" required><?php echo htmlspecialchars($_POST['brief_description'] ?? ''); ?></textarea>
                            <small class="text-muted">Word count: <span id="word-count">0</span>/250</small>
                        </div>
                    </div>

                    <!-- Market Analysis Section -->
                    <div class="form-section">
                        <h4><i class="fas fa-chart-line me-2"></i>Market Analysis</h4>
                        <div class="mb-3">
                            <label for="target_market" class="form-label">Target Market</label>
                            <textarea class="form-control" id="target_market" name="target_market" rows="3" 
                                      placeholder="Describe your target market..."><?php echo htmlspecialchars($_POST['target_market'] ?? ''); ?></textarea>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="competitors" class="form-label">Competitors</label>
                                <textarea class="form-control" id="competitors" name="competitors" rows="3" 
                                          placeholder="Who are your main competitors?"><?php echo htmlspecialchars($_POST['competitors'] ?? ''); ?></textarea>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="market_trends" class="form-label">Market Trends</label>
                                <textarea class="form-control" id="market_trends" name="market_trends" rows="3" 
                                          placeholder="What are the current market trends?"><?php echo htmlspecialchars($_POST['market_trends'] ?? ''); ?></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Implementation Section -->
                    <div class="form-section">
                        <h4><i class="fas fa-cogs me-2"></i>Implementation Plan</h4>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="initial_capital" class="form-label">Initial Capital Required</label>
                                <input type="text" class="form-control" id="initial_capital" name="initial_capital" 
                                       value="<?php echo htmlspecialchars($_POST['initial_capital'] ?? ''); ?>" 
                                       placeholder="e.g., $10,000">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="required_resources" class="form-label">Required Resources</label>
                                <textarea class="form-control" id="required_resources" name="required_resources" rows="3" 
                                          placeholder="What resources do you need?"><?php echo htmlspecialchars($_POST['required_resources'] ?? ''); ?></textarea>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="skills_needed" class="form-label">Skills Needed</label>
                                <textarea class="form-control" id="skills_needed" name="skills_needed" rows="3" 
                                          placeholder="What skills are required?"><?php echo htmlspecialchars($_POST['skills_needed'] ?? ''); ?></textarea>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="potential_partnerships" class="form-label">Potential Partnerships</label>
                                <textarea class="form-control" id="potential_partnerships" name="potential_partnerships" rows="3" 
                                          placeholder="Who could you partner with?"><?php echo htmlspecialchars($_POST['potential_partnerships'] ?? ''); ?></textarea>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="social_impact" class="form-label">Social Impact</label>
                                <textarea class="form-control" id="social_impact" name="social_impact" rows="3" 
                                          placeholder="How will your business impact society?"><?php echo htmlspecialchars($_POST['social_impact'] ?? ''); ?></textarea>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="technological_elements" class="form-label">Innovation </label>
                                <textarea class="form-control" id="technological_elements" name="technological_elements" rows="3" 
                                          placeholder="How innovative is your business idea? Describe what makes it unique and novel..."><?php echo htmlspecialchars($_POST['technological_elements'] ?? ''); ?></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Team Members Section -->
                    <div class="form-section">
                        <h4><i class="fas fa-users me-2"></i>Team Members (Optional - Maximum 2)</h4>
                        <p class="text-muted mb-3">Add details for up to two additional team members (optional)</p>
                        
                        <!-- Team Member 1 -->
                        <div class="team-member-section mb-4">
                            <h5 class="text-primary">Team Member 1</h5>
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="member1_name" class="form-label">Full Name</label>
                                    <input type="text" class="form-control" id="member1_name" name="member1_name" 
                                           value="<?php echo htmlspecialchars($_POST['member1_name'] ?? ''); ?>" 
                                           placeholder="Enter full name">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="member1_programme" class="form-label">Programme of Study</label>
                                    <input type="text" class="form-control" id="member1_programme" name="member1_programme" 
                                           value="<?php echo htmlspecialchars($_POST['member1_programme'] ?? ''); ?>" 
                                           placeholder="e.g., Business Administration">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="member1_level" class="form-label">Level</label>
                                    <select class="form-control" id="member1_level" name="member1_level">
                                        <option value="">Select Level</option>
                                        <option value="100" <?php echo ($_POST['member1_level'] ?? '') == '100' ? 'selected' : ''; ?>>Level 100</option>
                                        <option value="200" <?php echo ($_POST['member1_level'] ?? '') == '200' ? 'selected' : ''; ?>>Level 200</option>
                                        <option value="300" <?php echo ($_POST['member1_level'] ?? '') == '300' ? 'selected' : ''; ?>>Level 300</option>
                                        <option value="400" <?php echo ($_POST['member1_level'] ?? '') == '400' ? 'selected' : ''; ?>>Level 400</option>
                                        <option value="Graduate" <?php echo ($_POST['member1_level'] ?? '') == 'Graduate' ? 'selected' : ''; ?>>Graduate</option>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="member1_email" class="form-label">Email Address</label>
                                    <input type="email" class="form-control" id="member1_email" name="member1_email" 
                                           value="<?php echo htmlspecialchars($_POST['member1_email'] ?? ''); ?>" 
                                           placeholder="Enter email address">
                                </div>
                            </div>
                        </div>

                        <!-- Team Member 2 -->
                        <div class="team-member-section mb-4">
                            <h5 class="text-primary">Team Member 2</h5>
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="member2_name" class="form-label">Full Name</label>
                                    <input type="text" class="form-control" id="member2_name" name="member2_name" 
                                           value="<?php echo htmlspecialchars($_POST['member2_name'] ?? ''); ?>" 
                                           placeholder="Enter full name">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="member2_programme" class="form-label">Programme of Study</label>
                                    <input type="text" class="form-control" id="member2_programme" name="member2_programme" 
                                           value="<?php echo htmlspecialchars($_POST['member2_programme'] ?? ''); ?>" 
                                           placeholder="e.g., Business Administration">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="member2_level" class="form-label">Level</label>
                                    <select class="form-control" id="member2_level" name="member2_level">
                                        <option value="">Select Level</option>
                                        <option value="100" <?php echo ($_POST['member2_level'] ?? '') == '100' ? 'selected' : ''; ?>>Level 100</option>
                                        <option value="200" <?php echo ($_POST['member2_level'] ?? '') == '200' ? 'selected' : ''; ?>>Level 200</option>
                                        <option value="300" <?php echo ($_POST['member2_level'] ?? '') == '300' ? 'selected' : ''; ?>>Level 300</option>
                                        <option value="400" <?php echo ($_POST['member2_level'] ?? '') == '400' ? 'selected' : ''; ?>>Level 400</option>
                                        <option value="Graduate" <?php echo ($_POST['member2_level'] ?? '') == 'Graduate' ? 'selected' : ''; ?>>Graduate</option>
                                    </select>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="member2_email" class="form-label">Email Address</label>
                                    <input type="email" class="form-control" id="member2_email" name="member2_email" 
                                           value="<?php echo htmlspecialchars($_POST['member2_email'] ?? ''); ?>" 
                                           placeholder="Enter email address">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="text-center">
                        <button type="submit" class="btn btn-submit">
                            <i class="fas fa-paper-plane me-2"></i>Submit Proposal
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        // Word count functionality for brief description
        document.addEventListener('DOMContentLoaded', function() {
            const textarea = document.getElementById('brief_description');
            const wordCountSpan = document.getElementById('word-count');
            
            function updateWordCount() {
                const text = textarea.value;
                const words = text.trim().split(/\s+/).filter(word => word.length > 0);
                const wordCount = words.length;
                
                wordCountSpan.textContent = wordCount;
                
                // Change color if over limit
                if (wordCount > 250) {
                    wordCountSpan.style.color = 'red';
                    wordCountSpan.style.fontWeight = 'bold';
                } else {
                    wordCountSpan.style.color = '';
                    wordCountSpan.style.fontWeight = '';
                }
            }
            
            // Update on input
            textarea.addEventListener('input', updateWordCount);
            
            // Initial count
            updateWordCount();
        });
    </script>
</body>
</html>
