<form
    action="@if (!empty($packages)) {{ route('batchUserPackageChange') }} @else {{ route('batchUserBillingCycleChange') }} @endif"
    method="POST" class="flex flex-wrap" id="batchUserPackageChange">
    @csrf
    {{-- @else
        <form action="{{ route('batchUserBillingCycleChange') }}" method="POST" class="flex flex-wrap">
            @csrf
@endif --}}
    <input type="hidden" name="client_ids[]" id="client_ids">
    <input type="hidden" id="uuid" name="uuid" value="{{createUUID()}}">
    <div class="table-responsive">
        <table class="table table-bordered">
            <thead style="background: #78716C;color:white;">
                <tr>
                    <th class="text-center">
                        <input type="checkbox" id="allSelect">
                    </th>
                    <th scope="col" class="">
                        ID
                    </th>

                    <th scope="col" class="">
                        User name
                    </th>

                    <th scope="col" class="">
                        Expire Date
                    </th>

                    <th scope="col" class="">
                        Bill Cycle
                    </th>

                    @unlessrole('Sub Reseller')
                        <th scope="col" class="">
                            Package
                        </th>
                    @endunlessrole
                    <th scope="col" class="">
                        Sub Package
                    </th>

                    <th scope="col" class="">
                        Contact No
                    </th>


                </tr>
            </thead>
            <tbody class="">
                {{-- @dd($r) --}}
                @forelse ($r as $row)
                    <tr>
                        <td class="text-center">
                            <input type="checkbox" class="checkbox new_class_for_client_id_of_pending" name="id[]"
                                value="{{ $row->id }}">
                        </td>
                        <td>
                            {{-- <input type="hidden" value="{{ $row->id }}" name="ids[]"> --}}
                            {{ $row->id }}
                        </td>
                        <td>
                            {{ $row->userid }}
                        </td>

                        <td class="whitespace-nowrap">
                            {{ date('d-M-Y', strtotime($row->expire_date)) }}
                        </td>


                        <td>
                            {{ $row->billing_cycle }}
                        </td>
                        @unlessrole('Sub Reseller')
                            <td>
                                {{ $row->packages->package_name }}
                            </td>
                        @endunlessrole

                        <td>
                            @if ($row->subpack)
                                {{ $row->subpack->name }}
                            @endif
                        </td>

                        <td>
                            {{ $row->clientsinfo->contact_no ?? '' }}
                        </td>
                        {{-- <td class="px-6 text-left text-sm font-medium">

                        @if (!empty($packages))
                            <form action="{{ route('userPackageChange', $row->id) }}" method="POST"
                                class="flex flex-wrap">


                                @csrf
                                <input type="hidden" name="id" value="{{ $row->id }}">

                                <div class="row">
                                    <div class="col-md-7">


                                        @if ($row->pop->subreseller == 'yes')
                                            <select name="package_id" class="form-control" required>
                                                <option value="">Select</option>
                                                @foreach ($packages as $p)
                                                    <option value="{{ $p->id }}"
                                                        @if ($p->id == $r->sub_package_id) selected @endif>
                                                        ({{ $p->id }})
                                                        - {{ $p->name }} ({{ $p->rate }} TK)
                                                    </option>
                                                @endforeach

                                            </select>
                                        @else
                                            <select name="package_id" class="form-control" required>
                                                <option value="">Select</option>
                                                @foreach ($packages as $p)
                                                    <option value="{{ $p->id }}"
                                                        @if ($p->id == $row->package_id) selected @endif>
                                                        ({{ $p->id }})
                                                        - {{ $p->package_name }}
                                                        ({{ $p->package_rate }} TK)
                                                    </option>
                                                @endforeach

                                            </select>
                                        @endif

                                    </div>
                                    <div class="col-md-5">
                                        <button type="submit" class="btn btn-primary">Change Package</button>
                                    </div>
                                </div>



                            </form>
                        @else
                        @endif

                    </td> --}}
                    </tr>

                @empty
                @endforelse

                @if (!empty($packages))
                    <tr>
                        <td colspan="8">
                            <div class="row">
                                @can('batch-package-change-without-cost')
                                    <div class="col-sm-2">
                                        <label class="flex-1 block text-gray-700 text-sm font-bold mb-2"
                                            for="change_pack_without_cost">
                                            Change Package Without Cost
                                        </label>
                                    </div>
                                    <div class="col-sm-2">
                                        <select name="change_pack_without_cost" id="change_pack_without_cost"
                                            class="form-control">
                                            <option value="no">No</option>
                                            <option value="yes">Yes</option>
                                        </select>
                                    </div>
                                @endcan
                                <div class="col-sm-4"></div>
                                <div class="col-sm-2">

                                    @if ($row->pop->subreseller == 'yes')

                                        <select name="package_id" class="form-control" required>
                                            <option value="">Select</option>
                                            @foreach ($packages as $p)
                                                <option value="{{ $p->id }}">
                                                    ({{ $p->id }})
                                                    - {{ $p->name }} @if (globalPermission('package-change-price-show'))
                                                        ({{ $p->rate }} TK) (s)
                                                    @endif
                                                </option>
                                            @endforeach

                                        </select>
                                    @else
                                        <select name="package_id" class="form-control" required>
                                            <option value="">Select</option>
                                            @foreach ($packages as $p)
                                                <option value="{{ $p->id }}">
                                                    ({{ $p->id }})
                                                    - {{ $p->package_name }}
                                                    @if (globalPermission('package-change-price-show'))
                                                        ({{ $p->package_rate }} TK)
                                                    @endif
                                                </option>
                                            @endforeach

                                        </select>

                                    @endif

                                </div>

                                <div class="col-sm-2">
                                    {{-- <button type="submit" class="btn btn-primary">Change Package</button> --}}
                                    <button class="btn btn-primary" id="changePack">Change Package</button>
                                </div>
                            </div>


                        </td>
                    </tr>
                @else
                    <tr>
                        <td colspan="8">

                            <div class="container-fluid " style="background: #d8b4fe;">
                                <div class="row border  p-2">
                                    @can('change-billing-cycle-without-cost')
                                        <div class="col-sm-2">
                                            <label class="flex-1 block text-gray-700 text-sm font-bold mb-2"
                                                for="change_without_cost">
                                                Change Without Cost
                                            </label>
                                        </div>
                                        <div class="col-sm-2">
                                            <select name="change_without_cost" id="change_without_cost"
                                                class="form-control">
                                                <option value="no">No</option>
                                                <option value="yes">Yes</option>
                                            </select>
                                        </div>
                                    @endcan
                                    <div class="col-sm-4"></div>
                                    <div class="col-sm-2 border " id="billCycle">
                                        <select name="billCycle" class="form-control" required>
                                            <option value="">Select Cycle</option>
                                            @foreach (explode(',', $confday) as $item)
                                                <option value="{{ $item }}">
                                                    {{ $item }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="col-sm-2" id="expireDate" style="display: none">
                                        <input type="date" class="form-control" name="expire_date">
                                    </div>

                                    <div class="col-sm-2">
                                        {{-- <button type="submit" class="btn btn-primary">Billing Cycle Change</button> --}}
                                        <button class="btn btn-primary" id="changePack">Billing Cycle Change</button>
                                    </div>
                                </div>
                            </div>

                        </td>
                    </tr>

                @endif

                <!-- More items... -->
            </tbody>
        </table>
    </div>
</form>

<script>
    $(function() {
        $('#allSelect').on('click', function() {
            if (this.checked) {
                $('.new_class_for_client_id_of_pending').each(function() {
                    $(this).prop('checked', true); //check
                });
            } else {
                $('.new_class_for_client_id_of_pending').each(function() {
                    $(this).prop('checked', false); //uncheck
                });
            }
        });

        $('#changePack').on('click', function(e) {
            $('#changePack').prop('disabled', true);
            e.preventDefault();
            var ids = [];
            $('.new_class_for_client_id_of_pending').each(function() {
                id = $(this).filter(':checked').val();
                if (id != null) {
                    ids.push(id);
                }
            });
            $('#client_ids').val(ids);
            $('#batchUserPackageChange').submit();
        });

        $('#change_without_cost').on('change', function() {

            let type_val = $('#change_without_cost').val();

            if (type_val == 'yes') {
                $('#billCycle').hide();
                $('#expireDate').show();
            } else {
                $('#billCycle').show();
                $('#expireDate').hide();
            }
        });

    });
</script>
