<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Handle Add/Edit/Delete logic here if needed, or just list.
// For brevity, just listing.
$sql = "SELECT * FROM communication_templates";
$result = $conn->query($sql);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Communication Templates')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Communication Templates</h4>
                                <div class="page-title-right">
                                    <button class="btn btn-primary btn-sm"><i class="ri-add-line"></i> Add
                                        Template</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <table id="tplTable" class="table table-bordered dt-responsive nowrap">
                                        <thead>
                                            <tr>
                                                <th>Template Name</th>
                                                <th>Type (SMS/Email)</th>
                                                <th>Subject</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            if ($result) {
                                                while ($row = $result->fetch_assoc()):
                                                    ?>
                                                    <tr>
                                                        <td><?php echo htmlspecialchars($row['template_name']); ?></td>
                                                        <td><span
                                                                class="badge bg-<?php echo ($row['type'] == 'SMS' ? 'success' : 'primary'); ?>"><?php echo $row['type']; ?></span>
                                                        </td>
                                                        <td><?php echo htmlspecialchars($row['subject']); ?></td>
                                                        <td>
                                                            <button class="btn btn-sm btn-soft-primary"><i
                                                                    class="ri-pencil-line"></i></button>
                                                            <button class="btn btn-sm btn-soft-danger"><i
                                                                    class="ri-delete-bin-line"></i></button>
                                                        </td>
                                                    </tr>
                                                <?php endwhile;
                                            } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#tplTable').DataTable();</script>
</body>

</html>