@extends('layouts.backendTemplate')

@section('content')

    <!--begin::Main-->
    <div class="app-main flex-column flex-row-fluid" id="kt_app_main">

        <div class="d-flex flex-column flex-column-fluid">


            @include('uc.breadcrumbList', [
                'title' => 'Category Management',
                'pageName' => 'Category Management',
            ])


            <div id="kt_app_content" class="app-content flex-column-fluid">
                <!--begin::Content container-->
                <div id="kt_app_content_container" class="app-container container-xxl">
                    <!--begin::Card-->
                    <div class="card">

                        @if (Session::has('messageSuccess'))
                            <div class="card-body">
                                @include('uc/messageSuccess')
                            </div>
                        @endif

                        @if (Session::has('messageFail'))
                            <div class="card-body">
                                @include('uc/messageFail')
                            </div>
                        @endif

                        <!--begin::Card header-->
                        <div class="card-header border-0 pt-6">
                            <!--begin::Card title-->
                            <div class="d-flex align-items-center position-relative my-1">
                                <span class="svg-icon svg-icon-1 position-absolute ms-6">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none"
                                        xmlns="http://www.w3.org/2000/svg">
                                        <rect opacity="0.5" x="17.0365" y="15.1223" width="8.15546" height="2"
                                            rx="1" transform="rotate(45 17.0365 15.1223)" fill="currentColor" />
                                        <path
                                            d="M11 19C6.55556 19 3 15.4444 3 11C3 6.55556 6.55556 3 11 3C15.4444 3 19 6.55556 19 11C19 15.4444 15.4444 19 11 19ZM11 5C7.53333 5 5 7.53333 5 11C5 14.4667 7.53333 17 11 17C14.4667 17 17 14.4667 17 11C17 7.53333 14.4667 5 11 5Z"
                                            fill="currentColor" />
                                    </svg>
                                </span>
                                <input type="text" table-filter="search"
                                    class="form-control form-control-solid w-250px ps-15" placeholder="Search" />
                            </div>

                            <div class="card-toolbar">
                                <div class="w-150px me-3">
                                    <select class="form-select form-select-solid" data-control="select2"
                                        data-hide-search="true" data-placeholder="Status" table-filter="status">
                                        <option></option>
                                        <option value="all">All</option>
                                        <option value="active">Active</option>
                                        <option value="inactive">Inactive</option>
                                    </select>
                                </div>

                                <div class="d-flex justify-content-end" data-kt-customer-table-toolbar="base">
                                    <a href="{{ url('category/add') }}" class="btn btn-primary">Add </a>
                                </div>

                            </div>

                        </div>


                        <div class="card-body py-4">
                            <!--begin::Table-->
                            <table class="table align-middle table-row-dashed fs-6 gy-5" id="data-table-category">
                                <!--begin::Table head-->
                                <thead>
                                    <!--begin::Table row-->
                                    <tr class="text-start text-muted fw-bold fs-7 text-uppercase gs-0">
                                        <th class="min-w-125px">Pic</th>
                                        <th class="min-w-125px">Name</th>
                                        <th class="min-w-125px">Name (EN)</th>
                                        <th class="min-w-125px">Created</th>
                                        <th class="min-w-125px">Active</th>
                                        <th class="text-end min-w-100px">Actions</th>
                                    </tr>
                                    <!--end::Table row-->
                                </thead>
                                <!--end::Table head-->
                                <!--begin::Table body-->
                                <tbody class="text-gray-600 fw-semibold">

                                    @foreach ($itemView as $obj)
                                        <!--begin::Table row-->
                                        <tr>
                                            <td class="d-flex align-items-center">
                                                <div class="symbol symbol-100px symbol-2by3 me-4">
                                                    @if ($obj->image_url && $obj->image_name)
                                                        <a class="show-image-preview d-block overlay"
                                                            href="{{ url($obj->image_url . '/original/' . $obj->image_name) }}">
                                                            <div class="symbol-label"
                                                                style="background-image: url('{{ url($obj->image_url . '/thumbnail/' . $obj->image_name) }}')">
                                                            </div>
                                                        </a>
                                                    @else
                                                        <div class="symbol-label"
                                                            style="background-image: url('{{ url('assets/media/avatars/blank.png') }}')">
                                                        </div>
                                                    @endif
                                                </div>
                                            </td>

                                            <td>
                                                {{ $obj->name }}
                                            </td>

                                            <td>
                                                {{ $obj->name_en }}
                                            </td>

                                            <td class="">{{ date('d M, Y', strtotime($obj->created_at)) }}</td>


                                            <td class="">
                                                @if ($obj->active == 1)
                                                    <span class="badge py-3 px-4 fs-7 badge-light-success">Active</span>
                                                @else
                                                    <span class="badge py-3 px-4 fs-7 badge-light-danger">Inactive</span>
                                                @endif
                                            </td>


                                            <td class="min-w-100px text-end">

                                                {{-- Edit --}}
                                                <a href="{{ url('category/edit/' . $obj->id) }}"
                                                    class="btn btn-icon btn-bg-light btn-active-color-primary btn-sm me-1">
                                                    <span class="svg-icon svg-icon-3">
                                                        <svg width="24" height="24" viewBox="0 0 24 24"
                                                            fill="none" xmlns="http://www.w3.org/2000/svg">
                                                            <path opacity="0.3"
                                                                d="M21.4 8.35303L19.241 10.511L13.485 4.755L15.643 2.59595C16.0248 2.21423 16.5426 1.99988 17.0825 1.99988C17.6224 1.99988 18.1402 2.21423 18.522 2.59595L21.4 5.474C21.7817 5.85581 21.9962 6.37355 21.9962 6.91345C21.9962 7.45335 21.7817 7.97122 21.4 8.35303ZM3.68699 21.932L9.88699 19.865L4.13099 14.109L2.06399 20.309C1.98815 20.5354 1.97703 20.7787 2.03189 21.0111C2.08674 21.2436 2.2054 21.4561 2.37449 21.6248C2.54359 21.7934 2.75641 21.9115 2.989 21.9658C3.22158 22.0201 3.4647 22.0084 3.69099 21.932H3.68699Z"
                                                                fill="currentColor" />
                                                            <path
                                                                d="M5.574 21.3L3.692 21.928C3.46591 22.0032 3.22334 22.0141 2.99144 21.9594C2.75954 21.9046 2.54744 21.7864 2.3789 21.6179C2.21036 21.4495 2.09202 21.2375 2.03711 21.0056C1.9822 20.7737 1.99289 20.5312 2.06799 20.3051L2.696 18.422L5.574 21.3ZM4.13499 14.105L9.891 19.861L19.245 10.507L13.489 4.75098L4.13499 14.105Z"
                                                                fill="currentColor" />
                                                        </svg>
                                                    </span>
                                                </a>

                                                {{-- Delete --}}
                                                @if ($obj->active == 1)
                                                    <a href="#" data-id="{{ $obj->id }}"
                                                        class="btn btn-icon btn-bg-light btn-active-color-primary btn-sm delete-item">
                                                        <span class="svg-icon svg-icon-3">
                                                            <svg width="24" height="24" viewBox="0 0 24 24"
                                                                fill="none" xmlns="http://www.w3.org/2000/svg">
                                                                <path
                                                                    d="M5 9C5 8.44772 5.44772 8 6 8H18C18.5523 8 19 8.44772 19 9V18C19 19.6569 17.6569 21 16 21H8C6.34315 21 5 19.6569 5 18V9Z"
                                                                    fill="currentColor" />
                                                                <path opacity="0.5"
                                                                    d="M5 5C5 4.44772 5.44772 4 6 4H18C18.5523 4 19 4.44772 19 5V5C19 5.55228 18.5523 6 18 6H6C5.44772 6 5 5.55228 5 5V5Z"
                                                                    fill="currentColor" />
                                                                <path opacity="0.5"
                                                                    d="M9 4C9 3.44772 9.44772 3 10 3H14C14.5523 3 15 3.44772 15 4V4H9V4Z"
                                                                    fill="currentColor" />
                                                            </svg>
                                                        </span>
                                                    </a>
                                                @endif
                                            </td>


                                        </tr>
                                    @endforeach

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


