<?php 
include 'header.php';
$msg = "";
$err = "";
$upload_dir = "../uploads/celebrities/"; // Directory for image uploads

// Ensure upload directory exists
if (!file_exists($upload_dir)) {
    mkdir($upload_dir, 0777, true);
}

// Categories array (you can modify this as needed)
$categories = [
    'Actor',
    'Musician',
    'Athlete',
    'Influencer',
    'Model',
    'Comedian',
    'Other'
];

if (isset($_POST['save'])) {
    // Sanitize input data
    $name = text_input($_POST['name']);
    $category = text_input($_POST['category']);

    $rating = floatval($_POST['rating']);
    $happy_fans = intval($_POST['happy_fans']);
    $events_done = intval($_POST['events_done']);
    $response_rate = floatval($_POST['response_rate']);
    $about = text_input($_POST['about']);
    $languages = text_input($_POST['languages']);
    $response_time = text_input($_POST['response_time']);
    $regular_fee = floatval($_POST['regular_fee']);
    $vvip_fee = floatval($_POST['vvip_fee']);
    $gold_fee = floatval($_POST['gold_fee']);
    $vip_royal_fee = floatval($_POST['vip_royal_fee']);
    $personal_meetup_fee = floatval($_POST['personal_meetup_fee']);
    $event_booking_fee = floatval($_POST['event_booking_fee']);

    // Handle image upload
    $image_name = "";
    if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
        $allowed = ['jpg', 'jpeg', 'png', 'gif'];
        $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
        
        if (in_array(strtolower($ext), $allowed)) {
            $image_name = uniqid() . '.' . $ext;
            $image_path = $upload_dir . $image_name;
            
            if (!move_uploaded_file($_FILES['image']['tmp_name'], $image_path)) {
                $err = "Failed to upload image";
            }
        } else {
            $err = "Invalid image format. Only JPG, JPEG, PNG, and GIF allowed";
        }
    }

   
    if (empty($err)) {
        $query = "INSERT INTO celebrities (
            name, category, image, rating, happy_fans, 
            events_done, response_rate, about, languages, response_time, 
            regular_fee, vvip_fee, gold_fee, vip_royal_fee, personal_meetup_fee, 
            event_booking_fee
        ) VALUES (
            '$name', '$category', '$image_name',
            '$rating', '$happy_fans', '$events_done', '$response_rate', '$about', 
            '$languages', '$response_time', '$regular_fee', '$vvip_fee', 
            '$gold_fee', '$vip_royal_fee', '$personal_meetup_fee', 
            '$event_booking_fee'
        )";

        $save = mysqli_query($link, $query);
        if ($save) {
            $msg = "Celebrity added successfully";
        } else {
            $err = mysqli_error($link);
        }
    }
}
?>

<div class="page-content">
    <div class="container-fluid">
        <?php 
        if ($msg != "") {
            echo customAlert("success", $msg);
            echo pageRedirect("2", "add-celebrities.php");
        }
        if ($err != "") {
            echo customAlert("error", $err);
        }
        ?>

        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div class="card-header">
                            <h4 class="card-title mb-0">Add Celebrity</h4>
                        </div>
                        <form method="post" action="add-celebrities.php" enctype="multipart/form-data">
                            <div class="live-preview">
                                <div class="row gy-4">
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="nameInput" class="form-label">Name</label>
                                            <input type="text" class="form-control" name="name" id="nameInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="categoryInput" class="form-label">Category</label>
                                            <select class="form-control" name="category" id="categoryInput" required>
                                                <?php foreach ($categories as $cat): ?>
                                                    <option value="<?php echo $cat; ?>"><?php echo $cat; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="imageInput" class="form-label">Profile Image</label>
                                            <input type="file" class="form-control" name="image" id="imageInput" accept="image/*" required>
                                        </div>
                                    </div>
                                  
                                    
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="ratingInput" class="form-label">Rating (0-5)</label>
                                            <input type="number" step="0.1" min="0" max="5" class="form-control" name="rating" id="ratingInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="happyFansInput" class="form-label">Happy Fans</label>
                                            <input type="number" class="form-control" name="happy_fans" id="happyFansInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="eventsDoneInput" class="form-label">Events Done</label>
                                            <input type="number" class="form-control" name="events_done" id="eventsDoneInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="responseRateInput" class="form-label">Response Rate (%)</label>
                                            <input type="number" step="0.1" min="0" max="100" class="form-control" name="response_rate" id="responseRateInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="responseTimeInput" class="form-label">Response Time</label>
                                            <input type="text" class="form-control" name="response_time" id="responseTimeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="regularFeeInput" class="form-label">Regular Fan Card Fee ($)</label>
                                            <input type="number" step="0.01" class="form-control" name="regular_fee" id="regularFeeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="vvipFeeInput" class="form-label">VVIP Fan Card Fee ($)</label>
                                            <input type="number" step="0.01" class="form-control" name="vvip_fee" id="vvipFeeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="goldFeeInput" class="form-label">Gold Fee ($)</label>
                                            <input type="number" step="0.01" class="form-control" name="gold_fee" id="goldFeeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="vipRoyalFeeInput" class="form-label">VIP Royal Card Fee ($)</label>
                                            <input type="number" step="0.01" class="form-control" name="vip_royal_fee" id="vipRoyalFeeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="personalMeetupFeeInput" class="form-label">Personal Meetup Experience Fee ($)</label>
                                            <input type="number" step="0.01" class="form-control" name="personal_meetup_fee" id="personalMeetupFeeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-6 col-md-12">
                                        <div>
                                            <label for="eventBookingFeeInput" class="form-label">Event Booking Services Fee ($)</label>
                                            <input type="number" step="0.01" class="form-control" name="event_booking_fee" id="eventBookingFeeInput" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-12 col-md-12">
                                        <div>
                                            <label for="aboutInput" class="form-label">About Celebrity</label>
                                            <textarea class="form-control" name="about" id="aboutInput" rows="4" required></textarea>
                                        </div>
                                    </div>
                                    <div class="col-xxl-12 col-md-12">
                                        <div>
                                            <label for="languagesInput" class="form-label">Languages</label>
                                            <input type="text" class="form-control" name="languages" id="languagesInput" placeholder="e.g., English, Spanish" required>
                                        </div>
                                    </div>
                                    <div class="col-xxl-12 col-md-12 mt-4">
                                        <button type="submit" name="save" class="btn btn-primary">Save Celebrity</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php 
include 'footer.php';
?>