<?php
include '../connection.php';
if(!isset($_SESSION)){session_start();}
if(!isset($_SESSION["ID"])){header('Location: index.php');}
if($_SESSION["role"] != 2){header('Location: index.php');}
$password = isset($_POST["password"]) ? $_POST["password"] : "";
$repassword = isset($_POST["repassword"]) ? $_POST["repassword"] : "";
$errors = array();
$flag = true;
$errorList = "";
if(!isset($_GET["ID"]) && !isset($_SESSION["adminID"]))
{
    header('Location: show-admin.php');
}
elseif(isset($_GET["ID"]))
{
    $_SESSION["adminID"] = $_GET["ID"];
}
elseif(isset($_SESSION["adminID"]))
{
    $_SESSION["adminID"] ;
}
if(isset($_POST["save"]))
{
    if(empty($password) || empty($repassword))
    {
        $errors["empty"] = "يجب تعبئة جميع الحقول";
        $flag = false;
    }
    elseif($password != $repassword)
    {
        $errors["match"] = "يجب ان تتطابق كلمة المرور";
        $flag = false;
    }
    elseif(strlen($password) < 8)
    {
        $errors["size"] = "يجب ان تكون كلمة المرور 8 خانات واكبر";
        $flag = false;
    }

    if($flag === true)
    {
        $password = sha1($password);
        $sql = "update admin set password='{$password}' where ID = {$_SESSION["adminID"]}";
        $executeSql2 = mysqli_query($connection,$sql);
        $_SESSION["editPSuccess"] = 1;
        header('Location: show-admin.php'); 
    }
    else
    {
        foreach($errors as $error)
        {
          $errorList .= $error."<br>";
        }
    }
}

$sqlRetrive = "select * from admin where ID={$_SESSION["adminID"]}";
$executeSql = mysqli_query($connection,$sqlRetrive);
$data = mysqli_fetch_row($executeSql);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>خذ فرصة</title>
    <?php include 'cssFile.php'; ?>
    <style>
    .blockquote-footer
    {
        font-size:16px;
    }
    </style>
</head>
<body dir='rtl'>
    <?php include 'navbar.php';?>

    <div class="content-body">
        <div class='container' style='margin-top:150px;'>
            <div class="row justify-content-center">
                <div class="col-md-5 text-right">
                <?php
            if($flag === false)
            {
            echo "
            <div class='alert alert-danger text-right'>
            $errorList
            </div>";
            }
        ?>
                    <form action='edit-password.php' method='post'>
                    <div class="card border-success mb-3">
                    <div class="card-header">تعديل كلمة مرور - <?php echo $data[1];?> - </div>
                    <div class="card-body text-success">
                        <div class="form-row">
                            <div class="form-group col">
                                <label>كلمة المرور الجديدة</label>
                                <input type="password" name="password" id="" class="form-control">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col">
                                <label>تأكيد كلمة المرور</label>
                                <input type="password" name="repassword" id="" class="form-control">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col  text-center">
                                <button class='btn btn-success' name='save' type='submit'>تعديل</button>
                            </div>
                        </div>
                    </div>
                    </div>
</form>
                </div>
            </div>
        </div>
    </div>
    <!-- End content-body -->

    <?php include 'jsFile.php'; ?>
   
</body>
</html>