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

$captain_id = $_SESSION['captain_id'];
$message = '';
$message_type = '';

// Get captain info
$captain = $conn->query("SELECT * FROM captains WHERE id='$captain_id'")->fetch_assoc();

// Handle profile update
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['update_profile'])) {
        $name = $conn->real_escape_string($_POST['name']);
        $phone = $conn->real_escape_string($_POST['phone']);
        $vehicle_info = $conn->real_escape_string($_POST['vehicle_info']);
        
        $sql = "UPDATE captains SET name='$name', phone='$phone', vehicle_info='$vehicle_info' WHERE id='$captain_id'";
        
        if ($conn->query($sql) === TRUE) {
            $_SESSION['captain_name'] = $name;
            $_SESSION['captain_phone'] = $phone;
            $message = "تم تحديث الملف الشخصي بنجاح";
            $message_type = "success";
            // Refresh captain info
            $captain = $conn->query("SELECT * FROM captains WHERE id='$captain_id'")->fetch_assoc();
        } else {
            $message = "خطأ في تحديث الملف الشخصي: " . $conn->error;
            $message_type = "danger";
        }
    }
    
    if (isset($_POST['change_password'])) {
        $current_password = $_POST['current_password'];
        $new_password = $_POST['new_password'];
        $confirm_password = $_POST['confirm_password'];
        
        if (password_verify($current_password, $captain['password'])) {
            if ($new_password === $confirm_password) {
                $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
                $sql = "UPDATE captains SET password='$hashed_password' WHERE id='$captain_id'";
                
                if ($conn->query($sql) === TRUE) {
                    $message = "تم تغيير كلمة المرور بنجاح";
                    $message_type = "success";
                } else {
                    $message = "خطأ في تغيير كلمة المرور: " . $conn->error;
                    $message_type = "danger";
                }
            } else {
                $message = "كلمات المرور غير متطابقة";
                $message_type = "danger";
            }
        } else {
            $message = "كلمة المرور الحالية غير صحيحة";
            $message_type = "danger";
        }
    }
}

// Get captain statistics
$stats = $conn->query("
    SELECT 
        COUNT(*) as total_orders,
        COALESCE(SUM(total), 0) as total_earnings,
        COUNT(CASE WHEN DATE(created_at) = CURDATE() THEN 1 END) as today_orders
    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">
    <title>الملف الشخصي - Max Captain</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@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body {
            font-family: 'Tajawal', sans-serif;
            background-color: #f8f9fa;
            padding-bottom: 80px;
        }
        .navbar {
            background: #F57C00;
            color: white;
        }
        .info-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .stat-card {
            background: white;
            border-radius: 10px;
            padding: 15px;
            text-align: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            margin-bottom: 15px;
        }
        .bottom-nav {
            position: fixed;
            bottom: 0;
            right: 0;
            left: 0;
            background: white;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
            padding: 10px;
        }
        .nav-item {
            text-align: center;
        }
        .nav-link {
            color: #666;
        }
        .nav-link.active {
            color: #F57C00;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg">
        <div class="container-fluid">
            <span class="navbar-brand text-white">
                <i class="bi bi-person"></i> الملف الشخصي
            </span>
        </div>
    </nav>

    <div class="container mt-4">
        <?php if ($message): ?>
            <div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
                <?php echo $message; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <!-- Statistics -->
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="stat-card">
                    <h4 class="text-primary"><?php echo $stats['total_orders']; ?></h4>
                    <p class="mb-0">إجمالي الطلبات</p>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stat-card">
                    <h4 class="text-success"><?php echo number_format($stats['total_earnings'], 2); ?> ريال</h4>
                    <p class="mb-0">إجمالي الأرباح</p>
                </div>
            </div>
            <div class="col-md-4">
                <div class="stat-card">
                    <h4 class="text-warning"><?php echo $stats['today_orders']; ?></h4>
                    <p class="mb-0">طلبات اليوم</p>
                </div>
            </div>
        </div>

        <!-- Profile Information -->
        <div class="info-card">
            <h5 class="mb-3"><i class="bi bi-person-badge"></i> المعلومات الشخصية</h5>
            <form method="POST">
                <div class="row">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label for="name" class="form-label">الاسم</label>
                            <input type="text" class="form-control" id="name" name="name" value="<?php echo $captain['name']; ?>" required>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label for="phone" class="form-label">رقم الهاتف</label>
                            <input type="tel" class="form-control" id="phone" name="phone" value="<?php echo $captain['phone']; ?>" required>
                        </div>
                    </div>
                </div>
                <div class="mb-3">
                    <label for="vehicle_info" class="form-label">معلومات المركبة</label>
                    <input type="text" class="form-control" id="vehicle_info" name="vehicle_info" value="<?php echo $captain['vehicle_info']; ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">الحالة</label>
                    <div>
                        <span class="badge bg-<?php echo $captain['status'] == 'active' ? 'success' : 'secondary'; ?>">
                            <?php echo $captain['status'] == 'active' ? 'نشط' : 'غير نشط'; ?>
                        </span>
                    </div>
                </div>
                <button type="submit" name="update_profile" class="btn btn-primary">
                    <i class="bi bi-check-circle"></i> تحديث المعلومات
                </button>
            </form>
        </div>

        <!-- Change Password -->
        <div class="info-card">
            <h5 class="mb-3"><i class="bi bi-shield-lock"></i> تغيير كلمة المرور</h5>
            <form method="POST">
                <div class="mb-3">
                    <label for="current_password" class="form-label">كلمة المرور الحالية</label>
                    <input type="password" class="form-control" id="current_password" name="current_password" required>
                </div>
                <div class="mb-3">
                    <label for="new_password" class="form-label">كلمة المرور الجديدة</label>
                    <input type="password" class="form-control" id="new_password" name="new_password" required>
                </div>
                <div class="mb-3">
                    <label for="confirm_password" class="form-label">تأكيد كلمة المرور الجديدة</label>
                    <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                </div>
                <button type="submit" name="change_password" class="btn btn-warning">
                    <i class="bi bi-key"></i> تغيير كلمة المرور
                </button>
            </form>
        </div>

        <!-- Logout -->
        <div class="info-card text-center">
            <a href="logout.php" class="btn btn-danger">
                <i class="bi bi-box-arrow-left"></i> تسجيل الخروج
            </a>
        </div>
    </div>

    <!-- Bottom Navigation -->
    

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Bottom Navigation -->
<nav class="bottom-nav">
    <div class="row text-center">
        <div class="col-3">
            <a href="dashboard.php" class="nav-link">
                <i class="bi bi-house-door fs-4"></i>
                <div>الرئيسية</div>
            </a>
        </div>
        <div class="col-3">
            <a href="orders.php" class="nav-link">
                <i class="bi bi-list-check fs-4"></i>
                <div>الطلبات</div>
            </a>
        </div>
        <div class="col-3">
            <a href="completed_orders.php" class="nav-link">
                <i class="bi bi-check-circle fs-4"></i>
                <div>المنجزة</div>
            </a>
        </div>
        <div class="col-3">
            <a href="profile.php" class="nav-link">
                <i class="bi bi-person fs-4"></i>
                <div>الحساب</div>
            </a>
        </div>
    </div>
</nav>
</body>
</html>