<?php
/**
 * Deployed From Management Page
 * Dozar & Tripper Management System
 */

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../includes/auth.php';

$user = getCurrentUser();
$pdo = getPDO();

// Handle add/delete
if ($_SERVER['REQUEST_METHOD'] === 'POST' && hasRole('admin')) {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'add') {
        $entity_name = sanitize($_POST['entity_name'] ?? '');
        $entity_type = sanitize($_POST['entity_type'] ?? '');
        $contact_info = sanitize($_POST['contact_info'] ?? '');
        
        if ($entity_name) {
            $stmt = $pdo->prepare("INSERT INTO deployed_from (entity_name, entity_type, contact_info) VALUES (?, ?, ?)");
            $stmt->execute([$entity_name, $entity_type, $contact_info]);
            showMessage(t('msg_save_success', 'Added successfully!'), 'success');
        }
    } elseif ($action === 'delete') {
        $id = $_POST['id'] ?? '';
        if ($id) {
            $stmt = $pdo->prepare("DELETE FROM deployed_from WHERE id=?");
            $stmt->execute([$id]);
            showMessage(t('msg_delete_success', 'Deleted successfully!'), 'success');
        }
    }
}

$deployed = $pdo->query("SELECT * FROM deployed_from ORDER BY entity_name")->fetchAll();
?>
<?php include __DIR__ . '/../includes/header.php'; ?>

<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-building"></i> <?php echo t('nav_deployed', 'Deployed From'); ?></h2>
            <?php if (hasRole('admin')): ?>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
                <i class="fas fa-plus"></i> Add New
            </button>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php displayMessage(); ?>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Entity Name</th>
                        <th>Type</th>
                        <th>Contact Info</th>
                        <?php if (hasRole('admin')): ?>
                        <th>Actions</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($deployed as $d): ?>
                    <tr>
                        <td><?php echo $d['entity_name']; ?></td>
                        <td><?php echo ucfirst($d['entity_type']); ?></td>
                        <td><?php echo $d['contact_info'] ?? '-'; ?></td>
                        <?php if (hasRole('admin')): ?>
                        <td>
                            <a href="view.php?id=<?php echo $d['id']; ?>" class="btn btn-sm btn-info">
                                <i class="fas fa-eye"></i>
                            </a>
                            <a href="edit.php?id=<?php echo $d['id']; ?>" class="btn btn-sm btn-warning">
                                <i class="fas fa-pen"></i>
                            </a>
                            <form method="POST" style="display:inline;">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="id" value="<?php echo $d['id']; ?>">
                                <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Delete?')">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                        </td>
                        <?php endif; ?>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php if (hasRole('admin')): ?>
<div class="modal fade" id="addModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Deployed From</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <input type="hidden" name="action" value="add">
                    <div class="mb-3">
                        <label class="form-label">Entity Name *</label>
                        <input type="text" name="entity_name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Type</label>
                        <select name="entity_type" class="form-select">
                            <option value="kmc">KMC</option>
                            <option value="private">Private</option>
                            <option value="other">Other</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Contact Info</label>
                        <input type="text" name="contact_info" class="form-control">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Add</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php endif; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<?php include __DIR__ . '/../includes/footer.php'; ?>
