<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-file-alt me-2"></i><?= lang('comm.document_templates') ?></h4>
    <?php if (Gate::can('documents.manage')): ?>
    <a href="/communications/document-templates/create" class="btn btn-primary"><i class="fas fa-plus me-1"></i><?= lang('common.create') ?></a>
    <?php endif; ?>
</div>
<div class="card"><div class="card-body">
    <table id="doctemplates-table" class="table table-striped table-hover w-100"
           data-url="/communications/api/document-templates/datatable"
           data-columns='<?= json_encode([
               ["data"=>"id","title"=>"#","width"=>"50px"],
               ["data"=>"template_code","title"=>"Code"],
               ["data"=>"name_en","title"=>"Name"],
               ["data"=>"category","title"=>"Category"],
               ["data"=>"template_type","title"=>"Type"],
               ["data"=>"is_active","title"=>"Active"],
               ["data"=>"version","title"=>"Ver"],
               ["data"=>"id","title"=>"Actions","orderable"=>false,"searchable"=>false]
           ]) ?>'>
        <thead><tr><th>#</th><th>Code</th><th>Name</th><th>Category</th><th>Type</th><th>Active</th><th>Ver</th><th>Actions</th></tr></thead>
    </table>
</div></div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    DataTableHelper.init('doctemplates-table', {
        columnDefs: [
            { targets: 5, render: function(d){ return d==1?'<span class="badge bg-success">Active</span>':'<span class="badge bg-secondary">Inactive</span>'; }},
            { targets: 7, render: function(d){
                return '<div class="btn-group btn-group-sm">'
                    +'<a href="/communications/document-templates/'+d+'/edit" class="btn btn-outline-primary"><i class="fas fa-edit"></i></a>'
                    +' <button class="btn btn-outline-danger" onclick="deleteDocTpl('+d+')"><i class="fas fa-trash"></i></button>'
                    +'</div>';
            }}
        ]
    });
});
function deleteDocTpl(id){Swal.fire({title:'Deactivate?',icon:'warning',showCancelButton:true,confirmButtonColor:'#e53e3e',confirmButtonText:'Yes'}).then(r=>{if(r.isConfirmed)$.post('/communications/document-templates/'+id+'/delete',{_token:'<?= csrf_token() ?>'},function(r){if(r.success){FormHelper.showToast('success',r.message);DataTableHelper.reload('doctemplates-table');}});});}
</script>
<?php $__view->endPush() ?>