<?php 
require_once 'includes/db.php';

$error = '';
$success = '';

// Minimal backend logic for the new fields
if(isset($_POST['register_individual']) || isset($_POST['register_institute'])) {
    $type = isset($_POST['register_individual']) ? 'individual' : 'institute';
    $name = escape($_POST['name'] ?? $_POST['institute_name']);
    $email = escape($_POST['email']);
    $phone = escape($_POST['phone'] ?? '');
    $address = escape($_POST['address'] ?? '');
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    if($password !== $confirm_password) {
        $error = "Passwords do not match.";
    } else {
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        
        // Auto-alter users table for missing columns
        $cols = [];
        $check_cols = $conn->query("SHOW COLUMNS FROM users");
        while($row = $check_cols->fetch_assoc()){
            $cols[] = $row['Field'];
        }
        if(!in_array('type', $cols)) $conn->query("ALTER TABLE users ADD COLUMN type VARCHAR(50) DEFAULT 'individual' AFTER id");
        if(!in_array('designation', $cols)) $conn->query("ALTER TABLE users ADD COLUMN designation VARCHAR(255) NULL AFTER name");
        if(!in_array('address', $cols)) $conn->query("ALTER TABLE users ADD COLUMN address TEXT AFTER phone");
        if(!in_array('photo', $cols)) $conn->query("ALTER TABLE users ADD COLUMN photo VARCHAR(255) NULL AFTER address");
        if(!in_array('status', $cols)) $conn->query("ALTER TABLE users ADD COLUMN status VARCHAR(20) DEFAULT 'pending' AFTER password");

        // Basic check
        $check = $conn->query("SELECT id FROM users WHERE email='$email'");
        if($check->num_rows > 0) {
            $error = "Email already registered.";
        } else {
            $query = "INSERT INTO users (type, name, email, phone, address, password, status) VALUES ('$type', '$name', '$email', '$phone', '$address', '$hashed_password', 'pending')";
            if($conn->query($query)) {
                $success = "Registration successful! Your account is pending admin approval. <a href='login.php' class='fw-bold'>Login here</a>";
            } else {
                $error = "Registration failed. Please try again.";
            }
        }
    }
}

require_once 'includes/header.php'; 
?>

