@extends('layouts.app')

@section('content')
<style>
    .file {
        visibility: hidden;
        position: absolute;
    }
</style>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">

            <nav aria-label="breadcrumb">
                <ol class="breadcrumb pb-3">
                  <li class="breadcrumb-item"><a href="{{ route('home') }}">Home</a></li>
                  <li class="breadcrumb-item active" aria-current="page">Surat Wakil</li>
                </ol>
              </nav>

            <div class="card">
                <div class="card-body">
                    <div class="text-center">
                        <h5 class="text-uppercase">masukkan nombor kad pengenalan</h5>
                        <form name="frmsearch" onsubmit="verifynokp()" action="{{ route('khazanah.suratwakil') }}" method="GET">
                            <div class="row justify-content-center">
                                <div class="col-md-9">
                                    <div class="input-group my-3">
                                        <input type="text" class="form-control" id="noic" name="noic" placeholder="No Kad Pengenalan Pelanggan" aria-label="No Kad Pengenalan Pelanggan" autocomplete="off" value="{{$search == false ? '' : $noic ?? ''}}" aria-describedby="button-addon2">
                                        <div class="input-group-append">
                                            <select class="form-select" id="customerIDType" name="customerIDType" class="form-control">
                                                <option value="nokp" {{ $idtype == 'nokp' ? 'selected' : ''}}>Kad Pengenalan</option>
                                                <option value="passport" {{ $idtype == 'passport' ? 'selected' : ''}}>Passport</option>
                                            </select>
                                        </div>
                                        <div class="input-group-append">
                                            <button class="btn btn-outline-primary" type="submit" id="button-addon2">Cari</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                   
                </div>
            </div>
            <!-- Modal No KP -->
            
            <!-- Modal Passport -->
            
        </div>
    </div>
</div>
<script>
    var api_url = "<?php echo $api_url ?>";
    var search = "<?php echo $search ?>";
    var type_id = "<?php echo $idtype ?>";
    $(document).ready(function(){
        $('#passport_tarikh_lahir').datepicker({
            format: 'dd/mm/yyyy',
            endDate: new Date()
        });
        var noic = $('#noic').val();
        identifyIC(noic);

        if(search == true){
            Swal.fire({
                icon: 'info',
                title: 'Pelanggan Tiada Dalam Rekod Cawangan',
            });
            event.preventDefault();
            return false;
        }

    });   

    function identifyIC(noic){
        if(noic.match(/^(\d{2})(\d{2})(\d{2})-?\d{2}-?\d{4}$/)) {
            var year = noic.slice(0,2);
            var month = noic.slice(2,4);
            var day = noic.slice(4,6);
            var nl = noic.slice(6,8);
            var last_ic = noic.slice(8,12);

            var now = new Date().getFullYear().toString();

            var decade = now.substr(0, 2);
            if (now.substr(2,2) > year) {
                year = parseInt(decade.concat(year.toString()), 10);
            }

            if (parseInt(last_ic,10)%2 != 0){
                var jantina = 'Lelaki';
            }
            else{
                var jantina = 'Perempuan';
            }

            var johor_ic = ['01','21','22','23','24'];
            var kedah_ic = ['02','25','26','27'];
            var kelantan_ic = ['03','28','29'];
            var melaka_ic = ['04','30'];
            var negeri_ic = ['05','31','59'];
            var pahang_ic = ['06','32','33'];
            var penang_ic = ['07','34','35'];
            var perak_ic = ['08','36','37','38','39'];
            var perlis_ic = ['09','40'];
            var selangor_ic = ['10','41','42','43','44'];
            var terengganu_ic = ['11','45','46'];
            var sabah_ic = ['12','47','48','49'];
            var sarawak_ic = ['13','50','51','52','53'];
            var kl_ic = ['14','54','55','56','57'];
            var labuan_ic = ['15','58'];
            var putrajaya_ic = ['16'];
            var unknown_ic = ['82'];

            if(johor_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Johor';
            }else if(kedah_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Kedah';
            }else if(kelantan_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Kelantan';
            }else if(melaka_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Melaka';
            }else if(negeri_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Negeri Sembilan';
            }else if(pahang_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Pahang';
            }else if(penang_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Pulau Pinang';
            }else if(perak_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Perak';
            }else if(perlis_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Perlis';
            }else if(selangor_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Selangor';
            }else if(terengganu_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Terengganu';
            }else if(sabah_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Sabah';
            }else if(sarawak_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Sarawak';
            }else if(kl_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Kuala Lumpur';
            }else if(labuan_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Labuan';
            }else if(putrajaya_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Putrajaya';
            }else if(unknown_ic.indexOf(nl)>=0){
                var negeri_lahir = 'Negeri Tidak Diketahui';
            }
            var date = new Date(year, (month - 1), day, 0, 0, 0, 0);
            var tarikh_lahir_test = date.getFullYear() + '-' + month + '-' + day;

            var count_leap_year = countLeapYears(date.getFullYear(),month,day) - 1;

            var start = new Date(tarikh_lahir_test),
                end   = new Date(),
                diff  = new Date(end - start),
                days  = ((diff/1000/60/60/24)- count_leap_year)/365;

            var age = parseInt(days);
            $('#age').val(age);
            var tarikh_lahir = day + '/' + month + '/' + date.getFullYear();
            $('#jantina').val(jantina);
            $('#tarikh_lahir').val(tarikh_lahir);
            $('#negeri_lahir').val(negeri_lahir);
        }
    }

    var countLeapYears = function(y,m,d){
        var yearNow = new Date().getFullYear(),
            yearThen = y,
            beginYear = 0,
            endYear = 0,
            leapYearCount = 0;

        var isLeapYear = function(year){
            return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
        }

        if(yearNow < y){
            beginYear = yearNow;
            endYear = y;
        }else if(yearNow > y){
            beginYear = y;
            endYear = yearNow;
        }else if(yearNow == y){
            beginYear = y;
            endYear = y;
        }

        for(i = beginYear; i <= endYear; i++){
            if(isLeapYear(i)){
                leapYearCount++;
            }
        }

        return leapYearCount;
    }
    var regex = /^[a-zA-Z0-9]+$/g;

    function verifynokp(){
        var id_type = $('#customerIDType').val();
        var nokp = $('#noic').val();
        if(id_type=='nokp'){
            if($.isNumeric(nokp) == false){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Sila Masukkan Nombor Sahaja Pada No KP Pelanggan'
                });
                event.preventDefault();
                return false;
            }
            if(nokp.length != 12){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Sila Periksa No KP Pelanggan'
                });
                event.preventDefault();
                return false;
            }
        }else if(id_type=='passport'){
            if(nokp.length < 5){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Sila Periksa Passport Pelanggan'
                });
                event.preventDefault();
                return false;
            }
            if(nokp.indexOf(' ') > 0){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Sila Masukkan Passport Tanpa Ruang \'Kosong\' \n e.g(AP1111111)'
                });
                event.preventDefault();
                return false;
            }
            if(regex.test(nokp) == false){
                Swal.fire({
                    icon: 'error',
                    title: 'Amaran!',
                    text: 'Sila Periksa Passport Pelanggan'
                });
                event.preventDefault();
                return false;
            }
        }
    }
</script>
@endsection
