@extends('layouts.main')
@section('content')


@php
//fetch users with pagination
if (!empty($_GET['search'])) {
   $neenoe = request()->query('search');
   //where('title','like','%'.$search.'%')
   $cashouts = DB::table('cashouts')->where('status', 'pending')->where('phone','like','%'.$neenoe.'%')->orWhere('amount','like','%'.$neenoe.'%')->orWhere('phone','like','%'.$neenoe.'%')->paginate(10);
}else {
    $cashouts = DB::table('cashouts')->where('status', 'pending')->paginate(10);
}


@endphp

    <div class="col-lg-7">
        <div class="page-header-title">
            <div class="d-inline">
                <div class="card-header">
                    <h5>Pending withdrawals</h5>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="page-header-breadcrumb">
            <div class="col-sm-12">
                <form
                    method="GET">
                    @csrf
                    <div class="input-group input-group-success">
                        <input type="text" name="search" class="form-control"
                            placeholder="Enter Amount/Phone">
                        <button class="input-group-addon">
                            <i data-feather="search" class="menuicons icon-search f-15"></i>
                        </button>
                    </div>
                </form>

            </div>
        </div>
    </div>
    
    


<div class="col-xl-12 col-md-12">
    <!-- DOM/Jquery table start -->
   @if(Session::has('error'))
      <p class="alert alert-danger">{{ Session::get('error') }}</p>
@endif
   @if(Session::has('success'))
      <p class="alert alert-info">{{ Session::get('success') }}</p>
@endif
    <div class="card">
        <div class="card-header">
            <h5>Pending withdrawal History </h5>
        </div>
        <div class="card-block">
            <div class="table-responsive dt-responsive">
                <table id="dom-jqry"
                    class="table table-striped table-bordered nowrap">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Code</th>
                            <th>Username</th>
                            <th>Phone</th>
                            <th>Charge</th>
                            <th>Amount</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @if ($cashouts->count() > 0)

                        @foreach ($cashouts as $c)
                            

                        <tr>
                            <td>{{ \Carbon\Carbon::parse($c->created_at)->diffForHumans() }}
                            </td>
                            <td>{{ $c->code ?? 'empty' }}</td>
                            <td style="text-transform: lowercase">
                                @php
                                    
                                    $thisguy = DB::table('users')->where('phone', $c->phone)->first();
                                    $iduser = $thisguy->id ?? 1;
                                @endphp
                                <a href="{{ route('viewuser', $iduser) }}">
                                    <u>
                                        {{  $thisguy->username ?? "nouser" }}
                                    </u>
                                </a>                                              
                            </td>
                            <td>
                                {{ $c->phone }}</td>
                            <td>KSH {{ $c->Charge ?? '0' }}</td>
                            <td>KSH {{ $c->amount }}</td>
                            <td>
                                <a id="id{{$c->id}}" 
                                    onclick="confirmProceed('{{$c->username}}', 'id{{$c->id}}')"
                                    data-id="{{$c->username}}"
                                    href="./pay?user={{$c->username}}&&trx={{$c->id}}&&amt={{$c->amount}}"
                                    class="btn btn-info  md-trigger btn-mini feather icon-check-square f-12">
                                    Mpesa Pay
                                </a>

                                <button class="approveBtn btn btn-success  md-trigger btn-mini feather icon-check-square f-12" 
                                data-id="{{ $c->id }}" data-amount="{{ $c->amount }}" data-username="{{  $thisguy->username ?? 'nouser' }}">
                                Approve
                                </button>
                                <button class="declineBtn btn btn-danger md-trigger btn-mini feather icon-x-square f-12" 
                                data-id="{{ $c->id }}" data-amount="{{ $c->amount }}" data-username="{{  $thisguy->username ?? 'nouser' }}">
                                Decline
                                </button>
                            </td>
                        </tr>


                        @endforeach
                        @else
                        <tr>
                            <td class="text-center" colspan="100%">
                                No search results found
                            </td>
                        </tr>

                        @endif
                    </tbody>
                </table>
            </div>
            <div class="card-footer py-4">
                <nav aria-label="...">

                </nav>
            </div>
        </div>
    </div>
</div>
<script>
function confirmProceed(id, elementId) {
    // Prompt the user with a confirmation dialog including the ID
    var confirmation = confirm("Are you sure you want to proceed with ID: " + id + "?");
    
    // If the user confirms, continue with the action
    if (confirmation) {
        // Construct the href based on the provided ID
        var href = document.getElementById(elementId).getAttribute("href");
        // Proceed to the destination specified in the href
        window.location.href = href;
    } else {
        event.preventDefault();
        // If the user cancels, do nothing or provide feedback
        console.log("Action cancelled by the user.");
    }
}
</script>
@endsection