<?php
/**
 * Policy Products Management
 * Manage life insurance product types
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('manage_policy_products');

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($action === 'create') {
        $sql = "INSERT INTO life_insurance_products (product_name, product_type, description, minimum_sum_assured, 
                                                     maximum_sum_assured, minimum_premium, minimum_surrender_years, is_active)
                VALUES (?, ?, ?, ?, ?, ?, ?, 1)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("sssdddi",
            $_POST['product_name'],
            $_POST['product_type'],
            $_POST['description'] ?? null,
            $_POST['minimum_sum_assured'] ?? null,
            $_POST['maximum_sum_assured'] ?? null,
            $_POST['minimum_premium'] ?? null,
            $_POST['minimum_surrender_years'] ?? 0
        );
        if ($stmt->execute()) {
            $message = 'Product created successfully';
            $messageType = 'success';
            $action = 'list';
        } else {
            $message = 'Failed to create product';
            $messageType = 'danger';
        }
    }
}

$products = $conn->query("SELECT * FROM life_insurance_products WHERE is_active = 1 ORDER BY product_name")->fetch_all(MYSQLI_ASSOC);

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Policy Products')); ?>
    <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">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #ec4899 0%, #db2777 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Policy Products</h2>
                                <p>Manage life insurance product types and configurations</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="policy_products.php?action=create" class="btn btn-primary">
                                <i class="ri-product-hunt-line me-2"></i>Add Product
                            </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 Product</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <form method="POST">
                                            <div class="row">
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Product Name <span class="text-danger">*</span></label>
                                                    <input type="text" name="product_name" class="form-control" required>
                                                </div>
                                                <div class="col-md-6 mb-3">
                                                    <label class="form-label">Product Type <span class="text-danger">*</span></label>
                                                    <select name="product_type" class="form-select" required>
                                                        <option value="">Select Type</option>
                                                        <option value="TERM_LIFE">Term Life</option>
                                                        <option value="WHOLE_LIFE">Whole Life</option>
                                                        <option value="ENDOWMENT">Endowment</option>
                                                        <option value="GROUP_LIFE">Group Life</option>
                                                        <option value="CREDIT_LIFE">Credit Life</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label">Description</label>
                                                <textarea name="description" class="form-control" rows="3"></textarea>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-3 mb-3">
                                                    <label class="form-label">Min Sum Assured (KES)</label>
                                                    <input type="number" name="minimum_sum_assured" class="form-control" step="0.01">
                                                </div>
                                                <div class="col-md-3 mb-3">
                                                    <label class="form-label">Max Sum Assured (KES)</label>
                                                    <input type="number" name="maximum_sum_assured" class="form-control" step="0.01">
                                                </div>
                                                <div class="col-md-3 mb-3">
                                                    <label class="form-label">Minimum Premium (KES)</label>
                                                    <input type="number" name="minimum_premium" class="form-control" step="0.01">
                                                </div>
                                                <div class="col-md-3 mb-3">
                                                    <label class="form-label">Min Surrender Years</label>
                                                    <input type="number" name="minimum_surrender_years" class="form-control" value="0" min="0">
                                                </div>
                                            </div>
                                            <button type="submit" class="btn btn-primary">Save Product</button>
                                            <a href="policy_products.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">Products List</h4>
                                    </div>
                                    <div class="page-card-body">
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Product Name</th>
                                                        <th>Type</th>
                                                        <th>Min Sum Assured</th>
                                                        <th>Max Sum Assured</th>
                                                        <th>Min Premium</th>
                                                        <th>Min Surrender Years</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if (empty($products)): ?>
                                                        <tr><td colspan="7" class="text-center">No products found</td></tr>
                                                    <?php else: ?>
                                                        <?php foreach ($products as $product): ?>
                                                            <tr>
                                                                <td><strong><?php echo htmlspecialchars($product['product_name']); ?></strong></td>
                                                                <td><span class="badge bg-info"><?php echo htmlspecialchars($product['product_type']); ?></span></td>
                                                                <td>KES <?php echo number_format($product['minimum_sum_assured'] ?? 0, 2); ?></td>
                                                                <td>KES <?php echo number_format($product['maximum_sum_assured'] ?? 0, 2); ?></td>
                                                                <td>KES <?php echo number_format($product['minimum_premium'] ?? 0, 2); ?></td>
                                                                <td><?php echo $product['minimum_surrender_years']; ?> years</td>
                                                                <td>
                                                                    <a href="policy_rules.php?product_id=<?php echo $product['product_id']; ?>" class="btn btn-sm btn-success">
                                                                        <i class="ri-settings-3-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>
