<style>
/* Style to truncate long text in table cells */
.truncate-text {
    max-width: 300px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}
</style>

<div class="container mt-4">
    <div class="card modern-card shadow-sm border-0 rounded-4">
        <div class="card-body p-4">

            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">Riwayat Pesan</h5>
            </div>

            <div class="table-responsive">
                <table id="tablePesan" class="table modern-table align-middle w-100">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Waktu</th>
                            <th>Tipe</th>
                            <th>Penerima</th>
                            <th>Pesan</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>

        </div>
    </div>
</div>

<!-- Modal for full message -->
<div class="modal fade" id="pesanModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Isi Pesan Lengkap</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body" style="white-space: pre-wrap; word-wrap: break-word;">
                ...
            </div>
        </div>
    </div>
</div>


<script>
$(document).ready(function(){

    let table = $('#tablePesan').DataTable({
        dom: '<"dataTables_top d-flex justify-content-between mb-3"l f>rtip',
        processing: true,
        serverSide: true,
        responsive: false,
        autoWidth: false,
        order: [],
        ajax: {
            url: "<?= base_url('pesan/get_data'); ?>",
            type: "POST"
        },
        columns: [
            { data: 0, orderable: false },
            { data: 1 },
            { data: 2 },
            { data: 3 },
            { data: 4 },
            { data: 5 }
        ],
        columnDefs: [
            {
                targets: 4, // Target the 'Pesan' column
                createdCell: function (td, cellData, rowData, row, col) {
                    $(td).attr('title', 'Klik untuk lihat lengkap');
                }
            }
        ],
        language: {
            processing: `<div class="d-flex justify-content-center"><div class="spinner-border text-success"></div></div>`
        }
    });

    $('.dataTables_filter input').attr('placeholder', 'Cari pesan, nomor, email...').css({
        'min-width': '300px',
        'padding': '6px 12px',
        'border-radius': '6px',
        'border': '1px solid #ced4da',
        'font-size': '0.9rem',
        'box-sizing': 'border-box'
    });

    // Handle click on truncated text to show full message in modal
    $('#tablePesan tbody').on('click', 'td .truncate-text', function () {
        var fullText = $(this).text();
        $('#pesanModal .modal-body').text(fullText);
        var pesanModal = new bootstrap.Modal(document.getElementById('pesanModal'));
        pesanModal.show();
    });

});
</script>
