<?php
require_once 'includes/header.php';
$pdo = db();

// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $location = $_POST['location'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    
    $stmt = $pdo->prepare("INSERT INTO branches (name, location, phone, email) VALUES (?, ?, ?, ?)");
    $stmt->execute([$name, $location, $phone, $email]);
    
    // Log activity
    $adminId = $_SESSION['user_id'];
    $pdo->prepare("INSERT INTO activity_logs (user_id, action) VALUES (?, 'Added new branch: $name')")->execute([$adminId]);
    
    echo "<div style='padding: 1rem; background: #4caf50; color: white; margin-bottom: 1rem;'>Branch Added Successfully</div>";
}

// Delete Branch
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $pdo->prepare("DELETE FROM branches WHERE id = ?")->execute([$id]);
    echo "<div style='padding: 1rem; background: #f44336; color: white; margin-bottom: 1rem;'>Branch Deleted</div>";
}

$branches = $pdo->query("SELECT * FROM branches ORDER BY created_at DESC")->fetchAll();
?>

<div class="page-header">
    <h1>Branch Management</h1>
    <button onclick="document.getElementById('addBranchModal').style.display='block'" class="btn-sm btn-primary">Add New Branch</button>
</div>

<div class="card-grid" style="grid-template-columns: 1fr;">
    <table class="data-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Location</th>
                <th>Contact</th>
                <th>Created At</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($branches as $branch): ?>
            <tr>
                <td><?php echo htmlspecialchars($branch['name']); ?></td>
                <td><?php echo htmlspecialchars($branch['location']); ?></td>
                <td>
                    <?php echo htmlspecialchars($branch['phone']); ?><br>
                    <small><?php echo htmlspecialchars($branch['email']); ?></small>
                </td>
                <td><?php echo $branch['created_at']; ?></td>
                <td>
                    <a href="?delete=<?php echo $branch['id']; ?>" class="btn-sm btn-danger" onclick="return confirm('Delete this branch?')">Delete</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<!-- Simple Modal for Adding Branch -->
<div id="addBranchModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.8); z-index:1000;">
    <div style="background:var(--card-bg); width:400px; margin: 100px auto; padding:2rem; border-radius:8px; border:1px solid var(--border-color);">
        <h2 style="margin-bottom:1rem; color:var(--text-primary);">Add Branch</h2>
        <form method="POST">
            <div style="margin-bottom:1rem;">
                <label style="display:block; color:var(--text-secondary); margin-bottom:0.5rem;">Branch Name</label>
                <input type="text" name="name" required style="width:100%; padding:0.8rem; background:var(--bg-color); border:1px solid var(--border-color); color:var(--text-primary);">
            </div>
            <div style="margin-bottom:1rem;">
                <label style="display:block; color:var(--text-secondary); margin-bottom:0.5rem;">Location</label>
                <input type="text" name="location" required style="width:100%; padding:0.8rem; background:var(--bg-color); border:1px solid var(--border-color); color:var(--text-primary);">
            </div>
            <div style="margin-bottom:1rem;">
                <label style="display:block; color:var(--text-secondary); margin-bottom:0.5rem;">Phone</label>
                <input type="text" name="phone" style="width:100%; padding:0.8rem; background:var(--bg-color); border:1px solid var(--border-color); color:var(--text-primary);">
            </div>
             <div style="margin-bottom:1rem;">
                <label style="display:block; color:var(--text-secondary); margin-bottom:0.5rem;">Email</label>
                <input type="email" name="email" style="width:100%; padding:0.8rem; background:var(--bg-color); border:1px solid var(--border-color); color:var(--text-primary);">
            </div>
            <button type="submit" class="btn-sm btn-primary" style="width:100%;">Create Branch</button>
            <button type="button" onclick="document.getElementById('addBranchModal').style.display='none'" class="btn-sm" style="width:100%; margin-top:0.5rem; background:transparent; border:1px solid var(--border-color);">Cancel</button>
        </form>
    </div>
</div>

<script>
    // Auto-open modal if action=add
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.get('action') === 'add') {
        document.getElementById('addBranchModal').style.display = 'block';
    }
</script>
<?php require_once 'includes/footer.php'; ?>
