
<div class="breadcrumb">
    <h1>Xcare Lead GHL Set up </h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
    </ul>
</div>
<div class="widget-body">
    <div class="portlet-body">
                        
    <?php echo (isset($successMsg) && $successMsg!='') ? "<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button>".base64_decode($successMsg)."</div>" : '' ; ?>
    <?php echo (isset($failedMsg) && $failedMsg!='') ? "<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>".base64_decode($failedMsg)."</div>" : '' ?>
   
    <div id="status_msg" style="display: none;"><div class="alert alert-success"><button data-dismiss='alert' class='close' type='button'>×</button>&nbsp;</div></div>
    <div id="alert_msg" style="display: none;"><div class="alert alert-danger"><button data-dismiss='alert' class='close' type='button'>×</button>&nbsp;</div></div>

    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">GHL API Setting</div>
                <form action="" id='frm-ghlsetting' method="post" enctype="multipart/form-data">
                    
                    <div class="row">
                        <div class="col-md-6 form-group mb-3">
                            <label for="ghl_clientId">Client ID <span class="asterisk">*</span></label>
                            <input type="text" name="ghl_clientId" class="form-control" id="ghl_clientId" value="676d49fd42a77accd78272bf-m55b2clr">
                            <div class="invalid-tooltip" id="msg_ghl_clientId"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">

                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="ghl_client_secret">Client Secret<span class="asterisk">*</span></label>
                            <input type="text" name="ghl_client_secret" class="form-control" id="ghl_client_secret" value="68fad80d-2635-4927-b768-7d4a7879e59f">
                            <div class="invalid-tooltip" id="msg_ghl_client_secret"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">

                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="ghl_code">Code </label>
                            <!-- <span class="asterisk">*</span> -->
                            <textarea id="ghl_code" name="ghl_code" class="form-control" rows="7" cols="50"></textarea>
                            <div class="invalid-tooltip" id="msg_ghl_code"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            
                        </div>
                        <div class="col-md-6">
                            <input type="submit"  class="btn btn-primary" id="get_code" name="get_code" value="Get Code">
                            &emsp;&emsp;
                            <input type="submit" class="btn btn-primary" id="get_token" name="get_token" value="Get Token">

                            <!-- <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Get Token"> -->
                        </div>

                        <!-- <div class="col-md-6">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Get Token">
                        </div> -->
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    $( "#frm-ghlsetting" ).on( "submit", function(e) {
    e.preventDefault();
    var btn= $(this).find("input[type=submit]:focus").val();
    if(btn == 'Get Code'){
        var ghl_clientId = $("#ghl_clientId").val();
        var ghl_client_secret = $("#ghl_client_secret").val();

        if($("#ghl_clientId").val()==''){
            $("#ghl_clientId").addClass('error');
            $("#ghl_clientId").focus();
            $("#msg_ghl_clientId").html('Please input clientId').show();
            return false;
        }else if($("#ghl_client_secret").val()==''){
            if(ghl_clientId !=''){
                $("#ghl_clientId").removeClass('error');
                $("#msg_ghl_clientId").hide();
            }

            $("#ghl_client_secret").addClass('error');
            $("#ghl_client_secret").focus();
            $("#msg_ghl_client_secret").html('Please input client secret').show();
            return false;
        }else{
            if(ghl_client_secret !=''){
                $("#ghl_client_secret").removeClass('error');
                $("#msg_ghl_client_secret").hide();
            }

            $(".loader-bubble").show();
            $.ajax({      
                url: "xcare_ghl_lead_setup.php",
                data: { 
                    act:"getcode",
                    ghl_clientId:ghl_clientId,ghl_client_secret:ghl_client_secret,
                },
                method: 'POST',
                success: function(data){
                    $(".loader-bubble").hide();
                    $("#alert_msg").hide();
                    $("#alert_msg").html(data);
                }
            });
        } 
    }

    if(btn == 'Get Token'){
        var ghl_clientId = $("#ghl_clientId").val();
        var ghl_client_secret = $("#ghl_client_secret").val();
        var ghl_code = $("#ghl_code").val();
        if($("#ghl_clientId").val()==''){
            $("#ghl_clientId").addClass('error');
            $("#ghl_clientId").focus();
            $("#msg_ghl_clientId").html('Please input clientId').show();
            return false;
        }else if($("#ghl_client_secret").val()==''){
            if(ghl_clientId !=''){
                $("#ghl_clientId").removeClass('error');
                $("#msg_ghl_clientId").hide();
            }
            $("#ghl_client_secret").addClass('error');
            $("#ghl_client_secret").focus();
            $("#msg_ghl_client_secret").html('Please input client secret').show();
            return false;
        }else if($("#ghl_code").val()==''){
            if(ghl_client_secret !=''){
                $("#ghl_client_secret").removeClass('error');
                $("#msg_ghl_client_secret").hide();
            }

            $("#ghl_code").addClass('error');
            $("#ghl_code").focus();
            $("#msg_ghl_code").html('Please input code').show();
            return false;
        }else{
            if(ghl_code !=''){
                $("#ghl_code").removeClass('error');
                $("#msg_ghl_code").hide();
            }

            $(".loader-bubble").show();
            $.ajax({      
                url: "xcare_ghl_lead_setup.php",
                data: { 
                    act:"GHLaddcodetoken",
                    ghl_clientId:ghl_clientId,ghl_client_secret:ghl_client_secret,ghl_code:ghl_code,
                },
                method: 'POST',
                success: function(msg){

                    if(msg=='success'){
                        $(".loader-bubble").hide();
                        /*$("#status_msg").show();
                        $("#status_msg").html("<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button> Updated successfully...</div>");
                        hideMessage();*/
                        $.toast({
                            //heading: 'Positioning',
                            heading: '</em>',
                            text: 'Xcare Rewards GHL Setting updated successfully...',
                            position: 'top-right',
                            stack: false,
                            showHideTransition: 'slide',
                            icon: 'success'
                        });
                        $("#ghl_code").val('');
                        //window.location='gotosetting.php';
                    }else{
                        $(".loader-bubble").hide();
                        //$("#err_msg").html(msg);
                        /*$("#alert_msg").show();
                        $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>"+msg+"</div>");
                        hideMessage();*/

                        $.toast({
                            //heading: 'Positioning',
                            heading: '</em>',
                            text: msg,
                            position: 'top-right',
                            stack: false,
                            showHideTransition: 'slide',
                            icon: 'error'
                        });

                    }
                }
            });
        }
    }

});
</script>