<?php
// admin/dashboard.php - Complete Admin Dashboard with Dynamic Title
session_start();
require_once '../includes/config.php';
require_once '../includes/sms_config.php';

if(!isset($_SESSION['user_id']) || ($_SESSION['user_role'] != 'admin' && $_SESSION['user_role'] != 'instructor')) {
    header('Location: ../auth.php');
    exit;
}

$user_id = $_SESSION['user_id'];
$user_role = $_SESSION['user_role'];
$message = $_GET['msg'] ?? '';
$msg_type = $_GET['type'] ?? 'info';
$active_tab = $_GET['tab'] ?? 'overview';

// Get dynamic settings
$site_title = getSetting($pdo, 'site_title', 'বাংলা LMS');
$logo_text = getSetting($pdo, 'logo_text', 'বাংলা LMS');
$logo_icon = getSetting($pdo, 'logo_icon', 'fa-graduation-cap');
$use_image = getSetting($pdo, 'use_image_logo', '0');
$logo_image = getSetting($pdo, 'logo_image', '');
$primary_color = getSetting($pdo, 'primary_color', '#6C5CE7');

// ==================== PROFILE UPDATE ====================
$profile_msg = ''; $profile_error = '';
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_profile'])) {
    $full_name = trim($_POST['full_name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $bio = trim($_POST['bio'] ?? '');
    $designation = trim($_POST['designation'] ?? '');
    $expertise = trim($_POST['expertise'] ?? '');
    $facebook_url = trim($_POST['facebook_url'] ?? '');
    $linkedin_url = trim($_POST['linkedin_url'] ?? '');
    $youtube_url = trim($_POST['youtube_url'] ?? '');
    
    if(empty($full_name)) $profile_error = 'নাম প্রয়োজন';
    elseif(empty($email)) $profile_error = 'ইমেইল প্রয়োজন';
    elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) $profile_error = 'সঠিক ইমেইল ফরম্যাট লিখুন';
    else {
        $chk = $pdo->prepare("SELECT id FROM users WHERE email=? AND id!=?"); $chk->execute([$email, $user_id]);
        if($chk->fetch()) $profile_error = 'ইমেইলটি অন্য কেউ ব্যবহার করছে';
    }
    
    $avatar = $_POST['existing_avatar'] ?? 'default-avatar.png';
    if(!$profile_error && isset($_FILES['avatar']) && $_FILES['avatar']['error']==0) {
        $ext = strtolower(pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION));
        if(in_array($ext, ['jpg','jpeg','png','gif','webp'])) {
            if($avatar!='default-avatar.png' && file_exists('../uploads/avatars/'.$avatar)) unlink('../uploads/avatars/'.$avatar);
            $avatar = 'user_'.$user_id.'_'.time().'.'.$ext;
            move_uploaded_file($_FILES['avatar']['tmp_name'], '../uploads/avatars/'.$avatar);
        }
    }
    
    if(!$profile_error) {
        $pdo->prepare("UPDATE users SET full_name=?,email=?,phone=?,bio=?,avatar=?,designation=?,expertise=?,facebook_url=?,linkedin_url=?,youtube_url=? WHERE id=?")->execute([$full_name,$email,$phone,$bio,$avatar,$designation,$expertise,$facebook_url,$linkedin_url,$youtube_url,$user_id]);
        $_SESSION['user_name']=$full_name;
        $profile_msg='✅ প্রোফাইল আপডেট হয়েছে!';
    }
}

// ==================== PASSWORD CHANGE ====================
$pass_msg=''; $pass_error='';
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['change_password'])) {
    $c=$_POST['current_password']; $n=$_POST['new_password']; $cf=$_POST['confirm_password'];
    $s=$pdo->prepare("SELECT password FROM users WHERE id=?"); $s->execute([$user_id]); $up=$s->fetchColumn();
    if(!password_verify($c,$up)) $pass_error='বর্তমান পাসওয়ার্ড ভুল';
    elseif($n!==$cf) $pass_error='নতুন পাসওয়ার্ড মিলছে না';
    elseif(strlen($n)<6) $pass_error='পাসওয়ার্ড কমপক্ষে ৬ অক্ষর';
    else { $pdo->prepare("UPDATE users SET password=? WHERE id=?")->execute([password_hash($n,PASSWORD_DEFAULT),$user_id]); $pass_msg='✅ পাসওয়ার্ড পরিবর্তন হয়েছে!'; }
}

