<?php

include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();
$redirect = $conn->base_url()."district";

if(!empty($_POST['name'])) {
    $name = $_POST['name'];


//perform validation for class numeric
    $sql = "SELECT * FROM districts WHERE name =:name";
    $conn->query($sql);
    $conn->bind(":name", $name);
    if ($conn->rowCount() > 0){
        echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('District Name Already Exists');
      });
</script>";
        return false;
    }else {

//process inserting of data into database
        $sql = "INSERT INTO districts (name)
                VALUES(:name)";
        $conn->query($sql);
        $conn->bind(":name", $name);

        $send = $conn->execute();
        if ($send) {
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.success('District <b>$name</b> Added Successfully');
      });
</script><meta http-equiv='refresh' content='5; $redirect'>";
        }else {
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('An Error Occurred While Adding Data');
      });
</script> <meta http-equiv='refresh' content='4; $redirect'>";
        }
    }


}else {
    echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('All Marked (*) Are Required');
      });
</script>";
    return false;
}