@extends('merch_layouts.merch_master')
@section('page-title') 
    Merchant | Refunds
@endsection
@section('page-content')
<div class="row">
<div class="col-md-5">
    <div class="hpanel hblue">
        <div class="panel-heading hbuilt">Dealer Refund 44</div>
        <div class="panel-body">
            <!--  -->
            <form action="{{ url('merchant/pushrefunds') }}" method="POST" class="form-horizontal" id="dealerRefundForm">
                {{csrf_field()}}
                <input type="hidden" id="merchantId" name="merchant_id" value="{!! $merchant->id !!}">
                <div class="col-md-12" style="padding-bottom: 10px;">
                    @include('commons.notifications')
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label" for="dealerID">Select Dealer</label>
                    <div class="col-md-7">
                        <select required class="form-control" name="dealer_id" id="dealerID" placeholder="Merchant" >
                            <option>-- Select Account --</option>
                            <?php foreach ($dealers_arr as $row): ?>
                                <option value="{{ $row->id }}">{{ $row->name }} ({{ $row->phone }}) </option>      
                            <?php endforeach ?>
                        </select>
                    </div>
                </div>
                <div class="hr-line-dashed"></div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Amount</label>
                    <div class="col-md-7">
                        <input type="number"  name="amount" class="form-control" id="amount" placeholder="Refund Amount" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Transaction ID</label>
                    <div class="col-md-7">
                        <input type="number"  name="transaction_id" class="form-control" id="transactionId" placeholder="Reference Transaction ID" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">Reason</label>
                    <div class="col-md-7">
                        <input type="text"  name="reason" class="form-control" id="reason" placeholder="Brief text as Reason" required>
                    </div>
                </div>
                <div class="hr-line-dashed"></div>
                <div class="form-group">
                    <div class="col-sm-12 ">
                        <button class="btn btn-info btn-block btn-lg" id="refundFormBtn" type="submit"><i class="fa fa-money"></i> Refund Dealer</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="col-md-7">
    <div class="hpanel hblue">
        <div class="panel-heading hbuilt">Recent Dealer Refunds</div>
        <div class="panel-body">
            <div class="table-responsive">
            <table cellpadding="1" cellspacing="1" class="table table-bordered table-striped">
                <thead>
                <tr>
                    <th>Dealer Name</th>
                    <th>Dealer Phone</th>
                    <th>Refund Amount</th>
                    <th>Transaction ID</th>
                    <th>Date</th>
                </tr>
                </thead>
                <tbody>
                @forelse ($recent_refunds as $row)
                <tr>
                    <td>{{ $row->orgInfo->name }}</td>
                    <td>{{ $row->orgInfo->phone }}</td>
                    <td>{{ $row->amount }}</td>
                    <td>{{ $row->transaction_id ?? 'N/A' }}</td>
                    <td>{{ $row->created_at }}</td>
                </tr>
                @empty
                    <tr>                      
                        <td colspan="4">No Data</td>
                    </tr>
                @endforelse
                </tbody>
            </table>
            </div>
            {{ $recent_refunds->links() }}
        </div>
        <div class="panel-footer">
        Page : {{ $recent_refunds->currentPage() }} of {{ $recent_refunds->lastPage() }} |
        Total Records : <span id="totalRecords">{{ $recent_refunds->total() }}</span>
        </div>
    </div>
</div>
</div>
            
            



@endsection
@section('page-js')
<script>

    $(document).ready(function(){
        console.log('foo');
        $('#dealerID').select2({
              // tags : true
        });
        $('#dealerRefundFormZZZ').on('submit', function(evt) {
        evt.preventDefault();
        $('#successArea').addClass('hidden');
        $('#errorArea').addClass('hidden');
        var merchID = $('#merchantId').val();
        var dealerID = $('#dealerID').val();
        $.ajax({
            type: 'POST',
            url: base_url + '/merchant/lukers',
            // data: formdata, //a $(this).serialize(),
            data: { merchant_id: merchID, dealer_id: dealerID, amount: $('#amount').val(), reason : $('#reason').val()  },
            headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content") },
            success: function(response) {
                console.log(response);
                if (response.status == 'failed') {
                    $('#errorArea').removeClass('hidden');
                    $('#errorArea').html(response.message);
                }
                else{
                    $('#successArea').removeClass('hidden');
                    $('#successArea').html(response.message);
                    setTimeout(function() {
                        location.reload();
                    }, 3000);
                    
                }
            },
            error: function(xhr, status, error) {
                $('#errorArea').removeClass('hidden');
                // $('#errorArea').html(response.message);
                xhr.responseJSON.errors.forEach(function(value, index) {
                    console.log(index + ': ' + value);
                    $('#errorArea').html(index + ': ' + value);
                });
            }
        });
    });
    });
</script>

@endsection
