<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-bullhorn me-2"></i><?= lang('comm.announcements') ?></h4>
    <a href="/communications/announcements/create" class="btn btn-primary"><i class="fas fa-plus me-1"></i><?= lang('common.create') ?></a>
</div>
<div class="card"><div class="card-body">
    <table id="announcements-table" class="table table-striped table-hover w-100"
           data-url="/communications/api/announcements/datatable"
           data-columns='<?= json_encode([
               ["data"=>"id","title"=>"#","width"=>"50px"],
               ["data"=>"title_en","title"=>"Title"],
               ["data"=>"category","title"=>"Category"],
               ["data"=>"priority","title"=>"Priority"],
               ["data"=>"target_audience","title"=>"Audience"],
               ["data"=>"is_published","title"=>"Published"],
               ["data"=>"is_pinned","title"=>"Pinned"],
               ["data"=>"publish_date","title"=>"Publish Date"],
               ["data"=>"id","title"=>"Actions","orderable"=>false,"searchable"=>false]
           ]) ?>'>
        <thead><tr><th>#</th><th>Title</th><th>Category</th><th>Priority</th><th>Audience</th><th>Published</th><th>Pinned</th><th>Publish</th><th>Actions</th></tr></thead>
    </table>
</div></div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    DataTableHelper.init('announcements-table', {
        columnDefs: [
            { targets: 1, render: function(d){ return d ? d.substring(0,40)+(d.length>40?'...':'') : ''; }},
            { targets: 3, render: function(d){
                var map = {low:'secondary',normal:'primary',high:'warning',urgent:'danger'};
                return '<span class="badge bg-'+(map[d]||'secondary')+'">'+d+'</span>';
            }},
            { targets: 5, render: function(d,t,row){
                var cls = d==1?'btn-success':'btn-outline-secondary';
                var icon = d==1?'fa-eye':'fa-eye-slash';
                return '<button class="btn btn-sm '+cls+'" onclick="togglePublish('+row.id+')"><i class="fas '+icon+'"></i></button>';
            }},
            { targets: 6, render: function(d,t,row){
                var cls = d==1?'btn-warning':'btn-outline-secondary';
                return '<button class="btn btn-sm '+cls+'" onclick="togglePin('+row.id+')"><i class="fas fa-thumbtack"></i></button>';
            }},
            { targets: 7, render: function(d){ return d ? d.substring(0,10) : ''; }},
            { targets: 8, render: function(d){
                return '<div class="btn-group btn-group-sm">'
                    +'<a href="/communications/announcements/'+d+'/edit" class="btn btn-outline-primary"><i class="fas fa-edit"></i></a>'
                    +' <button class="btn btn-outline-danger" onclick="deleteAnn('+d+')"><i class="fas fa-trash"></i></button>'
                    +'</div>';
            }}
        ]
    });
});
function togglePublish(id){$.post('/communications/announcements/'+id+'/toggle-publish',{_token:'<?= csrf_token() ?>'},function(r){if(r.success){FormHelper.showToast('success',r.message);DataTableHelper.reload('announcements-table');}});}
function togglePin(id){$.post('/communications/announcements/'+id+'/toggle-pin',{_token:'<?= csrf_token() ?>'},function(r){if(r.success){FormHelper.showToast('success',r.message);DataTableHelper.reload('announcements-table');}});}
function deleteAnn(id){Swal.fire({title:'Delete?',icon:'warning',showCancelButton:true,confirmButtonColor:'#e53e3e',confirmButtonText:'Delete'}).then(r=>{if(r.isConfirmed)$.post('/communications/announcements/'+id+'/delete',{_token:'<?= csrf_token() ?>'},function(r){if(r.success){FormHelper.showToast('success',r.message);DataTableHelper.reload('announcements-table');}});});}
</script>
<?php $__view->endPush() ?>