@stop


@section('script')

    <script type="text/javascript">
        var datatable;
        datatable = $("#data-table-category").DataTable({
            "info": false,
            "order": []
        });

        // Filter by search
        const filterSearch = document.querySelector('[table-filter="search"]');
        filterSearch.addEventListener('keyup', function(e) {
            datatable.search(e.target.value).draw();
        });

        // filter by status
        const filterStatus = document.querySelector('[table-filter="status"]');
        $(filterStatus).on('change', e => {
            let value = e.target.value;
            if (value === 'all') {
                value = '';
            }
            datatable.column(4).search(value).draw();
        });


        // Remove
        $(document).on('click', '.delete-item', function() {
            swal.fire({
                allowOutsideClick: false,
                text: "Are you sure you would like to Delete?",
                icon: "warning",
                buttonsStyling: false,
                showDenyButton: true,
                confirmButtonText: "Yes",
                denyButtonText: 'No',
                customClass: {
                    confirmButton: "btn btn-primary",
                    denyButton: "btn btn-light-danger"
                }
            }).then((result) => {
                if (result.isConfirmed) {

                    alertLoading("Loading")

                    // set up ajax
                    initAjaxSetupToken();

                    const ajaxProp = {
                        url: "{{ url('category/delete') }}",
                        type: 'POST',
                        data: {
                            item_id: $(this).data('id')
                        },
                    };

                    $.ajax(ajaxProp).done(function(res) {
                        if (res.status) {
                            const url = "{{ url('category') }}";
                            alertSuccessWithUrl('Notice', "Delete success", url);
                            hideLoading();
                        } else {
                            alertFail('Notice', res.message ?? "Unsuccessfully");
                            hideLoading();
                        }
                    }).fail(function(xhr, status, error) {
                        logAjaxError(xhr, status, error);
                        alertFail('Notice', error.message ?? "Unsuccessfully");
                        hideLoading();
                    });
                }
            });
        });
    </script>

@endsection
