
    $(document).ready(function() {
            "use strict"
            if (sessionStorage.getItem("user_data_viewed") === "1") {
                $('#show_hide_icon').removeClass('fa-eye-slash');
                $('#show_hide_icon').addClass('fa-eye');
            }

            if (sessionStorage.getItem("user_data_viewed") === "0") {
                $('#show_hide_icon').removeClass('fa-eye');
                $('#show_hide_icon').addClass('fa-eye-slash');
            }

            show_hide_textbox();

            $("#manage_client_show_hide_password a").on('click', function(event) {
                common_helper.globalPasswordView('#manage_client_show_hide_password',event);
            });

            common_helper.formValidate('#payment-report_form',
                {'card_number': 'required', 'expiry': 'required', 'cvv': 'required'},
                {
                    'card_number': {required: "Please enter your card number"},
                    'expiry': {required: "Please enter expire date"},
                    'cvv': {required: "Please enter your card code"}
                }
            );
    });


    $('#client_form').validate({
            rules: {
            'creditreport[first_name]': 'required',
            'creditreport[last_name]': 'required',
            'creditreport[birth_date]': 'required',
            'creditreport[street_no]' : 'required',
            'creditreport[street_name]' : 'required',
            'creditreport[city]' : 'required',
            'creditreport[state]' : 'required',
            'creditreport[zip_code]' : 'required',
            'creditreport[phone]' : 'required',
            'creditreport[ssn]' : {'required': true,'minlength':4,'maxlength':4},
            'creditreport[password]': 'required',
            'creditreport[credit_report_company_type]': 'required'
        },
            messages: {
                                'creditreport[first_name]': {
                                required: "Please enter your first name"
                            },
                                'creditreport[last_name]': {
                                required: "Please enter your last name"
                            },
                                'creditreport[birth_date]': {
                                required: "Please enter your birth date",
                            },
                                'creditreport[street_no]': {
                                required: "Please enter your street number"
                            },
                                'creditreport[street_name]': {
                                required: "Please enter your street name"
                            },
                                'creditreport[state]': {
                                required: "Please enter your state"
                            },
                                'creditreport[city]': {
                                required: "Please enter your city"
                            },
                                'creditreport[zip_code]': {
                                required: "Please enter your zip code"
                            },
                                'creditreport[phone]': {
                                required: "Please enter your zip code"
                            },
                                'creditreport[ssn]': {
                                required: "Please enter last 4 digit of your ssn",
                                minlength:"Please enter last 4 digit of your ssn",
                                maxlength:"Please enter last 4 digit of your ssn"
                            },
                                'creditreport[password]': {
                                required: "Please enter your password"
                            },
                                'creditreport[credit_report_company_type]': {
                                required: "Please enter your source type"
                            }
                     },
            highlight: function (element) {
                         $(element).parent().addClass("has-error");
                     },
            submitHandler: function (form) {
                                form.submit();
                            }
        });

    $('input[name=isReceiveNotificationEmail]').click(function () {
                $('#alternate-email').attr('required', false);
                $('#alternate-email').attr('disabled', true);
                if ($(this).prop('checked')) {
                $('#alternate-email').attr('required', true);
                $('#alternate-email').attr('disabled', false);
            }
    });

    $('input[name=is_letterstream_return_address]').click(function () {
                        $('#client-city').attr('required', false);
                        $('#client-zip-code').attr('required', false);
                        $('#client-address').attr('required', false);
                        $('#client-state').attr('required', false);
                        $('#client-country').attr('required', false);
                        $('.letter-stream-check').removeClass('required')
                        // $('#alternate-email').attr('disabled',true);
                        if ($(this).prop('checked')) {
                        $('#client-city').attr('required', true);
                        $('#client-zip-code').attr('required', true);
                        $('#client-address').attr('required', true);
                        $('#client-state').attr('required', true);
                        $('.letter-stream-check').addClass('required')
                        //$('#alternate-email').attr('disabled',false);
                    }
            });

    function firstTimeLoginUserUpdate(){
                $('#first_time_login_view_modal').modal('show');
                $('#btn_first_time_login').click((e)=>{
                $(`#first_time_login_view_modal`).modal('hide');
                var scrollpanel =  $("#upload-document-panel").offset().top;
                $(window).scrollTop(scrollpanel);
                $('#upload-document-panel').focus();
        });
    }

    const showSubscription=()=> {
                    $('#recurring_view_modal').modal('show');
                    $('#btn_recurring').click((e)=>{
                            $('#recurring_view_modal').modal('hide');
                            $('#payment-report-modal').modal('show');
                    });
    }

    $("#show_hide_answer1 a").on('click', function(event) {
             common_helper.globalPasswordView('#show_hide_answer1',event);
    })
    $("#show_hide_answer2 a").on('click', function(event) {
        common_helper.globalPasswordView('#show_hide_answer2',event);
    })
    $("#show_hide_answer3 a").on('click', function(event) {
         common_helper.globalPasswordView('#show_hide_answer3',event);
    })

    const birthDateCompleted=()=>{
            let birth_date=$("[name='creditreport[birth_date]']").val();
            if(birth_date.length===2){
                         birth_date+='/';
            }
            else if(birth_date.length===5){
                    birth_date+='/';
            }
       $("[name='creditreport[birth_date]']").val(birth_date);
    }

    const sourceTypeOnchange=(current_source_type)=>{

            let selected_value =  $("select[name='credit_report_company_type']").val();

            let source_name = current_source_type === 1 ? 'SmartCredit' : "Identity IQ" ;

            sourceTypeConfigUnset();

            if( selected_value !== current_source_type){

                let html = 'Set up your '+ source_name+ ' first, use the same email and password you used to set up your credit zombies account';
                $('#common_message_text').text(html);
                sourceTypeConfigSetup(current_source_type);
                $('#common_confirmation_modal').modal('show');

            }
    //$("select[name='source_type']").val(current_source_type);
    }

    const showFirstTimeLoginVideoModalView=()=> {

          $('#first_time_login_video_modal').modal('show');

    }

    const showFirstTimeLoginModalView=()=> {
            sessionStorage.setItem('video_shown','1');
            $('#first_time_login_video_modal').modal('hide');
            $('#first_time_login_modal').modal('show');

    }

    const hideModalView=()=>{

        $('#first_time_login_modal').modal('hide');
        firstTimeLoginUserUpdate();
    }

    const hideFirstTimeLoginModalView=(providers)=>{

        $('#first_time_login_modal').modal('hide');

        let provider = $('#report_provider').val();
        let go_to_url = '';

        go_to_url = providers[parseInt(provider)];

        window.open(go_to_url,'_blank') ;

        firstTimeLoginUserUpdate();
    }

    const sourceTypeConfigUnset = () => {
            $('#ok-btn').remove()
            $('#common_confirmation_modal .modal-footer button').removeClass('btn2')
            $('#common_confirmation_modal .modal-footer button').removeAttr('onclick')
    }

    const sourceTypeConfigSetup = (current_source_type) => {
            $('#common_confirmation_modal .modal-footer button').addClass('btn2')
            $('#common_confirmation_modal .modal-footer button').attr('onclick',`setPrevious(${current_source_type})`)
            $('#common_confirmation_modal .modal-footer').prepend(sourceTypeSelectBtn())
    }

    const setPrevious = (value) => {
          $("select[name='credit_report_company_type']").val(value);
    }

    const sourceTypeSelectBtn = (value) =>{

         let html = `<button id="ok-btn" type="button" class="btn btn-secondary btn2 btn-lg" data-dismiss="modal">Ok</button>`;

         return html
    }

    const client_form_submit = (e,url,userType)=>{
            $('.has-error').removeClass('has-error')
            if($('#client_form').valid()) {
                let email = $('#client_form').find('input[name="creditreport[email]"]').val();
                let password = $('[name="creditreport[password]"]').val();
                let source_type =  $("#company_type").val();
                let ssn = $('[name="creditreport[ssn]"]').val();
                let data = {
                    action: 'GET_SUBSCRIPTION_STATUS_CREDIT_REPORT_PROVIDER',
                    email: email,
                    password: password,
                    source_type: source_type,
                    ssn: ssn,
                 };

                if(userType === 3){
                    $('#client_form').submit();
                }else {
                    common_helper.globalAjaxRequestAsync(url, 'POST', data, callbackfromreportprovider);
                }
            }
            return false;
    };

    const callbackfromreportprovider =(response)=>{

                console.log('test result',response);
                if(response.status) {
                      $('#client_form').submit();
                }else {
                    sourceTypeConfigUnset()
                    let html = response.message;
                    // let html = 'Email or Password do not match in credit report provider.';
                    $('#common_message_text').text(html);
                    $('#common_confirmation_modal').modal('show');
                }
    }

    const show_hide_textbox = (e)=>{
            $return_data = common_helper.show_hide_textbox($('#show_hide_icon'),
                $('[name="creditreport[phone]"]'),$('[name="creditreport[email]"]'),
                $('[name="creditreport[first_name]"]'),$('[name="creditreport[last_name]"]'),
                $('[name="creditreport[ssn]"]'),$('[name="creditreport[birth_date]"]'),
                $('[name="creditreport[street_no]"]'),$('[name="creditreport[street_name]"]'),
                $('[name="creditreport[city]"]'),$('[name="creditreport[state]"]'),
                $('[name="creditreport[zip_code]"]')
            );

            sessionStorage.setItem('user_data_viewed',$return_data);

    }

    const file_upload = (title,image,type,nameatrr)=>{

            let encoded = encodeURIComponent(image);
            $('#pic_header').text(title);
            $('#doc_type').val(type);
            $("#uploaded_doc").attr("src",decodeURIComponent(encoded));
            $('input[id="profile_pic"]').attr('name', nameatrr);

            $('#upload_pic_modal').modal('show');
    }

    const file_delete = (title,image)=>{
                let action = 'DELETE_CLIENT_DOC_FILE';
                let url = $('[name="ajax_url"]').val();
                let html_content = `<h4>Are you sure you want to delete </h4>`;
                html_content += `<h4>`+ title + ` file?` +`</h4>`;
                const swalresponse = common_helper.swalAleart(html_content, 'warning', 450, true, '', true, true, 'Yes');
                swalresponse.then(async function (result) {
                        if (result.isConfirmed) {
                        let response = await common_helper.globalAjaxRequest(url, 'POST', {
                                                action: action,
                                                userData: image,
                                                _token: $('meta[name="csrf-token"]').attr('content')
                                        });
                            if (response.status) {
                                    Swal.fire({icon: response.type, title: response.message, width: 450,closeOnConfirm: true});
                                    window.location = $('[name="redirect_url"]').val();
                            } else {
                                 Swal.fire({icon: response.type, title: response.message, width: 450,closeOnConfirm: true});
                            }
                        }
                });
        }

