<script type="text/javascript">
$(document).ready(function(){
	
	
});
function validate_product(){

	var plan_product = $("#plan_product");var planproductmsg = $("#msg_plan_product");
    var priceing_modal = $("#priceing_modal");var pricemodalmsg = $("#msg_priceing_modal");
    var plan_price = $("#plan_price");var plan_pricemsg = $("#msg_plan_price");
    var priceing_type = $("#priceing_type");var priceing_typemsg = $("#msg_priceing_type");
	var billing_period =$("#billing_period");var billing_periodmsg = $("#msg_billing_period");
    var plan_name =$("#plan_name");var plan_namemsg = $("#msg_plan_name");


    if(plan_product.val()=='')
    {
        plan_product.addClass('error');
        plan_product.focus();
        planproductmsg.html('Please select product name').show();
        return false;
	}else
    {
        plan_product.removeClass('error');
        planproductmsg.hide();
    }


    if(priceing_modal.val()=='')
    {
        priceing_modal.addClass('error');
        priceing_modal.focus();
        pricemodalmsg.html('Please select price modal').show();
        return false;
    }else
    {
        priceing_modal.removeClass('error');
        pricemodalmsg.hide();
    }

    if(plan_price.val()=='')
    {
        plan_price.addClass('error');
        plan_price.focus();
        plan_pricemsg.html('Please enter price').show();
        return false;
    }else
    {
        plan_price.removeClass('error');
        plan_pricemsg.hide();
    }

    if(priceing_type.val()=='')
    {
        priceing_type.addClass('error');
        priceing_type.focus();
        priceing_typemsg.html('Please enter price').show();
        return false;
    }else
    {
        priceing_type.removeClass('error');
        priceing_typemsg.hide();
    }

    if(billing_period.val()=='')
    {
        billing_period.addClass('error');
        billing_period.focus();
        billing_periodmsg.html('Please select billing period').show();
        return false;
    }else
    {
        billing_period.removeClass('error');
        billing_periodmsg.hide();
    }

    if(plan_name.val()=='')
    {
        plan_name.addClass('error');
        plan_name.focus();
        plan_namemsg.html('Please select plan').show();
        return false;
    }else
    {
        plan_name.removeClass('error');
        plan_namemsg.hide();
    }

    return true;
}

</script>
<div class="breadcrumb">
    <h1>Subscription Edit</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Edit Subscription</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">Add/Debit Credit points</div>
                <input type="text" hidden name="subs_id" class="form-control" id="subs_id" value="<?php echo (isset($subs_id)) ? $subs_id : 0; ?>">
                <input type="text" hidden name="customer" class="form-control" id="customer" value="<?php echo (isset($stripeCustomerId)) ? $stripeCustomerId : 0; ?>">
                <input type="text" hidden name="customer" class="form-control" id="subscribedID" value="<?php echo (isset($subscribedId)) ? $subscribedId : 0; ?>">
                <form action="" id='frm-creditpoints' method="post" enctype="multipart/form-data">
                    
                    <div class="row">
                        <div class="col-md-6 form-group mb-3">
                            <label for="total_balance">Total Balance <span class="asterisk">*</span></label>
                            <input type="text" disabled name="total_balance" class="form-control" id="total_balance" value="<?php echo (isset($creditbalance)) ? $creditbalance : 0; ?>">
                            <div class="invalid-tooltip" id="msg_total_balance"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            
                        </div>
                        
                        <div class="col-md-6 form-group mb-3">
                            <label for="add_debit_amount">Add/Debit Amount<span class="asterisk">*</span></label>
                            <input type="text" name="add_debit_amount" class="form-control" id="add_debit_amount" value="">
                            <div class="invalid-tooltip" id="msg_add_debit_amount"></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="add_credit" name="add_credit" value="Add Credit">
                            &emsp;&emsp;
                            <input type="submit" class="btn btn-primary" id="debit_credit" name="debit_credit" value="Debit Credit Balance">

                            <!-- <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>

