<?php

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

/* FETCH DISTRICTS */

$districts = mysqli_query($conn,"SELECT * FROM districts ORDER BY district_name");

/* SAVE DONOR */

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

$category_id  = intval($_POST['category_id']);

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

$assigned     = floatval($_POST['assigned_amount']);
$received     = floatval($_POST['received_amount']);

$next_date    = $_POST['next_date'];

/* INSERT */

mysqli_query($conn,"INSERT INTO donors
(category_id,donor_name,father_name,assigned_amount,received_amount,next_date)
VALUES
('$category_id','$donor_name','$father_name','$assigned','$received','$next_date')
");

echo "<script>alert('Donor Added Successfully');</script>";

}

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

?>

<div class="main">

<h2>Add Donor</h2>

<div class="form-box">

<form method="POST">

<label>Select District</label>

<select name="district_id" id="district" required>

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

<?php while($d=mysqli_fetch_assoc($districts)){ ?>

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

<?php } ?>

</select>


<label>Select Block</label>

<select name="block_id" id="block" required>

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

</select>


<label>Select Village</label>

<select name="village_id" id="village" required>

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

</select>


<label>Select Donation Work</label>

<select name="category_id" id="category" required>

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

</select>


<label>Donor Name</label>
<input type="text" name="donor_name" required>

<label>Father Name</label>
<input type="text" name="father_name">

<label>Assigned Amount</label>
<input type="number" name="assigned_amount" required>

<label>Received Amount</label>
<input type="number" name="received_amount" value="0">

<label>Next Date</label>
<input type="date" name="next_date">

<button type="submit" name="save" class="btn-save">
Save Donor
</button>

</form>

</div>

</div>

<script>

/* LOAD BLOCKS */

document.getElementById("district").addEventListener("change", function(){

var district_id = this.value;

fetch("../villages/get_blocks.php?district_id="+district_id)

.then(response => response.text())

.then(data => {

document.getElementById("block").innerHTML = data;

});

});


/* LOAD VILLAGES */

document.getElementById("block").addEventListener("change", function(){

var block_id = this.value;

fetch("../villages/get_villages.php?block_id="+block_id)

.then(response => response.text())

.then(data => {

document.getElementById("village").innerHTML = data;

});

});


/* LOAD DONATION WORKS */

document.getElementById("village").addEventListener("change", function(){

var village_id = this.value;

fetch("get_donation_categories.php?village_id="+village_id)

.then(response => response.text())

.then(data => {

document.getElementById("category").innerHTML = data;

});

});

</script>

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