{{--Get the Page Format--}}
@extends('layouts.app')

{{--Set Page Title--}}
@section('title', 'Company Payments Reports')

{{--Include any Internal JS Code--}}
@section('internal-js')
    <script>
        $(document).ready(function () {
            $('#insurers1').DataTable( {
                dom: 'lBfrtip',
                buttons: [
                    { extend: 'excel', className: 'download-buttons'},
                ]
            } );
            $('#insurer-list').select2();
            $('#status-list').select2();
            $('#currency-list').select2();
            $('#type-list').select2();
            $('#receipt-type-list').select2();

            $('#receipt-type-list').on('change', function (){
                let type = $(this).val();

                if(type == 'premium'){
                    $('#clients-div').show();
                    $('#insurers-div').hide();
                }else if(type == 'commission'){
                    $('#clients-div').hide();
                    $('#insurers-div').show();
                }else{
                    $('#clients-div').hide();
                    $('#insurers-div').hide();
                }

            });

            $('#client-name').on('input paste', function (){
                let name = $(this).val();
                if(name != ""){
                    $.ajax({
                        method: 'GET',
                        url: '/search-client-by-name/'+name,
                        data: name,
                        success: function (res){
                            if(res != ""){
                                $('#client-search-results').show();
                                $('#client-search-results').html(res);
                            }else{
                                $('#client-search-results').hide();
                            }
                        }
                    });
                }

            });


            $('#date-from').on('change', function (){
                $('#date-to').attr('required', true);
            });
            $('#date-to').on('change', function (){
                $('#date-from').attr('required', true);
            });
        });
    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
    <style>
        #insurers1 tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        #insurers1 th, td {
            text-align: left;
            padding: 5px;
        }
        #insurers1 tr:hover {background-color: #d8d9e1;}
    </style>

@endsection

{{--Control Page Header/Links--}}
@section('headers')
    @include('headers.finance-header')
@endsection

{{--Set Page Heading--}}
@section('heading', 'Company Payments Report')
@section('done-by', '')

