<?php session_start();
include '../db.php';

$page = "staff";
$action = $_GET['action'];

if ($_POST) {
// print_r($_POST);
// die();
    // $saveAry = array(
    //     "school_name"   =>  $_POST["school_name"],
    //     "contact"       =>  $_POST["contact"],
    //     "email"         =>  $_POST["email"],
    //     "website"       =>  $_POST["website"],
    //     "address"       =>  $_POST["address"],
    //     "status"        =>  $_POST['status'],
    //     "added_by"      =>  $_SESSION["admin"]["uid"],
    //     "added_role"    =>  "Super Admin",
    // );

    if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['photo']['tmp_name'],"../uploads/photo/".$lnewfile))
		{
			// $saveAry["photo"]=$lnewfile;
            $_POST["photo"] = $lnewfile;
		}
	}

    $_POST["added_by"] = $_SESSION["admin"]["uid"];
    $_POST["school_id"] = $_SESSION["admin"]["school_id"];

    if ($_GET['action'] == "add") {
        $flg = $db->insertAry("staff", $_POST);
        if(!is_null($flg)){
            echo "<script>alert('Staff added successfully.')</script>";
            redirect("$page");
        } else {
            echo "<script>alert('Something went wrong. Please try again.')</script>";
        }
    }

    if ($_GET['action'] == "edit" && $_GET['id'] != '') {
        $flg = $db->updateAry("staff", $_POST, "where id='$_GET[id]' ");

        echo "<script>alert('Updated successfully.')</script>";
        redirect("$page");
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include "../meta.php"; 

    $page = "staff";
    $action = $_GET['action'];
    
    ?>
</head>

<body>
    <?php
        include '../header.php'; ?>
        
        <div class="container">

            <div class="row">
                <div class="col-md-12">
                    <div class="inner-div">
                    <?php if (($action == "add") || ($action == "edit" && $_GET['id'] != "")) {
                        if ($action == "edit" && $_GET['id'] != "") {
                            $aryForm = $db->getRow("select * from staff where id=$_GET[id] ");
                        }
                    ?>
                        <h2 class="top-heading"><?php echo $page; ?> Details</h2>

                        <form method="post" enctype="multipart/form-data">

                        <div class="row">
                            <div class="col-md-4">

                                <div class="form-group mb-5 mt-3">
                                    <label>Name</label>    
                                    <input type="text" name="name" value="<?php echo $aryForm['name']; ?>" class="form-control" placeholder="name" required />
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="mobile" value="<?php echo $aryForm['mobile']; ?>" class="form-control" placeholder="Mobile" />
                                    <label>Mobile No.</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="email" name="email" value="<?php echo $aryForm['email']; ?>" class="form-control" placeholder="Email" />
                                    <label>Email</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="qualification" value="<?php echo $aryForm['qualification']; ?>" class="form-control" placeholder="Qualification" />
                                    <label>Qualification</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="designation" value="<?php echo $aryForm['designation']; ?>" class="form-control" placeholder="Designation" />
                                    <label>Designation</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="userid" value="<?php echo $aryForm['userid']; ?>" class="form-control" placeholder="UserId" />
                                    <label>UserId</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="password" value="<?php echo $aryForm['password']; ?>" class="form-control" placeholder="Password" />
                                    <label>Password</label>
                                </div>

                            </div>

                            <div class="col-md-4">

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="fname" value="<?php echo $aryForm['fname']; ?>" class="form-control" placeholder="Father Name" />
                                    <label>Father Name</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="salary" value="<?php echo $aryForm['salary']; ?>" class="form-control" placeholder="Salary" />
                                    <label>Salary</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="account" value="<?php echo $aryForm['account']; ?>" class="form-control" placeholder="Account" />
                                    <label>Account No.</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="ifsc" value="<?php echo $aryForm['ifsc']; ?>" class="form-control" placeholder="ifsc" />
                                    <label>IFSC Code</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="pan" value="<?php echo $aryForm['pan']; ?>" class="form-control" placeholder="PAN" />
                                    <label>Pan Card</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="aadhaar" value="<?php echo $aryForm['aadhaar']; ?>" class="form-control" placeholder="aadhaar" />
                                    <label>Aadhaar Card</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="text" name="uan" value="<?php echo $aryForm['uan']; ?>" class="form-control" placeholder="uan" />
                                    <label>UAN No.</label>
                                </div>

                            </div>

                            <div class="col-md-4">

                                <div class="form-floating mb-3 mt-3">
                                    <input type="date" name="doa" value="<?php echo $aryForm['doa']; ?>" class="form-control" placeholder="Date of Joining" />
                                    <label>Date of Joining</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <input type="date" name="dob" value="<?php echo $aryForm['dob']; ?>" class="form-control" placeholder="D.O.B." />
                                    <label>D.O.B.</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <select class="form-select" name="gender">
                                        <option value="">--Select--</option>
                                        <option value="Male">Male</option>
                                        <option value="Female">Female</option>
                                    </select>
                                    <label>Gender</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <textarea class="form-control" name="address" placeholder="Address"></textarea>
                                    <label>Address</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <select class="form-select" name="status">
                                    <option value="1" <?php if ($aryForm['status'] === 1) echo "selected"; ?>>Active</option>
                                        <option value="0" <?php if ($aryForm['status'] === 0) echo "selected"; ?>>Inactive</option>
                                    </select>
                                    <label>Status</label>
                                </div>

                                <div class="form-floating mb-3 mt-3">
                                    <select class="form-select" name="role">
                                        <option value="">--Select Role--</option>
                                        <?php $allroles = $db->getRows("select * from role");
                                            foreach ($allroles as $role) { ?>
                                                <option value="<?php echo $role['id']; ?>"  <?php if($user['role']==$role['id']) echo "selected"; ?>><?php echo $role['role_name']; ?></option>
                                                <?php
                                            }
                                            ?>
                                    </select>
                                    <label>Assign Role</label>
                                </div>


                                <div class="form-floating mt-2">
                                    <input type="file" class="form-control" name="photo" placeholder="Photo" />
                                    <label>Photo</label>
                                </div>

                            </div>

                            <div class="text-center mt-5 mb-5">
                                <input type="submit" value="Submit" />
                            </div>

                        </div>

                        </form>                    
                    <?php } else {
                        $aryForm = $db->getRows("select * from staff ");
                        ?>
                        <h2 class="top-heading"><?php echo $page; ?> List
                            <a href="<?php echo $page ?>?action=add" class="btn btn-success">Add <?php echo $page; ?></a>
                        </h2>


                        <table class="table table-striped">
                            <thead class="table-dark">
                                <tr>
                                    <th>S.No.</th>
                                    <th>Name</th>
                                    <th>Mobile</th>
                                    <th>Email</th>
                                    <th>Password</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($aryForm as $staff){ $i++;
                                    ?>
                                    <tr>
                                        <td>
                                            <?php echo $i; ?>
                                        </td>
                                    </tr>
                                    <?php
                                } ?>
                            </tbody>
                        </table>

                    <?php } ?>

                    </div>
                </div>
            </div>

        </div>        

        <?php
        // include '../dashboard.php';

        include '../footer.php';
    ?>

</body>

</html>