<?php

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

if(!empty($_POST['firstname']) && !empty($_POST['surname']) && !empty($_POST['sex']) 
&& !empty($_POST['church']) && !empty($_POST['district'])
&& !empty($_POST['phone'])) {
    $serial = $_POST['serial'];
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $sex = $_POST['sex'];
    $amount = $_POST['amount'];
    $district = $_POST['district'];
    $phone = $_POST['phone'];
    $church = $_POST['church'];
    $occupation = $_POST['occupation'];
    $conyear = $_POST['conyear'];
    $bible = $_POST['bible'];
    $id = $_POST['id'];


    //perform validation for phone
    $sql = "SELECT * FROM members WHERE serial =:serial AND id !=:id AND serial !=''";
    $conn->query($sql);
    $conn->bind(":serial", $serial);
    $conn->bind(":id", $id);
    if ($conn->rowCount() > 0){
        echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Serial Number Already Exists');
      });
</script>";
        return false;
    }


//perform validation for phone
    $sql = "SELECT * FROM members WHERE phone =:phone AND id !=:id AND phone !=''";
    $conn->query($sql);
    $conn->bind(":phone", $phone);
    $conn->bind(":id", $id);
    if ($conn->rowCount() > 0){
        echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Phone Number Already Exists');
      });
</script>";
        return false;
    }else {
//process inserting of data into database
        $sql = "UPDATE members SET serial=:serial, firstname=:firstname, surname=:surname, sex=:sex, districtID=:districtID,
                 phone=:phone, church=:church, occupation=:occupation, amount=:amount, conyear=:conyear, bible=:bible, id=:id WHERE id=:id";
        $conn->query($sql);
        $conn->bind(":serial", $serial);
        $conn->bind(":firstname", $firstname);
        $conn->bind(":surname", $surname);
        $conn->bind(":sex", $sex);
        $conn->bind(":districtID", $district);
        $conn->bind(":phone", $phone);
        $conn->bind(":church", $church);
        $conn->bind(":occupation", $occupation);
        $conn->bind(":amount", $amount);
        $conn->bind(":conyear", $conyear);
        $conn->bind(":bible", $bible);
        $conn->bind(":id", $id);


        $send = $conn->execute();
        if ($send) {
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.success('Member <b>$firstname $surname</b> Updated 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;
}