// ==================== CATEGORY & MODULE HANDLERS ====================
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['add_category'])) {
    $pdo->prepare("INSERT INTO categories (name_bn,name_en,slug,icon) VALUES (?,?,?,?)")->execute([trim($_POST['name_bn']),trim($_POST['name_en']),createSlug(trim($_POST['name_en'])),trim($_POST['icon']??'fa-book')]);
    header('Location: dashboard.php?tab=categories&msg=ক্যাটাগরি+যোগ+হয়েছে&type=success'); exit;
}
if(isset($_GET['delete_category'])) { $pdo->prepare("DELETE FROM categories WHERE id=?")->execute([$_GET['delete_category']]); header('Location: dashboard.php?tab=categories&msg=ক্যাটাগরি+ডিলিট+হয়েছে&type=success'); exit; }
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['add_module'])) {
    $pdo->prepare("INSERT INTO modules (course_id,title_bn,title_en,order_number) VALUES (?,?,?,?)")->execute([$_POST['course_id'],trim($_POST['title_bn']),trim($_POST['title_en']),(int)$_POST['order_number']]);
    $ct=$pdo->prepare("SELECT COUNT(*) FROM modules WHERE course_id=?"); $ct->execute([$_POST['course_id']]);
    $pdo->prepare("UPDATE courses SET module_count=? WHERE id=?")->execute([$ct->fetchColumn(),$_POST['course_id']]);
    header('Location: dashboard.php?tab=modules&msg=মডিউল+যোগ+হয়েছে&type=success'); exit;
}

