<?php
include 'config.php';
checkCaptainAuth();

$captain_id = $_SESSION['captain_id'];
$msg = $msg_type = '';

$captain = $conn->query("SELECT name, phone, vehicle_info, status FROM captains WHERE id='$captain_id'")->fetch_assoc();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(isset($_POST['update'])){
        $name = $conn->real_escape_string($_POST['name']);
        $phone = $conn->real_escape_string($_POST['phone']);
        $vehicle = $conn->real_escape_string($_POST['vehicle']);
        $conn->query("UPDATE captains SET name='$name', phone='$phone', vehicle_info='$vehicle' WHERE id='$captain_id'");
        $_SESSION['captain_name'] = $name;
        $msg = "تم التحديث";
        $msg_type = "success";
        $captain = $conn->query("SELECT name, phone, vehicle_info, status FROM captains WHERE id='$captain_id'")->fetch_assoc();
    }
    if(isset($_POST['change_pass'])){
        $old = $_POST['old'];
        $new = $_POST['new'];
        $confirm = $_POST['confirm'];
        $check = $conn->query("SELECT password FROM captains WHERE id='$captain_id'")->fetch_assoc();
        if(password_verify($old, $check['password'])){
            if($new === $confirm && strlen($new)>=6){
                $hash = password_hash($new, PASSWORD_DEFAULT);
                $conn->query("UPDATE captains SET password='$hash' WHERE id='$captain_id'");
                $msg = "تم تغيير كلمة المرور";
                $msg_type = "success";
            } else {
                $msg = "كلمات المرور غير متطابقة";
                $msg_type = "danger";
            }
        } else {
            $msg = "كلمة المرور الحالية غير صحيحة";
            $msg_type = "danger";
        }
    }
}

$stats = $conn->query("SELECT COUNT(*) as total, COALESCE(SUM(total),0) as earn FROM orders WHERE captain_id='$captain_id' AND status='delivered'")->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>الملف - ماكس كابتن</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        *{margin:0;padding:0;box-sizing:border-box;}
        body{font-family:'Tajawal',sans-serif;background:#f3f4f6;padding-top:60px;padding-bottom:70px;}
        .header{background:linear-gradient(135deg,#F57C00,#E65100);position:fixed;top:0;left:0;right:0;height:56px;display:flex;align-items:center;justify-content:center;z-index:1000;}
        .header span{color:white;font-weight:700;}
        .container{padding:1rem;max-width:500px;margin:0 auto;}
        .stats{display:grid;grid-template-columns:repeat(2,1fr);gap:0.75rem;margin-bottom:1rem;}
        .stat{background:white;border-radius:1rem;padding:1rem;text-align:center;box-shadow:0 1px 3px rgba(0,0,0,0.05);}
        .stat-num{font-size:1.5rem;font-weight:800;color:#F57C00;}
        .card{background:white;border-radius:1rem;padding:1rem;margin-bottom:1rem;box-shadow:0 1px 3px rgba(0,0,0,0.05);}
        .form-control{border-radius:0.75rem;border:1px solid #e5e7eb;padding:0.6rem;}
        .btn{width:100%;padding:0.6rem;border-radius:2rem;font-weight:600;border:none;}
        .btn-primary{background:#F57C00;color:white;}
        .btn-warning{background:#f59e0b;color:white;}
        .btn-danger{background:#ef4444;color:white;}
        .bottom-nav{position:fixed;bottom:0;left:0;right:0;background:white;padding:0.5rem 1rem;z-index:1000;box-shadow:0 -2px 10px rgba(0,0,0,0.05);}
        .nav-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:0.25rem;max-width:500px;margin:0 auto;}
        .nav-link{display:flex;flex-direction:column;align-items:center;padding:0.5rem;color:#6b7280;text-decoration:none;border-radius:0.75rem;}
        .nav-link.active{color:#F57C00;background:rgba(245,124,0,0.1);}
        .nav-link i{font-size:1.2rem;margin-bottom:0.2rem;}
    </style>
</head>
<body>
    <div class="header"><span><i class="bi bi-person"></i> الملف الشخصي</span></div>
    <div class="container">
        <?php if($msg): ?><div class="alert alert-<?php echo $msg_type; ?> alert-dismissible"><?php echo $msg; ?><button class="btn-close" data-bs-dismiss="alert"></button></div><?php endif; ?>
        <div class="stats"><div class="stat"><div class="stat-num"><?php echo $stats['total']; ?></div><div class="text-muted small">الطلبات</div></div><div class="stat"><div class="stat-num"><?php echo number_format($stats['earn'],0); ?></div><div class="text-muted small">الأرباح</div></div></div>
        <div class="card"><h5><i class="bi bi-person-badge"></i> معلوماتي</h5><form method="POST"><input type="text" name="name" class="form-control mb-2" value="<?php echo $captain['name']; ?>" required><input type="tel" name="phone" class="form-control mb-2" value="<?php echo $captain['phone']; ?>" required><input type="text" name="vehicle" class="form-control mb-2" value="<?php echo $captain['vehicle_info']; ?>" placeholder="المركبة"><button type="submit" name="update" class="btn btn-primary mt-2"><i class="bi bi-check-circle"></i> تحديث</button></form></div>
        <div class="card"><h5><i class="bi bi-shield-lock"></i> تغيير كلمة المرور</h5><form method="POST"><input type="password" name="old" class="form-control mb-2" placeholder="الحالية" required><input type="password" name="new" class="form-control mb-2" placeholder="الجديدة" required><input type="password" name="confirm" class="form-control mb-2" placeholder="التأكيد" required><button type="submit" name="change_pass" class="btn btn-warning mt-2"><i class="bi bi-key"></i> تغيير</button></form></div>
        <a href="logout.php" class="btn btn-danger mt-2"><i class="bi bi-box-arrow-left"></i> تسجيل الخروج</a>
    </div>
    <nav class="bottom-nav"><div class="nav-grid"><a href="captain_dashboard.php" class="nav-link"><i class="bi bi-house-door"></i><span>الرئيسية</span></a><a href="captain_orders.php" class="nav-link"><i class="bi bi-list-check"></i><span>الطلبات</span></a><a href="captain_completed.php" class="nav-link"><i class="bi bi-check-circle"></i><span>المنجزة</span></a><a href="captain_profile.php" class="nav-link active"><i class="bi bi-person"></i><span>الملف</span></a></div></nav>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>