@extends('layouts.master', ['title' => 'List - Dealer - Pages'])

@section('workspace')

    <!-- Breadcrumb -->
    <section class="breadcrumb lg:flex items-start">
        <div>
            <h1>Master Dealers</h1>
            <ul>
                <li><a href="#no-link">Pages</a></li>
                <li class="divider la la-arrow-right"></li>
                <li><a href="#no-link">Master Dealer</a></li>
                <li class="divider la la-arrow-right"></li>
                <li>List</li>
            </ul>
        </div>

        <div class="flex flex-wrap gap-2 items-center ltr:ml-auto rtl:mr-auto mt-5 lg:mt-0">

            <!-- Search -->
            <form class="flex flex-auto">
                <label class="form-control-addon-within rounded-full">
                    <input class="form-control border-none" placeholder="Search">
                    <button
                        class="text-gray-300 dark:text-gray-700 text-xl leading-none la la-search ltr:mr-4 rtl:ml-4"></button>
                </label>
            </form>

            <div class="flex gap-x-2">

                <!-- Sort By -->
                <div class="dropdown">
                    <button class="btn btn_outlined btn_secondary uppercase" data-toggle="dropdown-menu">
                        Sort By
                        <span class="ltr:ml-3 rtl:mr-3 la la-caret-down text-xl leading-none"></span>
                    </button>
                    <div class="dropdown-menu">
                        <a href="#no-link">Ascending</a>
                        <a href="#no-link">Descending</a>
                    </div>
                </div>

                <!-- Add New -->
                <a href="{{ route('admin.dealers.add-view') }}" class="btn btn_primary uppercase">Add New</a>
            </div>
        </div>
    </section>

    <!-- List -->
    <div class="card p-5">
        <div class="overflow-x-auto">
            <table class="table table-auto table_hoverable w-full">
                <thead>
                <tr>
                    <th class="ltr:text-left rtl:text-right uppercase">Company Name</th>
                    {{--                        <th class="text-center uppercase">Company Email</th>--}}
                    <th class="text-center uppercase">Contact Number</th>
                    <th class="text-center uppercase">State</th>
                    <th class="text-center uppercase">License(Used/Total)</th>
                    <th class="text-center uppercase">Status</th>
                    <th class="uppercase">Action</th>
                </tr>
                </thead>
                <tbody>
                @foreach($dealers as $dealer)
                    <tr>
                        <td>{{$dealer->company_name}}</td>
                        {{--                        <td class="text-center">{{$master-master-dealer->email}}</td>--}}
                        <td class="text-center">{{$dealer->contact_number}}</td>
                        <td class="text-center">{{ucwords($dealer->state)}}</td>
                        <td class="text-center">{{$dealer->used_license}}/{{$dealer->total_license}}</td>
                        <td class="text-center">
                            <div class="badge badge_outlined badge_secondary uppercase">{{$dealer->status}}</div>
                        </td>
                        <td class="ltr:text-right rtl:text-left whitespace-nowrap">
                            <div class="inline-flex ltr:ml-auto rtl:mr-auto">
                                <button class="btn btn-icon btn_outlined btn_primary" data-toggle="modal"
                                        data-target="#exampleModalCentered" onclick="openModal('{{$dealer->master_dealer_id}}')"><span
                                        class="la la-file-contract"></span></button>
                                <a href="#no-link" class="btn btn-icon btn_outlined btn_secondary ltr:ml-2 rtl:mr-2">
                                    <span class="la la-pen-fancy"></span>
                                </a>
                                <a href="#no-link" class="btn btn-icon btn_outlined btn_danger ltr:ml-2 rtl:mr-2">
                                    <span class="la la-trash-alt"></span>
                                </a>
                            </div>
                        </td>
                    </tr>
                @endforeach

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

    <div class="mt-5">
        @include('partials._pagination')
    </div>

    <div id="exampleModalCentered" class="modal" data-animations="fadeInDown, fadeOutUp" data-static-backdrop>
        <div class="modal-dialog modal-dialog_centered max-w-2xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title">Assign License</h2>
                    <button class="close la la-times" data-dismiss="modal" onclick="closeModal()"></button>
                </div>
                <div class="modal-body">
                    <div class="card p-5">
                        <div class="relative">
                            {{--                            <label--}}
                            {{--                                class="label absolute block bg-input top-0 ltr:ml-3 rtl:mr-3 px-1 rounded font-heading uppercase"--}}
                            {{--                                for="input-1">Assign License Quantity</label>--}}
                            {{--                            <input id="input-1" class="form-control mt-2 pt-5" placeholder="Enter quantity here">--}}
                            <label class="label block mb-2" for="license_qty">License Quantity</label>
                            <input id="license_qty" class="form-control" type="number" placeholder="Enter quantity here"
                                   min="1">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="flex ltr:ml-auto rtl:mr-auto">
                        <button class="btn btn_secondary uppercase" data-dismiss="modal" onclick="closeModal()">Close
                        </button>
                        <button class="btn btn_primary ltr:ml-2 rtl:mr-2 uppercase" onclick="submitFunction()">Submit
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection

@section('scripts')
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>

        let selectedMasterDealerId = '';

        function closeModal() {
            selectedMasterDealerId = '';
        }

        function openModal(masterDealerId) {
            selectedMasterDealerId = masterDealerId;
        }


        function submitFunction() {
            let license_qty = document.getElementById('license_qty').value;
            if (license_qty === '') {
                alert('Please enter license quantity');
                return;
            } else if (license_qty < 1) {
                alert('Please enter valid license quantity');
                return;
            }

            axios.post('{{route('admin.dealers.assign-license')}}', {
                _token: '{{csrf_token()}}',
                master_dealer_id: selectedMasterDealerId,
                license_qty: license_qty
            })
                .then(function (response) {
                    console.log(response);
                    if (response.status === 200) {
                        alert(response.data.message);
                        window.location.reload();
                    } else {
                        alert(response.data.message);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            console.log(license_qty);
        }

    </script>
@endsection