// ==================== GET DATA ====================
$user = getUserData($pdo, $user_id);
$stats = [
    'students'=>$pdo->query("SELECT COUNT(*) FROM users WHERE role='student'")->fetchColumn(),
    'courses'=>$pdo->query("SELECT COUNT(*) FROM courses")->fetchColumn(),
    'lessons'=>$pdo->query("SELECT COUNT(*) FROM lessons")->fetchColumn(),
    'quizzes'=>$pdo->query("SELECT COUNT(*) FROM quizzes")->fetchColumn(),
    'modules'=>$pdo->query("SELECT COUNT(*) FROM modules")->fetchColumn(),
    'revenue'=>$pdo->query("SELECT COALESCE(SUM(amount_paid),0) FROM enrollments WHERE payment_status='completed'")->fetchColumn(),
    'pending'=>$pdo->query("SELECT COUNT(*) FROM enrollments WHERE payment_status='pending'")->fetchColumn(),
];
$categories = $pdo->query("SELECT * FROM categories ORDER BY name_bn")->fetchAll();
$courses = $pdo->query("SELECT id,title_bn FROM courses WHERE status='published' ORDER BY title_bn")->fetchAll();
$modules = $pdo->query("SELECT m.*,c.title_bn as course_title FROM modules m JOIN courses c ON m.course_id=c.id ORDER BY c.title_bn,m.order_number")->fetchAll();
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>অ্যাডমিন - <?= htmlspecialchars($site_title) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
    <style>
        :root{--primary:<?=$primary_color?>;--g1:linear-gradient(135deg,<?=$primary_color?>,<?=$primary_color?>dd);--g2:linear-gradient(135deg,#f093fb,#f5576c);--g3:linear-gradient(135deg,#4facfe,#00f2fe);--g4:linear-gradient(135deg,#43e97b,#38f9d7);--g5:linear-gradient(135deg,#fa709a,#fee140);--shadow:0 4px 20px rgba(0,0,0,0.08);--dark:#1a1a2e}
        *{margin:0;padding:0;box-sizing:border-box}
        body{background:#f0f2f8;font-family:'Segoe UI','Hind Siliguri',Arial,sans-serif;min-height:100vh}
        .navbar{background:rgba(255,255,255,0.9);backdrop-filter:blur(20px);box-shadow:0 2px 20px rgba(0,0,0,0.06);padding:14px 0;position:sticky;top:0;z-index:1000}
        .container-fluid{max-width:1400px;padding:20px}
        
        /* Dynamic Logo */
        .navbar-brand{font-weight:800;font-size:1.4rem;background:var(--g1);-webkit-background-clip:text;-webkit-text-fill-color:transparent;text-decoration:none;display:flex;align-items:center;gap:8px}
        .navbar-brand img{height:36px}
        
        .sidebar{background:linear-gradient(180deg,<?=$primary_color?>dd,<?=$primary_color?>);border-radius:24px;padding:22px 18px;box-shadow:0 10px 40px rgba(0,0,0,0.2);position:sticky;top:100px;max-height:82vh;overflow-y:auto;border:1px solid rgba(255,255,255,0.1)}
        .sidebar::-webkit-scrollbar{width:4px}.sidebar::-webkit-scrollbar-thumb{background:rgba(255,255,255,0.2);border-radius:4px}
        .sidebar .nav-link{padding:10px 15px;border-radius:12px;margin-bottom:3px;color:rgba(255,255,255,0.85);font-weight:500;transition:0.3s;text-decoration:none;display:flex;align-items:center;gap:10px;font-size:0.88rem}
        .sidebar .nav-link:hover{background:rgba(255,255,255,0.12);transform:translateX(5px);color:white}
        .sidebar .nav-link.active{background:rgba(255,255,255,0.2);color:white;font-weight:700}
        .sidebar .nav-link i{width:20px;text-align:center}
        .sidebar hr{margin:8px 0;opacity:0.15}
        .sidebar .menu-label{font-size:0.65rem;text-transform:uppercase;letter-spacing:1.5px;color:rgba(255,255,255,0.5);padding:6px 10px 2px;font-weight:600}
        
        .sidebar-user{text-align:center;margin-bottom:18px;padding:16px 10px;border-radius:14px;background:rgba(255,255,255,0.08);border:1px solid rgba(255,255,255,0.1)}
        .sidebar-user img{width:55px;height:55px;border-radius:50%;object-fit:cover;border:3px solid rgba(255,255,255,0.4);cursor:pointer;transition:0.3s}
        .sidebar-user img:hover{transform:scale(1.08);border-color:white}
        .sidebar-user h6{color:white;font-weight:700;margin:6px 0 2px;font-size:0.85rem}
        .sidebar-user small{color:rgba(255,255,255,0.7);font-size:0.75rem}
        .sidebar-user .btn-icon{width:30px;height:30px;border-radius:50%;border:1px solid rgba(255,255,255,0.3);background:rgba(255,255,255,0.1);color:white;cursor:pointer;transition:0.3s;display:inline-flex;align-items:center;justify-content:center;font-size:0.75rem}
        .sidebar-user .btn-icon:hover{background:rgba(255,255,255,0.25);transform:scale(1.1)}
        
        .stat-card{background:white;border-radius:18px;padding:20px 16px;box-shadow:0 2px 12px rgba(0,0,0,0.04);transition:0.35s;border:1px solid transparent;position:relative;overflow:hidden;cursor:default}
        .stat-card:hover{transform:translateY(-6px);box-shadow:0 15px 35px rgba(0,0,0,0.1);border-color:rgba(108,92,231,0.1)}
        .stat-card .icon{width:44px;height:44px;border-radius:12px;display:flex;align-items:center;justify-content:center;color:white;font-size:1.1rem;margin-bottom:12px;transition:0.3s}
        .stat-card:hover .icon{transform:rotate(5deg) scale(1.08)}
        .stat-card h3{font-weight:800;font-size:1.5rem;margin:0;color:var(--dark)}
        .stat-card p{color:#888;font-size:0.8rem;margin:3px 0 0}
        
        .main-card{background:white;border-radius:18px;padding:24px;box-shadow:0 2px 12px rgba(0,0,0,0.04);margin-bottom:18px;transition:0.3s}
        .main-card:hover{box-shadow:var(--shadow)}
        .form-control,.form-select{border-radius:10px;padding:10px 14px;border:2px solid #e8eaef;width:100%;margin-bottom:8px;font-size:0.88rem;background:#fafbfd}
        .form-control:focus,.form-select:focus{border-color:var(--primary);outline:none;box-shadow:0 0 0 3px rgba(108,92,231,0.05);background:white}
        .btn{border-radius:50px;padding:8px 20px;font-weight:600;cursor:pointer;transition:0.3s;border:none}
        .btn-primary{background:var(--g1);color:white}.btn-primary:hover{transform:translateY(-2px);box-shadow:0 8px 20px rgba(108,92,231,0.3)}
        .btn-sm{padding:5px 12px;font-size:0.8rem}.btn-warning{background:#ffc107;color:#333}
        .btn-outline-primary{border:2px solid var(--primary);color:var(--primary);background:white}
        .btn-settings{background:white;color:var(--primary);border:2px solid var(--primary);padding:8px 16px;border-radius:50px;font-weight:600;font-size:0.85rem;cursor:pointer;transition:0.3s;text-decoration:none;display:inline-flex;align-items:center;gap:6px}
        .btn-settings:hover{background:var(--primary);color:white;transform:translateY(-2px);box-shadow:0 8px 20px rgba(108,92,231,0.25)}
        
        .modal-header{background:var(--g1);color:white;border-radius:18px 18px 0 0;padding:18px 22px}
        .modal-content{border-radius:18px;border:none}
        .rounded-pill{border-radius:50px!important}
        
        @media(max-width:991px){.sidebar{position:static;max-height:none;margin-bottom:20px}}
    </style>
</head>
<body>

<nav class="navbar">
    <div class="container-fluid"><div class="d-flex justify-content-between align-items-center">
        <!-- Dynamic Logo -->
        <a href="../index.php" class="navbar-brand">
            <?php if($use_image=='1' && !empty($logo_image) && file_exists('../uploads/logo/'.$logo_image)): ?>
                <img src="../uploads/logo/<?=$logo_image?>" alt="<?=htmlspecialchars($site_title)?>">
            <?php else: ?>
                <i class="fas <?=$logo_icon?> me-2"></i><?=htmlspecialchars($logo_text)?>
            <?php endif; ?>
        </a>
        <div class="d-flex gap-2">
            <?php if($user_role == 'admin'): ?>
                <a href="settings.php" class="btn-settings" title="সাইট সেটিংস">
                    <i class="fas fa-cog"></i> সেটিংস
                </a>
            <?php endif; ?>
            <a href="../index.php" class="btn btn-outline-secondary btn-sm rounded-pill">সাইট দেখুন</a>
            <a href="../logout.php" class="btn btn-sm rounded-pill" style="background:#ff4757;color:white;">লগআউট</a>
        </div>
    </div></div>
</nav>

<div class="container-fluid py-2"><div class="row">
    
    <!-- SIDEBAR -->
    <div class="col-lg-4 col-xl-3 mb-3">
        <div class="sidebar">
            <div class="sidebar-user">
                <img src="../uploads/avatars/<?=$user['avatar']?:'default-avatar.png'?>" onclick="new bootstrap.Modal(document.getElementById('profileModal')).show()" onerror="this.src='https://ui-avatars.com/api/?name=<?=urlencode($user['full_name'])?>&background=fff&color=<?=str_replace('#','',$primary_color)?>&size=55'">
                <h6><?=htmlspecialchars($user['full_name'])?></h6>
                <small><?=$user_role=='admin'?'👑 অ্যাডমিন':'👨‍🏫 ইন্সট্রাক্টর'?></small>
                <div class="mt-2 d-flex gap-2 justify-content-center">
                    <button class="btn-icon" onclick="new bootstrap.Modal(document.getElementById('profileModal')).show()"><i class="fas fa-user-edit"></i></button>
                    <button class="btn-icon" onclick="new bootstrap.Modal(document.getElementById('passwordModal')).show()"><i class="fas fa-key"></i></button>
                </div>
            </div>
            <div class="menu-label">⚡ মেনু</div>
            <a href="?tab=overview" class="nav-link <?=$active_tab=='overview'?'active':''?>"><i class="fas fa-th-large"></i> ড্যাশবোর্ড</a>
            <a href="?tab=modules" class="nav-link <?=$active_tab=='modules'?'active':''?>"><i class="fas fa-cubes"></i> মডিউল</a>
            <a href="?tab=categories" class="nav-link <?=$active_tab=='categories'?'active':''?>"><i class="fas fa-tags"></i> ক্যাটাগরি</a>
            <a href="sms-manager.php" class="nav-link"><i class="fas fa-sms"></i> বাল্ক SMS</a>
            <hr><div class="menu-label">📚 কোর্স</div>
            <a href="add-course.php" class="nav-link"><i class="fas fa-plus-circle"></i> নতুন কোর্স</a>
            <a href="courses.php" class="nav-link"><i class="fas fa-book"></i> সকল কোর্স</a>
            <a href="manage-lessons.php" class="nav-link"><i class="fas fa-tasks"></i> লেসন ম্যানেজ</a>
            <a href="live-classes.php" class="nav-link"><i class="fas fa-video"></i> লাইভ ক্লাস</a>
            <hr><div class="menu-label">👥 ইউজার</div>
            <a href="users.php" class="nav-link"><i class="fas fa-users-cog"></i> ইউজার</a>
            <a href="learners.php" class="nav-link"><i class="fas fa-users"></i> শিক্ষার্থী</a>
            <a href="approvals.php" class="nav-link"><i class="fas fa-clipboard-check"></i> অনুমোদন <span class="badge bg-warning text-dark ms-auto"><?=$pending_count?></span></a>
            <a href="instructors.php" class="nav-link"><i class="fas fa-chalkboard-teacher"></i> ইন্সট্রাক্টর</a>
            <hr><div class="menu-label">💰 ম্যানেজমেন্ট</div>
            <a href="enrollments.php" class="nav-link"><i class="fas fa-graduation-cap"></i> এনরোলমেন্ট</a>
            <a href="payments.php" class="nav-link"><i class="fas fa-credit-card"></i> পেমেন্ট</a>
            <a href="packages.php" class="nav-link"><i class="fas fa-box"></i> প্যাকেজ</a>
            <a href="certificates.php" class="nav-link"><i class="fas fa-certificate"></i> সার্টিফিকেট</a>
            <hr><div class="menu-label">🎨 অন্যান্য</div>
            <a href="banners.php" class="nav-link"><i class="fas fa-ad"></i> ব্যানার</a>
            <a href="brother-concerns.php" class="nav-link"><i class="fas fa-handshake"></i> ব্রাদার কনসার্ন</a>
            <?php if($user_role == 'admin'): ?>
                <a href="settings.php" class="nav-link"><i class="fas fa-cog"></i> সাইট সেটিংস</a>
                 <a href="landing-manager.php" class="nav-link"><i class="fas fa-handshake"></i>Loading Page Setting</a>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- MAIN -->
    <div class="col-lg-8 col-xl-9">
        <?php if($message):?><div class="alert alert-<?=$msg_type?> rounded-3 mb-3"><?=htmlspecialchars($message)?></div><?php endif;?>
        
        <!-- Stats -->
        <div class="row g-3 mb-4">
            <div class="col-6 col-md-4"><div class="stat-card"><div class="icon" style="background:var(--g1);"><i class="fas fa-users"></i></div><h3><?=$stats['students']?></h3><p>শিক্ষার্থী</p></div></div>
            <div class="col-6 col-md-4"><div class="stat-card"><div class="icon" style="background:var(--g2);"><i class="fas fa-book"></i></div><h3><?=$stats['courses']?></h3><p>কোর্স</p></div></div>
            <div class="col-6 col-md-4"><div class="stat-card"><div class="icon" style="background:var(--g3);"><i class="fas fa-cubes"></i></div><h3><?=$stats['modules']?></h3><p>মডিউল</p></div></div>
            <div class="col-6 col-md-4"><div class="stat-card"><div class="icon" style="background:var(--g4);"><i class="fas fa-video"></i></div><h3><?=$stats['lessons']?></h3><p>লেসন</p></div></div>
            <div class="col-6 col-md-4"><div class="stat-card"><div class="icon" style="background:var(--g5);"><i class="fas fa-question-circle"></i></div><h3><?=$stats['quizzes']?></h3><p>কুইজ</p></div></div>
            <div class="col-6 col-md-4"><div class="stat-card"><div class="icon" style="background:linear-gradient(135deg,#11998e,#38ef7d);"><i class="fas fa-money-bill"></i></div><h3>৳<?=number_format($stats['revenue']/1000,0)?>K</h3><p>আয়</p></div></div>
        </div>
        
        <!-- Tabs -->
        <?php if($active_tab=='overview'):?>
            <div class="row g-3">
                <div class="col-md-6"><div class="main-card"><h5><i class="fas fa-clock text-warning me-2"></i>পেন্ডিং</h5><h2 class="text-warning"><?=$stats['pending']?></h2><a href="enrollments.php?status=pending" class="btn btn-warning btn-sm rounded-pill mt-2">রিভিউ</a></div></div>
                <div class="col-md-6"><div class="main-card"><h5><i class="fas fa-bolt text-primary me-2"></i>কুইক অ্যাকশন</h5><div class="d-flex gap-2 flex-wrap mt-2"><a href="add-course.php" class="btn btn-primary btn-sm"><i class="fas fa-plus"></i> কোর্স</a><a href="?tab=modules" class="btn btn-outline-primary btn-sm rounded-pill"><i class="fas fa-cubes"></i> মডিউল</a><a href="sms-manager.php" class="btn btn-outline-primary btn-sm rounded-pill"><i class="fas fa-sms"></i> SMS</a><a href="settings.php" class="btn-settings btn-sm"><i class="fas fa-cog"></i> সেটিংস</a></div></div></div>
            </div>
        <?php elseif($active_tab=='modules'):?>
            <div class="row"><div class="col-md-5"><div class="main-card"><h5 class="mb-3">➕ নতুন মডিউল</h5><form method="POST"><input type="hidden" name="add_module" value="1"><select name="course_id" class="form-select" required><option value="">কোর্স সিলেক্ট</option><?php foreach($courses as $c):?><option value="<?=$c['id']?>"><?=htmlspecialchars($c['title_bn'])?></option><?php endforeach;?></select><input type="text" name="title_bn" class="form-control" placeholder="মডিউল নাম (বাংলা)" required><input type="text" name="title_en" class="form-control" placeholder="Module Name"><input type="number" name="order_number" class="form-control" value="1"><button class="btn btn-primary btn-sm w-100">যোগ করুন</button></form></div></div>
            <div class="col-md-7"><div class="main-card"><h5>সকল মডিউল</h5><?php foreach($modules as $m):?><div class="d-flex justify-content-between p-2 mb-1 rounded" style="border:1px solid #eee;"><div><strong><?=htmlspecialchars($m['title_bn'])?></strong><br><small><?=htmlspecialchars($m['course_title'])?> | ক্রম: <?=$m['order_number']?></small></div><a href="add-lesson.php?course_id=<?=$m['course_id']?>&module_id=<?=$m['id']?>" class="btn btn-outline-primary btn-sm rounded-pill">লেসন</a></div><?php endforeach;?></div></div></div>
        <?php elseif($active_tab=='categories'):?>
            <div class="row"><div class="col-md-5"><div class="main-card"><h5 class="mb-3">➕ নতুন ক্যাটাগরি</h5><form method="POST"><input type="hidden" name="add_category" value="1"><input type="text" name="name_bn" class="form-control" placeholder="নাম (বাংলা)" required><input type="text" name="name_en" class="form-control" placeholder="Name (English)" required><input type="text" name="icon" class="form-control" value="fa-book"><button class="btn btn-primary btn-sm w-100">যোগ করুন</button></form></div></div>
            <div class="col-md-7"><div class="main-card"><h5>সকল ক্যাটাগরি (<?=count($categories)?>)</h5><table class="table table-sm"><thead><tr><th>আইকন</th><th>নাম</th><th>অ্যাকশন</th></tr></thead><tbody><?php foreach($categories as $cat):?><tr><td><i class="fas <?=$cat['icon']?> fa-lg" style="color:var(--primary);"></i></td><td><?=htmlspecialchars($cat['name_bn'])?></td><td><a href="?delete_category=<?=$cat['id']?>&tab=categories" class="btn btn-danger btn-sm" onclick="return confirm('ডিলিট?')"><i class="fas fa-trash"></i></a></td></tr><?php endforeach;?></tbody></table></div></div></div>
        <?php endif;?>
    </div>
</div></div>

<!-- PROFILE MODAL -->
<div class="modal fade" id="profileModal" tabindex="-1">
    <div class="modal-dialog modal-lg"><div class="modal-content">
        <div class="modal-header"><h5><i class="fas fa-user-cog me-2"></i>প্রোফাইল কনফিগারেশন</h5><button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button></div>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="update_profile" value="1"><input type="hidden" name="existing_avatar" value="<?=$user['avatar']??'default-avatar.png'?>">
            <div class="modal-body">
                <?php if($profile_msg):?><div class="alert alert-success rounded-3"><?=$profile_msg?></div><?php endif;?>
                <?php if($profile_error):?><div class="alert alert-danger rounded-3"><?=$profile_error?></div><?php endif;?>
                <div class="text-center mb-3">
                    <img src="../uploads/avatars/<?=$user['avatar']?:'default-avatar.png'?>" style="width:90px;height:90px;border-radius:50%;object-fit:cover;border:4px solid var(--primary);cursor:pointer;" onclick="document.getElementById('avatarInput').click()" id="avatarPreview" onerror="this.src='https://ui-avatars.com/api/?name=<?=urlencode($user['full_name'])?>&background=<?=str_replace('#','',$primary_color)?>&color=fff&size=90'">
                    <input type="file" name="avatar" id="avatarInput" accept="image/*" style="display:none" onchange="previewAvatar(this)">
                    <p class="small text-muted mt-1">ছবি বদলাতে ক্লিক করুন</p>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-2"><label class="fw-bold small">পূর্ণ নাম *</label><input type="text" name="full_name" class="form-control" value="<?=h($user['full_name'])?>" required></div>
                    <div class="col-md-6 mb-2"><label class="fw-bold small">ইমেইল *</label><input type="email" name="email" class="form-control" value="<?=h($user['email'])?>" required></div>
                    <div class="col-md-6 mb-2"><label class="fw-bold small">ফোন</label><input type="text" name="phone" class="form-control" value="<?=h($user['phone'])?>"></div>
                    <div class="col-md-6 mb-2"><label class="fw-bold small">পদবি</label><input type="text" name="designation" class="form-control" value="<?=h($user['designation']??'')?>"></div>
                    <div class="col-12 mb-2"><label class="fw-bold small">এক্সপার্টাইজ</label><input type="text" name="expertise" class="form-control" value="<?=h($user['expertise']??'')?>"></div>
                    <div class="col-12 mb-2"><label class="fw-bold small">বায়ো</label><textarea name="bio" class="form-control" rows="2"><?=h($user['bio'])?></textarea></div>
                    <div class="col-md-4 mb-2"><label class="fw-bold small">Facebook</label><input type="url" name="facebook_url" class="form-control" value="<?=h($user['facebook_url']??'')?>"></div>
                    <div class="col-md-4 mb-2"><label class="fw-bold small">LinkedIn</label><input type="url" name="linkedin_url" class="form-control" value="<?=h($user['linkedin_url']??'')?>"></div>
                    <div class="col-md-4 mb-2"><label class="fw-bold small">YouTube</label><input type="url" name="youtube_url" class="form-control" value="<?=h($user['youtube_url']??'')?>"></div>
                </div>
            </div>
            <div class="modal-footer"><button type="button" class="btn btn-secondary rounded-pill" data-bs-dismiss="modal">বাতিল</button><button type="submit" class="btn btn-primary rounded-pill"><i class="fas fa-save me-1"></i> সংরক্ষণ</button></div>
        </form>
    </div></div>
</div>

<!-- PASSWORD MODAL -->
<div class="modal fade" id="passwordModal" tabindex="-1">
    <div class="modal-dialog"><div class="modal-content">
        <div class="modal-header" style="background:var(--g2);"><h5><i class="fas fa-lock me-2"></i>পাসওয়ার্ড পরিবর্তন</h5><button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button></div>
        <form method="POST"><input type="hidden" name="change_password" value="1">
            <div class="modal-body">
                <?php if($pass_msg):?><div class="alert alert-success rounded-3"><?=$pass_msg?></div><?php endif;?>
                <?php if($pass_error):?><div class="alert alert-danger rounded-3"><?=$pass_error?></div><?php endif;?>
                <div class="mb-2"><label class="fw-bold small">বর্তমান পাসওয়ার্ড</label><input type="password" name="current_password" class="form-control" required></div>
                <div class="mb-2"><label class="fw-bold small">নতুন (৬+ অক্ষর)</label><input type="password" name="new_password" class="form-control" required minlength="6"></div>
                <div class="mb-2"><label class="fw-bold small">নিশ্চিত করুন</label><input type="password" name="confirm_password" class="form-control" required></div>
            </div>
            <div class="modal-footer"><button type="button" class="btn btn-secondary rounded-pill" data-bs-dismiss="modal">বাতিল</button><button type="submit" class="btn rounded-pill" style="background:var(--g2);color:white;"><i class="fas fa-key me-1"></i> পরিবর্তন</button></div>
        </form>
    </div></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>AOS.init({duration:700,once:true});</script>
<script>function previewAvatar(i){if(i.files&&i.files[0])document.getElementById('avatarPreview').src=URL.createObjectURL(i.files[0]);}</script>
</body>
</html>