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

{{--Set Page Title--}}
@section('title', 'Advance Salary File')

{{--Include any Internal JS Code--}}
@section('internal-js')
    <script>
        $(document).ready(function () {
            $('#example1').dataTable();
            $('#example2').dataTable();

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

            $('#submit-search').on('click', function (){
                var account = $('#search-vehicle').val();
                if(account == ""){
                    alert('Please Select a Advance Salary File to Search');
                }else{
                    window.location = '/advance-salary-file/'+account;
                }
            });

            /**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();
            });
        });

        /**Create a Function to Create a Requisition for the Advance Salary**/
        function createAdvanceSalaryRequisition(id){
            var route_link = '/create-advance-salary-requisition/'+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: 550,
                    title: "Create New Requisition for the Advance Salary",
                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    var datas = $('#create-advance-salary-requisition').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,

                                        success: function (res){
                                            // alert(res);
                                            if(res == 'empty type'){
                                                alert('Please Provide the Requisition Type to proceed');
                                            }else if(res == 'empty debit type'){
                                                alert('Please provide the Debit Account to Proceed');
                                            }else if(res == 'empty amount'){
                                                alert('Please provide the Requisition Amount to Proceed');
                                            }else if(res == 'empty date'){
                                                alert('Please Provide Requisition Date');
                                            }else if(res == 'more amount'){
                                                alert('Requisition Amount cannot be greater than the Current Advance Salary Balance');
                                            }else if(res == 'req exists'){
                                                alert('Requisition Already Exists for the specified Advance Salary');
                                            }else if(res == 'error' ){
                                                alert('System is Unable to perform this Request. Please try again');
                                                location.reload();
                                            }else if(res == 'success'){
                                                alert('Requisition Created Successfully');
                                                location.reload();
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                                location.reload();
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 100});
                    }
                });
        }

        /**Create a Function to View a Payment's Details for a Specific Vendor Bill**/
        function viewPaymentStatement(id){
            var route_link = '/view-bill-payment-details/'+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>');
            $('#edit_dialog_content').load(encodeURI(route_link));
            $('#edit_dialog_content').dialog({
                modal: true,
                width: 950,
                title: "View Paid Bills Payment Report",
                open: function() {
                    $(this).parent().css({'top': 50});
                }
            });
        }

        {{--Function to view Advance Salary Report--}}
        function viewAdvanceSalaryReport(id){
            var route_link = '/advance-salary-repayment-report/'+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: 650,
                    title: "Advance Salary Repayment Report",
                    open: function() {
                        $(this).parent().css({'top': 50});
                    }
                });
        }

        /**Run a First Approval**/
        function firstApproval(id){
            if(confirm('Are you sure to First Approve this Advance Salary?')){
                $.ajax({
                    method: "GET",
                    url: '/advance-salary-first-approval/'+id,
                    data: id,
                    success: function (res){
                        if(res == 'error'){
                            alert('There was an internal error. Please contact System Admin');
                            location.reload();
                        }else if(res == 'success'){
                            alert('Advance Salary First Approved successfully');
                            location.reload();
                        }else{
                            alert('System encountered an error. please contact System Admin');
                            location.reload();
                        }
                    }
                });
            }
            return false;
        }

        /**Run a First Approval**/
        function secondApproval(id){
            if(confirm('Are you sure to Second Approve this Advance Salary?')){
                $.ajax({
                    method: "GET",
                    url: '/advance-salary-second-approval/'+id,
                    data: id,
                    success: function (res){
                        if(res == 'error'){
                            alert('There was an internal error. Please contact System Admin');
                            location.reload();
                        }else if(res == 'success'){
                            alert('Advance Salary Second Approved successfully');
                            location.reload();
                        }else{
                            alert('System encountered an error. please contact System Admin');
                            location.reload();
                        }
                    }
                });
            }
            return false;
        }
    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
    <style>
        #example1 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;}
    </style>
@endsection

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

