<?php

include "../../config/master.php";

/* Only Super Admin Can Add Scheme */
allowRoles([1]);

include "../layout/header.php";
include "../layout/sidebar.php";


/* SAVE SCHEME */

if(isset($_POST['save']))
{

$scheme_name = mysqli_real_escape_string($conn,$_POST['scheme_name']);
$category_id = intval($_POST['category_id']);
$department = mysqli_real_escape_string($conn,$_POST['department']);

/* EDUCATION FIX */

$education_id = ($_POST['education'] == 'Any') ? 0 : intval($_POST['education']);

$family_type = mysqli_real_escape_string($conn,$_POST['family_type']);
$age_min = intval($_POST['age_min']);
$age_max = intval($_POST['age_max']);
$gender = mysqli_real_escape_string($conn,$_POST['gender']);
$description = mysqli_real_escape_string($conn,$_POST['description']);


/* IMAGE UPLOAD */

$image_name = "";

if(!empty($_FILES['image']['name']))
{

$target_dir = "../../assets/schemes/";

if(!is_dir($target_dir)){
mkdir($target_dir,0777,true);
}

$image_name = time()."_".basename($_FILES["image"]["name"]);

$target_file = $target_dir.$image_name;

move_uploaded_file($_FILES["image"]["tmp_name"],$target_file);

}


/* INSERT */

$insert = mysqli_query($conn,"INSERT INTO schemes
(
scheme_name,
category_id,
department,
education_id,
family_type,
age_min,
age_max,
gender,
image,
description
)
VALUES
(
'$scheme_name',
'$category_id',
'$department',
'$education_id',
'$family_type',
'$age_min',
'$age_max',
'$gender',
'$image_name',
'$description'
)
");


if($insert){

echo "<script>
alert('Scheme Added Successfully');
window.location='view_schemes.php';
</script>";

}else{

echo "<div style='color:red;padding:10px;'>Error: ".mysqli_error($conn)."</div>";

}

}

?>

<div class="main">

<h2>Add Scheme</h2>

<div style="background:#fff;padding:25px;border-radius:6px;width:650px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">

<form method="POST" enctype="multipart/form-data">


<!-- Scheme Title -->

<div style="margin-bottom:15px;">
<label>Scheme Title</label>
<input type="text" name="scheme_name" required style="width:100%;padding:8px;">
</div>


<!-- Category -->

<div style="margin-bottom:15px;">
<label>Category</label>

<select name="category_id" required style="width:100%;padding:8px;">

<option value="">Select Category</option>

<?php

$cat=mysqli_query($conn,"SELECT * FROM scheme_categories");

while($c=mysqli_fetch_assoc($cat))
{
?>

<option value="<?php echo $c['id']; ?>">
<?php echo $c['category_name']; ?>
</option>

<?php } ?>

</select>

</div>


<!-- Department -->

<div style="margin-bottom:15px;">
<label>Department</label>
<input type="text" name="department" style="width:100%;padding:8px;">
</div>


<!-- Education -->

<div style="margin-bottom:15px;">
<label>Education Eligibility</label>

<select name="education" style="width:100%;padding:8px;">

<option value="Any">Any</option>

<?php

$edu=mysqli_query($conn,"SELECT * FROM education_master ORDER BY education_name");

while($e=mysqli_fetch_assoc($edu))
{
?>

<option value="<?php echo $e['id']; ?>">
<?php echo htmlspecialchars($e['education_name']); ?>
</option>

<?php } ?>

</select>

</div>


<!-- Family Type -->

<div style="margin-bottom:15px;">
<label>Family Type</label>

<select name="family_type" style="width:100%;padding:8px;">
<option value="Any">Any</option>
<option value="APL">APL</option>
<option value="BPL">BPL</option>
</select>

</div>


<!-- Age Range -->

<div style="margin-bottom:15px;">
<label>Age Range</label>

<div style="display:flex;gap:10px;">

<input type="number" name="age_min" placeholder="Min Age" style="width:50%;padding:8px;">

<input type="number" name="age_max" placeholder="Max Age" style="width:50%;padding:8px;">

</div>

</div>


<!-- Gender -->

<div style="margin-bottom:15px;">
<label>Gender</label>

<select name="gender" style="width:100%;padding:8px;">
<option value="Any">Any</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

</div>


<!-- Image -->

<div style="margin-bottom:15px;">
<label>Scheme Image</label>
<input type="file" name="image">
</div>


<!-- Description -->

<div style="margin-bottom:20px;">
<label>Description</label>

<textarea name="description" rows="5" style="width:100%;padding:8px;"></textarea>

</div>


<button type="submit" name="save"
style="background:#2c7be5;color:#fff;padding:10px 18px;border:none;border-radius:4px;">
Save Scheme
</button>

</form>

</div>

</div>

<?php include "../layout/footer.php"; ?>