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

$action = $_GET['action'];

if($_POST['submit']){
    $saveAry = array(
        "schoolname"    =>  $_POST["schoolname"],
        "userid"        =>  $_POST["userid"],
        "password"      =>  $_POST["password"],
        "mobile"        =>  $_POST["mobile"],
        "email"         =>  $_POST["email"],
        "name"          =>  $_POST["name"],
        "address"       =>  $_POST["address"],
        "added_by"      =>  $_SESSION["superadmin"]["uid"],
        "added_role"    =>  "Super Admin",
        "status"        =>  1
    );

    $userary = array(
        "userid"        =>  $_POST["userid"],
        "password"      =>  $_POST["password"],
        "schoolname"    =>  $_POST["schoolname"],
        "mobile"        =>  $_POST["mobile"],
        "email"         =>  $_POST["email"],
        "added_by"      =>  $_SESSION["superadmin"]["uid"],
        "added_role"    =>  "Super Admin",
        "status"        =>  1
    );

    if($_GET['action']=="add"){
        $flg = $db->insertAry("schools", $saveAry);
        if($flg > 0){
            $userary['school_id'] = $flg;
            $db->insertAry("users", $userary);

            echo "<script>alert('School added successfully.')</script>";
            redirect('add_school.php?action=view');
        }else {
            echo "<script>alert('Something went wrong. Please try again.')</script>";
        }
    
    }

    if($_GET['action'] == "edit" && $_GET['id']!=''){
        $flg = $db->updateAry("schools", $saveAry, "where id='$_GET[id]' ");
        $flg = $db->updateAry("users", $userary, " where school_id='$_GET[id]' ");

        echo "<script>alert('Updated successfully.')</script>";
        redirect('add_school.php?action=view');
    }

}

?>
<html>
    <head>
        <?php include 'meta.php'; ?>        
    </head>

<body>
    <?php include 'header.php'; ?>

    <div class="container-fluid">

    <div class="row">

        <div class="col-md-2 col-3">
            <?php include 'sidebar.php'; ?>
        </div>
        <div class="col-md-10 col-9">
            
            <div class="inner-div">

                <?php if(($action == "add") || ($action =="edit" && $_GET['id']!="" ) ){ 
                    if($action =="edit" && $_GET['id']!="" ){
                        $school = $db->getRow("select * from schools where id=$_GET[id] ");
                    }
                ?>

                <h2 class="top-heading">Add New School Details</h2>

                <form method="post" >
                    
                <div class="form-group">
                    <label>School Name <span class="mandatory">*</span></label>
                    <input type="text" name="schoolname" value="<?php echo $school['schoolname']; ?>" required />
                </div>
                    
                <div class="form-group">
                    <label>User Id <span class="mandatory">*</span></label>
                    <input type="text" name="userid" value="<?php echo $school['userid']; ?>" required />
                </div>

                <div class="form-group">
                    <label>Password <span class="mandatory">*</span></label>
                    <input type="text" name="password" value="<?php echo $school['password']; ?>" required />
                </div>

                <div class="form-group">
                    <label>Mobile No.</label>
                    <input type="text" name="mobile" value="<?php echo $school['mobile']; ?>" />
                </div>

                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" value="<?php echo $school['email']; ?>" />
                </div>

                <div class="form-group">
                    <label>User Name</label>
                    <input type="text" name="name" value="<?php echo $school['name']; ?>" />
                </div>

                <div class="form-group">
                    <label style="margin-top:-20px;">Address</label>
                    <textarea name="address"><?php echo $school['address']; ?></textarea>
                </div>
                
                <div class="text-center">
                    <input type="submit" name="submit" value="Submit" />
                </div>


                </form>
                <?php }
                    else {
                        $allschools = $db->getRows("select * from schools");
                        ?>

                        <h2 class="top-heading">Schools List
                            <a href="./add_school.php?action=add" class="btn btn-success">Add School</a>
                        </h2>

                        <table class="table table-striped table-sm">
                            <thead class="table-success">
                            <tr>
                                <th>S.No.</th>
                                <th>School Name</th>
                                <th>User Id</th>
                                <th>Password</th>
                                <th>Mobile</th>
                                <th>Email</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
                                <?php foreach($allschools as $school) { $i++; ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $school['schoolname']; ?></td>
                                    <td><?php echo $school['userid']; ?></td>
                                    <td><?php echo $school['password']; ?></td>
                                    <td><?php echo $school['mobile']; ?></td>
                                    <td><?php echo $school['email']; ?></td>
                                    <td>
                                        <a href="add_school.php?action=edit&id=<?php echo $school['id']; ?>">
                                            <button class="btn btn-success edit-btn">Edit</button>
                                        </a>
                                        <button onclick="delete_school(<?php echo $school['id']; ?>)" class="btn btn-danger delete-btn">Delete</button>
                                    </td>
                                </tr>
                                
                                <?php } ?>

                            </tbody>
                        </table>
                        
                <?php
                    }
                
                ?>

            </div>

        </div>

    </div>

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

    <script>
        function delete_school(id){
            if (confirm("Are you sure want to delete this school?") == true) {
                $.post( "ajax.php", { type: "delete_school", id: id  } , function( data ){			
                    alert(data);
                    location.reload();
                });
            }
        }
    </script>

</body>

</html>