@extends('admin.layouts.app')

@section('panel')
    <div class="row">
        <div class="col-12">
            <div class="row gy-4">

                <div class="col-xxl-3 col-sm-6">
                    <div class="widget-two style--two box--shadow2 b-radius--5 bg--19">
                        <div class="widget-two__icon b-radius--5 bg--primary">
                            <i class="las la-money-bill-wave-alt"></i>
                        </div>
                        <div class="widget-two__content">
                            <h3 class="text-white">{{ $general->cur_sym }}{{ showAmount($user->balance) }}</h3>
                            <p class="text-white">@lang('Balance')</p>
                        </div>
                        <a href="{{ route('admin.report.transaction') }}?search={{ $user->username }}"
                            class="widget-two__btn">@lang('View All')</a>
                    </div>
                </div>
                <!-- dashboard-w1 end -->


                <div class="col-xxl-3 col-sm-6">
                    <div class="widget-two style--two box--shadow2 b-radius--5 bg--primary">
                        <div class="widget-two__icon b-radius--5 bg--primary">
                            <i class="las la-wallet"></i>
                        </div>
                        <div class="widget-two__content">
                            <h3 class="text-white">{{ $general->cur_sym }}{{ showAmount($totalDonationMade) }}</h3>
                            <p class="text-white">@lang('Total Paid Donations')</p>
                        </div>
                        <a href="{{ route('admin.report.transaction', $user->id) }}?search={{ $user->username }}&remark={{ Status::DonationMade }}"
                            class="widget-two__btn">@lang('View All')</a>
                    </div>
                </div>
                <!-- dashboard-w1 end -->
                <div class="col-xxl-3 col-sm-6">
                    <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-wallet"></i>
                        </div>
                        <div class="widget-two__content">
                            <h3 class="text-white">{{ $general->cur_sym }}{{ showAmount($totalDonationReceived) }}</h3>
                            <p class="text-white">@lang('Total Received Donations')</p>
                        </div>
                        <a href="{{ route('admin.report.transaction') }}?search={{ $user->username }}&remark={{ Status::DonationReceived }}"
                            class="widget-two__btn">@lang('View All')</a>
                    </div>
                </div>
                <!-- dashboard-w1 end -->

                <div class="col-xxl-3 col-sm-6">
                    <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="las la-exchange-alt"></i>
                        </div>
                        <div class="widget-two__content">
                            <h3 class="text-white">{{ $totalTransaction }}</h3>
                            <p class="text-white">@lang('Transactions')</p>
                        </div>
                        <a href="{{ route('admin.report.transaction') }}?search={{ $user->username }}"
                            class="widget-two__btn">@lang('View All')</a>
                    </div>
                </div>
                <!-- dashboard-w1 end -->
                <div class="col-xxl-3 col-sm-6">
                    <div class="widget-two style--two box--shadow2 b-radius--5 bg--10">
                        <div class="widget-two__icon b-radius--5 bg--primary">
                            <i class="las la-gift"></i>
                        </div>
                        <div class="widget-two__content">
                            <h3 class="text-white">{{ $general->cur_sym }}{{ showAmount($totalReward) }}</h3>
                            <p class="text-white">@lang('Total Reward')</p>
                        </div>
                        <a href="{{ route('admin.report.transaction') }}?search={{ $user->username }}&remark={{ Status::CommissionReward }}"
                            class="widget-two__btn">@lang('View All')</a>
                    </div>
                </div>
                <div class="col-xxl-3 col-sm-6">
                    <div class="widget-two style--two box--shadow2 b-radius--5 bg--10">
                        <div class="widget-two__icon b-radius--5 bg--primary">
                            <i class="las la-user"></i>
                        </div>
                        <div class="widget-two__content">
                            <h3 class="text-white">{{ $user->rank }}</h3>
                            <p class="text-white">@lang('Rank')</p>
                        </div>

                    </div>
                </div>
                <!-- dashboard-w1 end -->


            </div>

            <div class="row gy-4 mt-2">

            </div>

            <div class="d-flex flex-wrap gap-3 mt-4">
                @haspermission('manage_user:user_balance_edit', 'admin')
                    <div class="flex-fill">
                        <button data-bs-toggle="modal" data-bs-target="#addSubModal"
                            class="btn btn--success btn--shadow w-100 btn-lg bal-btn" data-act="add">
                            <i class="las la-plus-circle"></i> @lang('Balance')
                        </button>
                    </div>

                    <div class="flex-fill">
                        <button data-bs-toggle="modal" data-bs-target="#addSubModal"
                            class="btn btn--danger btn--shadow w-100 btn-lg bal-btn" data-act="sub">
                            <i class="las la-minus-circle"></i> @lang('Balance')
                        </button>
                    </div>
                @endhaspermission

                <div class="flex-fill">
                    <a href="{{ route('admin.report.login.history') }}?search={{ $user->username }}"
                        class="btn btn--primary btn--shadow w-100 btn-lg">
                        <i class="las la-list-alt"></i>@lang('Logins')
                    </a>
                </div>

                <div class="flex-fill">
                    <a href="{{ route('admin.users.notification.log', $user->id) }}"
                        class="btn btn--secondary btn--shadow w-100 btn-lg">
                        <i class="las la-bell"></i>@lang('Notifications')
                    </a>
                </div>
                @haspermission('manage_user:login_as_user', 'admin')
                    <div class="flex-fill">
                        <a href="{{ route('admin.users.login', $user->id) }}" target="_blank"
                            class="btn btn--primary btn--gradi btn--shadow w-100 btn-lg">
                            <i class="las la-sign-in-alt"></i>@lang('Login as User')
                        </a>
                    </div>
                @endhaspermission

                @haspermission('manage_user:user_kyc_data_edit', 'admin')
                    <div class="flex-fill">
                        <a href="{{ route('admin.users.kyc.details', $user->id) }}"
                            class="btn btn--dark btn--shadow w-100 btn-lg">
                            <i class="las la-user-check"></i>@lang('KYC Data')
                        </a>
                    </div>
                @endhaspermission

                @haspermission('manage_user:user_ban', 'admin')
                    <div class="flex-fill">
                        @if ($user->status == Status::USER_ACTIVE)
                            <button type="button" class="btn btn--warning btn--gradi btn--shadow w-100 btn-lg userStatus"
                                data-bs-toggle="modal" data-bs-target="#userStatusModal">
                                <i class="las la-ban"></i>@lang('Ban User')
                            </button>
                        @else
                            <button type="button" class="btn btn--success btn--gradi btn--shadow w-100 btn-lg userStatus"
                                data-bs-toggle="modal" data-bs-target="#userStatusModal">
                                <i class="las la-undo"></i>@lang('Unban User')
                            </button>
                        @endif
                    </div>
                @endhaspermission
            </div>


            <div class="card mt-30">
                <div class="card-header">
                    <h5 class="card-title mb-0">@lang('Information of') {{ $user->fullname }} (<b>UserID</b>:
                        {{ $user->system_id }} | <b>Username</b>: {{ $user->username }})</h5>


                </div>
                <div class="card-body">

                    <div class="row">

                        <div class="col-12">
                            <form action="{{ route('admin.users.update', [$user->id]) }}" method="POST"
                                enctype="multipart/form-data">
                                @csrf

                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group ">
                                            <label>@lang('First Name')</label>
                                            <input class="form-control" type="text" name="firstname" required
                                                value="{{ $user->firstname }}">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>@lang('Last Name')</label>
                                            <input class="form-control" type="text" name="lastname" required
                                                value="{{ $user->lastname }}">
                                        </div>
                                    </div>

                                </div>

                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>@lang('Email') </label>
                                            @haspermission('manage_user:edit_email', 'admin')
                                                <input class="form-control" type="email" name="email"
                                                    value="{{ $user->email }}" required>
                                            @else
                                                <input class="form-control" type="email" value="{{ $user->email }}"
                                                    readonly>
                                            @endhaspermission
                                        </div>
                                    </div>

                                    @haspermission('manage_user:user_password_edit', 'admin')
                                        <div class="col-md-6">
                                            <div class="form-group">
                                                <label>@lang('Password')</label>
                                                <input class="form-control" type="text" name="pass_token"
                                                    autocomplete="new-password" value="{{ $user->pass_token }}">
                                            </div>
                                        </div>
                                    @endhaspermission

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>@lang('Mobile Number') </label>
                                            <div class="input-group ">
                                                <span class="input-group-text mobile-code"></span>
                                                @haspermission('manage_user:edit_number', 'admin')
                                                    <input type="number" name="mobile" value="{{ old('mobile') }}"
                                                        id="mobile" class="form-control checkUser" required>
                                                @else
                                                    <input type="number" name="mobile" value="{{ old('mobile') }}"
                                                        id="mobile" class="form-control checkUser" readonly>
                                                @endhaspermission
                                            </div>
                                        </div>
                                    </div>
                                    {{-- <div class="col-md-6">
                                        <div class="form-group">
                                            <label>@lang('Person Dummy Or Not') </label>
                                            <select class="form-control" name="dummy_person" id="floatingSelect"
                                                aria-label="Floating label select example">
                                                <option value="0"
                                                    {{ old('dummy_person', $user->dummy_person) == 0 ? 'selected' : '' }}>
                                                    No</option>
                                                <option value="1"
                                                    {{ old('dummy_person', $user->dummy_person) == 1 ? 'selected' : '' }}>
                                                    Yes</option>
                                            </select>
                                        </div>
                                    </div> --}}

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>@lang('Network Status')</label>
                                            @haspermission('manage_user:edit_network_status', 'admin')
                                                <select class="form-control" name="network_status" id="floatingSelect"
                                                    aria-label="Floating label select example">
                                                    <option value="{{ Status::NetworkPending }}" @selected(old('network_status', $user->network_status) == Status::NetworkPending)>
                                                        @lang('Pending')
                                                    </option>
                                                    <option value="{{ Status::NetworkPlanPurchased }}"
                                                        @selected(old('network_status', $user->network_status) == Status::NetworkPlanPurchased)>
                                                        @lang('Plan Purchased')
                                                    </option>
                                                    <option value="{{ Status::NetworkFullActivated }}"
                                                        @selected(old('network_status', $user->network_status) == Status::NetworkFullActivated)>
                                                        @lang('Full Activated')
                                                    </option>
                                                </select>
                                            @else
                                                <p class="form-control-plaintext">
                                                    @if ($user->network_status == Status::NetworkPending)
                                                        @lang('Pending')
                                                    @elseif ($user->network_status == Status::NetworkPlanPurchased)
                                                        @lang('Plan Purchased')
                                                    @elseif ($user->network_status == Status::NetworkFullActivated)
                                                        @lang('Full Activated')
                                                    @else
                                                        @lang('Unknown')
                                                    @endif
                                                </p>
                                            @endhaspermission
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        @haspermission('manage_user:edit_referel_person', 'admin')
                                            <div class="form-group">
                                                <label>@lang('Name Of Referel Person')</label>
                                                <select class="select2-basic" style="width: 100%" name="ref_by"
                                                    id="referedPerson">
                                                    @if ($user->ref_by)
                                                        <option value="{{ $user->ref_by }}" selected>
                                                            {{ $user->referral->username }}</option>
                                                    @else
                                                        <option value="" selected disabled>@lang('Select One')</option>
                                                    @endif
                                                </select>
                                                <small class="form-text text-muted">@lang('Email, UserId and username are allowed')</small>
                                            </div>
                                        @else
                                            <div class="form-group">
                                                <label>@lang('Name Of Referel Person')</label>
                                                <select class="select2-basic" style="width: 100%" readonly>
                                                    @if ($user->ref_by)
                                                        <option value="{{ $user->ref_by }}" selected>
                                                            {{ $user->referral->username }}</option>
                                                    @else
                                                        <option value="" selected disabled>@lang('No data available')</option>
                                                    @endif
                                                </select>
                                                <small class="form-text text-muted">@lang('Cannot edit. Permission denied.')</small>
                                            </div>
                                        @endhaspermission
                                    </div>
                                    @if ($user->ref_by)
                                        @push('script')
                                            <script>
                                                $(function() {
                                                    $('#referedPerson').select2({
                                                        data: [{
                                                            id: {{ $user->ref_by }},
                                                            text: "{{ $user->referral->username }}"
                                                        }],
                                                        ajax: {
                                                            url: '{{ route('admin.users.referedPerson.suggestions') }}',
                                                            type: 'get',
                                                            dataType: 'json',
                                                            delay: 250,
                                                            data: function(params) {
                                                                // console.log(params);
                                                                return {
                                                                    search: params.term
                                                                };
                                                            },
                                                            processResults: function(data) {
                                                                console.log(data);
                                                                return {
                                                                    results: data
                                                                };
                                                            },
                                                            cache: true
                                                        },
                                                        placeholder: 'Search for a user',
                                                        minimumInputLength: 2,
                                                        templateResult: formatResult,
                                                        templateSelection: formatSelection
                                                    });

                                                    function formatResult(data) {
                                                        if (data.loading) return data.text;
                                                        // var markup = "<div class='select-user'>" + data.text + "</div>";
                                                        var markup = data.text;

                                                        return markup;
                                                    }

                                                    function formatSelection(data) {
                                                        return data.text;
                                                    }
                                                });
                                            </script>
                                        @endpush
                                    @else
                                        @push('script')
                                            <script>
                                                $(function() {
                                                    $('#referedPerson').select2({
                                                        data: [],
                                                        ajax: {
                                                            url: '{{ route('admin.users.referedPerson.suggestions') }}',
                                                            type: 'get',
                                                            dataType: 'json',
                                                            delay: 250,
                                                            data: function(params) {
                                                                // console.log(params);
                                                                return {
                                                                    search: params.term
                                                                };
                                                            },
                                                            processResults: function(data) {
                                                                console.log(data);
                                                                return {
                                                                    results: data
                                                                };
                                                            },
                                                            cache: true
                                                        },
                                                        placeholder: 'Search for a user',
                                                        minimumInputLength: 2,
                                                        templateResult: formatResult,
                                                        templateSelection: formatSelection
                                                    });

                                                    function formatResult(data) {
                                                        if (data.loading) return data.text;
                                                        // var markup = "<div class='select-user'>" + data.text + "</div>";
                                                        var markup = data.text;

                                                        return markup;
                                                    }

                                                    function formatSelection(data) {
                                                        return data.text;
                                                    }
                                                });
                                            </script>
                                        @endpush
                                    @endif
                                </div>

                                <div class="row mt-4">
                                    <div class="col-md-12">
                                        <div class="form-group ">
                                            <label>@lang('Address')</label>
                                            <input class="form-control" type="text" name="address"
                                                value="{{ @$user->address->address }}">
                                        </div>
                                    </div>

                                    <div class="col-xl-3 col-md-6">
                                        <div class="form-group">
                                            <label>@lang('City')</label>
                                            <input class="form-control" type="text" name="city"
                                                value="{{ @$user->address->city }}">
                                        </div>
                                    </div>

                                    <div class="col-xl-3 col-md-6">
                                        <div class="form-group ">
                                            <label>@lang('State')</label>
                                            <input class="form-control" type="text" name="state"
                                                value="{{ @$user->address->state }}">
                                        </div>
                                    </div>

                                    <div class="col-xl-3 col-md-6">
                                        <div class="form-group ">
                                            <label>@lang('Zip/Postal')</label>
                                            <input class="form-control" type="text" name="zip"
                                                value="{{ @$user->address->zip }}">
                                        </div>
                                    </div>

                                    <input type="hidden" name="country_code">
                                    <input type="hidden" name="mobile_code">

                                    <div class="col-xl-3 col-md-6">
                                        <div class="form-group ">
                                            <label>@lang('Country')</label>
                                            <select name="country" class="form-control">
                                                @foreach ($countries as $key => $country)
                                                    <option data-mobile_code="{{ $country->dial_code }}"
                                                        value="{{ $key }}">{{ __($country->country) }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                </div>


                                <div class="row">
                                    @haspermission('manage_user:email_verification', 'admin')
                                    <div class="form-group  col-xl-3 col-md-6 col-12">
                                        <label>@lang('Email Verification')</label>
                                        <input type="checkbox" data-width="100%" data-onstyle="-success"
                                            data-offstyle="-danger" data-bs-toggle="toggle" data-on="@lang('Verified')"
                                            data-off="@lang('Unverified')" name="ev"
                                            @if ($user->ev) checked @endif>
                                    </div>
                                    @endhaspermission

                                    @haspermission('manage_user:mobile_verification', 'admin')
                                    <div class="form-group  col-xl-3 col-md-6 col-12">
                                        <label>@lang('Mobile Verification')</label>
                                        <input type="checkbox" data-width="100%" data-onstyle="-success"
                                            data-offstyle="-danger" data-bs-toggle="toggle" data-on="@lang('Verified')"
                                            data-off="@lang('Unverified')" name="sv"
                                            @if ($user->sv) checked @endif>
                                    </div>
                                    @endhaspermission
                                    {{-- <div class="form-group col-xl-3 col-md- col-12">
                                        <label>@lang('2FA Verification') </label>
                                        <input type="checkbox" data-width="100%" data-height="50"
                                            data-onstyle="-success" data-offstyle="-danger" data-bs-toggle="toggle"
                                            data-on="@lang('Enable')" data-off="@lang('Disable')" name="ts"
                                            @if ($user->ts) checked @endif>
                                    </div> --}}
                                    @haspermission('manage_user:kyc_verification', 'admin')
                                    <div class="form-group col-xl-3 col-md- col-12">
                                        <label>@lang('KYC') </label>
                                        <input type="checkbox" data-width="100%" data-height="50"
                                            data-onstyle="-success" data-offstyle="-danger" data-bs-toggle="toggle"
                                            data-on="@lang('Verified')" data-off="@lang('Unverified')" name="kv"
                                            @if ($user->kv == 1) checked @endif>
                                    </div>
                                    @endhaspermission
                                </div>


                                @haspermission('manage_user:user_edit', 'admin')
                                    <div class="row mt-4">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <button type="submit" class="btn btn--primary w-100 h-45">@lang('Submit')
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                @endhaspermission
                            </form>
                        </div>
                    </div>


                </div>
            </div>

        </div>
    </div>



    {{-- Add Sub Balance MODAL --}}
    <div id="addSubModal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><span class="type"></span> <span>@lang('Balance')</span></h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <i class="las la-times"></i>
                    </button>
                </div>
                <form action="{{ route('admin.users.add.sub.balance', $user->id) }}" method="POST">
                    @csrf
                    <input type="hidden" name="act">
                    <div class="modal-body">
                        <div class="form-group">
                            <label>@lang('Amount')</label>
                            <div class="input-group">
                                <input type="number" step="any" name="amount" class="form-control"
                                    placeholder="@lang('Please provide positive amount')" required>
                                <div class="input-group-text">{{ __($general->cur_text) }}</div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>@lang('Remark')</label>
                            <textarea class="form-control" placeholder="@lang('Remark')" name="remark" rows="4" required></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn--primary h-45 w-100">@lang('Submit')</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <div id="userStatusModal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        @if ($user->status == Status::USER_ACTIVE)
                            <span>@lang('Ban User')</span>
                        @else
                            <span>@lang('Unban User')</span>
                        @endif
                    </h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <i class="las la-times"></i>
                    </button>
                </div>
                <form action="{{ route('admin.users.status', $user->id) }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        @if ($user->status == Status::USER_ACTIVE)
                            <h6 class="mb-2">@lang('If you ban this user he/she won\'t able to access his/her dashboard.')</h6>
                            <div class="form-group">
                                <label>@lang('Reason')</label>
                                <textarea class="form-control" name="reason" rows="4" required></textarea>
                            </div>
                        @else
                            <p><span>@lang('Ban reason was'):</span></p>
                            <p>{{ $user->ban_reason }}</p>
                            <h4 class="text-center mt-3">@lang('Are you sure to unban this user?')</h4>
                        @endif
                    </div>
                    <div class="modal-footer">
                        @if ($user->status == Status::USER_ACTIVE)
                            <button type="submit" class="btn btn--primary h-45 w-100">@lang('Submit')</button>
                        @else
                            <button type="button" class="btn btn--dark"
                                data-bs-dismiss="modal">@lang('No')</button>
                            <button type="submit" class="btn btn--primary">@lang('Yes')</button>
                        @endif
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection


@push('script')
    <script>
        (function($) {
            "use strict"
            $('.bal-btn').click(function() {
                var act = $(this).data('act');
                $('#addSubModal').find('input[name=act]').val(act);
                if (act == 'add') {
                    $('.type').text('Add');
                } else {
                    $('.type').text('Subtract');
                }
            });
            let mobileElement = $('.mobile-code');
            $('select[name=country]').change(function() {
                mobileElement.text(`+${$('select[name=country] :selected').data('mobile_code')}`);
                $('input[name=mobile_code]').val($('select[name=country] :selected').data('mobile_code'));
                $('input[name=country_code]').val($('select[name=country] :selected').data('code'));
            });

            $('select[name=country]').val('{{ @$user->country_code }}');
            let dialCode = $('select[name=country] :selected').data('mobile_code');
            let mobileNumber = `{{ $user->mobile }}`;
            mobileNumber = mobileNumber.replace(dialCode, '');
            $('input[name=mobile]').val(mobileNumber);
            mobileElement.text(`+${dialCode}`);

        })(jQuery);
    </script>
@endpush
