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

{{--Set Page Title--}}
@section('title', 'Add new Client')

{{--Include any Internal JS Code--}}
@section('internal-js')

    <script>
        $(document).ready(function () {
            $('#account-handler').select2();
            $('#brought-by').select2();
            $('#sub-agent').select2();
            $('#branch').select2();
            $('#gender').select2();
            $('#scheme').select2();
            $('#source').select2();
            $('b[role="presentation"]').hide();

            $('#corporate').on('click', function () {
                $('#id_coi').text('CoI:');
                $('#dob').text('Date Created/D.O.I:');
                $('#sub_category').show().prop('required', true);

            });
            $('#individual').on('click', function () {
                $('#id_coi').text('ID No./Passport:');
                $('#dob').text('D.o.B:');
                $('#sub_category').hide().prop('required', false);

            });

           $('#cancelRequest').click(function (){
               if(confirm('Are you sure to cancel adding a New Client Request?')){
                   history.back();
               }
               return false;
           });

            $('#through-agent').on('click', function () {
                $('#business-leeds').hide();
                $('#agents-leeds').show();
                $('#source').prop('required', false);
                $('#sub-agent').prop('required', true).select2();
            });
            $('#direct-sale').on('click', function () {
                $('#business-leeds').show();
                $('#agents-leeds').hide();
                $('#sub-agent').prop('required', false);
                $('#source').prop('required', true);
            });

        });


    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
<style>
    #remove_button{
        background-color: #FF0000;
        color: white;
        font-weight: bold;
        border: none;
        padding: 4px 6px;
        border-radius: 5px;
    }
</style>
@endsection

{{--Control Page Header/Links--}}
@section('headers')
    @if($module != null)
        @if($module == 'General')
            @include('headers.dash-header')
        @elseif($module == 'Medical')
            @include('headers.medical-header')
        @elseif($module == 'Life')
            @include('headers.life-header')
        @endif
    @else
        @include('headers.dash-header')
    @endif

@endsection

{{--Set Page Heading--}}
@section('heading', 'Setup New Client')

{{--Page Content Goes here--}}
@section('content')

