<?php

include "../../config/master.php";
allowRoles([1]);
include "../layout/header.php";

/* SAVE DATA */
if(isset($_POST['save']))
{

$scheme_mode = $_POST['scheme_mode'] ?? 'targeted';
$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_id = ($_POST['education'] == "Any") ? NULL : intval($_POST['education']);

$family_type = $_POST['family_type'] ?? 'Any';
$age_min     = !empty($_POST['age_min']) ? intval($_POST['age_min']) : 0;
$age_max     = !empty($_POST['age_max']) ? intval($_POST['age_max']) : 120;
$gender      = $_POST['gender'] ?? 'Any';

/* TEXT FIELDS (EDITOR) */
$description   = $_POST['description'];
$benefits      = $_POST['benefits'];
$eligibility   = $_POST['eligibility'];
$documents     = $_POST['documents'];
$apply_online  = $_POST['apply_online'];
$apply_offline = $_POST['apply_offline'];

$image = "";

/* IMAGE UPLOAD */
if(!empty($_FILES['image']['name']))
{
    $target = "../../uploads/schemes/";
    if(!is_dir($target)){
        mkdir($target,0777,true);
    }

    $image = time()."_".basename($_FILES["image"]["name"]);
    move_uploaded_file($_FILES["image"]["tmp_name"], $target.$image);
}

/* INSERT */
mysqli_query($conn,"
INSERT INTO schemes 
(
scheme_mode, scheme_name, category_id, department,
education_id, family_type, age_min, age_max, gender,
description, benefits, eligibility, documents,
apply_online, apply_offline, image
)
VALUES
(
'$scheme_mode','$scheme_name','$category_id','$department',
".($education_id === NULL ? "NULL" : "'$education_id'").",
'$family_type','$age_min','$age_max','$gender',
'$description','$benefits','$eligibility','$documents',
'$apply_online','$apply_offline','$image'
)
") or die(mysqli_error($conn));

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

<div class="app-content">

<div class="card-box">
<h4>Add Scheme</h4>
</div>

<div class="card-box">

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

<label>Scheme Type</label>
<select name="scheme_mode" class="form-control">
<option value="targeted">Targeted</option>
<option value="general">General</option>
</select>

<label>Scheme Title</label>
<input type="text" name="scheme_name" class="form-control" required>

<label>Category</label>
<select name="category_id" class="form-control" required>
<option value="">Select</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>

<label>Department</label>
<input type="text" name="department" class="form-control">

<hr>

<h5>Filters</h5>

<label>Education</label>
<select name="education" class="form-control">
<option value="Any">Any</option>
<?php
$edu=mysqli_query($conn,"SELECT * FROM education_master");
while($e=mysqli_fetch_assoc($edu)){
?>
<option value="<?php echo $e['id']; ?>"><?php echo $e['education_name']; ?></option>
<?php } ?>
</select>

<label>Family Type</label>
<select name="family_type" class="form-control">
<option value="Any">Any</option>
<option value="APL">APL</option>
<option value="BPL">BPL</option>
</select>

<label>Age Range</label>
<div style="display:flex;gap:10px;">
<input type="number" name="age_min" class="form-control">
<input type="number" name="age_max" class="form-control">
</div>

<label>Gender</label>
<select name="gender" class="form-control">
<option value="Any">Any</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>

<hr>

<label>Image</label>
<input type="file" name="image" class="form-control">

<!-- ===== EDITOR FIELDS ===== -->

<label>Description</label>
<textarea name="description" id="description"></textarea>

<label>Benefits</label>
<textarea name="benefits" id="benefits"></textarea>

<label>Eligibility</label>
<textarea name="eligibility" id="eligibility"></textarea>

<label>Application Process (Online)</label>
<textarea name="apply_online" id="apply_online"></textarea>

<label>Application Process (Offline)</label>
<textarea name="apply_offline" id="apply_offline"></textarea>

<label>Documents Required</label>
<textarea name="documents" id="documents"></textarea>

<br>

<button type="submit" name="save" class="btn-mobile" style="background:#27ae60;color:#fff;">
Save Scheme
</button>

</form>

</div>

</div>

<!-- CKEDITOR -->
<script src="https://cdn.ckeditor.com/4.22.1/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('description');
CKEDITOR.replace('benefits');
CKEDITOR.replace('eligibility');
CKEDITOR.replace('apply_online');
CKEDITOR.replace('apply_offline');
CKEDITOR.replace('documents');
</script>

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