<?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"] : "";
$name = isset($_POST["name"]) ? $_POST["name"] : "";
$username = isset($_POST["username"]) ? $_POST["username"] : "";

$errors = array();
$flag = true;
$errorList = "";

if(isset($_POST["save"]))
{
    if(empty($password) || empty($repassword) || empty($username) || empty($name))
    {
        $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 = "insert into admin (name,username,password) values ('{$name}','{$username}','{$password}')";
        $executeSql2 = mysqli_query($connection,$sql);
        $_SESSION["addSuccess"] = 1;
        header('Location: show-admin.php'); 
    }
    else
    {
        foreach($errors as $error)
        {
          $errorList .= $error."<br>";
        }
    }
}

?>
<!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:100px;'>
            <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='add-admin.php' method='post'>
                    <div class="card border-success mb-3">
                    <div class="card-header">إضافة مسؤول جديد </div>
                    <div class="card-body text-success">
                    <div class="form-row">
                            <div class="form-group col">
                                <label>الإسم كامل</label>
                                <input type="text" name="name" id="" class="form-control">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col">
                                <label>إسم المستخدم</label>
                                <input type="text" name="username" id="" class="form-control">
                            </div>
                        </div>
                        <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>