<?php

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

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

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

$data = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM schemes WHERE id='$id'"));

if(!$data){
    echo "Scheme not found";
    exit;
}

/* UPDATE */

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

$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') ? 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']);

$scheme_mode = mysqli_real_escape_string($conn,$_POST['scheme_mode']);

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

$image_name = $data['image'];

/* IMAGE UPDATE */
if(!empty($_FILES['image']['name']))
{
    $target_dir = "../../uploads/schemes/";

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

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

/* UPDATE QUERY */
mysqli_query($conn,"UPDATE schemes SET
scheme_name='$scheme_name',
category_id='$category_id',
department='$department',
education_id='$education_id',
family_type='$family_type',
age_min='$age_min',
age_max='$age_max',
gender='$gender',
scheme_mode='$scheme_mode',
image='$image_name',
description='$description',
benefits='$benefits',
eligibility='$eligibility',
documents='$documents',
apply_online='$apply_online',
apply_offline='$apply_offline'
WHERE id='$id'
");

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

}
?>

<div class="app-content">

<div class="card-box">
<h4>Edit Scheme</h4>
<p style="font-size:12px;color:#777;">Update government scheme</p>
</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" <?php if($data['scheme_mode']=='targeted') echo 'selected'; ?>>Targeted</option>
<option value="general" <?php if($data['scheme_mode']=='general') echo 'selected'; ?>>General</option>
</select>

<label>Scheme Title</label>
<input type="text" name="scheme_name" class="form-control"
value="<?php echo htmlspecialchars($data['scheme_name']); ?>">

<label>Category</label>
<select name="category_id" class="form-control">
<?php
$cat=mysqli_query($conn,"SELECT * FROM scheme_categories");
while($c=mysqli_fetch_assoc($cat)){
?>
<option value="<?php echo $c['id']; ?>"
<?php if($data['category_id']==$c['id']) echo 'selected'; ?>>
<?php echo $c['category_name']; ?>
</option>
<?php } ?>
</select>

<label>Department</label>
<input type="text" name="department" class="form-control"
value="<?php echo $data['department']; ?>">

<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 if($data['education_id']==$e['id']) echo 'selected'; ?>>
<?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" <?php if($data['family_type']=='APL') echo 'selected'; ?>>APL</option>
<option value="BPL" <?php if($data['family_type']=='BPL') echo 'selected'; ?>>BPL</option>
</select>

<label>Age Range</label>
<div style="display:flex;gap:10px;">
<input type="number" name="age_min" class="form-control"
value="<?php echo $data['age_min']; ?>">

<input type="number" name="age_max" class="form-control"
value="<?php echo $data['age_max']; ?>">
</div>

<label>Gender</label>
<select name="gender" class="form-control">
<option value="Any">Any</option>
<option value="Male" <?php if($data['gender']=='Male') echo 'selected'; ?>>Male</option>
<option value="Female" <?php if($data['gender']=='Female') echo 'selected'; ?>>Female</option>
</select>

<hr>

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

<?php if($data['image']){ ?>
<img src="../../uploads/schemes/<?php echo $data['image']; ?>"
style="width:100%;margin-top:10px;border-radius:10px;">
<?php } ?>

<!-- EDITORS -->

<label>Description</label>
<textarea name="description" id="description"><?php echo $data['description']; ?></textarea>

<label>Benefits</label>
<textarea name="benefits" id="benefits"><?php echo $data['benefits']; ?></textarea>

<label>Eligibility</label>
<textarea name="eligibility" id="eligibility"><?php echo $data['eligibility']; ?></textarea>

<label>Application Process (Online)</label>
<textarea name="apply_online" id="apply_online"><?php echo $data['apply_online']; ?></textarea>

<label>Application Process (Offline)</label>
<textarea name="apply_offline" id="apply_offline"><?php echo $data['apply_offline']; ?></textarea>

<label>Required Documents</label>
<textarea name="documents" id="documents"><?php echo $data['documents']; ?></textarea>

<br>

<button type="submit" name="update" class="btn-mobile" style="background:#2980b9;color:#fff;">
Update 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"; ?>