<?php
include('../head.php');
include('../header.php'); 
requirePermission($pdo, $_SESSION['clientId'], 'calls');

$pspId = $_SESSION['pspId'];

// Fetch patients with welcome call data
$query = "SELECT 
    tblpatient.*,
    tblpatientdetails.patientId,
    tblpatientdetails.fileId,
    tblpatientdetails.createdDate,
    tblwcpart1.wcStatus,
    tblwcpart1.wcCompletionDate,
    tblwcpart1.wcCompletedBy,
    tblwcpart5.staff,
    tblwcpart5.patientDrugName
FROM tblpatient 
INNER JOIN tblpatientdetails ON tblpatient.id = tblpatientdetails.pId 
LEFT JOIN tblwcpart1 ON tblpatient.id = tblwcpart1.pId AND tblpatientdetails.fileId = tblwcpart1.fileId
LEFT JOIN tblwcpart5 ON tblpatient.id = tblwcpart5.pId AND tblpatientdetails.fileId = tblwcpart5.fileId
WHERE tblpatient.isDeleted='N' 
AND tblpatientdetails.isDeleted='N'
AND tblpatientdetails.pspId = ?
ORDER BY tblpatient.id DESC";

$stmt = $pdo->prepare($query);
$stmt->execute([$pspId]);
$patients = $stmt->fetchAll();
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Welcome Calls</h1>
                </div>
            </div>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Patient Welcome Call Status</h3>
                        </div>
                        <div class="card-body">
                            <table id="welcomeCallTable" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>Patient ID</th>
                                        <th>Patient Name</th>
                                        <th>DOB</th>
                                        <th>Province</th>
                                        <th>Drug Name</th>
                                        <th>WC Status</th>
                                        <th>Completion Date</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach($patients as $patient): ?>
                                    <tr>
                                        <td><?= $patient['patientId']; ?></td>
                                        <td><?= $patient['firstName'].' '.$patient['lastName']; ?></td>
                                        <td><?= $patient['dob']; ?></td>
                                        <td><?= $patient['province']; ?></td>
                                        <td><?= $patient['patientDrugName'] ?? 'N/A'; ?></td>
                                        <td>
                                            <?php if($patient['wcStatus'] == '5'): ?>
                                                <span class="badge badge-success">Completed</span>
                                            <?php elseif($patient['wcStatus']): ?>
                                                <span class="badge badge-warning">In Progress</span>
                                            <?php else: ?>
                                                <span class="badge badge-secondary">Pending</span>
                                            <?php endif; ?>
                                        </td>
                                        <td><?= $patient['wcCompletionDate'] ? date('Y-m-d', strtotime($patient['wcCompletionDate'])) : 'N/A'; ?></td>
                                        <td>
                                            <a href="edit_welcome_call.php?id=<?= $patient['id']; ?>&fileId=<?= $patient['fileId']; ?>" 
                                               class="btn btn-sm btn-info">
                                                <i class="fas fa-phone"></i> Update
                                            </a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php include('../footer.php'); ?>

<script>
$(document).ready(function() {
    $('#welcomeCallTable').DataTable({
        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": false,
        "responsive": true,
        "pageLength": 25
    });
});
</script>
