@extends('Template::layouts.master')
@section('content')
    <div class="dashboard-body">
        <div class="data-table">
            <div class="data-table__header">
                <h6 class="data-table__title">
                    {{ __($pageTitle) }}
                </h6>

                <form class="data-table__search">
                    <div class="input-group input--group input--group-submit">
                        <input type="search" name="search" class="form-control form--control" value="{{ request()->search }}"
                            placeholder="@lang('Search by transactions')">
                        <button class="input-group-text">
                            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"
                                fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                stroke-linejoin="round" class="lucide lucide-search-icon lucide-search">
                                <path d="m21 21-4.34-4.34" />
                                <circle cx="11" cy="11" r="8" />
                            </svg>
                        </button>
                    </div>
                </form>
            </div>
            <div class="data-table__body">
                @if (!$deposits->isEmpty())
                    <div class="table--responsive">
                        <table class="table table--custom table--responsive-md">
                            <thead>
                                <tr>
                                    <th>@lang('Gateway | TRX')</th>
                                    <th class="text-center">@lang('Initiated')</th>
                                    <th class="text-center">@lang('Amount')</th>
                                    <th class="text-center">@lang('Charge')</th>
                                    <th class="text-center">@lang('Status')</th>
                                    <th>@lang('Details')</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($deposits as $deposit)
                                    <tr>
                                        <td data-label="Gateway | Transaction">
                                            <div>
                                                <span class="fw-bold">
                                                    <span class="text--base">
                                                        @if ($deposit->method_code < 5000)
                                                            {{ __($deposit->gateway?->name) }}
                                                        @else
                                                            @lang('Google Pay')
                                                        @endif
                                                    </span>
                                                </span>
                                                <br>
                                                <small> {{ $deposit->trx }} </small>
                                            </div>
                                        </td>

                                        <td data-label="Initiated">
                                            {{ showDateTime($deposit->created_at) }}<br>{{ diffForHumans($deposit->created_at) }}
                                        </td>
                                        <td data-label="Amount">
                                            <div>
                                                {{ showAmount($deposit->amount) }} + <span class="text--danger"
                                                    data-bs-toggle="tooltip"
                                                    title="@lang('Processing Charge')">{{ showAmount($deposit->charge) }}
                                                </span>
                                                <br>
                                                <strong data-bs-toggle="tooltip" title="@lang('Amount with charge')">
                                                    {{ showAmount($deposit->amount + $deposit->charge) }}
                                                </strong>
                                            </div>
                                        </td>
                                        <td data-label="Conversion">
                                            <div>
                                                {{ showAmount(1) }} =
                                                {{ showAmount($deposit->rate, currencyFormat: false) }}
                                                {{ __($deposit->method_currency) }}
                                                <br>
                                                <strong>{{ showAmount($deposit->final_amount, currencyFormat: false) }}
                                                    {{ __($deposit->method_currency) }}</strong>
                                            </div>
                                        </td>
                                        <td data-label="Status">
                                            @php echo $deposit->statusBadge @endphp
                                        </td>

                                        @php
                                            $details = [];
                                            if ($deposit->method_code >= 1000 && $deposit->method_code <= 5000) {
                                                foreach ($deposit->detail ?? [] as $key => $info) {
                                                    $details[] = $info;
                                                    if ($info->type == 'file') {
                                                        $details[$key]->value = route(
                                                            'user.download.attachment',
                                                            encrypt(getFilePath('verify') . '/' . $info->value),
                                                        );
                                                    }
                                                }
                                            }
                                        @endphp

                                        <td>
                                            @if ($deposit->method_code >= 1000 && $deposit->method_code <= 5000)
                                                <a href="javascript:void(0)"
                                                    class="btn btn--icon  btn--sm btn--base-two detailBtn"
                                                    data-info="{{ json_encode($details) }}"
                                                    @if ($deposit->status == Status::PAYMENT_REJECT) data-admin_feedback="{{ $deposit->admin_feedback }}" @endif>
                                                    <i class="las la-desktop"></i>
                                                </a>
                                            @else
                                                <button type="button" class="btn btn--icon btn--success btn--sm"
                                                    data-bs-toggle="tooltip" aria-label="Automatically processed"
                                                    data-bs-original-title="Automatically processed">
                                                    <i class="las la-check"></i>
                                                </button>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    @include('Template::partials.data_not_found', [
                        'data' => 'No Deposit Found',
                    ])
                @endif
            </div>
        </div>
        @if ($deposits->hasPages())
            <div class="pagination-wrapper">
                {{ paginateLinks($deposits) }}
            </div>
        @endif
    </div>

    {{-- APPROVE MODAL --}}
    <div id="detailModal" class="modal custom--modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">@lang('Details')</h5>
                    <button type="button" class="btn btn--sm btn--close btn-soft--secondary btn--icon"
                        data-bs-dismiss="modal" aria-label="Close">
                    </button>
                </div>
                <div class="modal-body">
                    <ul class="list-group list-group--custom userData mb-2">
                    </ul>
                    <div class="feedback"></div>
                </div>
            </div>
        </div>
    </div>
@endsection


@push('script')
    <script>
        (function($) {
            "use strict";
            $('.detailBtn').on('click', function() {
                var modal = $('#detailModal');

                var userData = $(this).data('info');
                var html = '';
                if (userData) {
                    userData.forEach(element => {
                        if (element.type != 'file') {
                            html += `
                            <li class="list-group-item">
                                <span>${element.name}</span>
                                <strong>${element.value}</strong>
                            </li>`;
                        } else {
                            html += `
                            <li class="list-group-item">
                                <span>${element.name}</span>
                                <a href="${element.value}"><i class="fa-regular fa-file"></i> @lang('Attachment')</a>
                            </li>`;
                        }
                    });
                }

                modal.find('.userData').html(html);

                if ($(this).data('admin_feedback') != undefined) {
                    var adminFeedback = `
                        <div class="my-3">
                            <strong>@lang('Admin Feedback')</strong>
                            <p>${$(this).data('admin_feedback')}</p>
                        </div>
                    `;
                } else {
                    var adminFeedback = '';
                }

                modal.find('.feedback').html(adminFeedback);


                modal.modal('show');
            });

            var tooltipTriggerList = [].slice.call(document.querySelectorAll('[title], [data-title], [data-bs-title]'))
            tooltipTriggerList.map(function(tooltipTriggerEl) {
                return new bootstrap.Tooltip(tooltipTriggerEl)
            });

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