{{--    <form method="POST" action="{{route('add-client')}}">--}}
<div class="inputDivider">
    {!! Form::open(['route'=>['client.store'], 'method'=>'POST', 'id'=>'target-form', 'class'=>'fields']) !!}
    <div class="error" style="text-align: center;font-weight: bold;">On this Window, You are required to populate Client Details to open a New File/Account</div>
    <div class="guestDetails_inputDivider input_divider"></div>


    <!--New client details will go here-->
    <div class="new_client">
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">Client Category:<span class="error">*</span>
                <input style="margin-left: 16px" type="radio" name="category" value="0" id="individual"> Individual
                <input style="margin-left: 16px" type="radio" name="category" value="1" id="corporate" > Corporate

                <select name="sub_category" id="sub_category" style="display: none; margin-left: 30px" >
                    <option value="">--Select Corporate Sub-Category--</option>
                    <option value="Government Institution">Government Institution</option>
                    <option value="Multinational">Multinational</option>
                    <option value="Industrial">Industrial</option>
                    <option value="Retail">Retail</option>
                    <option value="SME">SME</option>
                </select>
            </div>
        </div>
        <div class="guestDetails_inputDivider input_divider"></div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">Full Name(s): @if(app('ClientKYCFields')->name == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('kyc-docs', app('CompanySetup')->require_client_docs, ['id'=>'client-kyc-docs-checker']) !!}
                {!! Form::hidden('module', $module ?? 'General') !!}
                {!! Form::hidden('nms-req', app('ClientKYCFields')->name, ['id'=>'nms-req']) !!}
                {!! Form::text('nms', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">Phone Number:@if(app('ClientKYCFields')->phone == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('pn-req', app('ClientKYCFields')->phone, ['id'=>'pn-req']) !!}
                {!! Form::text('pn', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Email:@if(app('ClientKYCFields')->email == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('email-req', app('ClientKYCFields')->email, ['id'=>'email-req']) !!}
                {!! Form::email('email', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" id="id_coi">ID/Passport:@if(app('ClientKYCFields')->id_no == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('idn-req', app('ClientKYCFields')->id_no, ['id'=>'idn-req']) !!}
                {!! Form::text('idn', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" id="id_coi">PIN:@if(app('ClientKYCFields')->pin == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('kra-req', app('ClientKYCFields')->pin, ['id'=>'kra-req']) !!}
                {!! Form::text('kra', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" id="dob">D.O.B:@if(app('ClientKYCFields')->dob == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('dob-req', app('ClientKYCFields')->dob, ['id'=>'dob-req']) !!}
                {!! Form::date('dob', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>
        <div class="guestDetails_inputDivider1 input_divider"></div>

        {{--Second Row--}}
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">Address/Location:@if(app('ClientKYCFields')->address == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('address-req', app('ClientKYCFields')->address, ['id'=>'address-req']) !!}
                {!! Form::text('address', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >P.O BOX:@if(app('ClientKYCFields')->box == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('box-req', app('ClientKYCFields')->box, ['id'=>'box-req']) !!}
                {!! Form::text('box', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Occupation:@if(app('ClientKYCFields')->occupation == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('occupation-req', app('ClientKYCFields')->occupation, ['id'=>'occupation-req']) !!}
                {!! Form::text('occupation', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Office Tel:@if(app('ClientKYCFields')->office_tel == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('office_tel-req', app('ClientKYCFields')->office_tel, ['id'=>'office_tel-req']) !!}
                {!! Form::text('office_tel', '', ['size'=>'16px','class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Alternative Email:@if(app('ClientKYCFields')->alt_email == 1)<span class="error">*</span>@endif</div>
            <div class="guestDetails_input">
                {!! Form::hidden('alt_email-req', app('ClientKYCFields')->alt_email, ['id'=>'alt_email-req']) !!}
                {!! Form::email('alt_email', '', ['size'=>'16px', 'class'=>'input-text-height']) !!}
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" id="id_coi">Gender:</div>
            <div class="guestDetails_input">
                <select name="gender" id="gender" >
                    <option value="">--Select Gender--</option>
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                    <option value="Other">Other</option>
                </select>
            </div>
        </div>
        <div class="guestDetails_inputDivider input_divider1"></div>
        <div class="error" style="text-align: center;font-weight: bold;">HINT: Alternative Contact Person(s) Details: Click on Add Button below to Add New Fields</div><br>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Name(s):</div>
            <div class="guestDetails_input">
                <input type="text" name="alt_nm[]" size="16px" class="input-text-height">
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Relationship:</div>
            <div class="guestDetails_input">
                <input type="text" name="rltn[]" size="16px" class="input-text-height">
            </div>
        </div>

        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Tel No.:</div>
            <div class="guestDetails_input">
                <input type="text" name="alt_tel[]" size="16px" class="input-text-height">
            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel" >Email:</div>
            <div class="guestDetails_input">
                <input type="email" name="alt_psn_email[]" size="30px" class="input-text-height">
            </div>
        </div>
        <div class="guestDetails_inputDivider input_divider"></div>
        <div class="added-fields"></div>
        <button type="button" id="add-person" class="navButtons" style="background-color: #1e61e1; margin-left: 0">+ Add Another Person</button>

        <div class="guestDetails_inputDivider input_divider"></div>
        <div class="account-handlers">
            <div class="error" style="text-align: center;font-weight: bold;">HINT: On the below Section, provide further Details to people who know and will be handling the Client</div><br>

            @if(count(app('CompanyBranches')) > 0)
                <div class="guestDetails_input">
                    <div class="guestDetails_inputLabel" >Branch:<span class="error">*</span></div>
                    <div class="guestDetails_input">
                        <select id="branch" name="branch" style="min-width: 120px">
                            {{--<option value="">Select Branch</option>--}}
                            @foreach(app('CompanyBranches') as $branch)
                                <option value="{{$branch->id}}">{{$branch->name}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            @endif
            @if(app('CompanySetup')->require_risk_manager == 1)
                <div class="guestDetails_input">
                    <div class="guestDetails_inputLabel" >Client Introduced By:<span class="error">*</span></div>
                    <div class="guestDetails_input">
                        <select id="brought-by" name="brought_by" style="min-width: 200px" required>
                            <option value="">Select who Brought this Client</option>
                            @foreach($riskmanagers as $riskmanager)
                                <option value="{{$riskmanager->stamp}}">{{$riskmanager->name}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            @endif
            @if(app('CompanySetup')->require_account_handler == 1)
                <div class="guestDetails_input">
                    <div class="guestDetails_inputLabel" >Account Handler:<span class="error">*</span></div>
                    <div class="guestDetails_input">
                        <select id="account-handler" name="account_handler" style="min-width: 200px" required>
                            <option value="">Select Account Handler</option>
                            @foreach($users as $user)
                                <option value="{{$user->id}}">{{$user->firstName}} {{$user->lastName}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            @endif
            <div class="guestDetails_input">
                <div class="guestDetails_inputLabel" >Scheme:</div>
                <div class="guestDetails_input">
                    <select id="scheme" name="scheme" style="min-width: 200px">
                        <option value="">Select a Scheme</option>
                        @foreach($schemes as $row)
                            <option value="{{$row->id}}">{{$row->name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="guestDetails_input">
                <div class="guestDetails_inputLabel"><i>Client Type of Sale/Business:</i><span class="error">*</span></div>
                <div class="guestDetails_input">
                    <input  type="radio" name="sales-type" id="direct-sale" value="direct" > Direct Sale
                    <input style="margin-left: 20px" type="radio" name="sales-type" id="through-agent" value="agent" > Through an Agent
                </div>
            </div>
            <div class="guestDetails_input" id="business-leeds" style="display: none">
                <div class="guestDetails_inputLabel"><i style="color: #FF0000">Business Leeds:</i><span class="error">*</span> &nbsp;&nbsp;How did the Client know about us?</div>
                <div class="guestDetails_input">
                    <select style="width: 300px" name="source" id="source">
                        <option value="">--Select Business Source--</option>
                        @foreach(app('BusinessSources') as $business_source)
                            <option value="{{$business_source->id}}">{{$business_source->name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="guestDetails_input" id="agents-leeds" style="display: none">
                <div class="guestDetails_inputLabel"><i>Select Sub-Agent:</i><span class="error">*</span></div>
                <div class="guestDetails_input">
                    <select style="width: 300px" name="sub_agent" id="sub-agent">
                        @if($subagents != null)
                            <option value="">--Select Sub-Agent--</option>
                            @foreach($subagents as $subagent)
                                <option value="{{$subagent->stamp}}">{{strtoupper($subagent->contact_person)}}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
            </div>


        </div>
        <div class="guestDetails_inputDivider input_divider"></div>

    </div>
    <div class="inputDivider fields" >
        <div class="fields">
            <a href="" class="inner"><button type="button" name="cancel" id="cancelRequest" class="cancelButton">Cancel Process</button></a>
            <button type="submit" name="step1" class="continueButton" id="add-client" >Save & Continue</button>
        </div>
    </div>
    {!! Form::close() !!}
</div>
@endsection

@section('other-js')
    <script type="text/javascript" src="{{asset('js/client/add-contact-person.js')}}"></script>
    <script>
        $(document).ready(function (){
            $('#target-form').submit(function (e) {
                e.preventDefault();

                var det = $('#client-kyc-docs-checker').val();
                $("#add-client").text("Please wait...").attr('disabled', true).css('backgroundColor', 'gray');

                var results = verifyClient();
                if(results == 'Good'){
                    if(det == 1){
                        var clientType = "Individual";
                        if($('#corporate').is(':checked')){
                            clientType = 'Corporate';
                        }
                        var route_link = '/client-kyc-docs/'+clientType;

                        $('#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: "Upload Client KYC Documents",
                                buttons: [
                                    {
                                        text: "Save Documents",
                                        icon: "ui-icon-check",
                                        id: "my-button-1",
                                        click: function(e) {
                                            if(confirm('Are you sure to Upload Attached Documents?')){
                                                e.preventDefault();
                                                $("#my-button-1").attr('disabled', true);
                                                $("#my-button-1").text('Please Wait....');
                                                var datas = new FormData($('#upload-client-kyc-documents')[0]);

                                                $.ajax({
                                                    method: "POST",
                                                    url: route_link,
                                                    data: datas,
                                                    enctype: 'multipart/form-data',
                                                    contentType: false,
                                                    processData: false,
                                                    success: function (res){
                                                        if(res == 'missing doc name'){
                                                            alert('Please Enter Document Names in provided Fields');
                                                            $("#my-button-1").attr('disabled', false);
                                                            $("#my-button-1").text('Save Documents');
                                                        }else if(res == 'name and file missing'){
                                                            alert('Please Enter Document Name(s) and Attach Document(s) for Upload');
                                                            $("#my-button-1").attr('disabled', false);
                                                            $("#my-button-1").text('Save Documents');
                                                        }else if(res == 'File Missing'){
                                                            alert('Please ensure all KYC Documents have been attached in the provided fields');
                                                            $("#my-button-1").attr('disabled', false);
                                                            $("#my-button-1").text('Save Documents');
                                                        }else if(res == 'Wrong file format'){
                                                            alert('Wrong File Format attached, Please attach only PDF, JPG ,JPEG or PNG Files');
                                                            $("#my-button-1").attr('disabled', false);
                                                            $("#my-button-1").text('Save Documents');
                                                        }else if(res == 'stamp error'){
                                                            alert('System Encountered an Error. Please Try Again');
                                                            $("#my-button-1").attr('disabled', false);
                                                            $("#my-button-1").text('Save Documents');
                                                            window.location = '/add-client';
                                                        }else if(res == 'error'){
                                                            alert('System Encountered an Error. Please Try Again');
                                                            $("#my-button-1").attr('disabled', false);
                                                            $("#my-button-1").text('Save Documents');
                                                            window.location = '/add-client';
                                                        }else if($.isNumeric(res)){
                                                            alert('Documents have been uploaded Successfully. Click OK to Continue');
                                                            addClient(res);
                                                        }
                                                    }
                                                });
                                                return false;
                                            }
                                        }
                                    }
                                ],
                                open: function() {
                                    $(this).parent().css({'top': 120, 'margin-left' : 'auto', 'margin-right' : 'auto'});
                                },
                                close: function (){
                                    $("#add-client").text("Save").attr('disabled', false).css('backgroundColor', 'royalblue');
                                }
                            });
                    }else{
                        addClient(0);
                    }
                }else{
                    alert(results);
                    $("#add-client").text("Save").attr('disabled', false).css('backgroundColor', 'royalblue');
                }
            });
        });

        function verifyClient(){
            if(!$("input[name = 'category']:checked").val()){
                return 'Please Select Client Category to Proceed';
            }else if($('#nms-req').val() == 1 && $('input[name = nms]').val() == ''){
                return 'Please fill out Client Name(s) to Proceed';
            }else if($('#pn-req').val() == 1 && $('input[name = pn]').val() == ''){
                return 'Please fill out Client Phone number to easily contact Client';
            }else if($('#email-req').val() == 1 && $('input[name = email]').val() == ''){
                return 'Please fill out Client Email to easily contact Client';
            }else if($('#idn-req').val() == 1 && $('input[name = idn]').val() == ''){
                return 'Please fill out Client ID/Passport';
            }else if($('#kra-req').val() == 1 && $('input[name = kra]').val() == ''){
                return 'Please fill out Client PIN Number';
            }else if($('#dob-req').val() == 1 && $('input[name = dob]').val() == ''){
                return 'Please fill out Client Date of Birth';
            }else if($('#address-req').val() == 1 && $('input[name = address]').val() == ''){
                return 'Please fill out Client Address Details';
            }else if($('#box-req').val() == 1 && $('input[name = box]').val() == ''){
                return 'Please fill out Client P.O Box Details';
            }else if($('#occupation-req').val() == 1 && $('input[name = occupation]').val() == ''){
                return 'Please fill out Client Occupation Details';
            }else if($('#office_tel-req').val() == 1 && $('input[name = office_tel]').val() == ''){
                return 'Please fill out Client Alternative Tel Number';
            }else if($('#alt_email-req').val() == 1 && $('input[name = alt_email]').val() == ''){
                return 'Please fill out Client Alternative Email';
            }else{
                return 'Good';
            }

        }



        function addClient(id){
            var ulr_link = '/add-client/'+id;
            var datas = $('#target-form').serializeArray();
            $.ajax({
                type: 'POST',
                /*url: "{--{ route('post-clients')}--}",*/
                url: ulr_link,
                data: datas,

                success: function (res) {
                    if(res == 'client exist'){
                        alert('Client Already Exists');
                        $("#add-client").text("Save & Continue").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }else if($.isNumeric(res)){
                        //alert('Client Account Created Successfully');
                        window.location = '/client/' + res + '?type=summary';
                    }else if(res == 'empty sales type'){
                        alert('Please select how the Client was Introduced to Us. Either Direct or Through an Agent');
                        $("#add-client").text("Save & Continue").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }
                    else{
                        //alert(res);
                        alert('System encountered an Error and is unable to process your Request');
                        $("#add-client").text("Save & Continue").attr('disabled', false).css('backgroundColor', 'royalblue');
                    }

                },
                error: function (res) {
                    //alert(res);
                }
            });
        }

    </script>
@endsection

{{--System Hints Page--}}
@section('modal_heading')
    <h1>New Client(s)</h1>
@endsection
@section('modal_title')
    <h3><u>Add New Client Dashboard</u></h3>
    <b>
        <ol>
            <li>Add A Single client in the System</li>
            <li>Add an Alternative Contact Person to a Client</li>
            <li>Upload a List of Clients</li>
        </ol>
    </b>
@endsection