<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">Update Subscription</div>
                <form action="" id='frm-updatesubs' method="post" enctype="multipart/form-data">
                    
                    <div class="row">
                        <div class="col-md-6 form-group mb-3" id="applied_coupon">
                            <label for="total_balance">Applied Coupon </label>
                            <input type="text" disabled name="applied_coupon" class="form-control" id="applied_coupon" value="<?php echo (isset($appliedCoupon)) ? $appliedCoupon : ''; ?>">
                            <div class="invalid-tooltip" id="msg_total_balance"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            
                        </div>
                        
                        <div class="col-md-6 form-group mb-3">
                            <label for="add_debit_amount">Add/Update Coupon(add coupon id)<span class="asterisk">*</span></label>
                            <input type="text" name="addupdate_coupon" class="form-control" id="addupdate_coupon" value="">
                            <div class="invalid-tooltip" id="msg_addupdate_coupon"></div>
                            <input type="button"  class="btn btn-primary" id="updatecoupon" name="updatecoupon" value="Apply Coupon">
                        </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_subs_details" name="get_subs_details" value="Retrieve Subscription">
                            &emsp;&emsp;
                            <!-- <input type="submit" class="btn btn-primary" id="debit_credit" name="debit_credit" value="Debit Credit Balance"> -->

                            <!-- <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>

<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">Add Trial</div>
                <form action="" id='frm-addtrial' method="post" enctype="multipart/form-data">
                    
                    <div class="row">
                        <div class="col-md-6 form-group mb-3" id="div_addtrial_date">
                            <label for="add_trial_date">Trial End Date <span class="asterisk">*</span></label>
                            <input type="date" class="form-control" id="add_trial_date" name="add_trial_date" >
                            <div class="invalid-tooltip" id="msg_add_trial_date"></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="add_trial" name="add_trial" value="Add Trial">
                            &emsp;&emsp;
                            <!-- <input type="submit"  class="btn btn-primary" id="start_subscription" name="start_subscription" value="Start Subscription"> -->
                            &emsp;&emsp;
                        </div>

                        
                    </div>
                </form>
            </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">Cancel Subscription</div>
                <form action="" id='frm-cancelsubs' method="post">
                    <div class="row">
                        <div class="col-md-12">
                           <p>Clicking the button below will immediately cancel the subscription in Stripe (if active) and revert the user to the Basic Plan.</p>
                        </div>
                        <div class="col-md-6">
                            <input type="button" class="btn btn-danger" id="btn_cancel_subscription" value="Cancel Subscription & Revert to Basic">
                            <div id="cancel_msg" style="margin-top:10px;"></div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="./assets/js/jquery.multiselect.js"></script>
<link rel="stylesheet" href="./assets/styles/css/jquery.multiselect.css">

<link rel="stylesheet" href="./assets/styles/css/jquery.toast.css">
<script src="./assets/js/jquery.toast.js"></script>

<link href="./assets/styles/css/select2.min.css" rel="stylesheet" />
<script src="./assets/js/select2.min.js"></script>

