<?php 
include 'koneksi.php';

// Cek session
if(!isset($_SESSION['user'])) {
    header("Location: index.php");
    exit();
}

$user = $_SESSION['user'];
$message = '';
$error = '';

// Proses ganti password
if(isset($_POST['ganti'])) {
    $password_lama = $_POST['password_lama'];
    $password_baru = $_POST['password_baru'];
    $konfirmasi = $_POST['konfirmasi'];
    
    // Validasi password tidak boleh kosong
    if(empty($password_lama) || empty($password_baru) || empty($konfirmasi)) {
        $error = "Semua field harus diisi!";
    } elseif($password_baru != $konfirmasi) {
        $error = "Password baru dan konfirmasi tidak cocok!";
    } elseif(strlen($password_baru) < 6) {
        $error = "Password baru minimal 6 karakter!";
    } else {
        // Cek password lama
        $password_lama_md5 = md5($password_lama);
        $user_id = $_SESSION['user_id'];
        
        $check = mysqli_query($conn, "SELECT id FROM users WHERE id = '$user_id' AND password = '$password_lama_md5'");
        
        if(mysqli_num_rows($check) == 1) {
            // Update password
            $password_baru_md5 = md5($password_baru);
            $query = "UPDATE users SET password = '$password_baru_md5' WHERE id = '$user_id'";
            
            if(mysqli_query($conn, $query)) {
                $message = "Password berhasil diubah! Silakan login ulang.";
                // Logout setelah ganti password
                echo "<script>
                    alert('Password berhasil diubah! Silakan login ulang.');
                    window.location.href = 'logout.php';
                </script>";
                exit();
            } else {
                $error = "Gagal mengubah password: " . mysqli_error($conn);
            }
        } else {
            $error = "Password lama salah!";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Ganti Password - SIM ISTIQLAL</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
    <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.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/theme-istiqlal-green.css">
    <style>
        .content-wrapper { background: #f1f5f9; }
        .card { border-radius: 20px; box-shadow: 0 5px 20px rgba(0,0,0,0.08); border: none; }
        .card-header { border-radius: 20px 20px 0 0; padding: 15px 20px; background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%); color: white; }
        .card-header h4 { margin: 0; font-size: 18px; font-weight: 600; }
        .btn-save { background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%); border: none; color: white; padding: 10px 20px; border-radius: 8px; }
        .btn-save:hover { transform: translateY(-2px); }
        .form-control:focus { border-color: #0b5e4e; box-shadow: 0 0 0 0.2rem rgba(11,94,78,0.25); }
        .info-user { background: #f8fafc; border-radius: 12px; padding: 15px; margin-bottom: 20px; }
    </style>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
    <?php include 'sidebar.php'; ?>
    
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0" style="font-size: 20px;">
                            <i class="fas fa-key text-warning"></i> Ganti Password
                        </h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                            <li class="breadcrumb-item active">Ganti Password</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="container-fluid">
                <div class="row justify-content-center">
                    <div class="col-md-6">
                        <!-- Info User -->
                        <div class="info-user">
                            <div class="d-flex align-items-center">
                                <i class="fas fa-user-circle fa-3x text-success me-3"></i>
                                <div>
                                    <h5 class="mb-0"><?= htmlspecialchars($user['full_name']) ?></h5>
                                    <small class="text-muted">
                                        <i class="fas fa-tag"></i> <?= $user['role'] ?> | 
                                        <i class="fas fa-building"></i> <?= $user['unit'] ?>
                                    </small>
                                </div>
                            </div>
                        </div>

                        <!-- Form Ganti Password -->
                        <div class="card">
                            <div class="card-header">
                                <h4><i class="fas fa-lock"></i> Form Ganti Password</h4>
                            </div>
                            <div class="card-body">
                                <?php if($message): ?>
                                <div class="alert alert-success alert-dismissible fade show">
                                    <i class="fas fa-check-circle"></i> <?= $message ?>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                                </div>
                                <?php endif; ?>

                                <?php if($error): ?>
                                <div class="alert alert-danger alert-dismissible fade show">
                                    <i class="fas fa-exclamation-triangle"></i> <?= $error ?>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                                </div>
                                <?php endif; ?>

                                <form method="POST">
                                    <div class="mb-3">
                                        <label class="form-label">Password Lama</label>
                                        <div class="input-group">
                                            <span class="input-group-text"><i class="fas fa-lock"></i></span>
                                            <input type="password" name="password_lama" class="form-control" placeholder="Masukkan password lama" required>
                                        </div>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Password Baru</label>
                                        <div class="input-group">
                                            <span class="input-group-text"><i class="fas fa-key"></i></span>
                                            <input type="password" name="password_baru" class="form-control" placeholder="Minimal 6 karakter" required>
                                        </div>
                                        <small class="text-muted">Password minimal 6 karakter</small>
                                    </div>
                                    <div class="mb-3">
                                        <label class="form-label">Konfirmasi Password Baru</label>
                                        <div class="input-group">
                                            <span class="input-group-text"><i class="fas fa-check-circle"></i></span>
                                            <input type="password" name="konfirmasi" class="form-control" placeholder="Ketik ulang password baru" required>
                                        </div>
                                    </div>

                                    <div class="alert alert-info small">
                                        <i class="fas fa-info-circle"></i> 
                                        <strong>Catatan:</strong> Setelah mengganti password, Anda akan diminta login ulang.
                                    </div>

                                    <button type="submit" name="ganti" class="btn btn-save w-100">
                                        <i class="fas fa-save"></i> Ganti Password
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer class="main-footer">
        <strong>&copy; <?= date('Y') ?> SIM ISTIQLAL - Sistem Administrasi Guru</strong>
    </footer>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/js/adminlte.min.js"></script>
</body>
</html>