<?php
include "include/config.php";

// Step 2: Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Step 3: Sanitize and validate form data
    $organization = $conn->real_escape_string($_POST['organization']);
    $email = $conn->real_escape_string($_POST['email']);
    $phone = $conn->real_escape_string($_POST['phone']);
    $district = $conn->real_escape_string($_POST['district']);
    $sector = $conn->real_escape_string($_POST['sector']);
    $workers = $conn->real_escape_string($_POST['workers']);
    $membership_fee = $conn->real_escape_string($_POST['membership_fee']);
    $subscription_fee = $conn->real_escape_string($_POST['subscription_fee']);
    $representative = $conn->real_escape_string($_POST['representative']);
    $password = $_POST['pass']; // Hash password before storing

    $updates = isset($_POST['updates']) ? 1 : 0; // 1 if checked, 0 if not

    // Step 4: Prepare SQL statement to insert data into ufez_members table
    $sql = "INSERT INTO ufez_members (organization, email, phone, district, sector, workers, membership_fee, subscription_fee, representative, pass, updates)
            VALUES ('$organization', '$email', '$phone', '$district', '$sector', '$workers', '$membership_fee', '$subscription_fee', '$representative', '$password', '$updates')";

    // Step 5: Execute SQL query and check for success
    if ($conn->query($sql) === TRUE) {
        // Redirect with success message
        header("Location: index.php?success=You have Successfully registered as UFEZ Member");

        // Send a welcome email to the user
        require __DIR__ . '/email/class.phpmailer.php';
        require __DIR__ . '/email/class.smtp.php';

        $msubject = "Welcome to UFEZ";
        $to = $email;
        $welcomeMessage = "Hello $organization, <br><br>Welcome to UFEZ! We're excited to have you as a member. Your registration was successful.<br> You can now access your account and enjoy all the benefits that come with being a UFEZ member.<br><br>Best regards,<br>The UFEZ Team";

        $mail = new PHPMailer();
        $mail->SMTPDebug = 0;

        try {
            $mail->isSMTP();
            $mail->Host       = 'ufez.online';
            $mail->SMTPAuth   = true;
            $mail->Username   = 'contact@ufez.online';
            $mail->Password   = '@Mypass==locked'; 
            $mail->SMTPSecure = 'tls'; 
            $mail->Port       = 587;

            $mail->setFrom('contact@ufez.online', 'UFEZ');

            // Add recipient (user's email)
            $mail->addAddress($to);

            $mail->isHTML(true);
            $mail->Subject = $msubject;
            $mail->Body    = $welcomeMessage;

            // Send email
            if ($mail->send()) {
                header("Location: index.php?success= You have Successfully registered as UFEZ Member");
            } else {
                echo "<center style='color:red;'>Failed to send welcome email. Please try again later.</center>";
            }

        } catch (Exception $e) {
            echo "Failed to send email. Error: " . $e->getMessage();
        }

    } else {
      header("Location: index.php?error= Registration failed. The email or organization already exists in the system.");

    }

    // Step 6: Close the database connection
    $conn->close();
}
?>
