<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();

$redirect = $conn->base_url()."member";


        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $username = $_POST['username'];

        //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{
            //all checks passed, update password
            $sql = "UPDATE members SET password =:password WHERE username =:username";
            $conn->query($sql);
            $conn->bind(":password", $conn->Password_Encryption($password));
            $conn->bind(":username", $username);

            if($conn->execute()){
                echo "<script>
            $(function () { //ready
                      toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                      toastr.success('Password Updated Successfully');
                  });
            </script>";
            }else{
                echo "<script>
                $(function () { //ready
                          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                          toastr.error('An Error ocurred While Updating Password');
                      });
                </script>";
            }
        }
    
