@extends('admin.layouts.app')

@section('panel')
    <div class="row justify-content-center">

        <div class="col-lg-12">
            <div class="show-filter mb-3 text-end">
                <button type="button" class="btn btn-outline--primary showFilterBtn btn-sm"><i class="las la-filter"></i>
                    @lang('Filter')</button>
            </div>
            <div class="card responsive-filter-card mb-4">
                <div class="card-body">
                    <form action="">
                        <div class="d-flex flex-wrap gap-4">
                            <div class="flex-grow-1">
                                <label>@lang('Email/Username/UserID/code')</label>
                                <input type="text" name="search" value="{{ request()->search }}" class="form-control">
                            </div>

                            <div class="flex-grow-1">
                                <label>@lang('Date')</label>
                                <input name="date" type="text" data-range="true" data-multiple-dates-separator=" - "
                                    data-language="en" class="datepicker-here form-control" data-position='bottom right'
                                    placeholder="@lang('Start date - End date')" autocomplete="off" value="{{ request()->date }}">
                            </div>

                            <div class="flex-grow-1 align-self-end">
                                <button class="btn btn--primary w-100 h-45"><i class="fas fa-filter"></i>
                                    @lang('Filter')</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>


            @if (!request()->routeIs('admin.users.dummy'))
                <div class="d-flex justify-content-end">
                    <div class="btn-group mb-3" role="group" aria-label="Basic example">
                        <a href="{{ route('admin.coupon.Export', ['search' => request()->search, 'date' => request()->date]) }}"
                            class="btn btn--success"><i class="las la-file-excel"></i> @lang('Export Excel')</a>
                        <a href="{{ route('admin.coupon.pdf', ['search' => request()->search, 'date' => request()->date  ]) }}"
                            class="btn btn--danger"><i class="las la-file-pdf"></i> @lang('Export PDF')</a>
                    </div>
                </div>
            @endif

        <div class="col-md-12">
            <div class="card b-radius--10">
                <div class="card-body p-0">
                    <div class="table-responsive--sm table-responsive">
                        <table class="table table--light style--two">
                            <thead>
                                <tr>
                                    <th>@lang('Username')</th>
                                    <th>@lang('Code')</th>
                                    <th>@lang('Published At')</th>
                                    <th>@lang('Expires At')</th>
                                    <th>@lang('Used At')</th>
                                    <th>@lang('Status')</th>

                                    <th>@lang('Action')</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($coupons as $coupon)
                                    <tr>
                                        <td>
                                            <span class="fw-bold">{{ $coupon->user->username }}</span>
                                            <br>
                                            <span>UserId: <b>{{ $coupon->user->system_id }}</b></span>
                                            <br>
                                            <span>Mobile Number: <b>{{ $coupon->user->mobile }}</b></span>
                                        </td>
                                        <td>
                                            <span class="fw-bold">{{ $coupon->code }}</span>
                                        </td>
                                        <td>{{ showDateTime($coupon->published_at) }}</td>
                                        <td>{{ showDateTime($coupon->expires_at) }}</td>
                                        <td>{{ showDateTime($coupon->used_at) }}</td>
                                        <td>@php echo $coupon->statusBadge @endphp</td>
                                        <td>
                                            @haspermission('coupon:edit', 'admin')
                                                <button type="button" class="btn btn-sm btn-outline--primary editBtn"
                                                    data-bs-route="{{ route('admin.coupon.edit', $coupon->id) }}"
                                                    data-bs-title="@lang('Edit Coupon : ') {{ $coupon->code }}" data-bs-toggle="modal"
                                                    data-bs-target="#couponModal">
                                                    <i class="la la-pen"></i> @lang('Edit')
                                                </button>
                                            @endhaspermission
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td class="text-muted text-center" colspan="100%">{{ __($emptyMessage) }}</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table><!-- table end -->
                    </div>
                </div>
                @if ($coupons->hasPages())
                    <div class="card-footer py-4">
                        @php echo paginateLinks($coupons) @endphp
                    </div>
                @endif
            </div><!-- card end -->
        </div>
    </div>

    <div class="modal fade" id="couponModal" tabindex="-1" role="dialog" aria-labelledby="couponModalLabel"
        aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="couponModalLabel">@lang('Edit Coupon')</h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <i class="las la-times"></i>
                    </button>
                </div>
                <div class="modal-body">
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="bulkUploadModal" tabindex="-1" role="dialog" aria-labelledby="bulkUploadModalLabel"
        aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="bulkUploadModalLabel">@lang('Upload Bulk Coupon')</h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <i class="las la-times"></i>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('admin.coupon.bulk-upload') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        <div class="form-group">
                            <div class="input-group">
                                <div class="custom-file">
                                    <input type="file" class="custom-file-input" name="file" accept=".csv" required>
                                    <label class="custom-file-label" for="file">@lang('Choose CSV file')</label>
                                </div>
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-sm btn-outline--primary"><i
                                            class="las la-upload"></i>@lang('Upload')</button>
                                </div>
                            </div>
                        </div>
                        <p><a href="{{ route('admin.coupon.sample-bulk-upload') }}" download><i
                                    class="las la-download"></i>@lang('Download Sample CSV')</a></p>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection


@push('breadcrumb-plugins')
    @haspermission('coupon:create', 'admin')
        <button type="button" class="btn btn-sm btn-outline--primary editBtn" data-bs-route="{{ route('admin.coupon.edit') }}"
            data-bs-title="@lang('Create Coupon')" data-bs-toggle="modal" data-bs-target="#couponModal">
            <i class="la la-plus"></i> @lang('Create')
        </button>
    @endhaspermission

    @haspermission('coupon:bulk_upload','admin')
    <button type="button" class="btn btn-sm btn-outline--primary" data-bs-toggle="modal"
        data-bs-target="#bulkUploadModal"><i class="las la-upload"></i>@lang('Upload Bulk Coupon')</button>
    @endhaspermission

    <x-search-form dateSearch='no' :placeholder="__('Email/UserId/username')" />
@endpush


@push('script')
    <script>
        (function($) {
            $('.editBtn').on('click', function() {
                var modal = $('#couponModal');
                modal.find('#couponModalLabel').text($(this).data('bs-title'));
                var url = $(this).data('bs-route');
                $.ajax({
                    url: url,
                    method: 'GET',
                    success: function(response) {
                        modal.find('.modal-title').text($(this).data('bs-title'));
                        modal.find('.modal-body').html(response);
                        modal.modal('show');
                    }
                });
            });
        })(jQuery);
    </script>
@endpush

@push('style-lib')
    <link rel="stylesheet" href="{{ asset('assets/admin/css/vendor/datepicker.min.css') }}">
@endpush


@push('script-lib')
    <script src="{{ asset('assets/admin/js/vendor/datepicker.min.js') }}"></script>
    <script src="{{ asset('assets/admin/js/vendor/datepicker.en.js') }}"></script>
@endpush
