@extends('layouts.master')

@section('content')
    <div class="az-content-breadcrumb">
        <span>Product Parent Categories</span>
    </div>
    <div class="d-flex justify-content-between align-items-center">
        <div>
            <h2 class="az-content-title" style="display: inline">Product Parent Category List</h2>
            <nav aria-label="breadcrumb" class="mt-3">
                <ol class="breadcrumb bg-transparent p-2  border rounded">
                    <li class="breadcrumb-item">
                        <a href="{{ url('product_cate_main/list') }}" class="text-decoration-none text-dark">
                            <i class="fas fa-home me-1"></i> Product Main Category List
                        </a>
                    </li>
                    <li class="breadcrumb-item active text-primary" aria-current="page">
                        <i class="fas fa-folder-open me-1"></i> {{ $product->products_category_name }}
                    </li>
                </ol>
            </nav>
        </div>

        <span>
            <a href="{{ url('product_cate_main/list') }}" class="btn btn-outline-secondary me-2">
                <i class="fas fa-arrow-left me-1"></i>
                Back to Product Main Category
            </a>
            @can('Product Categories add')
                <a id="addbtn" href="#" class="btn btn-az-primary" style="float: right" data-toggle="modal"
                    data-target="#createModal">
                    <i class="fa-solid fa-plus"></i> Add Product Parent Category
                </a>
            @endcan
        </span>
    </div>

    <div class="row">
        <div class="col-md-12 col-lg-12 col-xl-12">
            <div class="card card-body pd-20">
                <div>
                    <table id="example2" class="table table-striped">
                        <thead>
                            <tr>
                                <th class="wd-10p">S.No</th>
                                <th class="wd-10p">Product Sub Category Name</th>
                                <th class="wd-10p">Product Sub Category (Urdu)</th>
                                <th class="wd-10p">Status</th>
                                <th class="wd-10p">Created By</th>
                                <th class="wd-10p">Created</th>
                                <th class="wd-20p">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($productsSubCategoryParent as $key => $prod)
                                <tr>
                                    <td>{{ $key + 1 }}</td>
                                    <td>{{ $prod->name }}</td>
                                    <td>{{ $prod->name_urdu }}</td>
                                    <td>
                                        @if ($prod->status == 1)
                                            <span class="btn btn-rounded btn-success text-white">Active</span>
                                        @else
                                            <span class="btn btn-rounded btn-danger">In Active</span>
                                        @endif
                                    </td>
                                    @php
                                        $user = \App\User::find($prod->created_by);
                                    @endphp
                                    <td>{{ $user ? $user->name : $prod->created_by }}</td>
                                    <td>{{ date('d-m-Y', strtotime($prod->created_at)) }}</td>
                                    <td>
                                        <a class="btn rounded shadow-base text-info"
                                            href="{{ url('product_cate_child/list/' . Crypt::encrypt($product->id_pro_cate_main) . '/' . Crypt::encrypt($prod->id_pro_sub_cat_parent)) }}">
                                            <i class="fa-solid fa-arrow-up-right-from-square"></i>
                                            <strong>Sub Category</strong>
                                        </a>
                                        @can('Product Categories edit')
                                            <a class="btn rounded shadow-base" title="Edit Product" href="#"
                                                data-toggle="modal" data-target="#editModal"
                                                data-id="{{ $prod->id_pro_sub_cat_parent }}" data-name="{{ $prod->name }}"
                                                data-name_urdu="{{ $prod->name_urdu }}" data-status="{{ $prod->status }}"
                                                data-id_pro_cate_main="{{ $product->id_pro_cate_main }}">
                                                <i class="text-primary fa-regular fa-pen-to-square"></i>
                                                <span class="text-primary">Edit</span>
                                            </a>
                                        @endcan
                                        @can('Product Categories delete')
                                            <a class="btn rounded shadow-base" title="Delete Product" href="#"
                                                data-toggle="modal" data-target="#deleteModal"
                                                data-id="{{ $prod->id_pro_sub_cat_parent }}">
                                                <i class="text-danger fa-solid fa-trash-can"></i>
                                                <span class="text-danger">Remove</span>
                                            </a>
                                        @endcan
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot>
                            <tr>
                                <th class="wd-10p">S.No</th>
                                <th class="wd-10p">Product Sub Category Name</th>
                                <th class="wd-10p">Product Sub Category (Urdu)</th>
                                <th class="wd-10p">Status</th>
                                <th class="wd-10p">Created By</th>
                                <th class="wd-10p">Created</th>
                                <th class="wd-20p">Action</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Create Modal -->
    <div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="createModalLabel">Add New Product Sub Category</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form id="createForm" action="{{ url('product_cate_parent/store') }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        <select class="form-control" name="id_pro_cate_main" id="" hidden>
                            <option value="{{ $product->id_pro_cate_main }}">
                                {{ $product->products_category_name }}
                            </option>
                        </select>

                        <div class="form-group">
                            <label for="name">Product Sub Category Name</label>
                            <input type="text" class="form-control" name="name" value="{{ old('name') }}" required>
                        </div>
                        <div class="form-group">
                            <label for="name_urdu">Product Sub Category Name (Urdu)</label>
                            <input type="text" class="form-control" name="name_urdu" value="{{ old('name_urdu') }}">
                        </div>
                        <div class="form-group">
                            <label for="status">Status</label>
                            <select class="form-control" name="status" required>
                                <option value="1">Active</option>
                                <option value="0">Inactive</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary text-light" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Edit Modal -->
    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editModalLabel">Edit Product Sub Category</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form id="editForm" action="" method="POST">
                    @csrf
                    <div class="modal-body">
                        <input type="hidden" name="id" id="edit-id">
                        <select class="form-control" name="id_pro_cate_main" hidden id="edit-id_pro_cate_main">
                            <option value="{{ $product->id_pro_cate_main ?? '' }}">
                                {{ $product->products_category_name ?? 'Select Category' }}
                            </option>
                        </select>

                        <div class="form-group">
                            <label for="edit-name">Product Sub Category Name</label>
                            <input type="text" class="form-control" id="edit-name" name="name" required>
                        </div>
                        <div class="form-group">
                            <label for="edit-name-urdu">Product Sub Category Name (Urdu)</label>
                            <input type="text" class="form-control" id="edit-name-urdu" name="name_urdu">
                        </div>
                        <div class="form-group">
                            <label for="edit-status">Status</label>
                            <select class="form-control" id="edit-status" name="status">
                                <option value="1">Active</option>
                                <option value="0">Inactive</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary text-light" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Update</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Delete Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteModalLabel">Confirm Deletion</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    Are you sure you want to delete this Product Parent Category?
                </div>
                <div class="modal-footer">
                    <form id="deleteForm" action="" method="POST">
                        @csrf
                        <button type="button" class="btn btn-secondary text-light" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-danger">Delete</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script type="text/javascript">
        document.addEventListener('keydown', function(event) {
            // Check if CTRL + A (or CMD + A) is pressed
            if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'a') {
                event.preventDefault(); // Prevent default "Select All" behavior

                // Trigger the click event on the "Add Goods Receipts" button
                document.getElementById('addbtn').click();
            }
        });
        document.addEventListener('keydown', function(event) {
            if ((event.ctrlKey || event.metaKey) && event.key === 's') {
                event.preventDefault();

                // Submit the form
                document.getElementById('createForm').submit();
            }

            if (event.key === 'Enter') {
                event.preventDefault();
            }
        });

        $(function() {
            $('#example2').DataTable({
                dom: 'Bfrtip',
                buttons: [{
                        extend: 'csv',
                        text: 'CSV',
                        titleAttr: 'Export to CSV',
                        className: 'btn btn-success btn-sm'
                    },
                    {
                        extend: 'excel',
                        text: 'Excel',
                        titleAttr: 'Export to Excel',
                        className: 'btn btn-primary btn-sm'
                    },
                    {
                        extend: 'pdf',
                        text: 'PDF',
                        titleAttr: 'Export to PDF',
                        className: 'btn btn-danger btn-sm'
                    }
                ]
            });

            $('#editModal').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget); // Button that triggered the modal
                var id = button.data('id');
                var id_pro_cate_main = button.data(
                    'id_pro_cate_main'); // Ensure this is correctly set
                var name = button.data('name');
                var name_urdu = button.data('name_urdu');
                var status = button.data('status');

                var modal = $(this);
                modal.find('#edit-id').val(id);
                modal.find('#edit-id_pro_cate_main').val(
                    id_pro_cate_main); // Ensure this line is correct
                modal.find('#edit-name').val(name);
                modal.find('#edit-name-urdu').val(name_urdu);
                modal.find('#edit-status').val(status);

                // Use the pattern that's working for you
                modal.find('#editForm').attr('action', "{{ url('product_cate_parent/update') }}/" + id);
            });


            $('#deleteModal').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget); // Button that triggered the modal
                var id = button.data('id');
                var modal = $(this);
                modal.find('#deleteForm').attr('action', "{{ url('product_cate_parent/destroy') }}/" + id);
            });
        });
    </script>
@endpush
