<?php
/**
 * Employers Management
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';

requireLogin();
requirePermission('view_employers');

$action = $_GET['action'] ?? 'list';
$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($action === 'create') {
        $sql = "INSERT INTO employers (employer_code, employer_name, registration_number, tax_pin, contact_person, email, phone_number, scheme_id, is_active)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("sssssssi", 
            $_POST['employer_code'], $_POST['employer_name'], $_POST['registration_number'] ?? null,
            $_POST['tax_pin'] ?? null, $_POST['contact_person'] ?? null, $_POST['email'] ?? null,
            $_POST['phone_number'] ?? null, $_POST['scheme_id'] ?? null
        );
        if ($stmt->execute()) {
            $message = 'Employer created successfully';
            $messageType = 'success';
            $action = 'list';
        } else {
            $message = 'Failed to create employer';
            $messageType = 'danger';
        }
    }
}

// Get employers
$employers = $conn->query("SELECT e.*, ps.scheme_name FROM employers e LEFT JOIN pension_schemes ps ON e.scheme_id = ps.scheme_id WHERE e.is_active = 1 ORDER BY e.created_at DESC")->fetch_all(MYSQLI_ASSOC);
$schemes = $conn->query("SELECT scheme_id, scheme_name FROM pension_schemes WHERE is_active = 1")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'Employers';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Employers Management')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Header -->
                    <div class="welcome-header" style="background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Employers Management</h2>
                                <p>Manage employer organizations and their remittances</p>
                            </div>
                        </div>
                    </div>

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

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="employers.php?action=create" class="btn btn-primary">
                                <i class="ri-building-2-line me-2"></i>Add Employer
                            </a>
                        </div>
                    </div>

                    <?php if ($action === 'create'): ?>
                        <div class="row">
                            <div class="col-12">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title">Add New Employer</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <form method="POST">
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Employer Code <span class="text-danger">*</span></label>
                                                    <input type="text" name="employer_code" class="form-control" required>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Employer Name <span class="text-danger">*</span></label>
                                                    <input type="text" name="employer_name" class="form-control" required>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Registration Number</label>
                                                    <input type="text" name="registration_number" class="form-control">
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Tax PIN (KRA)</label>
                                                    <input type="text" name="tax_pin" class="form-control">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Contact Person</label>
                                                    <input type="text" name="contact_person" class="form-control">
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Scheme</label>
                                                    <select name="scheme_id" class="form-select">
                                                        <option value="">Select Scheme</option>
                                                        <?php foreach ($schemes as $scheme): ?>
                                                            <option value="<?php echo $scheme['scheme_id']; ?>">
                                                                <?php echo htmlspecialchars($scheme['scheme_name']); ?>
                                                            </option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Email</label>
                                                    <input type="email" name="email" class="form-control">
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Phone Number</label>
                                                    <input type="text" name="phone_number" class="form-control">
                                                </div>
                                            </div>
                                            <button type="submit" class="btn btn-primary">Save Employer</button>
                                            <a href="employers.php" class="btn btn-secondary">Cancel</a>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="row">
                            <div class="col-12">
                                <div class="page-card">
                                    <div class="page-card-header">
                                        <h4 class="page-card-title">Employers List</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Code</th>
                                                        <th>Name</th>
                                                        <th>Scheme</th>
                                                        <th>Registration #</th>
                                                        <th>Contact</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if (empty($employers)): ?>
                                                        <tr><td colspan="6" class="text-center">No employers found</td></tr>
                                                    <?php else: ?>
                                                        <?php foreach ($employers as $employer): ?>
                                                            <tr>
                                                                <td><?php echo htmlspecialchars($employer['employer_code']); ?></td>
                                                                <td><?php echo htmlspecialchars($employer['employer_name']); ?></td>
                                                                <td><?php echo htmlspecialchars($employer['scheme_name'] ?? 'N/A'); ?></td>
                                                                <td><?php echo htmlspecialchars($employer['registration_number'] ?? 'N/A'); ?></td>
                                                                <td><?php echo htmlspecialchars($employer['contact_person'] ?? 'N/A'); ?></td>
                                                                <td>
                                                                    <a href="employer_details.php?id=<?php echo $employer['employer_id']; ?>" class="btn btn-sm btn-info">
                                                                        <i class="ri-eye-line"></i>
                                                                    </a>
                                                                    <a href="employer_remittances.php?employer_id=<?php echo $employer['employer_id']; ?>" class="btn btn-sm btn-success">
                                                                        <i class="ri-bank-line"></i>
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
