<?php
session_start();

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

$settings = json_decode(file_get_contents('data/settings.json'), true);
$content = json_decode(file_get_contents('data/content.json'), true);
$projects = json_decode(file_get_contents('data/projects.json'), true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CMS Admin - <?php echo htmlspecialchars($settings['site_title']); ?></title>
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="css/style.css">
    <style>
        body { background-color: #f0f2f5; font-family: 'Montserrat', sans-serif; margin: 0; }
        
        /* Modern Sidebar */
        .admin-sidebar {
            width: 260px;
            background: #111;
            color: #fff;
            position: fixed;
            top: 0;
            left: 0;
            height: 100vh;
            display: flex;
            flex-direction: column;
            box-shadow: 4px 0 15px rgba(0,0,0,0.1);
        }
        .sidebar-header {
            padding: 30px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.05);
        }
        .sidebar-header h2 { font-family: 'Oswald', sans-serif; color: var(--accent); margin: 0; font-size: 1.5rem; letter-spacing: 2px; }
        .sidebar-nav { flex: 1; padding: 20px 0; }
        .sidebar-nav a {
            display: flex;
            align-items: center;
            padding: 15px 30px;
            color: #a0a0a0;
            text-decoration: none;
            transition: all 0.3s;
            font-weight: 500;
        }
        .sidebar-nav a i { margin-right: 15px; font-size: 1.2rem; width: 20px; text-align: center; }
        .sidebar-nav a:hover { background: rgba(255,255,255,0.05); color: #fff; border-left: 4px solid var(--accent); padding-left: 26px; }
        
        .sidebar-footer { padding: 20px; border-top: 1px solid rgba(255,255,255,0.05); }
        .sidebar-footer a { display: flex; align-items: center; color: #e74c3c; text-decoration: none; font-weight: 500; }
        .sidebar-footer a i { margin-right: 15px; }

        /* Modern Main Content */
        .admin-content { margin-left: 260px; padding: 40px; }
        .dashboard-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; }
        .dashboard-header h1 { font-size: 2rem; color: #111; margin: 0; }
        
        /* Modern Cards */
        .panel {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.03);
            margin-bottom: 40px;
            overflow: hidden;
        }
        .panel-header {
            padding: 20px 30px;
            border-bottom: 1px solid #eee;
            background: #fafafa;
        }
        .panel-header h3 { margin: 0; font-size: 1.2rem; color: #333; }
        .panel-body { padding: 30px; }
        
        /* Modern Forms */
        .form-group { margin-bottom: 25px; }
        .form-group label { display: block; margin-bottom: 8px; font-weight: 600; color: #555; font-size: 0.9rem; }
        .form-control {
            width: 100%;
            padding: 14px 16px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-family: 'Montserrat', sans-serif;
            transition: all 0.3s;
            background: #fdfdfd;
        }
        .form-control:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(220, 168, 81, 0.1); background: #fff; }
        textarea.form-control { resize: vertical; }
        
        .btn-submit {
            background: #111;
            color: #fff;
            border: none;
            padding: 14px 30px;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.3s;
        }
        .btn-submit:hover { background: var(--accent); }
        
        /* Success Message */
        .success-msg {
            background: #d4edda; color: #155724; padding: 15px 20px; border-radius: 8px; margin-bottom: 30px;
            display: flex; align-items: center; border-left: 5px solid #28a745;
        }
        .success-msg i { margin-right: 10px; }

        /* Project Table */
        .project-table { width: 100%; border-collapse: collapse; }
        .project-table th { text-align: left; padding: 15px; border-bottom: 2px solid #eee; color: #777; font-weight: 600; }
        .project-table td { padding: 15px; border-bottom: 1px solid #eee; vertical-align: middle; }
        .project-thumbnail { width: 60px; height: 60px; border-radius: 8px; object-fit: cover; }
        .status-badge { padding: 5px 12px; border-radius: 20px; font-size: 0.8rem; font-weight: 600; }
        .status-ongoing { background: #e8f4fd; color: #3498db; }
        .status-completed { background: #eafaf1; color: #2ecc71; }
        .status-upcoming { background: #fef5e7; color: #f39c12; }
        .action-btn { background: none; border: none; color: #e74c3c; cursor: pointer; font-size: 1.1rem; padding: 5px; }
    </style>
</head>
<body>

    <div class="admin-sidebar">
        <div class="sidebar-header">
            <h2>CMS Admin</h2>
        </div>
        <div class="sidebar-nav">
            <a href="#settings"><i class="fas fa-cog"></i> Global Settings</a>
            <a href="#pages"><i class="fas fa-file-alt"></i> Page Content</a>
            <a href="#add-project"><i class="fas fa-plus-circle"></i> Add Project</a>
            <a href="#manage-projects"><i class="fas fa-list"></i> Manage Projects</a>
        </div>
        <div class="sidebar-footer">
            <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Log Out</a>
        </div>
    </div>

    <div class="admin-content">
        <div class="dashboard-header">
            <h1>Dashboard</h1>
            <a href="index.php" target="_blank" class="btn-submit" style="text-decoration: none; background: var(--accent);"><i class="fas fa-external-link-alt"></i> View Live Site</a>
        </div>

        <?php if (isset($_GET['success'])): ?>
            <div class="success-msg"><i class="fas fa-check-circle"></i> <?php echo htmlspecialchars($_GET['success']); ?></div>
        <?php endif; ?>
        
        <!-- Global Settings -->
        <div class="panel" id="settings">
            <div class="panel-header"><h3>Global Settings & Logo</h3></div>
            <div class="panel-body">
                <form action="api.php" method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="action" value="update_settings">
                    
                    <div style="display: flex; gap: 40px; margin-bottom: 25px; align-items: center;">
                        <div style="background: #111; padding: 20px; border-radius: 8px;">
                            <img src="<?php echo $settings['logo_url']; ?>" alt="Logo" style="height: 60px;">
                        </div>
                        <div class="form-group" style="flex: 1; margin: 0;">
                            <label>Upload New Logo</label>
                            <input type="file" name="logo" class="form-control" style="padding: 10px;">
                        </div>
                    </div>
                    
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                        <div class="form-group">
                            <label>Site Title (Company Name)</label>
                            <input type="text" name="site_title" class="form-control" value="<?php echo htmlspecialchars($settings['site_title']); ?>">
                        </div>
                        <div class="form-group">
                            <label>Phone Number</label>
                            <input type="text" name="phone" class="form-control" value="<?php echo htmlspecialchars($settings['phone']); ?>">
                        </div>
                        <div class="form-group">
                            <label>Email Address</label>
                            <input type="text" name="email" class="form-control" value="<?php echo htmlspecialchars($settings['email']); ?>">
                        </div>
                        <div class="form-group">
                            <label>Corporate Office</label>
                            <input type="text" name="corporate_office" class="form-control" value="<?php echo htmlspecialchars($settings['corporate_office']); ?>">
                        </div>
                    </div>
                    <button type="submit" class="btn-submit">Save Settings</button>
                </form>
            </div>
        </div>

        <!-- Page Content -->
        <div class="panel" id="pages">
            <div class="panel-header"><h3>Page Content (Home & About)</h3></div>
            <div class="panel-body">
                <form action="api.php" method="POST">
                    <input type="hidden" name="action" value="update_content">
                    
                    <div class="form-group">
                        <label>Home - Hero Title</label>
                        <input type="text" name="home_hero_title" class="form-control" value="<?php echo htmlspecialchars($content['home_hero_title']); ?>">
                    </div>
                    
                    <div class="form-group">
                        <label>Home - Hero Subtitle</label>
                        <textarea name="home_hero_subtitle" class="form-control" rows="3"><?php echo htmlspecialchars($content['home_hero_subtitle']); ?></textarea>
                    </div>
                    
                    <hr style="border: 0; border-top: 1px solid #eee; margin: 40px 0;">
                    
                    <div class="form-group">
                        <label>About - Title</label>
                        <input type="text" name="about_title" class="form-control" value="<?php echo htmlspecialchars($content['about_title']); ?>">
                    </div>
                    
                    <div class="form-group">
                        <label>About - Paragraph 1</label>
                        <textarea name="about_p1" class="form-control" rows="4"><?php echo htmlspecialchars($content['about_p1']); ?></textarea>
                    </div>
                    
                    <button type="submit" class="btn-submit">Save Content</button>
                </form>
            </div>
        </div>

        <!-- Add Project -->
        <div class="panel" id="add-project">
            <div class="panel-header"><h3>Add New Project</h3></div>
            <div class="panel-body">
                <form action="api.php" method="POST" enctype="multipart/form-data">
                    <input type="hidden" name="action" value="add_project">
                    
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                        <div class="form-group">
                            <label>Project Title</label>
                            <input type="text" name="title" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Location</label>
                            <input type="text" name="location" class="form-control" required>
                        </div>
                        <div class="form-group">
                            <label>Status</label>
                            <select name="status" class="form-control" required>
                                <option value="Upcoming">Upcoming</option>
                                <option value="Ongoing">Ongoing</option>
                                <option value="Completed">Completed</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Upload Project Image</label>
                            <input type="file" name="project_image" class="form-control" style="padding: 10px;" required>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>Short Description (Card View)</label>
                        <textarea name="shortDesc" class="form-control" rows="2" required></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label>Long Description (Details Page)</label>
                        <textarea name="longDesc" class="form-control" rows="4" required></textarea>
                    </div>
                    
                    <button type="submit" class="btn-submit">Create Project</button>
                </form>
            </div>
        </div>

        <!-- Manage Projects -->
        <div class="panel" id="manage-projects">
            <div class="panel-header"><h3>Manage Projects</h3></div>
            <div class="panel-body" style="padding: 0;">
                <table class="project-table">
                    <thead>
                        <tr>
                            <th style="padding-left: 30px;">Image</th>
                            <th>Title & Location</th>
                            <th>Status</th>
                            <th style="text-align: right; padding-right: 30px;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($projects as $p): 
                            $statusClass = 'status-' . strtolower($p['status']);
                        ?>
                        <tr>
                            <td style="padding-left: 30px;">
                                <img src="<?php echo htmlspecialchars($p['image']); ?>" class="project-thumbnail" alt="Thumb">
                            </td>
                            <td>
                                <strong style="display: block; font-size: 1.1rem; color: #333; margin-bottom: 5px;"><?php echo htmlspecialchars($p['title']); ?></strong>
                                <span style="color: #777; font-size: 0.9rem;"><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($p['location']); ?></span>
                            </td>
                            <td>
                                <span class="status-badge <?php echo $statusClass; ?>"><?php echo htmlspecialchars($p['status']); ?></span>
                            </td>
                            <td style="text-align: right; padding-right: 30px;">
                                <a href="api.php?action=delete_project&id=<?php echo $p['id']; ?>" class="action-btn" onclick="return confirm('Delete this project forever?');" title="Delete">
                                    <i class="fas fa-trash-alt"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if(empty($projects)): ?>
                        <tr>
                            <td colspan="4" style="text-align: center; padding: 40px; color: #777;">No projects available.</td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

    </div>

</body>
</html>
