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

{{--Set Page Title--}}
@section('title', 'Payment')

{{--Include any Internal JS Code--}}
@section('internal-js')
    <script>
        $(document).ready(function () {
            $('#docsTable').DataTable();
            $('#example1').dataTable();
            $('#example2').dataTable();
            $('#example3').dataTable();
            $('#example7').dataTable();
            $('#example').DataTable( {
                dom: 'lBfrtip',
                buttons: [
                    { extend: 'excel', className: 'download-buttons' },
                    { extend: 'print', className: 'download-buttons' },
                ]
            } );

            $('#search-vehicle').select2();

            $('#submit-search').on('click', function (){
                var payment = $('#search-vehicle').val();
                if(payment == ""){
                    alert('Please Select a Payment to Search');
                }else{
                    @if($module == 'finance')
                        window.location = '/payment/'+payment+'/finance?type=summary';
                    @else
                        window.location = '/payment/'+payment+'?type=summary';
                    @endif
                }
            });

            /**MAXIMIZE AND MINIMIZE THE HEADER SECTION**/
            $('#minimize').on('click', function (){
                $('#main-div-section').slideUp();
                $('#maximize').show();
                $('#minimize').hide();
            });
            $('#maximize').on('click', function (){
                $('#main-div-section').slideDown();
                $('#maximize').hide();
                $('#minimize').show();
            });

            $('#summary-url').on('click', function (){
                window.history.pushState({}, null, '?type=summary');
            });
            $('#log-url').on('click', function (){
                window.history.pushState({}, null, '?type=log');
            });
            $('#trash-url').on('click', function (){
                window.history.pushState({}, null, '?type=trash');
            });
        });

        {{--POLICY PRINT OPTIONS--}}
        function printOptions(id){
            var route_link = '/payment-print-options/'+id;

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="{{asset('images/loads/loader.gif')}}" border=0></div>')
            .load(encodeURI(route_link))
            .dialog({
                modal: true,
                width: 500,
                title: "Payment Preview & Printing Options",
                buttons: [
                    {
                        text: "Continue to Preview & Print",
                        icon: "ui-icon-check",
                        click: function(e) {
                            e.preventDefault();
                            var datas = $('#payment-print-options').serializeArray();

                            $.ajax({
                                method: "POST",
                                url: route_link,
                                data: datas,
                                success: function (res){
                                    if(res == 'empty type'){
                                        alert('Please select at least one option of the Documents to Preview');
                                    }else{
                                        window.open(res, '_blank');
                                    }
                                }
                            });
                            return false;
                        }
                    }
                ],
                open: function() {
                    $(this).parent().css({'top': 50});
                }
            });
        }

        {{--REVERSE A DIRECT PAYMENT VOUCHER--}}
        function reversePayment(id){
            var route_link = '/reverse-payment/'+id;

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="{{asset('images/loads/loader.gif')}}" border=0></div>')
                .load(encodeURI(route_link))
                .dialog({
                modal: true,
                width: 520,
                title: "Reverse Payment",
                buttons: [
                    {
                        text: "Continue to Reverse Payment",
                        icon: "ui-icon-check",
                        click: function(e) {
                            if(confirm('Are you sure to Reverse this Payment?')){
                                e.preventDefault();
                                var datas = $('#reverse-payment').serializeArray();

                                $.ajax({
                                    method: "POST",
                                    url: route_link,
                                    data: datas,
                                    success: function (res){
                                        if(res == 'empty reason'){
                                            alert('Please provide a reason for Reversing this Direct PV');
                                        }else if(res == 'error'){
                                            alert('System is unable to perform the Requested action. Please Try Again!');
                                            window.location = '/receipt/'+id+'/finance?type=summary&reverseRCT=false';
                                        }else if(res == 'success'){
                                            window.location = '/receipt/'+id+'/finance?type=summary&reverseRCT=true';
                                        }else{
                                            alert('System Encountered an Error. Please Contact System Admin for Help');
                                            location.reload();
                                        }
                                    }
                                });
                                return false;
                            }
                            return  false;
                        }
                    }
                ],
                open: function() {
                    $(this).parent().css({'top': 20});
                }
            });
        }

        {{--FUNCTION TO EDIT THE PAYMENT REF NUMBER--}}
        function editPaymentRefNumber(id){
            var route_link = '/edit-payment-ref-number/'+id;

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="{{asset('images/loads/loader.gif')}}" border=0></div>')
            .load(encodeURI(route_link))
            .dialog({
                modal: true,
                width: 500,
                title: "Edit Payment Reference Number",
                buttons: [
                    {
                        text: "Update Reference Number",
                        icon: "ui-icon-check",
                        click: function(e) {
                            if(confirm('Are you sure to Save Entered Changes?')){
                                e.preventDefault();
                                var datas = $('#edit-reference-number').serializeArray();

                                $.ajax({
                                    method: "POST",
                                    url: route_link,
                                    data: datas,
                                    success: function (res){
                                        if(res == 'empty ref'){
                                            alert('Please provide the Payment Reference Number');
                                        }else if(res == 'error'){
                                            alert('System is unable to perform the Requested action. Please Try Again!');
                                           location.reload();
                                        }else if(res == 'success'){
                                            location.reload();
                                        }else{
                                            alert('System Encountered an Error. Please Contact System Admin for Help');
                                            location.reload();
                                        }
                                    }
                                });
                                return false;
                            }
                            return false;
                        }
                    }
                ],
                open: function() {
                    $(this).parent().css({'top': 50});
                }
            });
        }


        {{--Next Policy Quick Navigation--}}
        function nextPlcy(id){
            @if($module == 'finance')
                window.location = '/payment/'+id+'/finance?type=summary';
            @else
                window.location = '/payment/'+id+'?type=summary';
            @endif
        }

        {{--Previuos Policy Quick Navigation--}}
        function prevPlcy(id){
            @if($module == 'finance')
                window.location = '/payment/'+id+'/finance?type=summary';
            @else
                window.location = '/payment/'+id+'?type=summary';
            @endif
        }
    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
    <style>
        #example th, td {
            text-align: left;
            padding: 5px;
        }


        #example1 tr:hover {background-color: #a3aef6;}

        #example2 th, td {
            text-align: left;
            padding: 5px;
        }

        #example2 tr:hover {background-color: #a3aef6;}
        #example3 th, td {
            text-align: left;
            padding: 5px;
        }

        #example3 tr:hover {background-color: #a3aef6;}
        #example7 th, td {
            text-align: left;
            padding: 5px;
        }
        #example7 tr:hover {background-color: #a3aef6;}

        #docsTable th, td {
            text-align: left;
            padding: 5px;
        }
        #docsTable tr:hover {background-color: #a3aef6;}
    </style>
