<?php
error_reporting(0);
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();
$redirect = $conn->main_url()."/logout";

if(!empty($_POST['username']) && !empty($_POST['day']) && !empty($_POST['month']) 
&& !empty($_POST['password']) && !empty($_POST['confirm_password'])) {
    $id = $_POST['id'];
    $month = $_POST['month'];
    $day = $_POST['day'];
    $year = $_POST['year'];
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $username = $_POST['username'];
    $verify = $_POST['verify'];
    $photo_name = $_FILES['photo']['name'];
    $photo_tmpname = $_FILES['photo']['tmp_name'];
    $photo_size = $_FILES['photo']['size'];

    $photofilenameAr = explode('.', $photo_name);
    $photo_extension = end($photofilenameAr);
    $photo_ext = strtolower($photo_extension);

//check if image was selected
    if (!empty($photo_name)){
        if ($photo_size > 100000) {
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Photo Size Exceeded, Max Size is 100K');
      });
</script>";
            return false;
        }
        if ($photo_ext == 'png' || $photo_ext == 'jpg' || $photo_ext == 'jpeg' || $photo_ext == 'svg') {
            $strgPath = "upload/" . $photo_name;
            if (file_exists($strgPath)) {
                echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Photo With Same Name Already Exist');
      });
</script>";
                return false;
            } else {
                $photo_upload = move_uploaded_file($photo_tmpname, $strgPath);
            }
        } else {
            echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Only Png, Jpg, Jpeg and Svg Formats Are Allowed. Photo Not Uploaded');
      });
</script>";
            return false;

        }
    }

    //check for username availability
    $sql = "SELECT * FROM members WHERE username =:username";
    $conn->query($sql);
    $conn->bind(":username", $username);
    if ($conn->rowCount() > 0) {
        echo "<script>
$(function () { //ready
    toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.error('Username Already Exists');
      });
</script>";
        return false;
    }


   //check for password strength or validity
   if(!$conn->Checkpassword($password)){
    echo "<script>
    $(function () { //ready
              toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
              toastr.error('Password must include one uppercase letter, one lowercase letter, one number, one special character such as $ or % and length should be between 6 and 16');
          });
    </script>";
    return false;
}

//check if both password match
if($password != $confirm_password){
    echo "<script>
    $(function () { //ready
              toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
              toastr.error('Passwords Do Not Match');
          });
    </script>";
    return false;
}else {
//process inserting of data into database
        $sql = "UPDATE members SET photo =:photo, day=:day, month=:month, year=:year, username=:username, verify=:verify, password=:password WHERE id =:id";
        $conn->query($sql);
        $conn->bind(":id", $id);
        $conn->bind(":month", $month);
        $conn->bind(":day", $day);
        $conn->bind(":year", $year);
        $conn->bind(":photo", $photo_name);
        $conn->bind(":verify", $verify);
        $conn->bind(":username", $username);
        $conn->bind(":password", $conn->Password_Encryption($password));


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