@extends('layouts.app')

@section('title', trans('documentations.page.title'))

@section('content')
    <div class="nk-block-head nk-block-head-sm">
        <div class="nk-block-between">
            <div class="nk-block-head-content">
                <h3 class="nk-block-title page-title">@lang('documentations.page.heading')</h3>
            </div>
            <div class="nk-block-head-content">
                <div class="toggle-wrap nk-block-tools-toggle">
                    <a href="javascript:void(0);" class="btn btn-icon btn-trigger toggle-expand mr-n1" data-target="pageMenu">
                        <em class="icon ni ni-more-v"></em>
                    </a>
                    @can('documentation.add')
                        <div class="toggle-expand-content" data-content="pageMenu">
                            <ul class="nk-block-tools g-3">
                                <li class="nk-block-tools-opt">
                                    <a href="{{ route('documentations.create') }}" class="btn btn-primary" async-modal async-modal-size="xl">
                                        <em class="icon ni ni-plus"></em>
                                        <span>@lang('documentations.page.buttons.create')</span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    @endcan
                </div>
            </div>
        </div>
    </div>

    <div class="nk-block">
        <div class="card card-bordered card-stretch">
            <div class="card-aside-wrap">
                <div class="card-content">
                    <ul class="nav nav-tabs nav-tabs-mb-icon nav-tabs-card">
                        <li class="nav-item">
                            <a class="nav-link active" data-bs-toggle="tab" href="#active"><em class="icon ni ni-activity"></em><span>@lang('global.table.type.active')</span></a>
                        </li>
                        @if (auth()->user()->can('documentation.delete'))
                            <li class="nav-item">
                                <a class="nav-link" data-bs-toggle="tab" href="#archived" id="archived_tab"><em class="icon ni ni-trash"></em><span>@lang('global.table.type.deleted')</span></a>
                            </li>
                        @endif
                    </ul>
                    <div class="card-inner position-relative card-tools-toggle table-responsive">
                        <div class="tab-content">
                            <div class="tab-pane active" id="active">
                                <table id="documentations-dt" class="table nowrap nk-tb-list nk-tb-ulist dataTable no-footer" width="100%">
                                    <thead>
                                        <tr class="nk-tb-item nk-tb-head">
                                            <th><span class="sub-text">#</span></th>
                                            <th><span class="sub-text">@lang('documentations.table.header.key')</span></th>
                                            <th><span class="sub-text">@lang('documentations.table.header.title')</span></th>
                                            <th><span class="sub-text">@lang('documentations.table.header.content')</span></th>
                                            <th><span class="sub-text">@lang('documentations.table.header.created')</span></th>
                                            <th><span class="sub-text">@lang('documentations.table.header.updated')</span></th>
                                            @if (auth()->user()->can('documentation.update'))
                                                <th class="text-right"><span class="sub-text">@lang('documentations.table.header.actions')</span></th>
                                            @endif
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                            @if (auth()->user()->can('documentation.delete'))
                                <div class="tab-pane" id="archived">
                                    <table id="archived-documentations-dt" class="table nowrap nk-tb-list nk-tb-ulist dataTable no-footer" width="100%">
                                        <thead>
                                            <tr class="nk-tb-item nk-tb-head">
                                                <th><span class="sub-text">#</span></th>
                                                <th><span class="sub-text">@lang('documentations.table.header.key')</span></th>
                                                <th><span class="sub-text">@lang('documentations.table.header.title')</span></th>
                                                <th><span class="sub-text">@lang('documentations.table.header.content')</span></th>
                                                <th><span class="sub-text">@lang('documentations.table.header.created')</span></th>
                                                <th><span class="sub-text">@lang('documentations.table.header.updated')</span></th>
                                                @if (auth()->user()->can('documentation.update'))
                                                    <th class="text-right"><span class="sub-text">@lang('documentations.table.header.actions')</span></th>
                                                @endif
                                            </tr>
                                        </thead>
                                    </table>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script src="{{ asset('assets/libs/tinymce/tinymce.min.js') }}" referrerpolicy="origin"></script>
    <script>
        var table = $('#documentations-dt').DataTable({
            processing: true,
            serverSide: true,
            scrollX: false,
            ordering: true,
            autoWidth: true,
            ajax: "{{ route('documentations.dataTable') }}",
            columns: [{
                    data: 'DT_RowIndex',
                    name: 'uuid',
                    orderable: false,
                    searchable: false
                },
                {
                    data: 'key',
                    name: 'key'
                },
                {
                    data: 'title',
                    name: 'title'
                },
                {
                    data: 'content',
                    name: 'content'
                },
                {
                    data: 'created',
                    name: 'created',
                },
                {
                    data: 'updated',
                    name: 'updated'
                },
                @if (auth()->user()->can(['documentation.update']))
                    {
                        data: 'action',
                        name: 'action'
                    },
                @endif
            ],
            createdRow: function(row, data, index) {
                $(row).addClass('nk-tb-item');
            }
        });

        var archivedTable;

        $(document).ready(function() {
            @if (auth()->user()->can('documentation.delete'))
                // Reload active table when the "Active" tab is clicked
                $('a[data-bs-toggle="tab"]').on('shown.bs.tab', function(event) {
                    var target = $(event.target).attr("href"); // Get the tab being shown
                    if (target === "#active") {
                        table.ajax.reload();
                    } else if (target === "#archived") {
                        // Initialize the archived table if not already done
                        if (!archivedTable) {
                            archivedTable = $('#archived-documentations-dt').DataTable({
                                processing: true,
                                serverSide: true,
                                scrollX: false,
                                ordering: false,
                                autoWidth: true,
                                ajax: "{{ route('documentations.dataTable.archived') }}",
                                columns: [{
                                        data: 'DT_RowIndex',
                                        name: 'uuid',
                                        orderable: false,
                                        searchable: false
                                    },
                                    {
                                        data: 'key',
                                        name: 'key'
                                    },
                                    {
                                        data: 'title',
                                        name: 'title'
                                    },
                                    {
                                        data: 'content',
                                        name: 'content'
                                    },
                                    {
                                        data: 'created',
                                        name: 'created',
                                    },
                                    {
                                        data: 'updated',
                                        name: 'updated'
                                    },
                                    @if (auth()->user()->can(['documentation.update']))
                                        {
                                            data: 'action',
                                            name: 'action'
                                        },
                                    @endif
                                ],
                                createdRow: function(row, data, index) {
                                    $(row).addClass('nk-tb-item');
                                }
                            });
                        } else {
                            // If already initialized, reload the archived table
                            archivedTable.ajax.reload();
                        }
                    }
                });
            @endif
        });

        $(document).on("asyncmodal.loaded", function() {
            var editor = tinymce.get("editor-container");

            if (editor) editor.destroy();

            richTextEditor("editor-container");
        });
    </script>
@endpush
