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

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

requireLogin();
requirePermission('view_trustees');

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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $action === 'create') {
    $sql = "INSERT INTO trustees (trustee_code, trustee_name, trustee_type, national_id, registration_number, email, phone_number, is_active)
            VALUES (?, ?, ?, ?, ?, ?, ?, 1)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssssss", 
        $_POST['trustee_code'], $_POST['trustee_name'], $_POST['trustee_type'],
        $_POST['national_id'] ?? null, $_POST['registration_number'] ?? null,
        $_POST['email'] ?? null, $_POST['phone_number'] ?? null
    );
    if ($stmt->execute()) {
        $message = 'Trustee created successfully';
        $messageType = 'success';
        $action = 'list';
    } else {
        $message = 'Failed to create trustee';
        $messageType = 'danger';
    }
}

$trustees = $conn->query("SELECT * FROM trustees WHERE is_active = 1 ORDER BY created_at DESC")->fetch_all(MYSQLI_ASSOC);

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Trustees 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, #14b8a6 0%, #0d9488 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Trustees Management</h2>
                                <p>Manage scheme trustees and their assignments</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="trustees.php?action=create" class="btn btn-primary">
                                <i class="ri-shield-user-line me-2"></i>Add Trustee
                            </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 Trustee</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <form method="POST">
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Trustee Code <span class="text-danger">*</span></label>
                                                    <input type="text" name="trustee_code" class="form-control" required>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Trustee Name <span class="text-danger">*</span></label>
                                                    <input type="text" name="trustee_name" class="form-control" required>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Trustee Type <span class="text-danger">*</span></label>
                                                    <select name="trustee_type" class="form-select" required>
                                                        <option value="INDIVIDUAL">Individual</option>
                                                        <option value="CORPORATE">Corporate</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label" id="id_label">National ID / Registration Number</label>
                                                    <input type="text" name="national_id" id="national_id" class="form-control">
                                                </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 Trustee</button>
                                            <a href="trustees.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">Trustees 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>Type</th>
                                                        <th>Email</th>
                                                        <th>Phone</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if (empty($trustees)): ?>
                                                        <tr><td colspan="6" class="text-center">No trustees found</td></tr>
                                                    <?php else: ?>
                                                        <?php foreach ($trustees as $trustee): ?>
                                                            <tr>
                                                                <td><?php echo htmlspecialchars($trustee['trustee_code']); ?></td>
                                                                <td><?php echo htmlspecialchars($trustee['trustee_name']); ?></td>
                                                                <td><span class="badge bg-info"><?php echo htmlspecialchars($trustee['trustee_type']); ?></span></td>
                                                                <td><?php echo htmlspecialchars($trustee['email'] ?? 'N/A'); ?></td>
                                                                <td><?php echo htmlspecialchars($trustee['phone_number'] ?? 'N/A'); ?></td>
                                                                <td>
                                                                    <a href="trustee_details.php?id=<?php echo $trustee['trustee_id']; ?>" class="btn btn-sm btn-info">
                                                                        <i class="ri-eye-line"></i>
                                                                    </a>
                                                                    <a href="trustee_assignments.php?trustee_id=<?php echo $trustee['trustee_id']; ?>" class="btn btn-sm btn-success">
                                                                        <i class="ri-links-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'; ?>
    <script>
    document.querySelector('[name="trustee_type"]').addEventListener('change', function() {
        const label = document.getElementById('id_label');
        const input = document.getElementById('national_id');
        if (this.value === 'INDIVIDUAL') {
            label.textContent = 'National ID';
            input.name = 'national_id';
        } else {
            label.textContent = 'Registration Number';
            input.name = 'registration_number';
        }
    });
    </script>
</body>
</html>
