<script type="text/javascript">
$(document).ready(function(){
    /*$("#frm-clinicsetting").submit(function(e){
            if(!validate_practice()){
                e.preventDefault();
            }
            $("#status_msg").show();
            $("#status_msg").html("<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button> Updated successfully...</div>");
            $("#status_msg_td").html(msg);
            return false; 
            //hideMessage();
    });*/

    $('#frm-xcaresetting').on('submit',function (e) {
            e.preventDefault();
              $.ajax({
                type: 'post',
                url: 'xcare_default_setting.php?act=setting',
                data: $('#frm-xcaresetting').serialize(),
                success: function () {
                    $("#status_msg").show();
                    $("#status_msg").html("<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button> Updated successfully...</div>");
                    //$("#status_msg_td").html(msg);
                    hideMessage();
                }
              });
        });

    var maxField = 30; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><br/><input type="text" name="field_setting1[]" value="" placeholder="email"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="field_setting2[]" value="" placeholder="info@dmo.com" /><a href="javascript:void(0);" class="remove_button"><img src="<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/remove-icon.png" ?>"/></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1
    //<input type="text" name="field_setting2[]" value=""/>
    //Once add button is clicked
    $(addButton).click(function(){
        //Check maximum number of input fields
        if(x < maxField){ 
            x++; //Increment field counter
            $(wrapper).append(fieldHTML); //Add field html
        }
    });
    
    //Once remove button is clicked
    $(wrapper).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});


</script>
<div class="breadcrumb">
    <h1>Xcare Default Setting</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>/cliniclist.php">Xcare Default List</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="separator-breadcrumb border-top"></div>
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Xcare Default Setting</div>
                <form action="" id='frm-xcaresetting' method="post" enctype="multipart/form-data">
                    <?php  if(isset($xcaredefultDetail->setting_id) && $xcaredefultDetail->setting_id!=0) { ?>
                       <input type="hidden" id="setting_id" name="setting_id" value="<?php echo $xcaredefultDetail->setting_id ?>" />
                    <?php } ?>
                     
                    <div class="row">
                        <div>
                        </div>
                        <div class="col-md-4 mt-3 mt-md-0"  id="div_communicationprovider">
                        <!-- <label for="communicationprovider ">Communication Provider</label> -->
                        <select class="form-control" id="communicationprovider" name="communicationprovider">
                            <!-- <option value="">Select provider</option> -->
                            <?php 
                            $communiprovider = json_decode($xcaredefultDetail->setting_value);
                                ?>
                            <option value="twilio" <?php echo (isset($communiprovider) && $communiprovider->smsprovider == "twilio") ? "selected='selected'" : ""; ?>>Twilio</option>
                            <option value="goto" <?php echo (isset($communiprovider) && $communiprovider->smsprovider == "goto") ? "selected='selected'" : ""; ?>>Goto</option>
                            <option value="mango" <?php echo (isset($communiprovider) && $communiprovider->smsprovider == "mango") ? "selected='selected'" : ""; ?>>Mango</option>

                        </select>
                    </div>
                    
                        <?php

                            if(isset($xcaredefultDetail) && $xcaredefultDetail > 0)
                            {
                                //foreach ($clinicDetail as $ckey => $clinic_value) 
                                //{
                                    if(isset($xcaredefultDetail->setting_value) && $xcaredefultDetail->setting_value != '')
                                    {
                                        $xcaredefultsetting = json_decode($xcaredefultDetail->setting_value,1);

                                        foreach ($xcaredefultsetting as $skey => $settingvalue) 
                                        { if($skey != 'smsprovider'){?>
                                            
                                            <div class="col-md-8 form-group mb-3">
                                                <input type="text"  name="field_setting1[]" value="<?php echo $skey; ?>"/>&nbsp;&nbsp;&nbsp;&nbsp;
                                                <input type="text" name="field_setting2[]" value="<?php echo $settingvalue; ?>"/>
                                                
                                            </div>
                                            <div class="col-md-4 form-group mb-3">
                                            </div>
                                       <?php } }
                                    }
                                    
                                //}
                                
                            }
                        ?>
                        
                        <div class="col-md-6 form-group mb-3 field_wrapper">
                            <input type="text" name="field_setting1[]" value="" placeholder="email" />&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="text" name="field_setting2[]" value="" placeholder="info@dmo.com"/>
                            <a href="javascript:void(0);" class="add_button" title="Add field"><img src=<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/add-icon.png" ?>></a>
                        </div>

                        
                        
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row" id="div_getcode" style="display:none;">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">GoToSetting</div>
                <form action="" id='frm-gotosetting' method="post" enctype="multipart/form-data">
                    
                    <div class="row">
                        <div class="col-md-6 form-group mb-3">
                            <label for="goto_clientId">Client ID <span class="asterisk">*</span></label>
                            <input type="text" name="goto_clientId" class="form-control" id="goto_clientId">
                            <div class="invalid-tooltip" id="msg_goto_clientId"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            
                        </div>
                        
                        <div class="col-md-6 form-group mb-3">
                            <label for="goto_client_secret">Client Secret<span class="asterisk">*</span></label>
                            <input type="text" name="goto_client_secret" class="form-control" id="goto_client_secret">
                            <div class="invalid-tooltip" id="msg_goto_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="goto_code">Code </label>
                            <!-- <span class="asterisk">*</span> -->
                            <textarea id="goto_code" name="goto_code" class="form-control" rows="7" cols="50"></textarea>
                            <div class="invalid-tooltip" id="msg_goto_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">
   
   $("#communicationprovider").on('change',function(){

        var communicationprovider = $("#communicationprovider").val();

        if(communicationprovider == 'goto')
        {
            $('#div_getcode').show();
        }else{
            $('#div_getcode').hide();
        }
        
    });


    $("#frm-gotosetting").on('submit',function(e){
        e.preventDefault();
        var btn= $(this).find("input[type=submit]:focus").val();
        /*console.log(btn);
        return false;*/

        if(btn == 'Get Code')
        {
            var goto_clientId = $("#goto_clientId").val();
            var goto_client_secret = $("#goto_client_secret").val();

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

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

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

        if(btn == 'Get Token')
        {
            var goto_clientId = $("#goto_clientId").val();
            var goto_client_secret = $("#goto_client_secret").val();
            var goto_code = $("#goto_code").val();

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

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

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

                $(".loader-bubble").show();
                $.ajax({
                    url: "xcare_default_setting.php",
                    data: 
                    { 
                        act:"load",
                        goto_clientId:goto_clientId,goto_client_secret:goto_client_secret,goto_code:goto_code,
                    },
                    method: 'POST',
                    success: function(msg){

                        if(msg=='success'){
                            $(".loader-bubble").hide();
                            $('#div_getcode').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();
                            $("#goto_code").val('');
                            $("#goto_clientId").val('');
                            $("#goto_client_secret").val('');
                            window.location='xcare_default_setting.php?event_name=xcare_default';
                        }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();
                        }
                    }
                });
            }
        }
    });

</script>