<?php
include('dbconnection.php');
session_start();

// Redirect if not logged in
if (!$_SESSION['emp_email']) {
    header("Location: login.php");
    exit;
}

// Handle user deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_user']) && isset($_POST['delete_id'])) {
    $delete_id = intval($_POST['delete_id']);
    if ($delete_id != $_SESSION['emp_id']) { // Prevent self-deletion
        $delete_sql = "DELETE FROM employee WHERE emp_id = $delete_id";
        if ($con->query($delete_sql)) {
            echo "<script>alert('کاربر با موفقیت حذف شد'); window.location.href='app-profile.php';</script>";
            exit;
        } else {
            echo "<script>alert('خطا در حذف کاربر');</script>";
        }
    } else {
        echo "<script>alert('شما نمی‌توانید خود را حذف کنید');</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>مشاهده پروفایل</title>
    <link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <link href="vendor/jquery-nice-select/css/nice-select.css" rel="stylesheet">
    <link href="vendor/datatables/css/jquery.dataTables.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <script>
    document.addEventListener("keydown", function(event) {
        if (event.key === "Escape") {
            setTimeout(() => { window.history.back(); }, 100);
        }
    });
    </script>
</head>

<body>
<div id="main-wrapper">

    <!-- Nav Header -->
    <div class="nav-header">
        <a href="index.php" class="brand-logo">
            <img src="images/logo1.png" alt="" height="70" width="70">
        </a>
        <div class="nav-control">
            <div class="hamburger"><span class="line"></span><span class="line"></span><span class="line"></span></div>
        </div>
    </div>

    <!-- Header -->
    <div class="header">
        <div class="header-content">
            <nav class="navbar navbar-expand">
                <div class="collapse navbar-collapse justify-content-between">
                    <div class="header-left">
                        <div class="dashboard_bar"><?php echo $_SESSION['emp_type']; ?> پروفایل</div>
                    </div>
                </div>
            </nav>
        </div>
    </div>

    <!-- Sidebar -->
   Sidebar start
        ***********************************-->
    	<div class="dlabnav">
            <div class="dlabnav-scroll">
				<ul class="metismenu" id="menu">
					<li class="dropdown header-profile">
						<a class="nav-link" href="javascript:void(0);" role="button" data-bs-toggle="dropdown">
							<img src="images/profile/profile.png" width="20" alt="">
							<div class="header-info ms-3">
								<span class="font-w600 "><?php echo $_SESSION['emp_name']; ?></span>
								<small class="text-end font-w400"><?php echo $_SESSION['emp_email']; ?></small>
							</div>
						</a>
						<div class="dropdown-menu dropdown-menu-end">
							<a href="app-profile.php" class="dropdown-item ai-icon">
								<svg id="icon-user1" xmlns="http://www.w3.org/2000/svg" class="text-primary" width="18" height="18" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
								<span class="ms-2">پروفایل</span>
							</a>
							<a href="logout.php" class="dropdown-item ai-icon">
								<svg id="icon-logout" xmlns="http://www.w3.org/2000/svg" class="text-danger" width="18" height="18" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
								<span class="ms-2">خروج از سیتم </span>
							</a>
						</div>
					</li>
              
					<li><a class="ai-icon" href="index.php" aria-expanded="false" style='font-size:20px;font-weight: bold;'>
						<i class="flaticon-025-dashboard"></i>
						<span class="nav-text">صفحه اصلی</span>
					</a>
				
				</li>
                    <li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" style='font-size:20px;font-weight: bold;'>
						<i class="icon-user"></i>
							<span class="nav-text">لیست محصلین</span>
							
						</a>
                        <ul aria-expanded="false">
                            <li><a href="add_student.php" style='font-size:20px;font-weight: bold;'>اضافه محصل</a></li>
							<li><a href="view_student.php" style='font-size:20px;font-weight: bold;'>مشاهده محصل</a></li>
							<li><a href="view_all_student.php" style='font-size:20px;font-weight: bold;'>مشاهده محصلین</a></li>
							
                        </ul>
                    </li>
                  

  
                    <li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false">
							<i class="fa fa-book"></i>
							<span class="nav-text" style='font-size:20px;font-weight: bold;'>بخش صنف</span>
						</a>
                        <ul aria-expanded="false">
							<li><a href="add_course.php" style='font-size:20px;font-weight: bold;'>اضافه صنف جدید </a></li>
							<li><a href="view_course.php" style='font-size:20px;font-weight: bold;'>مشاهده صنوف</a></li>

                        </ul>
                    </li>
					<li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" style='font-size:20px;font-weight: bold;'>
						<i class="icon-user"></i>
							<span class="nav-text">بخش اساتید</span>
							
						</a>
                        <ul aria-expanded="false">
                            <li><a href="view_teacher.php" style='font-size:20px;font-weight: bold;'>مشاهده</a></li>
							
							
                        </ul>
                    </li>
					<li><a  class="has-arrow ai-icon" aria-expanded="false">
							<i class="fa fa-file-invoice-dollar"></i>
							<span class="nav-text" style='font-size:20px;font-weight: bold;'>بخش پول صیف</span>
						</a>
						<ul aria-expanded="false">
                            
                            <li><a href="view_cash_point_details.php" style='font-size:20px;font-weight: bold;'>راپور صیف</a></li>
							<li><a href="add_view_expenses.php" style='font-size:20px;font-weight: bold;'>اضافه و مشاهده مصارف</a></li>
							
							
                        </ul>
					</li>

					<li><a  class="has-arrow ai-icon" aria-expanded="false">
							<i class="fa fa-calendar"></i>
							<span class="nav-text" style='font-size:20px;font-weight: bold;'>مشاهده راپور ها</span>
						</a>
						<ul aria-expanded="false">
                            
                            <li><a href="cash_point_report.php" style='font-size:20px;font-weight: bold;'>راپور صیف</a></li>
							<li><a href="student_report.php" style='font-size:20px;font-weight: bold;'>راپور محصلین</a></li>

							
                        </ul>
					</li>
                    <li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" style='font-size:20px;font-weight: bold;'>
						<i class="icon-user"></i>
							<span class="nav-text">تغییر رمز</span>
							
						</a>
                        <ul aria-expanded="false">
                            <li><a href="change_password.php" style='font-size:20px;font-weight: bold;'>تغییر</a></li>
							
							
                        </ul>
                    </li>

                </ul>
			<div class="copyright">
					<p><strong>Student MIS Admin Dashboard</strong> © 2025 All Rights Reserved</p>
					<p class="fs-12">Made with <span class="heart"></span> by Dynamic & Cryptic</p>
				</div>
			</div>
        </div>
        <!--**********************************
            Sidebar end
        *

    <!-- Content Body -->
    <div class="content-body">
        <div class="container-fluid">
            <!-- Profile Card -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="profile card card-body px-3 pt-3 pb-0">
                        <div class="profile-head">
                            <div class="profile-info">
                                <div class="profile-details">
                                    <div class="profile-name px-3 pt-2">
                                        <p>کاربر</p>
                                        <h4 class="text-primary mb-0"><?php echo $_SESSION['emp_name']; ?></h4>
                                    </div>
                                    <div class="profile-email px-2 pt-2">
                                        <p>ایمیل</p>
                                        <h4 class="text-muted mb-0"><?php echo $_SESSION['emp_email']; ?></h4>
                                    </div>
                                    <div class="profile-email px-2 pt-2">
                                        <p>صلاحیت کاربر</p>
                                        <h4 class="text-muted mb-0"><?php echo $_SESSION['emp_type']; ?></h4>
                                    </div>
                                    
                                    <div class="dropdown ms-auto pt-2">
                                        <?php
                                        if ($_SESSION['emp_type'] === 'admin') {
                                            echo '<a href="emp_register.php"><button class="btn btn-primary btn-sm">ایجاد کاربر جدید</button></a>';
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Employee Table for Admin -->
            <?php if ($_SESSION['emp_type'] === 'admin'): ?>
            <div class="row mt-4">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header"><h4 class="card-title">لیست کارمندان</h4></div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="example3" class="table table-bordered table-striped text-center" style="font-size:18px;">
                                    <thead class="bg-light">
                                        <tr>
                                            <th>نمبر</th>
                                            <th>کاربر</th>
                                            <th>ایمیل</th>
                                            <th>صلاحیت</th>
                                            <th>عملیات</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $q2 = "SELECT * FROM employee";
                                        $r = $con->query($q2);
                                        while ($row = $r->fetch_array(MYSQLI_ASSOC)) {
                                            echo "<tr>";
                                            echo "<td>{$row['emp_id']}</td>";
                                            echo "<td>{$row['emp_name']}</td>";
                                            echo "<td>{$row['emp_email']}</td>";
                                            echo "<td>{$row['emp_type']}</td>";
                                            echo "<td>
                                                <form method='POST' onsubmit=\"return confirm('آیا مطمئن هستید که می‌خواهید این کاربر را حذف کنید؟');\">
                                                    <input type='hidden' name='delete_id' value='{$row['emp_id']}'>
                                                    <button type='submit' name='delete_user' class='btn btn-danger btn-sm'>حذف</button>
                                                </form>
                                            </td>";
                                            echo "</tr>";
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Footer -->
    <div class="footer">
        <div class="copyright">
            <p>© 2025 Student MIS Admin Dashboard</p>
        </div>
    </div>

</div>

<!-- JS -->
<script src="vendor/global/global.min.js"></script>
<script src="vendor/bootstrap-select/dist/js/bootstrap-select.min.js"></script>
<script src="vendor/jquery-nice-select/js/jquery.nice-select.min.js"></script>
<script src="vendor/datatables/js/jquery.dataTables.min.js"></script>
<script src="js/plugins-init/datatables.init.js"></script>
<script src="js/custom.min.js"></script>
<script src="js/dlabnav-init.js"></script>
<script src="js/demo.js"></script>
<script src="js/styleSwitcher.js"></script>

</body>
</html>