<script type="text/javascript">
   
    var clinic_data = "<?php echo  $productDetail['subscription_product_clinic_id'];?>";
    if(clinic_data !== '')
    {
        //make array
        var dataarray=clinic_data.split(",");
        $("#clinics").val(dataarray);
        $("#clinics").multiselect("refresh");    
    }
    

    $('#clinics').multiselect({
        columns: 1,
        placeholder: 'Select clinic',
        search: true
    });

    $( "#frm-creditpoints" ).on( "submit", function(e) {
      e.preventDefault();
      
      var btn= $(this).find("input[type=submit]:focus").val();
      if(btn == 'Add Credit')
      {
            var add_debit_amount = $("#add_debit_amount").val();
            var customer         = $("#customer").val();
            var subs_id          = $("#subs_id").val();

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

                $(".loader-bubble").show();
                $.ajax({      
                        url: "subscriptionedit.php",
                        data: 
                        { 
                            act:"adddebitcredit",
                            add_debit_amount:add_debit_amount,customer:customer,subs_id:subs_id,
                        },
                        method: 'POST',
                        success: function(data){
                            $(".loader-bubble").hide();
                            $("#alert_msg").hide();
                            $("#alert_msg").html(data);
                            $.toast({
                                //heading: 'Positioning',
                                heading: '</em>',
                                text: data,
                                position: 'top-right',
                                stack: false,
                                showHideTransition: 'slide',
                                icon: 'success'
                            });
                        }
                    });
            } 
      }
      if(btn == 'Debit Credit Balance')
      {
            var add_debit_amount = $("#add_debit_amount").val();
            var customer         = $("#customer").val();
            var subs_id          = $("#subs_id").val();

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

                $(".loader-bubble").show();
                $.ajax({      
                        url: "subscriptionedit.php",
                        data: 
                        { 
                            act:"debitcredit",
                            add_debit_amount:add_debit_amount,customer:customer,subs_id:subs_id,
                        },
                        method: 'POST',
                        success: function(data){
                            $(".loader-bubble").hide();
                            $("#alert_msg").hide();
                            $("#alert_msg").html(data);
                            $.toast({
                                //heading: 'Positioning',
                                heading: '</em>',
                                text: data,
                                position: 'top-right',
                                stack: false,
                                showHideTransition: 'slide',
                                icon: 'success'
                            });
                        }
                    });
            } 
      }
    });

    $( "#frm-updatesubs" ).on( "submit", function(e) {
      e.preventDefault();
      
      var btn= $(this).find("input[type=submit]:focus").val();
      if(btn == 'Retrieve Subscription')
      {
            var customer         = $("#customer").val();
            var subscribedID          = $("#subscribedID").val();
            $.ajax({      
                    url: "subscriptionedit.php",
                    data: 
                    { 
                        act:"retrievesubs",
                        subscribedID:subscribedID,
                    },
                    method: 'POST',
                    success: function(data){
                        $(".loader-bubble").hide();
                        $("#alert_msg").hide();
                        $("#alert_msg").html(data);
                        $.toast({
                            //heading: 'Positioning',
                            heading: '</em>',
                            text: data,
                            position: 'top-right',
                            stack: false,
                            showHideTransition: 'slide',
                            icon: 'success'
                        });
                    }
            }); 
      }
    });

    $("#updatecoupon").on( "click", function(e) {
        e.preventDefault();
        var couponname = $("#addupdate_coupon").val();
        var subsID = $("#subscribedID").val();
        var subs_id = $("#subs_id").val();
        $.ajax({      
            url: "subscriptionedit.php",
            data: 
            { 
                act:"addcoupon",
                couponname:couponname,subsID:subsID,subs_id:subs_id,
            },
            method: 'POST',
            success: function(data){
                $(".loader-bubble").hide();
                $("#alert_msg").hide();
                $("#alert_msg").html(data);
                $.toast({
                    //heading: 'Positioning',
                    heading: '</em>',
                    text: data,
                    position: 'top-right',
                    stack: false,
                    showHideTransition: 'slide',
                    icon: 'success'
                });
            }
        });
    });

    var today = new Date().toISOString().split('T')[0];
    document.getElementsByName("add_trial_date")[0].setAttribute('min', today);

    $( "#frm-addtrial" ).on( "submit", function(e) {
        e.preventDefault();
        var trialenddate = $("#add_trial_date").val();
        var subsID = $("#subscribedID").val();
        var subs_id = $("#subs_id").val();
        $.ajax({      
            url: "subscriptionedit.php",
            data: 
            { 
                act:"addtrial",
                trialenddate:trialenddate,subsID:subsID,subs_id:subs_id,
            },
            method: 'POST',
            success: function(data){
                $(".loader-bubble").hide();
                $("#alert_msg").hide();
                $("#alert_msg").html(data);
                $.toast({
                    //heading: 'Positioning',
                    heading: '</em>',
                    text: data,
                    position: 'top-right',
                    stack: false,
                    showHideTransition: 'slide',
                    icon: 'success'
                });
            }
        });
    });

    $("#btn_cancel_subscription").click(function(){
        if(confirm("Are you sure you want to cancel this subscription immediately?")){
            var subs_id = $("#subs_id").val();
            var subscribedID = $("#subscribedID").val(); // Stripe Sub ID
            
            $(".loader-bubble").show();
            $.ajax({
                url: "subscriptionedit.php",
                type: "POST",
                data: {
                    act: "cancel_subscription",
                    subs_id: subs_id,
                    stripe_sub_id: subscribedID
                },
                success: function(response){
                    $(".loader-bubble").hide();
                     $("#cancel_msg").html(response);
                     if(response.includes("Success")){
                         $("#cancel_msg").css("color", "green");
                         setTimeout(function(){ location.reload(); }, 2000);
                     } else {
                         $("#cancel_msg").css("color", "red");
                     }
                }
            });
        }
    });
</script>