<?php

include ('includes/database.php');
include ('includes/functions.php');

$conn = new Functions();
$redirect = $conn->base_url()."settings";


if (!empty($_POST['name']) && !empty($_POST['curreg'])){
    $name = $_POST['name'];
    $tagline = $_POST ['church_tagline'];
    $phone_no = $_POST ['phone_no'];
    $email = $_POST ['email'];
    $address = $_POST ['address'];
    $curreg = $_POST ['curreg'];
    $footer = $_POST ['footer'];
    $settings_id = $_POST ['settings_id'];
    $main_url = $_POST ['main_url'];
    $church_website = $_POST ['church_website'];
    $username_prefix = $_POST ['username_prefix'];


    //perform validations

    $sql = "SELECT * FROM general_settings WHERE church_name = :churchname AND church_name != '' AND id !=:settings_id";
    $conn->query($sql);
    $conn->bind(":churchname", $name);
    $conn->bind(":settings_id", $settings_id);
    if ($conn->rowCount() > 0){
        echo "<script>
        $(function () { //ready
                  toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                  toastr.error('Name Of Church Already Exists');
              });
        </script>";
        return false;
    }

    $sql = "SELECT * FROM general_settings WHERE phone_no = :phone_no AND phone_no != '' AND id !=:settings_id";
    $conn->query($sql);
    $conn->bind(":phone_no", $phone_no);
    $conn->bind(":settings_id", $settings_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;
    }

    $sql = "SELECT * FROM general_settings WHERE email = :email AND email != '' AND id !=:settings_id";
    $conn->query($sql);
    $conn->bind(":email", $email);
    $conn->bind(":settings_id", $settings_id);
    if ($conn->rowCount() > 0){
        echo "<script>
              $(function () { //ready
                        toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                        toastr.error('Administrative Email Already Exists');
                    });
              </script>";
        return false;
    }else{

        //process updating of data into database
        $sql = "UPDATE general_settings SET church_name =:churchname, church_tagline =:church_tagline, phone_no =:phone_no, email =:email,
                address =:address, curreg=:curreg, footer =:footer, church_website =:church_website, main_url =:main_url, username_prefix =:username_prefix";
        $conn->query($sql);
        $conn->bind(":churchname", ucwords($name));
        $conn->bind(":church_tagline", $tagline);
        $conn->bind(":phone_no", $phone_no);
        $conn->bind(":email", $email);
        $conn->bind(":main_url", $main_url);
        $conn->bind(":curreg", $curreg);
        $conn->bind(":username_prefix", $username_prefix);
        $conn->bind(":address", $address);
        $conn->bind(":church_website", $church_website);
        $conn->bind(":footer", $footer);


        $send = $conn->execute();
        if ($send) {
            echo "<script>
            $(function () { //ready
                      toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                      toastr.success('Settings 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>
            toastr['error']('An Fields Are Required');
            </script>";
}