@endsection

{{--Control Page Header/Links--}}
@section('headers')
    @if($module == 'finance')
        @include('headers.finance-header')
    @else
        @include('headers.dash-header')
    @endif
@endsection

{{--Set Page Heading--}}

@section('heading')

    {{--ucwords(strtolower($pv->receiptType($pv->type)))--}} Payment No.:
    @isset($pv)
        <span class="error">PV{{$pv->pv_id}}</span>
    @endisset
    @if($data['prev'] != null)
        <span style="margin-left: 3%; cursor: pointer" onclick="prevPlcy('{{$data['prev']}}')" class="quick-buttons"> <i class="fa fa-chevron-left" aria-hidden="true" style="font-size: 9pt" title="Previous Payment"> Prev</i> </span>&nbsp;&nbsp; |
    @else
        <span style="margin-left: 3%; cursor: pointer; color: grey" disabled="true" class="quick-buttons"> <i class="fa fa-chevron-left" aria-hidden="true" style="font-size: 9pt" title="No Previous Payment"> No Prev</i> </span>&nbsp;&nbsp; |
    @endif

    @if($data['next'] != null)
        <span style="margin-left: 1%; cursor: pointer" onclick="nextPlcy('{{$data['next']}}')" class="quick-buttons" title="Next Payment"> <i class="fa fa">Next</i> <i class="fa fa-chevron-right" aria-hidden="true" style="font-size: 9pt"></i></span>
    @else
        <span style="margin-left: 1%; cursor: pointer; color: grey" disabled="true" class="quick-buttons"> <i class="fa fa" title="No Next Payment">No Next</i> <i class="fa fa-chevron-right" aria-hidden="true" style="font-size: 9pt"></i></span>
    @endif
