<?php
session_start();
include('include/config.php');
if(strlen($_SESSION['alogin'])==0){    
    header('location:index.php');
} else {

date_default_timezone_set('Asia/Kolkata');
$currentTime = date('d-m-Y h:i:s A', time());

// CREATE OR UPDATE DEPARTMENT
if(isset($_POST['submit']))
{
    $university=$_POST['university'];
    $college=$_POST['college'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $password=$_POST['password'];
    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;

    if($id>0){
        // UPDATE
        mysqli_query($bd,"UPDATE department SET name='$name', college='$college', university='$university', email='$email', password='$password' WHERE id='$id'");
        $_SESSION['msg']="✅ Department Updated !!";
    } else {
        // INSERT
        mysqli_query($bd,"INSERT INTO department(name, college, university, email, password) VALUES('$name','$college','$university','$email','$password')");
        $_SESSION['msg']="✅ Department Created !!";
    }
}

// DELETE DEPARTMENT
if(isset($_GET['del'])){
    mysqli_query($bd,"DELETE FROM department WHERE id='".$_GET['id']."'");
    $_SESSION['delmsg']="❌ Department Deleted !!";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin | Department</title>

<!-- Bootstrap & Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">

<style>
body { font-family: 'Poppins', sans-serif; background:#fff5f0; margin:0; }
.navbar { background: linear-gradient(45deg,#ff6600,#ff9933); position:fixed; top:0; left:0; width:100%; z-index:1000; }
.sidebar { height:100vh; background:#ff6600; color:#fff; padding-top:1rem; position:fixed; width:250px; top:60px; overflow-y:auto;}
.sidebar a { color:#fff; display:block; padding:10px 20px; text-decoration:none;}
.sidebar a:hover, .sidebar a.active { background:#ff9933; border-radius:5px;}
.main-content { margin-left:260px; margin-top:70px; padding:30px; }
.card { border-radius:10px; box-shadow:0 4px 15px rgba(0,0,0,0.1); background:#fff; }
.card-header { background:linear-gradient(90deg,#ff6600,#ff9933); color:#fff; font-weight:600; border-radius:10px 10px 0 0; }
.btn-custom { background:#ff6600; color:#fff; border:none; border-radius:6px; transition:0.3s; }
.btn-custom:hover { background:#ff9933; }
footer { background:#fff; padding:15px; text-align:center; margin-left:260px; border-top:1px solid #ddd; color:#333;}
</style>
</head>
<body>

<?php include('include/header.php'); ?>
<div class="sidebar"><?php include('include/sidebar.php'); ?></div>

<div class="main-content">
    <div class="container-fluid">
        <h4>Departments</h4>

        <?php if(isset($_SESSION['msg']) && $_SESSION['msg']!=""){ ?>
            <div class="alert alert-success text-center"><?php echo htmlentities($_SESSION['msg']); $_SESSION['msg']=""; ?></div>
        <?php } ?>
        <?php if(isset($_SESSION['delmsg']) && $_SESSION['delmsg']!=""){ ?>
            <div class="alert alert-danger text-center"><?php echo htmlentities($_SESSION['delmsg']); $_SESSION['delmsg']=""; ?></div>
        <?php } ?>

        <!-- Add Department Button -->
        <div class="mb-3">
            <button class="btn btn-custom" data-bs-toggle="modal" data-bs-target="#departmentModal">+ Add Department</button>
        </div>

        <!-- Departments Table -->
        <div class="card">
            <div class="card-body table-responsive">
                <table class="table table-bordered table-striped align-middle">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>University</th>
                            <th>College</th>
                            <th>Department</th>
                            <th>Email</th>
                            <th>Password</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $query=mysqli_query($bd,"SELECT * FROM department");
                    $cnt=1;
                    while($row=mysqli_fetch_array($query)){ ?>
                        <tr>
                            <td><?php echo $cnt;?></td>
                            <td><?php echo htmlentities($row['university']);?></td>
                            <td><?php echo htmlentities($row['college']);?></td>
                            <td><?php echo htmlentities($row['name']);?></td>
                            <td><?php echo htmlentities($row['email']);?></td>
                            <td><?php echo htmlentities($row['password']);?></td>
                            <td>
                                <button class="btn btn-sm btn-custom" data-bs-toggle="modal" data-bs-target="#editDepartment<?php echo $row['id']; ?>"><i class="bi bi-pencil-fill"></i></button>
                                <a href="department.php?id=<?php echo $row['id'];?>&del=delete" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete?')"><i class="bi bi-trash-fill"></i></a>
                            </td>
                        </tr>

                        <!-- Edit Department Modal -->
                        <div class="modal fade" id="editDepartment<?php echo $row['id']; ?>" tabindex="-1" aria-labelledby="editDepartmentLabel<?php echo $row['id']; ?>" aria-hidden="true">
                          <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header" style="background:linear-gradient(90deg,#ff6600,#ff9933); color:white;">
                                <h5 class="modal-title" id="editDepartmentLabel<?php echo $row['id']; ?>">Edit Department</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                              </div>
                              <div class="modal-body">
                                <form method="post">
                                    <input type="hidden" name="id" value="<?php echo $row['id']; ?>">
                                    <div class="mb-3">
                                        <label class="form-label">Select University</label>
                                        <select name="university" class="form-select" required>
                                            <option value="<?php echo $row['university']; ?>"><?php echo $row['university']; ?></option>
                                            <?php $uquery=mysqli_query($bd,"SELECT * FROM university");
                                            while($urow=mysqli_fetch_array($uquery)){
                                                if($urow['name']==$row['university']) continue; ?>
                                                <option value="<?php echo $urow['name']; ?>"><?php echo $urow['name']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Select College</label>
                                        <select name="college" class="form-select" required>
                                            <option value="<?php echo $row['college']; ?>"><?php echo $row['college']; ?></option>
                                            <?php $cquery=mysqli_query($bd,"SELECT * FROM college");
                                            while($crow=mysqli_fetch_array($cquery)){
                                                if($crow['name']==$row['college']) continue; ?>
                                                <option value="<?php echo $crow['name']; ?>"><?php echo $crow['name']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Department Name</label>
                                        <input type="text" name="name" class="form-control" value="<?php echo $row['name'];?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Email</label>
                                        <input type="text" name="email" class="form-control" value="<?php echo $row['email'];?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Password</label>
                                        <input type="password" name="password" class="form-control" value="<?php echo $row['password'];?>" required>
                                    </div>
                                    <button type="submit" name="submit" class="btn btn-custom w-100">Update</button>
                                </form>
                              </div>
                            </div>
                          </div>
                        </div>

                    <?php $cnt++; } ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Add Department Modal -->
        <div class="modal fade" id="departmentModal" tabindex="-1" aria-labelledby="departmentModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header" style="background:linear-gradient(90deg,#ff6600,#ff9933); color:white;">
                <h5 class="modal-title" id="departmentModalLabel">Add Department</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
              </div>
              <div class="modal-body">
                <form method="post">
                    <div class="mb-3">
                        <label class="form-label">Select University</label>
                        <select name="university" class="form-select" required>
                            <?php $uquery=mysqli_query($bd,"SELECT * FROM university");
                            while($urow=mysqli_fetch_array($uquery)){ ?>
                                <option value="<?php echo $urow['name']; ?>"><?php echo $urow['name']; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Select College</label>
                        <select name="college" class="form-select" required>
                            <?php $cquery=mysqli_query($bd,"SELECT * FROM college");
                            while($crow=mysqli_fetch_array($cquery)){ ?>
                                <option value="<?php echo $crow['name']; ?>"><?php echo $crow['name']; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Department Name</label>
                        <input type="text" name="name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <input type="text" name="email" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Password</label>
                        <input type="password" name="password" class="form-control" required>
                    </div>
                    <button type="submit" name="submit" class="btn btn-custom w-100">Create</button>
                </form>
              </div>
            </div>
          </div>
        </div>

    </div>
</div>

<footer>© <?php echo date("Y"); ?> Admin Dashboard. All rights reserved.</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php } ?>