<div class="container-fluid min-vh-100 d-flex align-items-center justify-content-center py-5" style="background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);">
    <div class="container">
        <div class="row justify-content-center">
            <!-- Left Branding Side (Hidden on small screens) -->
            <div class="col-lg-5 d-none d-lg-block">
                <div class="h-100 d-flex flex-column justify-content-center p-5 text-white" style="background: linear-gradient(rgba(162, 14, 55, 0.9), rgba(162, 12, 57, 0.9)), url('https://nahpa.in/storage/images/background/signup.jpg'); background-size: cover; border-radius: 20px 0 0 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.1);">
                    <div class="text-center mb-4">
                        <img src="https://i.ibb.co/4gVbp8YZ/Whats-App-Image-2026-04-23-at-9-54-25-PM.png" alt="Logo" class="img-fluid bg-white p-2 rounded-circle mb-3" style="max-height: 100px;">
                        <h2 class="fw-bold">BHARTIYA SEVA</h2>
                        <p class="fs-5 opacity-75">Trust Registration & Support Portal</p>
                    </div>
                    <div class="mt-4">
                        <div class="d-flex align-items-start mb-4">
                            <i class="fas fa-check-circle mt-1 me-3 fs-3"></i>
                            <div>
                                <h5 class="fw-bold mb-1">Official Recognition</h5>
                                <p class="small opacity-75">Access verified government registration data and trust deeds.</p>
                            </div>
                        </div>
                        <div class="d-flex align-items-start mb-4">
                            <i class="fas fa-id-badge mt-1 me-3 fs-3"></i>
                            <div>
                                <h5 class="fw-bold mb-1">Member ID-Cards</h5>
                                <p class="small opacity-75">Download your official membership certificate instantly.</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Right Form Side -->
            <div class="col-lg-6 col-md-10">
                <div class="card border-0 shadow-lg" style="border-radius: 0 20px 20px 0; min-height: 600px;">
                    <div class="card-body p-4 p-md-5">
                        
                        <div class="text-center mb-4">
                            <h2 class="fw-bold color-primary">Signup</h2>
                            <p class="text-muted">Already have an account? <a href="login.php" class="text-primary fw-bold text-decoration-none">Login</a></p>
                        </div>

                        <?php if($error): ?>
                            <div class="alert alert-danger"><?php echo  $error ?></div>
                        <?php endif; ?>
                        <?php if($success): ?>
                            <div class="alert alert-success"><?php echo  $success ?></div>
                        <?php endif; ?>

                        <!-- Membership Type Toggle -->
                        <div class="d-flex justify-content-center mb-4">
                            <div class="btn-group w-100 p-1 bg-light rounded-pill" role="group" style="border: 1px solid #ddd;">
                                <button type="button" class="btn rounded-pill fw-bold border-0 type-toggle active" id="btn-individual" onclick="toggleMembership('individual')">Individual membership</button>
                                <button type="button" class="btn rounded-pill fw-bold border-0 type-toggle" id="btn-institute" onclick="toggleMembership('institute')">Institute Membership</button>
                            </div>
                        </div>

                        <!-- Individual Form -->
                        <form action="signup.php" method="POST" id="form-individual">
                            <div class="mb-3">
                                <label class="form-label fw-bold">Your Name <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control bg-light border-0" required placeholder="Enter Your Name">
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Email ID <span class="text-danger">*</span></label>
                                <input type="email" name="email" class="form-control bg-light border-0" required placeholder="Enter Email ID">
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Password <span class="text-danger">*</span></label>
                                <input type="password" name="password" class="form-control bg-light border-0" required placeholder="Enter Password">
                            </div>
                            <div class="mb-4">
                                <label class="form-label fw-bold">Confirm Password <span class="text-danger">*</span></label>
                                <input type="password" name="confirm_password" class="form-control bg-light border-0" required placeholder="Confirm Password">
                            </div>

                            <!-- Dummy Google reCAPTCHA placeholder -->
                            <div class="mb-4 p-3 bg-light rounded border d-flex align-items-center justify-content-between" style="max-width: 310px;">
                                <div class="d-flex align-items-center">
                                    <div class="border p-1 bg-white me-2">
                                        <input type="checkbox" style="width:20px; height:20px; cursor:pointer;" onclick="alert('Verification Successful!')">
                                    </div>
                                    <span class="small fw-bold">I'm not a robot</span>
                                </div>
                                <div class="text-center">
                                    <img src="https://www.gstatic.com/recaptcha/api2/logo_48.png" style="width:30px;" alt="recaptcha">
                                    <div class="text-muted" style="font-size:0.6rem;">reCAPTCHA</div>
                                    <a href="#" class="text-decoration-none" style="font-size:0.6rem; color:#666;">Privacy - Terms</a>
                                </div>
                            </div>

                            <button type="submit" name="register_individual" class="btn btn-primary w-100 py-3 rounded-pill fw-bold shadow">Submit</button>
                        </form>

                        <!-- Institute Form -->
                        <form action="signup.php" method="POST" id="form-institute" style="display: none;">
                            <div class="mb-3">
                                <label class="form-label fw-bold">Board/Council/Universities/College Name <span class="text-danger">*</span></label>
                                <input type="text" name="institute_name" class="form-control bg-light border-0" required placeholder="Enter Institute Name">
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Email ID <span class="text-danger">*</span></label>
                                <input type="email" name="email" class="form-control bg-light border-0" required placeholder="Enter Email ID">
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Mobile Number <span class="text-danger">*</span></label>
                                <input type="text" name="phone" class="form-control bg-light border-0" required placeholder="Enter Your Mobile Number">
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Address <span class="text-danger">*</span></label>
                                <textarea name="address" class="form-control bg-light border-0" rows="2" required placeholder="Enter Your Address"></textarea>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Password <span class="text-danger">*</span></label>
                                <input type="password" name="password" class="form-control bg-light border-0" required placeholder="Enter Password">
                            </div>
                            <div class="mb-4">
                                <label class="form-label fw-bold">Confirm Password <span class="text-danger">*</span></label>
                                <input type="password" name="confirm_password" class="form-control bg-light border-0" required placeholder="Confirm Password">
                            </div>

                            <!-- Dummy Google reCAPTCHA placeholder as requested in screenshot -->
                            <div class="mb-4 p-3 bg-light rounded border d-flex align-items-center justify-content-between" style="max-width: 310px;">
                                <div class="d-flex align-items-center">
                                    <div class="border p-1 bg-white me-2">
                                        <input type="checkbox" style="width:20px; height:20px; cursor:pointer;" onclick="alert('Verification Successful!')">
                                    </div>
                                    <span class="small fw-bold">I'm not a robot</span>
                                </div>
                                <div class="text-center">
                                    <img src="https://www.gstatic.com/recaptcha/api2/logo_48.png" style="width:30px;" alt="recaptcha">
                                    <div class="text-muted" style="font-size:0.6rem;">reCAPTCHA</div>
                                    <a href="#" class="text-decoration-none" style="font-size:0.6rem; color:#666;">Privacy - Terms</a>
                                </div>
                            </div>

                            <button type="submit" name="register_institute" class="btn btn-primary w-100 py-3 rounded-pill fw-bold shadow">Submit</button>
                        </form>

                        <div class="mt-4 text-center">
                            <a href="index.php" class="btn btn-warning btn-sm rounded-pill px-4 fw-bold shadow-sm">Go Back to Home</a>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .color-primary { color: #A20E37; }
    .type-toggle { color: #A20C39; padding: 10px 0; transition: all 0.3s; }
    .type-toggle.active { background-color: #A20E37 !important; color: white !important; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); }
    .form-control:focus { background-color: #fff !important; box-shadow: 0 0 0 3px rgba(162, 14, 55, 0.2) !important; border: 1px solid #A20E37 !important; }
</style>

<script>
function toggleMembership(type) {
    const btnInd = document.getElementById('btn-individual');
    const btnInst = document.getElementById('btn-institute');
    const formInd = document.getElementById('form-individual');
    const formInst = document.getElementById('form-institute');

    if(type === 'individual') {
        btnInd.classList.add('active');
        btnInst.classList.remove('active');
        formInd.style.display = 'block';
        formInst.style.display = 'none';
    } else {
        btnInd.classList.remove('active');
        btnInst.classList.add('active');
        formInd.style.display = 'none';
        formInst.style.display = 'block';
    }
}
</script>

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