<?php
session_start();

if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$action = $_POST['action'] ?? '';

// Helper function
function saveJson($file, $data) {
    file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
}

// 1. Update Settings (Logos, Contact Info)
if ($action === 'update_settings') {
    $settings = json_decode(file_get_contents('data/settings.json'), true);
    
    $settings['site_title'] = $_POST['site_title'] ?? $settings['site_title'];
    $settings['phone'] = $_POST['phone'] ?? $settings['phone'];
    $settings['email'] = $_POST['email'] ?? $settings['email'];
    $settings['corporate_office'] = $_POST['corporate_office'] ?? $settings['corporate_office'];
    $settings['head_office'] = $_POST['head_office'] ?? $settings['head_office'];
    $settings['footer_desc'] = $_POST['footer_desc'] ?? $settings['footer_desc'];
    
    // Handle logo upload
    if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
        $tmp_name = $_FILES['logo']['tmp_name'];
        $name = basename($_FILES['logo']['name']);
        $path = "uploads/" . time() . "_" . $name;
        if (move_uploaded_file($tmp_name, $path)) {
            $settings['logo_url'] = $path;
        }
    }
    
    saveJson('data/settings.json', $settings);
    header('Location: admin.php?success=Settings Updated');
    exit;
}

// 2. Update Content
if ($action === 'update_content') {
    $content = json_decode(file_get_contents('data/content.json'), true);
    
    foreach ($_POST as $key => $value) {
        if ($key !== 'action') {
            $content[$key] = $value;
        }
    }
    
    saveJson('data/content.json', $content);
    header('Location: admin.php?success=Content Updated');
    exit;
}

// 3. Add Project
if ($action === 'add_project') {
    $projects = json_decode(file_get_contents('data/projects.json'), true);
    
    $newProject = [
        'id' => 'proj_' . time(),
        'title' => $_POST['title'],
        'location' => $_POST['location'],
        'status' => $_POST['status'],
        'shortDesc' => $_POST['shortDesc'],
        'longDesc' => $_POST['longDesc'],
        'image' => 'images/hero.png' // Default
    ];
    
    // Handle image upload
    if (isset($_FILES['project_image']) && $_FILES['project_image']['error'] === UPLOAD_ERR_OK) {
        $tmp_name = $_FILES['project_image']['tmp_name'];
        $name = basename($_FILES['project_image']['name']);
        $path = "uploads/proj_" . time() . "_" . $name;
        if (move_uploaded_file($tmp_name, $path)) {
            $newProject['image'] = $path;
        }
    }
    
    $projects[] = $newProject;
    saveJson('data/projects.json', $projects);
    header('Location: admin.php?success=Project Added');
    exit;
}

// 4. Delete Project
if (isset($_GET['action']) && $_GET['action'] === 'delete_project') {
    $id = $_GET['id'];
    $projects = json_decode(file_get_contents('data/projects.json'), true);
    $projects = array_filter($projects, function($p) use ($id) { return $p['id'] !== $id; });
    saveJson('data/projects.json', array_values($projects));
    header('Location: admin.php?success=Project Deleted');
    exit;
}

header('Location: admin.php');
exit;
?>
