<?php

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

allowRoles([1]);

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

/* HIDE SIDEBAR */
echo "<style>
.sidebar{display:none !important;}
.topbar{margin-left:0 !important;}
.main{margin-left:0 !important;padding-bottom:80px;}
</style>";

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

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

$district_id = intval($_POST['district_id']);
$block_name  = mysqli_real_escape_string($conn, trim($_POST['block_name']));
$block_name_hi  = mysqli_real_escape_string($conn, trim($_POST['block_name_hi']));

/* CHECK DUPLICATE */

$check = mysqli_query($conn,"
SELECT id FROM blocks
WHERE district_id='$district_id'
AND LOWER(block_name)=LOWER('$block_name')
");

if(mysqli_num_rows($check) > 0)
{
echo "<script>alert('Block already exists');window.location='add_block.php';</script>";
exit;
}
else
{
mysqli_query($conn,"
INSERT INTO blocks
(district_id,block_name,block_name_hi)
VALUES
('$district_id','$block_name','$block_name_hi')
");

echo "<script>alert('Block Added Successfully');window.location='add_block.php';</script>";
exit;
}

}
?>

<div class="app-content">

<div class="card-box">
<h4>Add Block</h4>
<p style="font-size:12px;color:#777;">Create new block</p>
</div>

<div class="card-box">

<form method="POST">

<label>Select District</label>

<select name="district_id" class="form-control" 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 style="margin-top:10px;">Block Name (English)</label>
<input type="text" name="block_name" class="form-control" required>

<label>Block Name (Hindi)</label>
<input type="text" name="block_name_hi" class="form-control">

<br>

<button type="submit" name="add" class="btn-mobile"
style="background:#27ae60;color:#fff;">
Add Block
</button>

</form>

</div>

</div>

<div class="bottom-nav">
    <a href="../dashboard.php"><i class="fa fa-house"></i>Home</a>
    <a href="../villages/village_data.php"><i class="fa fa-map"></i>Villages</a>
    <a href="../families/view_families.php"><i class="fa fa-users"></i>Families</a>
    <a href="../schemes/view_schemes.php"><i class="fa fa-list"></i>Schemes</a>
</div>

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