@extends('layouts.master')
@section('content')
    <div class="az-content-breadcrumb">
        <span>Purchaser</span>
    </div>
    <h2 class="az-content-title" style="display: inline">Purchaser<span>
            @can('Purchasers add')
                <a id="addbtn" href="{{ route('purchaser.create') }}" class="btn btn-az-primary" style="float: right">
                    <i class="fa-solid fa-plus"></i> Add Purchaser</a>
            @endcan
        </span>
    </h2>

    <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">Purchaser Name</th>
                                <th class="wd-10p">Purchaser Name (Urdu)</th>
                                <th class="wd-10p">Purchaser Email</th>
                                <th class="wd-10p">Purchaser Address </th>
                                <th class="wd-10p">Purchaser City </th>
                                <th class="wd-10p">Contact Person </th>
                                <th class="wd-10p">Phone</th>
                                <th class="wd-10p">Website </th>
                                <th class="none">Status </th>
                                <th class="none">Created</th>
                                <th class="none">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($purchasers as $key => $purchaser)
                                <tr>
                                    <td>{{ $key + 1 }}</td>
                                    <td>{{ $purchaser->purchaser_name }} </td>
                                    <td>{{ $purchaser->purchaser_name_urdu }}</td>
                                    <td>{{ $purchaser->purchaser_email }}</td>
                                    <td>{{ $purchaser->purchaser_address }}</td>
                                    <td>{{ getCity($purchaser->purchaser_city) }}</td>
                                    <td>{{ $purchaser->contact_person }}</td>
                                    <td>{{ $purchaser->phone_1 }}</td>
                                    <td>{{ $purchaser->website }}</td>
                                    <td>
                                        @if ($purchaser->status == 1)
                                            <span class="btn btn-rounded btn-success text-white">Active</span>
                                        @else
                                            <span class="btn btn-rounded btn-danger">Inactive</span>
                                        @endif
                                    </td>
                                    <td>{{ date('d-m-Y', strtotime($purchaser->created_at)) }}</td>
                                    <td>
                                        @can('Purchasers edit')
                                            <a class="btn rounded shadow-base" title="Edit Purchasers"
                                                href="{{ route('purchaser.edit', \Crypt::encrypt($purchaser->id_purchaser)) }}">
                                                <i class="text-primary fa-regular fa-pen-to-square"></i> <span
                                                    class="text-primary">Edit</span>
                                            </a>
                                        @endcan

                                        {{-- @can('Purchasers view')
                                            <a class="btn rounded shadow-base" title="Purchasers Details"
                                                href="{{ route('purchaser.view', \Crypt::encrypt($purchaser->id_purchaser)) }}">
                                                <span class="mdi mdi-account-details-outline text-success"></span> <span
                                                    class="text-success">
                                                    Summary</span>
                                            </a>
                                        @endcan --}}
                                        @can('Purchasers delete')
                                            <a class="btn rounded shadow-base" title="Remove Supplier" data-toggle="modal"
                                                data-target="#deleteModal" data-id="{{ $purchaser->id_purchaser }}">
                                                <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">Purchaser Name</th>
                                <th class="wd-10p">(کھاتہ)</th>
                                <th class="wd-10p">Purchaser Email</th>
                                <th class="wd-10p">Purchaser Address </th>
                                <th class="wd-10p">Purchaser City </th>
                                <th class="wd-10p">Contact Person </th>
                                <th class="wd-10p">Phone</th>
                                <th class="wd-10p">Website </th>
                                <th class="none">Status </th>
                                <th class="none">Created</th>
                                <th class="none">Action</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            <!-- card -->
        </div>
        <!-- col -->
    </div>

    <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 Purchaser?
                </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>
    <!-- Brand Modal -->
    {{-- <div class="modal fade" id="brandModal" tabindex="-1" role="dialog" aria-labelledby="brandModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="brandModalLabel">Assign Brand to Supplier</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="brandForm" action="{{ route('supplier.assign.brand') }}" method="POST">
                        @csrf
                        <input type="hidden" name="supplier_id" id="supplier_id" value="">
                        <div class="form-group">
                            <label class="az-content-label tx-11 tx-medium tx-gray-600">Select Brand</label>
                            <select class="form-control" name="brand_id" id="brand_id">
                                @foreach ($brands as $brand)
                                    <option value="{{ $brand->id_brands }}">{{ $brand->brands_name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div> --}}

    {{-- </div><!-- az-content-body --> --}}
@endsection

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

                // Trigger the click on the "Add Customers" button
                document.getElementById('addbtn').click();
            }
        });

        function confirmDelete() {
            return confirm('Are you sure you want to delete this Purchaser?');
        }

        // <script type="text/javascript">
        $(function() {
            oTable = $('#example2').DataTable({
                dom: 'Bfrtip',
                buttons: [{
                        extend: 'csv',
                        text: 'CSV',
                        title: 'Purchaser List',
                        className: 'btn btn-default',
                        exportOptions: {
                            columns: 'th:not(:last-child)'
                        }
                    },
                    {
                        extend: 'excel',
                        text: 'Excel',
                        title: 'Purchaser List',
                        className: 'btn btn-default',
                        exportOptions: {
                            columns: 'th:not(:last-child)'
                        }
                    },
                    {
                        extend: 'pdf',
                        text: 'PDF',
                        title: 'Purchaser List',
                        className: 'btn btn-default',
                        exportOptions: {
                            columns: 'th:not(:last-child)'
                        }
                    },
                    {
                        extend: 'print',
                        text: 'Print',
                        title: 'Purchaser List',
                        className: 'btn btn-default',
                        exportOptions: {
                            columns: 'th:not(:last-child)'
                        }
                    }
                ],
                responsive: !0
            });
        });


        $(document).ready(function() {
            $('#deleteModal').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget);
                var id = button.data('id');
                var actionUrl = '{{ url('purchaser/destroy') }}/' + id;
                var modal = $(this);
                modal.find('#deleteForm').attr('action', actionUrl);
            });
        });

        $(document).ready(function() {
            $('#brandModal').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget);
                var supplierId = button.data('id');
                var modal = $(this);
                modal.find('#supplier_id').val(supplierId);
            });
        });
    </script>
@endpush
