@extends('admin.layouts.app')
@section('panel')
    <div class="row">
        <div class="col-lg-12">
            <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('Username')/@lang('User ID')</label>
                                    <input type="text" name="search" value="{{ request()->search }}"
                                        class="form-control">
                                </div>

                                <div class="flex-grow-1">
                                    <label>@lang('Rank')</label>
                                    <select class="form-control" name="rank">
                                        <option value="">@lang('Any')</option>
                                        @foreach (range(1, 7) as $rank)
                                            <option value="{{ $rank }}" @selected(request()->rank == $rank)>
                                                {{ __(keyToTitle($rank)) }}</option>
                                        @endforeach
                                    </select>
                                </div>

                                <div class="flex-grow-1">
                                    <label>@lang('Status')</label>
                                    <select class="form-control" name="status">
                                        <option value="">@lang('Any')</option>
                                        <option value="{{ Status::Pending }}" @selected(request()->status == Status::Pending)>
                                            {{ __('Pending') }}</option>
                                        <option value="{{ Status::Approved }}" @selected(request()->status == Status::Approved)>
                                            {{ __('Approved') }}</option>
                                        <option value="{{ Status::Rejected }}" @selected(request()->status == Status::Rejected)>
                                            {{ __('Rejected') }}</option>
                                    </select>
                                </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>

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

                <div class="row">
                    <div class="col-xxl-6 col-sm-6 mb-3">
                        <div class="widget-two style--two box--shadow2 b-radius--5 bg--1">
                            <div class="widget-two__icon b-radius--5 bg--primary">
                                <i class="fas fa-money-bill-wave"></i>
                            </div>
                            <div class="widget-two__content">
                                <h3 class="text-white">{{ $general->cur_sym }}{{ showAmount($total_rewards_ammount) }}
                                </h3>
                                <p class="text-white">@lang('Total Reward Income')</p>
                            </div>
                            {{-- <a href="{{ route('admin.withdraw.log') }}?search={{ $user->username }}" class="widget-two__btn">@lang('View All')</a> --}}
                        </div>
                    </div>

                    <div class="col-xxl-6 col-sm-6 mb-3">
                        <div class="widget-two style--two box--shadow2 b-radius--5 bg--17">
                            <div class="widget-two__icon b-radius--5 bg--primary">
                                <i class="fas fa-users"></i>
                            </div>
                            <div class="widget-two__content">
                                <h3 class="text-white">{{ $total_members }}</h3>
                                <p class="text-white">@lang('Total Members')</p>
                            </div>
                            {{-- <a href="{{ route('admin.report.transaction') }}?search={{ $user->username }}" class="widget-two__btn">@lang('View All')</a> --}}
                        </div>
                    </div>
                </div>

                <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('User ID')</th>
                                        <th>@lang('Name')</th>
                                        <th>@lang('Amount')</th>
                                        <th>@lang('Status')</th>
                                        <th>@lang('Rank')</th>
                                        <th>@lang('Created on')</th>
                                        <th>@lang('Action')</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($rewards as $reward)
                                        <tr>
                                            <td><span>{{ @$reward->user->system_id }}</span></td>
                                            <td>
                                                <b>{{@$reward->user->fullname}}</b>
                                                <br>
                                                <span>
                                                    (<span>@</span>{{ @$reward->user->username }})
                                                </span>

                                            </td>

                                            <td>
                                                <span>{{ showAmount($reward->prize) }}
                                                    {{ __($general->cur_text) }}</span>
                                            </td>

                                            <td>
                                                @php
                                                    echo $reward->statusBadge;
                                                @endphp
                                            </td>

                                            <td>
                                                <span class="fw-bold">{!! $reward->rankBadge !!}</span>
                                            </td>
                                            <td>
                                                <span class="">{{ showDateTime($reward->created_at) }}</span>
                                            </td>

                                            <td>
                                                @if ($reward->status == Status::Pending)
                                                    <button class="btn btn-outline--primary btn-sm approve-btn"
                                                        data-id="{{ $reward->id }}" data-bs-toggle="modal"\
                                                        data-reward="{{ json_encode($reward) }}"
                                                        data-bs-target="#approveModal"
                                                        data-action="{{ route('admin.reward.status', $reward->id) }}">
                                                        <i class="fa fa-check"></i> @lang('Approve')
                                                    </button>
                                                    <button class="btn btn-outline--danger btn-sm reject-btn"
                                                        data-id="{{ $reward->id }}" data-bs-toggle="modal"
                                                        data-bs-target="#rejectModal"
                                                        data-action="{{ route('admin.reward.status', $reward->id) }}">
                                                        <i class="fa fa-ban"></i> @lang('Reject')
                                                    </button>
                                                @else
                                                    @if ($reward->status == Status::Approved)
                                                        <span class="text-success">{{ trans('Approved at') }}
                                                            {{ showDateTime($reward->status_changed_at) }}</span>
                                                    @elseif($reward->status == Status::Rejected)
                                                        <span class="text-danger">{{ trans('Rejected at') }}
                                                            {{ showDateTime($reward->status_changed_at) }}</span>
                                                        <br>
                                                        <div
                                                            style="max-width: 400px; white-space: wrap; overflow: hidden; text-overflow: ellipsis;">
                                                            <span class="text-secondary"><b>Reason:
                                                                </b>{{ $reward->reject_reason }}</span>
                                                        </div>
                                                    @endif
                                                @endif
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td class="text-muted text-center" colspan="100%">{{ __($emptyMessage) }}</td>
                                        </tr>
                                    @endforelse

                                </tbody>
                            </table>
                        </div>
                    </div>
                    @if ($rewards->hasPages())
                        <div class="card-footer py-4">
                            @php echo paginateLinks($rewards) @endphp
                        </div>
                    @endif
                </div>
            </div>
        </div>


        <x-confirmation-modal />

        <div class="modal fade" id="rejectModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
            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="exampleModalCenterTitle">@lang('Reject Reward')</h5>
                        <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                            <i class="las la-times"></i>
                        </button>
                    </div>
                    <form action="" method="POST">
                        @csrf
                        <input type="hidden" name="status" value="{{ Status::Rejected }}">
                        <div class="modal-body">
                            <div class="form-group">
                                <label for="reject_reason">@lang('Reason for Rejection')</label>
                                <textarea class="form-control" id="reject_reason" name="reject_reason" rows="5" required></textarea>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline--primary"
                                data-bs-dismiss="modal">@lang('Close')</button>
                            <button type="submit" class="btn btn--danger">@lang('Submit')</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="modal fade" id="approveModal" tabindex="-1" role="dialog"
            aria-labelledby="exampleModalCenterTitle" 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="exampleModalCenterTitle">@lang('Approve Reward')</h5>
                        <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                            <i class="las la-times"></i>
                        </button>
                    </div>
                    <form action="" method="POST">
                        @csrf
                        <div class="modal-body">
                            <div class="form-group">
                                <label for="charge">@lang('Charge')</label>
                                <div class="input-group">
                                    <span class="input-group-text">{{ __($general->cur_sym) }}</span>
                                    <input type="number" class="form-control" id="charge" name="charge" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="payable">@lang('Payable Amount')</label>
                                <div class="input-group">
                                    <span class="input-group-text">{{ __($general->cur_sym) }}</span>
                                    <input type="number" class="form-control" id="payable" name="payable" required>
                                </div>
                            </div>

                            <input type="hidden" name="status" value="{{ Status::Approved }}">

                            <div class="form-group">
                                <label for="transaction_no">@lang('Transaction No')</label>
                                <input type="text" class="form-control" id="transaction_no" name="trx" required>
                            </div>
                            <div class="form-group">
                                <label for="payment_mode">@lang('Payment Mode')</label>
                                <select class="form-control" id="payment_mode" name="payment_mode" required>
                                    <option value="">@lang('-- Select --')</option>
                                    <option value="{{ Status::PaymentModeUpi }}">@lang('UPI')</option>
                                    <option value="{{ Status::PaymentModeBank }}">@lang('Bank Transfer')</option>
                                </select>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline--primary"
                                data-bs-dismiss="modal">@lang('Close')</button>
                            <button type="submit" class="btn btn--success">@lang('Submit')</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('breadcrumb-plugins')
@endpush

@push('script')
    <script>
        "use strict";

        $(document).on('click', '.approve-btn, .reject-btn', function() {
            var action = $(this).data('action');
            var modal = $(this).data('bs-target');
            $(modal + ' form').attr('action', action);
        });

        $(document).on('click', '.approve-btn', function() {
            var reward = $(this).data('reward');
            var modal = $(this).data('bs-target');
            $(modal + ' #charge').val(0);
            $(modal + ' #payable').val(reward.prize);
        });
    </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
@push('script')
    <script>
        (function($) {
            "use strict";
            if (!$('.datepicker-here').val()) {
                $('.datepicker-here').datepicker();
            }
        })(jQuery)
    </script>
@endpush
