<?php
include "../../config/master.php";
allowRoles([1,5]);

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

/* =========================
   INIT
========================= */
$role_id = $_SESSION['role_id'] ?? 0;
$id = intval($_GET['id'] ?? 0);

/* DEFAULT VALUES */
$name = "";
$designation = "";
$description = "";
$image = "";
$village_id = "";

/* =========================
   EDIT MODE FETCH
========================= */
if($id > 0)
{
    $res = mysqli_query($conn,"SELECT * FROM special_thanks WHERE id='$id'");
    $data = mysqli_fetch_assoc($res);

    if($data)
    {
        $name = $data['name'];
        $designation = $data['designation'];
        $description = $data['description'];
        $image = $data['image'];
        $village_id = $data['village_id'];
    }
    else
    {
        echo "Invalid ID";
        exit;
    }
}
else
{
    // ADD MODE DEFAULT VILLAGE
    if($role_id == 1){
        $village_id = $_POST['village_id'] ?? 0;
    }else{
        $village_id = $_SESSION['village_id'];
    }
}

/* =========================
   SAVE (INSERT + UPDATE)
========================= */
if(isset($_POST['submit']))
{
    $name = mysqli_real_escape_string($conn,$_POST['name']);
    $designation = mysqli_real_escape_string($conn,$_POST['designation']);
    $description = mysqli_real_escape_string($conn,$_POST['description']);

    if($role_id == 1){
        $village_id = $_POST['village_id'];
    }

    /* IMAGE HANDLING */
    if(!empty($_FILES['image']['name']))
    {
        $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
        $new_image = time().".".$ext;

        if(move_uploaded_file(
            $_FILES['image']['tmp_name'],
            "../../uploads/special_thanks/".$new_image
        )){
            // delete old image in edit
            if($id > 0 && !empty($image) && file_exists("../../uploads/special_thanks/".$image)){
                unlink("../../uploads/special_thanks/".$image);
            }

            $image = $new_image;
        }
    }

    if($id > 0)
    {
        // UPDATE
        $query = "UPDATE special_thanks SET
        village_id='$village_id',
        name='$name',
        designation='$designation',
        description='$description',
        image='$image'
        WHERE id='$id'";
    }
    else
    {
        // INSERT
        $query = "INSERT INTO special_thanks
        (village_id,name,designation,image,description)
        VALUES
        ('$village_id','$name','$designation','$image','$description')";
    }

    $run = mysqli_query($conn,$query);

    if($run){
        echo "<script>alert('Saved Successfully');window.location='special_thanks_list.php';</script>";
    }else{
        echo "<script>alert('Error');</script>";
    }
}
?>

<div class="main">

<h2><?php echo ($id>0) ? "Edit Special Thanks" : "Add Special Thanks"; ?></h2>

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

<?php if($role_id == 1){ ?>

<label>Select Village</label>
<select name="village_id" required>

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

<?php
$v = mysqli_query($conn,"SELECT id,village_name FROM villages");
while($row=mysqli_fetch_assoc($v)){
?>
<option value="<?php echo $row['id']; ?>"
<?php if($row['id'] == $village_id) echo "selected"; ?>>
<?php echo $row['village_name']; ?>
</option>
<?php } ?>

</select>

<?php } ?>

<label>Name</label>
<input type="text" name="name" value="<?php echo htmlspecialchars($name); ?>" required>

<label>Designation</label>
<input type="text" name="designation" value="<?php echo htmlspecialchars($designation); ?>">

<!-- IMAGE -->
<?php if(!empty($image)){ ?>
<label>Current Image</label><br>
<img src="../../uploads/special_thanks/<?php echo $image; ?>" width="120" style="margin-bottom:10px;">
<?php } ?>

<label><?php echo ($id>0) ? "Change Image" : "Upload Image"; ?></label>
<input type="file" name="image">

<label>Description</label>
<textarea name="description" id="editor"><?php echo $description; ?></textarea>

<button type="submit" name="submit" class="btn-save">
<?php echo ($id>0) ? "Update" : "Save"; ?>
</button>

</form>

</div>

<!-- CKEDITOR -->
<script src="https://cdn.ckeditor.com/4.22.1/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('editor');
</script>

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