{{--Page Content Goes here--}}
@section('content')
{{--    <button onclick="addNewPayment()" type="button" class="navButtons" style="background-color: #4682a8; margin-bottom: 0; margin-left: 0"> <i class="fa fa-plus-circle" ></i> &nbsp;Add New Payment</button>--}}
    <div class="inputDivider" style="margin-top: 10px; text-align: left; background-color: lightcyan">
        {!! Form::open(['action'=>['Payments\PaymentController@showPayments'], 'method'=>'POST', 'id'=>'filter-payments', 'class'=>'fields-small']) !!}
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                &nbsp;
            </div>
            <div class="guestDetails_input_right">
                <button type="submit" name="search" style="cursor: pointer"><i class="fa fa-search" aria-hidden="true"></i></button>
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Date Added To:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                {!! Form::date('to', $search['to'] ?? null, ['id'=>'date-to','style'=>'cursor:pointer', 'class'=>'input-text-height']) !!}
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Date Added From:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                {!! Form::date('from',  $search['from'] ?? null, ['id'=>'date-from','style'=>'cursor:pointer', 'class'=>'input-text-height']) !!}
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Payment Voucher No:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                {!! Form::text('payment-no', '', ['size'=>10,'class'=>'input-text-height', 'id'=>'receipt-no','autocomplete'=>'off']) !!}
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Currency:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                <select name="currency" id="currency-list" style="width: 100px">
                    <option value="all">ALL</option>
                    @foreach($currencies as $row)
                        <option value="{{$row->id}}">{!! strtoupper($row->abbr) !!}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Payment Status:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                <select name="payment-status" id="type-list" style="width: 120px">
                    <option value="all" >ALL</option>
                    <option value="reversed">REVERSED</option>
                    <option value="allocated">FULLY ALLOCATED</option>
                    <option value="pending">PENDING ALLOCATION</option>
                </select>
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Payment Type:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                <select name="payment-type" id="receipt-type-list" style="width: 120px">
                    <option value="all" >ALL</option>
                    <option value="bill-payment">BILL PAYMENT</option>
                    <option value="bank-transfer">BANK TRANSFER</option>
                    <option value="payroll">PAYROLL</option>
                </select>
            </div>
        </div>
        <div class="guestDetails_inputDividerNone" style="height: 0"></div>
        {!! Form::close() !!}
    </div>

    @if($payments != null)
        <div class="guestDetails_inputDivider"></div>
        <!--View and Setup your Insurers-->
        <div>
            <!--Export Links/Buttons-->
            <span onclick="this.parentElement.style.display='none'" class="topright" style="margin-left: 20px">&times</span>
            <table id="insurers1" style="width: 100%; border-collapse: collapse; ">
                <thead>
                <tr style="background-color: #a1b5d3">
                    <th>No</th>
                    <th>PV No.</th>
                    <th>PV Type</th>
                    <th>Paid From</th>
                    <th>GL Account</th>
                    <th>Paid To</th>
                    <th>Mode</th>
                    <th>REF</th>
                    <th>Narration</th>
                    <th>Amount</th>
                    <th>Currency</th>
                    <th>Exchange rate</th>
                    <th>Status</th>
                    <th>Date Paid</th>
                    <th>Date of Payment</th>
                    <th>User</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @if($payments != null || session('payments'))
                    @foreach($payments as $pv)
                        <tr>
                            <td class="ar"></td>
                            <td><a href="{{url('payment/'.$pv->stamp.'/finance?type=summary')}}" class="inner"><b class="error">PV{{$pv->pv_id}}</b></a></td>
                            <td>{{$pv->type}}</td>
                            <td>
                                <b>
                                    @if($pv->account != null)
                                        {!! strtoupper($pv->account->acc_name) !!}
                                    @endif
                                </b>
                            </td>
                            <td>
                                <b>
                                    @if($pv->glAccount != null)
                                        {!! strtoupper($pv->glAccount->acc_name) !!}
                                    @endif
                                </b>
                            </td>
                            <td>
                                @if($pv->type == 'refund-premium')
                                    @if($pv->client != null)
                                        {!! strtoupper($pv->client->nms) !!}
                                    @endif
                                @elseif($pv->type == 'remit-premium')
                                    @if($pv->insurer != null)
                                        {!! strtoupper($pv->insurer->nm) !!}
                                    @endif
                                @endif
                            </td>

                            <td>
                                @if($pv->modeOfPayment != null)
                                    {!! $pv->modeOfPayment->name !!}
                                    @if($pv->subModeOfPayment != null)
                                        - {!! $pv->subModeOfPayment->name !!}
                                    @endif
                                @endif
                            </td>
                            <td>{!! $pv->ref !!}</td>
                            <td>{!! $pv->nar !!}</td>
                            <td><b>{!! number_format($pv->amount,2) !!}</b></td>
                            <td>
                                @if($pv->currencyDetails != null)
                                    <b class="error">{!! $pv->currencyDetails->abbr !!}</b>
                                @endif
                            </td>
                            <td>{!! number_format($pv->conversion_rate,2) !!}</td>
                            <td>{!! $pv->pv_status !!}</td>
                            <td>{{date('d/m/Y', strtotime($pv->pv_date))}}</td>
                            <td>{{date('d/m/Y', strtotime($pv->created_at))}}</td>
                            <td>
                                @if($pv->user != null)
                                    {!! $pv->user->firstName[0].". ".$pv->user->lastName !!}
                                @endif
                            </td>
                            <td><a href="{{url('payment-pdf/'.$pv->stamp.'/AddWatermark')}}" class="inner" target="_blank"><i class="fa fa-print error" style="font-size: 16px"></i></a></td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
                <tfoot>
                <tr style="background-color: #a1b5d3">
                    <th>No</th>
                    <th>PV No.</th>
                    <th>PV Type</th>
                    <th>Paid From</th>
                    <th>GL Account</th>
                    <th>Paid To</th>
                    <th>Mode</th>
                    <th>REF</th>
                    <th>Narration</th>
                    <th>Amount</th>
                    <th>Currency</th>
                    <th>Exchange rate</th>
                    <th>Status</th>
                    <th>Date Paid</th>
                    <th>Date of Payment</th>
                    <th>User</th>
                    <th></th>
                </tr>
                </tfoot>
            </table>

        </div>
    @endif

@endsection

{{--System Hints Page--}}
@section('modal_heading')
    <h1>Company Payments</h1>
@endsection
@section('modal_title')
    <h3><u>Dashboard Functionalities</u></h3>
    <b>
        <ol>
            <li>View all Types of Payments for the Company</li>
            <li>Filter through available Payments to Generate Reports</li>
            <li>Export Payments Report</li>

        </ol>
    </b>
@endsection



