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

{{--Set Page Title--}}
@section('title', 'Direct Business Sources')

{{--Include any Internal JS Code--}}
@section('internal-js')
    <script>
        $(document).ready(function () {
            $('#example').dataTable();
            $('#example1').dataTable();
        });
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        function deleteSource(id, name, type){
            var connect;
            if(type == 'Restore'){
                connect = ' to ';
            }else if(type == 'Delete'){
                connect = ' from ';
            }
            if(confirm( type +' '+ name + connect +'list of Business Sources')){
                $.ajax({
                   method: 'PUT',
                    url: 'setup-business-leeds/'+id,
                    data: id,
                    success: function (res){
                       if(res == 'success'){
                           window.location = 'setup-business-leeds?success=updated';
                       }else if(res == 'error'){
                           alert('System was unable to perform the Request. Please try again');
                           window.location = 'setup-business-leeds?success=failed';
                       }else{
                           alert('System Encountered and Error. Please Contact System Admin');
                           window.location = 'setup-business-leeds';
                       }
                    }
                });
            }
        }

        function addSource(){
            var route_link = '/add-business-source';

            $('#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: 450,
                title: "Add New Business Source",
                buttons: [
                    {
                        text: "Save Details",
                        icon: "ui-icon-check",
                        click: function(e) {
                            if(confirm('Are you sure to Save Entered Details?')){
                                e.preventDefault();

                                //var name = $("input[name=source-name]").val();
                                //var status = $("input[status=status]").val();
                                var datas = $('#add-source').serializeArray();

                                $.ajax({
                                    method: "POST",
                                    url: route_link,
                                    data: datas,
                                    success: function (res){
                                        if(res == 'exist'){
                                            alert('Entered Source already Exists');
                                        }else if(res == 'error'){
                                            alert('System was unable to perform the Request. Please Try again')
                                        }
                                        else if(res == 'success'){
                                            window.location = '/setup-business-leeds?success=true';

                                        }else{
                                            alert('System Encountered an Error. Please Contact System Admin');
                                            window.location = '/setup-business-leeds';
                                        }
                                    }
                                });
                                return false;
                            }
                        }
                    }
                ],
                open: function() {
                    $(this).parent().css({'top': 50});
                },
                close: function (){
                    $("#add-client").text("Save").attr('disabled', false).css('backgroundColor', 'royalblue');
                }
            });
        }
    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
    <style>

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

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


    </style>
@endsection

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

{{--Set Page Heading--}}

@section('heading', 'Setup Direct Business Sources')
@section('searches', '')
@section('done-by', '')

{{--Page Content Goes here--}}
@section('content')
    @isset($_GET['success'])
        <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['success'] == 'true')
                <div class="yesFound">Business Source Created Successfully</div><br>
            @elseif($_GET['success'] == 'updated')
                <div class="yesFound">Business Source Deleted Successfully</div><br>
            @elseif($_GET['success'] == 'failed')
                <div class="notFound">System was unable to Delete the Business Source</div><br>
            @endif
        </div>
    @endisset
    <div class="tab">
        <button class="tablinks" onclick="openCity(event, 'leeds')" id="defaultOpen">Active Sources</button>
        <button class="tablinks" onclick="openCity(event, 'disabled')" id="defaultOpen">Disabled Sources</button>
        @if(app('UserReads')->trash == 1)
            <button class="tablinks" style="background-color: #FF4C39; color: white" onclick="openCity(event, 'trash')" id="@if(isset($_GET['trash'])){{'defaultOpen'}} @endif">Trash </button>
        @endif
    </div>

    <div class="tabcontent" id="leeds">
        <div class="topright">
            <a class="inner" href="#" ><img src="{{asset('images/exports/pdf.png')}}"></a>
            <a class="inner" href="#" ><img src="{{asset('images/exports/excel.png')}}"></a>
            <span onclick="this.parentElement.style.display='none'" class="topright" style="margin-left: 20px">&times</span>
        </div>
        <h3 class="error">Direct Business Listing
            <button type="button" onclick="addSource()" style="margin-left: 20px; cursor: pointer;height: 25px">+ Add New Business Source Type</button>
        </h3>
        <table id="example" class="display" style="width:100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Name</th>
                <th>New Businesses</th>
                <th>Renewals</th>
                <th>Total Sales</th>
                <th style="width: 10px"></th>
                <th style="width: 10px"></th>
            </tr>
            </thead>
            <tbody>
            @isset($sources)
                @foreach($sources as $source)
                    @if($source->status == 1)
                    <tr>
                        <td class="ar"></td>
                        <td><a href="{{url('#')}}" class="inner" style="font-weight: bold" ><span class="error">{{$source->name}}</span></a></td>
                        <td >0</td>
                        <td >0</td>
                        <td >0</td>
                        <td >
                            <a  href="javascript: void(0);" class="inner"  onclick="editSource('{{$source->id}}')"><b class="error"><i class="fa fa-pencil fa-md" style="font-size: 15px"></i></b></a>
                        </td>
                        <td >
                            <a href="javascript: void(0);" style="margin-left: 15px"><img src="{{asset('images/bin1.png')}}" title="Delete Source" onclick="deleteSource('{{$source->id}}', '{{$source->name}}', 'Delete')"></a>
                        </td>
                    </tr>
                    @endif
                @endforeach
            @endisset

            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Name</th>
                <th>New Businesses</th>
                <th>Renewals</th>
                <th>Total Sales</th>
                <th style="width: 10px"></th>
                <th style="width: 10px"></th>
            </tr>
            </tfoot>
        </table>
    </div>

    <div class="tabcontent" id="disabled">
        <div class="topright">
            <a class="inner" href="#" ><img src="{{asset('images/exports/pdf.png')}}"></a>
            <a class="inner" href="#" ><img src="{{asset('images/exports/excel.png')}}"></a>
            <span onclick="this.parentElement.style.display='none'" class="topright" style="margin-left: 20px">&times</span>
        </div>
        <h3 class="error">Disabled Business Listing</h3>
        <table id="example1" class="display" style="width:100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Name</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            @isset($sources)
                @foreach($sources as $source)
                    @if($source->status == 0)
                        <tr>
                            <td class="br"></td>
                            <td >{{ucwords($source->name)}}</td>
                            <td >
                                <i class="fa fa-undo" onclick="deleteSource('{{$source->id}}', '{{$source->name}}', 'Restore')" aria-hidden="true" title="Restore Source" style="cursor: pointer; font-size: 16px"></i>
                            </td>
                        </tr>
                    @endif
                @endforeach
            @endisset

            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Name</th>
                <th></th>
            </tr>
            </tfoot>
        </table>
    </div>
@endsection

{{--System Hints Page--}}

@section('modal_heading')
    <h1>Client File</h1>
@endsection
@section('modal_title')
    <h3><u>Dashboard Functionalities</u></h3>
    <ol><b>
            <li>View Client Details</li>
            <li>View Client Policies, Quotes, Unauthorized Policies, Payments, Claims etc</li>
            <li>Amend Client Details. This includes Activating and Deactivating Client</li>
            <li>Update Contact Persons for the given Client</li>
            <li>View Client Logs</li>
        </b>
    </ol>
@endsection




