<?php
session_start();
include 'includes/header.php'; 
include 'includes/sidebar.php'; 
include 'includes/connect.php';

// Get lifeline configuration ID
if(!isset($_GET['id']) || empty($_GET['id'])){
    echo "<script>alert('Invalid Configuration ID'); window.location.href='lifeline_list.php';</script>";
    exit;
}

$id = intval($_GET['id']);

// Fetch configuration data
$config_query = "SELECT * FROM lifeline_configuration WHERE id=$id";
$config_result = mysqli_query($conn, $config_query);

if(mysqli_num_rows($config_result) == 0){
    echo "<script>alert('Configuration not found'); window.location.href='lifeline_list.php';</script>";
    exit;
}

$config = mysqli_fetch_assoc($config_result);

// Fetch categories for dropdown
$cat_query = "SELECT * FROM category ORDER BY category_name ASC";
$cat_result = mysqli_query($conn, $cat_query);

// Update configuration
if(isset($_POST['update_lifeline'])){
    $category_id = intval($_POST['category_id']);
    $label_name = mysqli_real_escape_string($conn, $_POST['label_name']);
    $start_day = intval($_POST['start_day']);
    $end_day = intval($_POST['end_day']);

    if($category_id > 0 && !empty($label_name) && $start_day >= 0 && $end_day >= 0){
        $update_query = "UPDATE lifeline_configuration 
                         SET category_id=$category_id, label_name='$label_name', start_day=$start_day, end_day=$end_day 
                         WHERE id=$id";

        if(mysqli_query($conn, $update_query)){
            echo "<script>alert('Configuration updated successfully'); window.location.href='lifeline_list.php';</script>";
            exit;
        } else {
            $error = "Error updating configuration.";
        }
    } else {
        $error = "Please fill all fields correctly. Days must be numbers.";
    }
}
?>

<div class="main-content-inner">
    <div class="main-content-wrap">

        <div class="flex items-center flex-wrap justify-between gap20 mb-30">
            <h3>Edit Lifeline Configuration</h3>
        </div>

        <div class="wg-box">
            <?php if(isset($error)){ ?>
                <div class="text-red-500 mb-4"><?php echo $error; ?></div>
            <?php } ?>

            <form method="POST">
                <div class="wg-box mb-30" style="display:flex; gap:20px; flex-wrap:wrap;">

                    <!-- Category -->
                    <fieldset style="flex:1; min-width:150px;">
                        <div class="body-title mb-10">Category <span class="tf-color-1">*</span></div>
                        <select name="category_id" required>
                            <option value="">Select Category</option>
                            <?php while($cat = mysqli_fetch_assoc($cat_result)){ ?>
                                <option value="<?php echo $cat['id']; ?>" <?php if($cat['id']==$config['category_id']) echo 'selected'; ?>>
                                    <?php echo htmlspecialchars($cat['category_name']); ?>
                                </option>
                            <?php } ?>
                        </select>
                    </fieldset>

                    <!-- Label Name -->
                    <fieldset style="flex:1; min-width:150px;">
                        <div class="body-title mb-10">Label Name <span class="tf-color-1">*</span></div>
                        <input type="text" name="label_name" value="<?php echo htmlspecialchars($config['label_name']); ?>" required>
                    </fieldset>

                    <!-- Start Day -->
                    <fieldset style="flex:1; min-width:150px;">
                        <div class="body-title mb-10">Start Day <span class="tf-color-1">*</span></div>
                        <input type="number" name="start_day" value="<?php echo $config['start_day']; ?>" min="0" required>
                    </fieldset>

                    <!-- End Day -->
                    <fieldset style="flex:1; min-width:150px;">
                        <div class="body-title mb-10">End Day <span class="tf-color-1">*</span></div>
                        <input type="number" name="end_day" value="<?php echo $config['end_day']; ?>" min="0" required>
                    </fieldset>

                </div>

                <div class="cols gap10">
                    <button type="submit" name="update_lifeline" class="tf-button w380">Update</button>
                    <a href="lifeline_list.php" class="tf-button style-3 w380">Cancel</a>
                </div>
            </form>
        </div>

    </div>
</div>

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