{{--Set Page Heading--}}
@section('heading')
    Advance Salary File for:
    @isset($advance)
        <span class="error">{!! $advance->employee->firstName." ".$advance->employee->lastName !!}</span>
    @endisset
@endsection

@section('searches')
    <div style="float: right">
        <br>
        {!! Form::open(['method'=>'POST']) !!}
        <div class="guestDetails_input">
            <div class="guestDetails_input" style="text-align: center">
                <select name="search-vehicle" id="search-vehicle" style="width: 200px">
                    <option value="">--Select Advance Salary File--</option>
                    @if(isset($advances))
                        @foreach($advances as $row)
                            <option value="{{$row->id}}">{!! $row->employee->firstName." ".$row->employee->lastName." -> KES ".$advance->amount !!}</option>
                        @endforeach
                    @endif
                </select>
            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_input">
                <button type="button" id="submit-search" style="width: auto; height: 22px; cursor: pointer" title="Search a Product"><i class="fa fa-search" aria-hidden="true"></i></button>
            </div>
        </div>
        {!! Form::close() !!}
    </div>
@endsection
{{--Page Content Goes here--}}
@section('content')
    <div style="float: left">
        <button onclick="printOptions('{{$advance->id}}')" type="button" class="navButtons" style="margin-left: 0px;background-color: #f1d414; color: black"><i class="fa fa-print"></i> &nbsp;PRINT OPTIONS</button>
        @if($advance->first_approved == 0)
            <button onclick="firstApproval('{{$advance->id}}')" class="navButtons" style="background-color: #2dd314; margin-left: 0"><i class="fa fa-check" aria-hidden="true"></i> FIRST APPROVAL</button>
        @else
            @if($advance->second_approved == 0)
                <button onclick="secondApproval('{{$advance->id}}')" class="navButtons" style="background-color: #2dd314; margin-left: 0"><i class="fa fa-check" aria-hidden="true"></i> SECOND APPROVAL</button>
            @else
                @if($advance->disbursed == 0)
                    <button onclick="createAdvanceSalaryRequisition('{{$advance->id}}')" type="button" class="navButtons" style="background-color: #04ab25; color: white"><i class="fa fa-money"></i> &nbsp;CREATE REQUISITION</button>
                @endif

            @endif
        @endif
    </div>
    <div style="text-align: right">
        @if($advance->disbursed == 0)
            <button onclick="disaprroveAdvance('{{$advance->id}}')" type="button" class="navButtons" style="background-color: #f10e21"><i class="fa fa-trash"></i> &nbsp;DISAPPROVE ADVANCE SALARY</button>
        @endif
    </div>
    <div class="guestDetails_inputDividerNone"></div>
    @if($advance->first_approved == 0)
        <div class="notice">Pending First Approval</div>
    @else
        @if($advance->second_approved == 0)
            <div class="notice">Pending Second Approval</div>
        @else
            @if($advance->disbursed == 0)
                <div class="notice">Pending Disbursement</div>
            @elseif($advance->disbursed == 1)
                <div class="notice">Successfully Disbursed on {!! date('d/m/Y H:i:s a', strtotime($advance->date_disbursed)) !!}</div>
            @endif
        @endif
    @endif
    <div class="guestDetails_inputDividerNone"></div>
    <div style="background-color: rgba(234,236,238,0.72); border-radius: 5px; padding: 5px 6px">
        <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>Advance Salary No:</th>
                            <td><b class="error">{!! $advance->id !!}</b></td>
                        </tr>
                        <tr>
                            <th>Branch:</th>
                            <td><b class="error">Main</b></td>
                        </tr>
                        <tr>
                            <th>Date Created:</th>
                            <td><b>{{$advance->created_at->format('j F Y H:i:s')}}</b></td>
                        </tr>
                    </table>
                </div>
                <div class="column">
                    <table class="table" style="width: 100%; border-collapse:collapse; margin: 5px">
                        <tr>
                            <th>Current Asset Account:</th>
                            <td><b class="error">STAFF CURRENT ACCOUNT</b></td> {{--This Account is already Mappable--}}
                        </tr>
                        <tr>
                            <th>Employee No:</th>
                            <td><b class="error">{!! $advance->employee->e_no !!}</b></td>
                        </tr>
                        <tr>
                            <th>Employee Names:</th>
                            <td><b>{!! $advance->employee->firstName." ".$advance->employee->lastName !!}</b></td>
                        </tr>
                    </table>

                </div>
                <div class="column">
                    <table class="table" style="width: 100%; border-collapse:collapse; margin: 5px">
                        <tr>
                            <th>Payee Account Number:</th>
                            <td><b class="error">{!! strtoupper($advance->employee->acc_no ?? '-') !!}</b></td>
                        </tr>
                        <tr>
                            <th>Bank Details:</th>
                            <td><b class="error">{!! strtoupper($advance->employee->bank_name."/".$advance->employee->bank_branch ?? '-') !!}</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">ACC BAL
                                    (<span class="error">KES</span>):
                                    <b class="error" style="margin-left: 10px">
                                        {{number_format($advance->advanceSalaryBalance($advance->id),2)}}
                                    </b>
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div><br>
    <div class="tab">
        <button class="tablinks" onclick="openCity(event, 'items')" id="defaultOpen" >Advance Salary Item</button>
        <button class="tablinks" onclick="openCity(event, 'reqs')" id="defaultOpen" >Pending Requisitions
            <span class="
            @if(count($pendingRequisitions) > 0)
                {{'countItems'}}
            @else
                {{'zeroItems'}}
            @endif
            ">&nbsp;{{count($pendingRequisitions)}}&nbsp;</span>
        </button>
    </div>
    <div class="tabcontent" id="items">
        <h3 ><span class="error" style="margin-right: 20px">Advance Salary Particulars</span></h3>
        <table id="example1" class="display" style="width: 100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Applied Amount</th>
                <th>Repayment Cycles</th>
                <th>Repaid Amount</th>
                <th>Advance Balance</th>
                <th>First Approved</th>
                <th>First Approved By</th>
                <th>Second Approved</th>
                <th>Second Approved By</th>
                <th>Disbursed</th>
                <th>Fully Paid</th>
                <th>View Schedule</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td class="ar"></td>
                <td><b class="error">{!! number_format($advance->amount, 2) !!}</b></td>
                <td><b>{!! $advance->duration !!}</b></td>
                <td><b class="success">{!! number_format($row->advanceSalaryPaidAmount($advance->id),2) !!}</b></td>
                <td><b class="error">{!! number_format($row->advanceSalaryBalance($advance->id),2) !!}</b></td>
                <td>
                    @if($advance->first_approved == 1)
                        <b class="success">YES</b>
                    @else
                        <b class="error">NO</b>
                    @endif
                </td>
                <td>
                    @if($advance->firstApprover)
                        {!! $advance->firstApprover->firstName." ".$advance->firstApprover->lastName !!}
                    @else
                        -
                    @endif
                </td>
                <td>
                    @if($advance->second_approved == 1)
                        <b class="success">YES</b>
                    @else
                        <b class="error">NO</b>
                    @endif
                </td>
                <td>
                    @if($advance->secondApprover)
                        {!! $advance->secondApprover->firstName." ".$advance->secondApprover->lastName !!}
                    @else
                        -
                    @endif
                </td>
                <td>
                    @if($advance->disbursed == 1)
                        <b class="success">YES</b>
                    @else
                        <b class="error">NO</b>
                    @endif
                </td>
                <td>
                    @if($advance->fully_paid == 1)
                        <b class="success">YES</b>
                    @else
                        <b class="error">NO</b>
                    @endif
                </td>
                <td><i onclick="viewAdvanceSalaryReport('{{$advance->id}}')" class="fa fa-eye error" style="cursor: pointer; font-size: 14px"></i></td>
            </tr>
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Applied Amount</th>
                <th>Repayment Cycles</th>
                <th>Repaid Amount</th>
                <th>Advance Balance</th>
                <th>First Approved</th>
                <th>First Approved By</th>
                <th>Second Approved</th>
                <th>Second Approved By</th>
                <th>Disbursed</th>
                <th>Fully Paid</th>
                <th>View Schedule</th>
            </tr>
            </tfoot>
        </table>
    </div>

    {{--Pending Requisitions--}}
    <div class="tabcontent" id="reqs">
        <h3 ><span class="error" style="margin-right: 20px">Unauthorized and Unpaid Requisitions</span></h3>

        <table id="example2" class="display" style="width: 100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Req Number</th>
                <th>Type</th>
                <th>Dr Ledger</th>
                <th>Payee</th>
                <th>Requisition Amount</th>
                <th>Currency</th>
                <th>Exchange rate</th>
                <th>Status</th>
                <th>Date Requisitioned</th>
                <th>Requisitioned By</th>
                <th>View More</th>
            </tr>
            </thead>
            <tbody>
            @foreach($pendingRequisitions as $req)
                <tr>
                    <td class="br"></td>
                    <td><a href="{{url('requisition-details/'.$req->id.'?type=summary')}}" class="inner" target="_blank"><b class="error">RQ00{{$req->id}}</b></a></td>
                    <td>{!! strtoupper($req->req_type) !!}</td>
                    <td>
                        @if($req->account != null)
                            {!! strtoupper($req->account->acc_name) !!}
                        @endif
                    </td>
                    <td>{!! $req->payee !!}</td>
                    <td><b>{!! number_format($req->amount,2) !!}</b></td>
                    <td>
                        @if($req->currencyDetails != null)
                            <b class="error">{!! $req->currencyDetails->abbr !!}</b>
                        @endif
                    </td>
                    <td>{!! number_format($req->conversion_rate,2) !!}</td>
                    <td>
                        @if($req->first_approved == 0)
                            <i class="error">Pending First Approval</i>
                        @else
                            @if($req->second_approved == 0)
                                <i class="error">Pending Second Approval</i>
                            @else
                                <b class="error">{!!  $req->req_status !!}</b>
                            @endif
                        @endif
                    </td>
                    <td>{{date('d/m/Y', strtotime($req->req_date))}}</td>
                    <td>
                        @if($req->user != null)
                            {!! $req->user->firstName[0].". ".$req->user->lastName !!}
                        @endif
                    </td>
                    <td><a href="{{url('requisition-details/'.$req->id.'?type=summary')}}" class="inner" target="_blank"><i class="fa fa-eye error" style="font-size: 16px; cursor: pointer" onclick="viewVenedorRequisition()"></i></a></td>
                </tr>
            @endforeach
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Req Number</th>
                <th>Type</th>
                <th>Dr Ledger</th>
                <th>Payee</th>
                <th>Requisition Amount</th>
                <th>Currency</th>
                <th>Exchange rate</th>
                <th>Status</th>
                <th>Date Requisitioned</th>
                <th>Requisitioned By</th>
                <th>View More</th>
            </tr>
            </tfoot>
        </table>
    </div>
@endsection
@section('created-by')
    * Advance File Created
    @if($advance->user != null)
        By <b>{{$advance->user->firstName." ".$advance->user->lastName}}</b>
    @endif
    on <b>{{$advance->created_at->format('j F Y H:i:s')}}</b>
@endsection

{{--System Hints Page--}}
@section('modal_heading')
    <h1>Advance Salary File</h1>
@endsection
@section('modal_title')
    <h3><u>Dashboard Functionalities</u></h3>
    <b>
        <ol>
            <li>View Advance Salary Details</li>
            <li>Advance Salary Transactional Report</li>
            <li>Delete a Wrongly created Advance Salary</li>
        </ol>
    </b>
@endsection
