@extends('layouts.admin_master')
@section('page-title') 
    Admin | {{ $page_title }}
@endsection
@section('page-content')

<div class="row">
@include('super_admin.partials.dealer_top_form')
<div class="col-md-12">
        <div class="hpanel">
            <div class="panel-heading">List of Organisations (Dealers)  <br>
            <a class="btn btn-info btn-sm pull-right" href="{{ url('admin/organisations/create') }}"><i class="fa fa-plus-circle"></i> New Dealer</a>
            <div class="clearfix"></div>
                @include('commons.notifications')                    
            </div>
            <div class="panel-body">
                   

                <div class="table-responsive">
                <table cellpadding="1" cellspacing="1" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Balance (MWK)</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Status</th>
                        <th>Date Created</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse ($organisations as $row)
                    <tr>
                        <td>{{ $row->name }}</td>
                        <td>{{ number_format($row->balance) }}</td>
                        <td>@if($row->email) {{ $row->email }} @else {{ 'N/A' }} @endif</td>
                        <td>@if($row->phone) {{ $row->phone }} @else {{ 'N/A' }} @endif</td>
                        <td class="@if($row->status == 'active') {{ 'bg-success' }} @else {{ 'bg-danger' }} @endif">{{ ucfirst($row->status) }}</td>
                        <td>{{ Carbon\Carbon::parse($row->created_at)->format('F d, Y') }}</td>
                        <td>
                            <input type="hidden" class='orgRowId' name="org_id" value="{{ $row->id }}">
                            <input type="hidden" class='orgRowName' name="org_name" value="{{ $row->name }}">
                            <button class="btn btn-success btn-sm topUpBtn" title="Click to Top Up Balance" type="button"><i class="fa fa-plus"></i> <span class="bold">Top Up</span></button>
                            <!-- <a class="btn btn-info btn-sm" title="View details" href="{{ url('admin/organisation/show', $row->id) }}" ><i class="fa fa-eye"></i></a> -->
                            <a class="btn btn-info btn-sm" title="Edit this entry" href="{{ url('admin/organisation/edit', $row->id) }}" ><i class="fa fa-pencil"></i> <span class="bold">Edit</span></a>                            
                        </td>
                    </tr>
                    @empty
                        <tr>                      
                            <td colspan="8">No Data</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
                </div>
                {{ $organisations->links() }}
            </div>
            <div class="panel-footer">
            Page : {{ $organisations->currentPage() }} of {{ $organisations->lastPage() }} |
            Total Records : <span id="totalRecords">{{ $organisations->total() }}</span>
            </div>
        </div>
    </div>
</div>
@endsection


@section('page-js')
<script>
$(document).ready(function() {
    $('.topUpBtn').click(function() {
        var org_id = $(this).siblings('.orgRowId').val();
        var orgName = $(this).siblings('.orgRowName').val();
        console.log(org_id);
        $('#selectedOrgId').val(org_id);
        $('.titleHead').text('Top Up Balance for ' + orgName);
        $('#orgTopUpModal').modal('show');
    });


    $('#dealerTopForm').submit(function(evt){
        evt.preventDefault();
        var topUpAmount = $('#topUpAmount').val();
        var discountValue = $('#discount').val();
        var finalTopUpAmount = parseFloat(topUpAmount) + parseFloat((topUpAmount * discountValue)/100);
        $.confirm({
            title: 'Confirm Action',
            content: 'Final Top Up Amount is ' + finalTopUpAmount + '. Are you sure you want to proceed?',
            buttons: {
                confirm: function () {
                    var formData = new FormData(this);
                    $.ajax({
                        url: base_url + '/admin/topups',
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        beforeSend: function() {
                            $('#topupError').addClass('hidden');
                            console.log('Sending data...');
                        },
                        success: function(data) {
                            console.log('Success:', data);
                            $('#topupError').addClass('hidden');
                            $('#topupSuccess').removeClass('hidden');
                            $('#topupSuccess').html('<p>' + data.message + '</p>');
                            $('#dealerTopForm')[0].reset();
                            $('#orgTopUpModal').modal('hide');
                            $('#topupSuccess').delay(5000).fadeOut();
                            $('#topupSuccess').html('');
                            setTimeout(function() {
                                location.reload();
                            }, 2000);
                        },
                        error: function(xhr, status, error) {
                            console.error('Error:', error);
                            if (xhr.status === 422) {
                                $('#topupError').removeClass('hidden');
                                //console.log(xhr.responseJSON.errors); // Display validation errors
                                $.each(xhr.responseJSON.errors, function(key, value) {
                                    $('#topupError').append('<p>' + value[0] + '</p>');
                                });
                            }
                        }
                    });
                },
                cancel: function () {
                    return false;
                }
            }
        });        
    });

});
</script>
@endsection