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

{{--Set Page Title--}}
@section('title', 'Setup Employee Deduction Types')

{{--Include any Internal JS Code--}}
@section('internal-js')
    <script>
        $(document).ready(function () {
            $('#active-table').dataTable({
                dom: 'lBfrtip',
                buttons: [
                    { extend: 'excel', className: 'download-buttons' },
                    { extend: 'pdf', className: 'download-buttons' }
                ],
                "paging":   false,
            });
        });

        {{--Create New User Account Function--}}
        function addNewEmployeeDeductionItem(){
            var route_link = '/add-employee-deduction-type';

            $('#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: "Add New Employee Deduction Type",

                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            id: "my-button-1",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    $("#my-button-1").attr('disabled', true).text('Please Wait....');
                                    var datas = $('#add-employee-deduction-type').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res){
                                            if(res == 'success'){
                                                window.location = '/setup-deductions?addDeduction=true';
                                            }else if(res == 'empty fields'){
                                                alert('Please provide both the Name and Basis for the Deduction Type');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            }else if(res == 'exists'){
                                                alert('The Entered Deduction Type already Exists');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 50});
                    }
                });
        }

        {{--Create a Function to Add/Update Deduction Calculator--}}
        function addDeductionCalculator(id){
            var route_link = '/add-employee-deduction-calculator/'+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: 700,
                    title: "Update/Add New Employee Deduction Calculator",

                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            id: "my-button-1",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    $("#my-button-1").attr('disabled', true).text('Please Wait....');
                                    var datas = $('#add-employee-deduction-calculator').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res){
                                            if(res == 'success'){
                                                window.location = '/setup-deductions?addDeductionCalculator=true';
                                            }else if(res == 'no rates'){
                                                alert('Please provide the Applicable Rates to Proceed');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            }else if(res == 'error'){
                                                alert('There was an Error while trying to update your Records');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 50});
                    }
                });
        }

        {{--Update Employee Numbers--}}
        function updateEmployeeNumber(id) {
            var route_link = '/update-employee-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: 750,
                    title: "Update Employee Number",

                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            id: "my-button-1",
                            click: function (e) {
                                if (confirm('Are you sure to Save Entered Details?')) {
                                    e.preventDefault();
                                    $("#my-button-1").attr('disabled', true).text('Please Wait....');
                                    var datas = $('#update-employee-number').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res) {
                                            if (res == 'success') {
                                                window.location = '/view-company-employees?updateEmployeeNumber=true';
                                            } else if (res == 'empty number') {
                                                alert('Please Provide Employee Number to Save Details');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            } else {
                                                alert('System Encountered an Error. Please Contact System Admin');
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function () {
                        $(this).parent().css({'top': 50});
                    }
                });


        }

        {{--Affiliate a Deduction to an Expense ledger--}}
        function addExpenseLedger(id) {
            var route_link = '/affiliate-deduction-to-expense-ledger/' + 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: "Affiliate an Expense Ledger to a Deduction Account",

                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            id: "my-button-1",
                            click: function (e) {
                                if (confirm('Are you sure to Save Entered Details?')) {
                                    e.preventDefault();
                                    $("#my-button-1").attr('disabled', true).text('Please Wait....');
                                    var datas = $('#affiliate-expense-ledger').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res) {
                                            if (res == 'success') {
                                                window.location = '/setup-deductions?updateExpenseLedger=true';
                                            } else if (res == 'empty ledger') {
                                                alert('Please Select at least one Ledger to Proceed');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            } else {
                                                alert('System Encountered an Error. Please Contact System Admin');
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function () {
                        $(this).parent().css({'top': 50});
                    }
                });
        }

        {{--Setup Employee Fields that are Requeired for the Given Deduction--}}
        function manageRequiredEmployeeFields(id) {
            var route_link = '/manage-employee-deduction-fields/' + 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: "Manage Employee Deduction Fields",

                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            id: "my-button-1",
                            click: function (e) {
                                if (confirm('Are you sure to Save Entered Details?')) {
                                    e.preventDefault();
                                    $("#my-button-1").attr('disabled', true).text('Please Wait....');
                                    var datas = $('#update-employee-deduction-fields').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res) {
                                            if (res == 'success') {
                                                window.location = '/setup-deductions?updateEmployeeFields=true';
                                            } else if (res == 'empty fields') {
                                                alert('You did not Provide any Fields. Close this Window to save none');
                                                $("#my-button-1").attr('disabled', false).text('Save Details');
                                            } else {
                                                alert('System Encountered an Error. Please Contact System Admin');
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function () {
                        $(this).parent().css({'top': 50});
                    }
                });
        }
    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
    <style>
        #active-table tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        #active-table th, td {
            text-align: left;
            padding: 4px;
        }
        #active-table tr:hover {background-color: #cbd1f8;}
        .switch-certificate {
            position: relative;
            display: inline-block;
            width: 38px;
            height: 17px;
        }
    </style>
@endsection

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

{{--Set Page Heading--}}
@section('heading', 'Employee Deductions Listing')

{{--Page Content Goes here--}}
@section('content')
    @isset($_GET['addDeduction'])
        <div class="error-section">
            <span onclick="this.parentElement.style.display='none'" class="topright" style="color: white ;margin-top: -4px;font-weight: bold; margin-right: 5px">&times</span>
            @if($_GET['addDeduction'] == 'true')
                <div class="yesFound">New Employee Deduction Type has been Created Successfully</div><br>
            @elseif($_GET['addDeduction'] == 'false')
                <div class="notFound">There was an error while trying to Create Employee Deduction Type</div><br>
            @endif
        </div>
    @endisset
    @isset($_GET['updateExpenseLedger'])
        <div class="error-section">
            <span onclick="this.parentElement.style.display='none'" class="topright" style="color: white ;margin-top: -4px;font-weight: bold; margin-right: 5px">&times</span>
            @if($_GET['updateExpenseLedger'] == 'true')
                <div class="yesFound">Deduction Account Type has been affiliated to an Expense Ledger Account Successfully</div><br>
            @elseif($_GET['updateExpenseLedger'] == 'false')
                <div class="notFound">There was an error while trying to perform the Requested action</div><br>
            @endif
        </div>
    @endisset
    <div class="tab">
        <button class="tablinks" onclick="openCity(event, 'deductions')" id="defaultOpen">Active Employee Deductions</button>
    </div>

    <div id="deductions" class="tabcontent">
        <h3 class="error">
            Employee Deduction Types
            <button onclick="addNewEmployeeDeductionItem()" class="section-button">+ Add New Employee Deduction Type</button>
        </h3>
        <table id="active-table" class="table-overflow" style="width: 100%; border-collapse: collapse; ">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Name:</th>
                <th>Computation Basis</th>
                <th>Min. Amount Deductible</th>
                <th>Has Calculator</th>
                <th>Add a Calculator</th>
                <th>Has Expense Ledger</th>
                <th>Expense Ledger Name</th>
                <th>Has Employee Fields</th>
                <th>Manage Required Fields</th>
                <th>Edit</th>
                <th>Created By</th>
                <th>Date & Time</th>
            </tr>
            </thead>
            <tbody>
            @foreach($deductions as $row)
                <tr>
                    <td class="ar"></td>
                    <td><b class="error">{!! strtoupper($row->name) !!}</b></td>
                    <td><b>{!! ucwords($row->basis) !!}</b></td>
                    <td><b class="error">{!! number_format($row->min_amount, 2) !!}</b></td>
                    <td>
                        @if(count($row->calculator) > 0)
                            <b class="success">YES</b>
                        @else
                            <b class="error">No</b>
                        @endif
                    </td>
                    <td><i onclick="addDeductionCalculator('{{$row->id}}')" class="fa fa-calculator" style="font-size: 14px; cursor: pointer"></i></td>

                    @if($row->expenseLedger)
                        <td><b class="success">YES</b></td>
                        <td><b>{!! strtoupper($row->expenseLedger->acc_name) !!}</b></td>
                    @else
                        <td>
                            <b class="error">No</b>  <i onclick="addExpenseLedger('{{$row->id}}')" class="fa fa-bank error" style="margin-left: 10px; cursor: pointer"> Click to Add Ledger</i>
                        </td>
                        <td>-</td>
                    @endif

                    @if(count($row->employeeDeductionFields) > 0)
                        <td><b class="success">YES</b></td>
                    @else
                        <td><b class="error">No</b></td>
                    @endif
                    <td><i onclick="manageRequiredEmployeeFields('{{$row->id}}')" class="fa fa-plus error" style="margin-left: 10px; cursor: pointer"> Add Fields</i></td>

                    <td><i class="fa fa-pencil error" style="font-size: 14px; cursor: pointer"></i></td>
                    <td>
                        @if($row->user)
                            {!! $row->user->firstName." ".$row->user->lastName !!}
                        @endif
                    </td>
                    <td>{!! $row->created_at->format('d/m/Y H:i:s a') !!}</td>
                </tr>
            @endforeach
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Name:</th>
                <th>Computation Basis</th>
                <th>Min. Amount Deductible</th>
                <th>Has Calculator</th>
                <th>Add a Calculator</th>
                <th>Has Expense Ledger</th>
                <th>Expense Ledger Name</th>
                <th>Has Employee Fields</th>
                <th>Manage Required Fields</th>
                <th>Edit</th>
                <th>Created By</th>
                <th>Date & Time</th>
            </tr>
            </tfoot>
        </table>
    </div>
@endsection

@section('modal_heading')
    <h1>Setup Employee Deductions</h1>
@endsection
@section('modal_title')
    <h3><u>Dashboard Functionalities</u></h3>
    <b>
        <ol>
            <li>View all Setup Employee Deductions </li>
            <li>Add New Types of Employee Deductions</li>
        </ol>
    </b>
@endsection