@endsection

@section('searches', '')
{{--Page Content Goes here--}}
@section('content')
    <div style="float: left">
        <button onclick="printOptions('{{$pv->stamp}}')" type="button" class="navButtons" style="margin-left: 0px;background-color: #f1d414; color: black"><i class="fa fa-print"></i> &nbsp;PRINT OPTIONS</button>
    </div>
    <div style="text-align: right">
        {{-- @if($pv->premiumReceiptBalance($pv->stamp) > 0 && $pv->rct_status != 'reversed')
             <button type="button" onclick="AllocateReceipt('{{$pv->pv_id}}')" class="navButtons" style="background-color: #a1c1da;color: black"><i class="fa fa-file-text-o"></i> &nbsp;ALLOCATE RECEIPT</button>
         @endif--}}

        <button type="button" onclick="uploadFiles('{{$pv->pv_id}}')" class="navButtons" style="background-color: #bdbbb2;color: black"><i class="fa fa-upload"></i> &nbsp;UPLOAD FILES</button>

        @if($pv->pv_status != 'reversed')
            <button onclick="reverseRCT('{{$pv->stamp}}')" type="button" class="navButtons" style="background-color: #f10e21"><i class="fa fa-undo"></i> &nbsp;REVERSE PAYMENT</button>
        @else
            <button onclick="restorePV('{{$pv->stamp}}')" type="button" class="navButtons" style="background-color: #57e505"><i class="fa fa-undo"></i> &nbsp;RESTORE PAYMENT</button>
        @endif
        <div class="guestDetails_inputDivider"></div>
        {{--NOTICE SECTION--}}
        @if($pv->pv_status == 'reversed')
            <div class="notice">Reversed!</div><br>
        @endif
    </div>
    <div style="background-color: rgba(234,236,238,0.72); border-radius: 5px; padding: 5px 6px">
        <div style="font-size: 12px">
            Paid To: <span class="error">
                    @if($pv->type == 'remit-premium')
                    @if($pv->insurer != null)
                        {{strtoupper($pv->insurer->lgnm)}}
                    @endif
                @elseif($pv->type == 'commission')
                    @if($pv->insurer != null)
                        {{strtoupper($pv->insurer->lgnm)}}
                    @endif
                @endif
                </span>
        </div>
        <div style="float: right;">
            <i id="minimize" class="fa fa-chevron-up" aria-hidden="true" title="Minimize" style="cursor: pointer"></i>
            <i id="maximize" class="fa fa-chevron-down" aria-hidden="true" title="Maximize" style="cursor: pointer; display: none"></i>
        </div><br>
        <div id="main-div-section">
            <div class="wrapperer" id="client-table-wrapper">
                <div class="column">
                    <table  class="table" style="width: 100%; border-collapse:collapse; margin: 5px">
                        <tr>
                            <th>Description:</th>
                            <td><b class="error">PAYMENT VOUCHER</b></td>
                        </tr>
                        <tr>
                            <th>Paid By:</th>
                            <td>
                                @if($pv->user != null)
                                   <b>{!! strtoupper($pv->user->firstName." ".$pv->user->lastName) !!}</b>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <th>Date of Payment:</th>
                            <td><b>{{date('d-m-Y', strtotime($pv->pv_date))}}</b></td>
                        </tr>
                        <tr>
                            <th>Date Registered:</th>
                            <td><b>{{date('d-m-Y', strtotime($pv->created_at))}}</b></td>
                        </tr>
                    </table>
                </div>
                <div class="column">
                    <table class="table" style="width: 100%; border-collapse:collapse; margin: 5px">
                        <tr>
                            <th>Paid From:</th>
                            <td>
                                @if($pv->account != null)
                                    <b class="error">{{strtoupper($pv->account->acc_name)}}</b>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <th>Ledger Account:</th>
                            <td>
                                @if($pv->glAccount != null)
                                    <b class="error">{{strtoupper($pv->glAccount->acc_name)}}</b>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <th>Mode of Payment:</th>
                            <td>

                                @if($pv->modeOfPayment != null)
                                    <b class="error" > {{$pv->modeOfPayment->name}}</b>
                                    @if($pv->subModeOfPayment != null)
                                        <b> - {{ $pv->subModeOfPayment->name}}</b>
                                    @endif
                                @endif

                            </td>
                        </tr>
                        <tr>
                            <th>Reference No.:</th>
                            <td><b class="error">{{$pv->ref}}</b></td>
                            <td style=" text-align: right">
                                @if(auth()->user()->admin == 1)
                                    <a href="javascript: void(0);" onclick="editPaymentRefNumber('{{$pv->id}}')" class="inner"><b class="error">EDIT <i class="fa fa-pencil fa-md"></i></b></a>
                                @endif
                            </td>
                        </tr>
                    </table>

                </div>
                <div class="column">
                    <table class="table" style="width: 100%; border-collapse:collapse; margin: 5px">
                        <tr>
                            <th>Currency Name:</th>
                            <td>
                                @if($pv->currencyDetails != null)
                                    <b class="error">{{$pv->currencyDetails->name}}({{$pv->currencyDetails->abbr}})</b>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <th>Exchange Rate:</th>
                            <td><b class="error">{{number_format($pv->conversion_rate,2)}}</b></td>
                        </tr>
                    </table>
                    <div class="guestDetails_inputDivider"></div>
                    <table style="width: 100%; border-collapse: collapse;">
                        <tr>
                            <td>
                                <button type="button" class="navButtons" style="background-color: #d9ffff;color: black; margin-bottom: 0;font-weight: bold; min-width: 200px ;font-size: 14px">PV Amount
                                    @if($pv->currencyDetails != null)
                                        ({!! $pv->currencyDetails->abbr !!})
                                    @endif
                                    :
                                    <b class="error" style="margin-left: 10px">
                                        @if($pv->currencyDetails != null)
                                            {!! $pv->currencyDetails->symbol !!}
                                        @endif
                                        {{number_format($pv->amount ?? 0,2)}}
                                    </b>
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <div class="guestDetails_inputDivider"></div>
            <table style="width: 100%; border-collapse: collapse">
                <tr style="padding: 6px">
                    <th style="width: 10%">Payment Narration:</th>
                    <td>{{$pv->nar}}</td>
                </tr>
                <tr style="padding: 6px">
                    <th style="width: 10%">Amount in Words:</th>
                    <td>{{strtoupper($amountInWords.' '.$pv->currencyDetails->name.' only')}}</td>
                </tr>
            </table>
        </div>

        @if($pv->reversal_reason != null)
            <div class="guestDetails_inputDivider"></div>
            <div class="policy-notes">
                <table style="border-collapse: collapse; width: 100%" >
                    <tr>
                        <td><b>REVERSAL COMMENTS: </b> {!! $pv->reversal_reason !!}</td>
                        <td style="text-align: right">Reversed By:
                            @if($pv->reversedBy != null)
                                <b>{{$pv->reversedBy->firstName." ".$pv->reversedBy->lastName}}</b>
                            @endif
                        </td>
                    </tr>
                </table>
            </div>
        @endif

    </div><br>


    <div class="tab">
        <button class="tablinks" onclick="openCity(event, 'policies')" @if(isset($_GET['type']) && $_GET['type'] == 'summary')id="defaultOpen" @else id="summary-url"@endif>Allocations</button>
        <button class="tablinks" onclick="openCity(event, 'documents')" @if(isset($_GET['type']) && $_GET['type'] == 'documents') id="defaultOpen" @else id="documents-url"@endif>Documents <span class="
            @if(!1)
                {{'countItems'}}
            @else
                {{'zeroItems'}}
            @endif
            ">&nbsp;{{0}}&nbsp;</span> </button>
        <button class="tablinks" onclick="openCity(event, 'log')" @if(isset($_GET['type']) && $_GET['type'] == 'log')id="defaultOpen" @else id="log-url"@endif>LOG <span class="zeroItems">&nbsp;{{0}}&nbsp;</span> </button>
    </div>

    <div class="tabcontent" id="policies">
        <span onclick="this.parentElement.style.display='none'" class="topright" style="margin-left: 20px">&times</span>
        <h3 ><span class="error" style="margin-right: 20px">Payment Allocation Listing</span></h3>
        {{--We will Loop Through the Different Types of Payments and be able to Display Items being Paid for--}}
    </div>

    {{--DOCUMENTS SECTION--}}
    <div class="tabcontent" id="documents">
        <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
        <h3 class="error">View Client's Documents Listing</h3>

        <table id="docsTable" class="display" style="width:100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Document Name</th>
                <th>Uploaded By</th>
                <th>Date Uploaded</th>
                <th>Print Document</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            {{--@foreach($documents as $doc)
                <tr>
                    <td style="width: 50px" class="lr"></td>
                    <td><b class="error">{!! strtoupper($doc->filename) !!}</b></td>
                    <td>
                        @if($doc->user)
                            {!! $doc->user->firstName." ".$doc->user->lastName !!}
                        @endif
                    </td>
                    <td>{!! date('d/m/Y', strtotime($doc->created_at)) !!}</td>
                    <td style="width: 80px"><a href="{{asset($doc->path)}}" class="inner" target="_blank">
                            <b><i class="fa fa-file error" style="font-size: 14px" title="Print Client Document"></i></b>
                        </a></td>
                    <td style="width: 50px">
                        @if(auth()->user()->admin == 1)
                            <i onclick="delClientDocument('{{$doc->id}}')" class="fa fa-trash-o error" title="Delete Client Document" style="font-size: 14px; cursor: pointer"></i>
                        @endif
                    </td>
                </tr>
            @endforeach--}}
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Document Name</th>
                <th>Uploaded By</th>
                <th>Date Uploaded</th>
                <th>Print Document</th>
                <th></th>
            </tr>
            </tfoot>
        </table>
    </div>
    <div class="tabcontent" id="log">
        <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
        <h3 class="error">Audit Trails (LOGs)</h3>
        <table id="example7" class="display" style="width:100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th style="text-align: left;">No</th>
                <th style="text-align: left">Log Narration.</th>
                <th style="text-align: left">User</th>
                <th style="text-align: left">Date & Time</th>
            </tr>
            </thead>
            <tbody>
            {{--@foreach($pv->history as $histo)
                <tr>
                    <td class="mr"></td>
                    <td class="error">{!! $histo->narration !!}</td>
                    <td>-</td>
                    <td>{{$histo->created_at->format('d/m/Y') .' at '. $histo->created_at->format('h:i:s a')}}</td>
                </tr>
            @endforeach--}}
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th style="text-align: left">No</th>
                <th style="text-align: left">Log Narration.</th>
                <th style="text-align: left">User</th>
                <th style="text-align: left">Date & Time</th>
            </tr>
            </tfoot>
        </table>
    </div>
@endsection
@section('created-by')
    * Payment Registered
    @if($pv->user != null)
        By <b>{{$pv->user->firstName." ".$pv->user->lastName}}</b>
    @endif
    on <b>{{$pv->created_at->format('j F Y H:i:s')}}</b>
@endsection
{{--System Hints Page--}}
@section('modal_heading')
    <h1>Payment File</h1>
@endsection
@section('modal_title')
    <h3><u>Dashboard Functionalities</u></h3>
    <b>
        <ol>
            <li>View Payment Details</li>
            <li>View Utilization of the Payment</li>
            <li>Print payment Document</li>
            <li>View Logs on the Payment</li>
        </ol>
    </b>
@endsection
