<script type="text/javascript">
$(document).ready(function(){
	$("#frm-product").submit(function(e){
            if(!validate_product()){
                e.preventDefault();
            }
	});

    $( "#frm-product_allinone" ).on( "submit", function(e) {
        e.preventDefault();
        
        var plan_id = $("#plan_id").val();
        var displayplan_price = $("#displayplan_price").val();

        if($("#displayplan_price").val()=='')
        {
              $("#displayplan_price").addClass('error');
              $("#displayplan_price").focus();
              $("#msg_displayplan_price").html('Please input price').show();
              return false;
        }else{
            if(displayplan_price !='')
            {
                $("#displayplan_price").removeClass('error');
                $("#msg_displayplan_price").hide();
            }
            $(".loader-bubble").show();

            $.ajax({      
                  url: "planadd.php",
                  data: 
                  { 
                      act:"display_price",
                      plan_id:plan_id,displayplan_price:displayplan_price,
                  },
                  method: 'POST',
                  success: function(){
                      $(".loader-bubble").hide();
                      /*$("#alert_msg").hide();
                      $("#alert_msg").html(data);*/

                      window.location='planlist.php';

                  }
            });
        }
    });
        
	
});
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>Plan</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Plan</li>
    </ul>
</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 New Plan</div>
                <?php if($_REQUEST['product_mode'] != 'allinone'){ ?>
                <form action="" id='frm-product' method="post" enctype="multipart/form-data">
                    <?php if(isset($planDetail->plan_id ) && $planDetail->plan_id !=0) { ?>
                       <input type="hidden" id="plan_id" name="plan_id" value="<?php echo $planDetail->plan_id; ?>" />
                       <input type="hidden" id="plan_stripe_id" name="plan_stripe_id" value="<?php echo $planDetail->plan_stripe_id; ?>" />
                       
                    <?php } ?>
                    <div class="row">
                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_product">Product </label>
                            <select class="form-control" id="plan_product" name="plan_product">
                                <option value="">Select Product</option>
                                <?php foreach ($allProduct as $key => $value) {
                                    $product_id = $_REQUEST['product_id'];
                                    if($product_id != '')
                                    {
                                    ?>
                                        <option value="<?php echo $value->subscription_product_stripe_id;?>" <?php echo 
                                                ($product_id==$value->subscription_product_id)?"selected='selected'":""  ?> ><?php echo $value->subscription_product_name;?></option> 
                                    <?php }else{ ?>

                                        <option value="<?php echo $value->subscription_product_stripe_id;?>" <?php echo (isset($planDetail)&& $planDetail->plan_master_product_id==$value->subscription_product_id)?"selected='selected'":""  ?> ><?php echo $value->subscription_product_name;?></option>

                                    <?php } ?>
                                <?php } ?>

                            </select>
                            <div class="invalid-tooltip" id="msg_plan_product"></div>
                        </div>
                    	<div class="col-md-6 form-group mb-3">
                            <label for="priceing_modal">Priceing modal <span class="asterisk">*</span></label>
                            <select class="form-control" id="priceing_modal" name="priceing_modal">
                                <option value="standard pricing">Standard Pricing</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_priceing_modal"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_price">Price <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="plan_price" name="plan_price" value="<?php echo $planDetail->plan_amount ?>" placeholder="Enter price" pattern="^[0-9]*$">
                            <div class="invalid-tooltip" id="msg_plan_price"></div>
                        </div>
                        
                        <div class="col-md-6 form-group mb-3">
                            <label for="priceing_type">Plan type <span class="asterisk">*</span></label>
                            <select class="form-control" id="priceing_type" name="priceing_type">
                                <option value="recurring">Recurring</option>
                                <!-- <option value="one time">One time</option> -->
                            </select>
                            <div class="invalid-tooltip" id="msg_priceing_type"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="billing_period">Billing period <span class="asterisk">*</span></label>
                            <select class="form-control" id="billing_period" name="billing_period">
                                <option value="monthly" <?php echo (isset($planDetail)&& $planDetail->plan_interval=='month')?"selected='selected'":""  ?>>Monthly</option>
                                <option value="yearly" <?php echo (isset($planDetail)&& $planDetail->plan_interval=='year')?"selected='selected'":""  ?>>Yearly</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_billing_period"></div>
                        </div>

                        <!-- <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_name">Plan name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="plan_name" name="plan_name" value="<?php echo $productDetail['subscription_product_name'] ?>" placeholder="Enter plan name">
                            <div class="invalid-tooltip" id="msg_plan_name"></div>
                        </div>   -->

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_name">Plan name <span class="asterisk">*</span></label>
                            <select class="form-control" id="plan_name" name="plan_name">
                                <option value="">Select Plan</option>
                                <?php foreach ($allmasterplan as $key => $value) {?>
                                 <option value="<?php echo $value->master_plan_id;?>"  <?php echo (isset($planDetail)&& $planDetail->plan_master_plan_id==$value->master_plan_id)?"selected='selected'":""  ?> ><?php echo $value->master_plan_name;?></option> 
                                <?php } ?>

                            </select>
                            <div class="invalid-tooltip" id="msg_plan_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_title">Plan title </label>
                            <input type="text" class="form-control" id="plan_title" name="plan_title" value="<?php echo $planDetail->plan_title ?>" placeholder="Enter plan title">
                            <div class="invalid-tooltip" id="msg_plan_title"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="plan_image">Image </label>
                            <input type="file" class="form-control col-md-6" id="plan_image" name="plan_image" />
                            <?php if($planDetail->plan_img_name!='' && file_exists(DIR_UPLOAD_PLAN.$planDetail->plan_img_name)){  ?>
                                <img src="<?php echo DIR_UPLOAD_FORM_PLAN_SHOW.$planDetail->plan_img_name ?>" width="100px" />
                            <?php } ?>
                            <input type="hidden" name="hidden_logo" id="hidden_logo" value="<?php echo $planDetail->plan_img_name ?>">
                            <div class="invalid-tooltip" id="msg_plan_image"></div>
                        </div>  

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_short_desc">Plan short description </label>
                            <input type="text" class="form-control" id="plan_short_desc" name="plan_short_desc" value="<?php echo $planDetail->plan_short_desc ?>" placeholder="Enter short description">
                            <div class="invalid-tooltip" id="msg_plan_short_desc"></div>
                        </div>                
                        
                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_breif_desc">Plan breif description </label>
                            <textarea class="form-control" id="plan_breif_desc" name="plan_breif_desc" ><?php echo $planDetail->plan_breif ?></textarea>
                            <div class="invalid-tooltip" id="msg_plan_short_desc"></div>
                        </div>


                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
                <?php }else if($_REQUEST['product_mode'] == 'allinone'){ ?>
                    <form action="" id='frm-product_allinone' method="post" enctype="multipart/form-data">
                        <?php if(isset($planDetail->plan_id ) && $planDetail->plan_id !=0) { ?>
                       <input type="hidden" id="plan_id" name="plan_id" value="<?php echo $planDetail->plan_id; ?>" />
                       <input type="hidden" id="plan_stripe_id" name="plan_stripe_id" value="<?php echo $planDetail->plan_stripe_id; ?>" />
                       
                        <?php } ?>

                        <div class="row">
                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_product">Product </label>
                            <select class="form-control" id="plan_product" name="plan_product" disabled>
                                <option value="">Select Product</option>
                                <?php foreach ($allProductallinone as $key => $value) {
                                    $product_id = $_REQUEST['product_id'];
                                    if($product_id != '')
                                    {
                                    ?>
                                        <option value="<?php echo $value->subscription_product_stripe_id;?>" <?php echo 
                                                ($product_id==$value->subscription_product_id)?"selected='selected'":""  ?> ><?php echo $value->subscription_product_name;?></option> 
                                    <?php }else{ ?>

                                        <option value="<?php echo $value->subscription_product_stripe_id;?>" <?php echo (isset($planDetail)&& $planDetail->plan_master_product_id==$value->subscription_product_id)?"selected='selected'":""  ?> ><?php echo $value->subscription_product_name;?></option>

                                    <?php } ?>
                                <?php } ?>

                            </select>
                            <div class="invalid-tooltip" id="msg_plan_product"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="displayplan_price">Display Price <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="displayplan_price" name="displayplan_price" value="<?php echo $planDetail->plan_display_amount; ?>" placeholder="Enter display price" pattern="^[0-9]*$">
                            <div class="invalid-tooltip" id="msg_displayplan_price"></div>
                        </div>
                        

                        <div class="col-md-6 form-group mb-3">
                            <label for="billing_period">Billing period <span class="asterisk">*</span></label>
                            <select class="form-control" id="billing_period" name="billing_period" disabled>
                                <option value="monthly" <?php echo (isset($planDetail)&& $planDetail->plan_interval=='month')?"selected='selected'":""  ?>>Monthly</option>
                                <option value="yearly" <?php echo (isset($planDetail)&& $planDetail->plan_interval=='year')?"selected='selected'":""  ?>>Yearly</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_billing_period"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="plan_name">Plan name <span class="asterisk">*</span></label>
                            <select class="form-control" id="plan_name" name="plan_name" disabled>
                                <option value="">Select Plan</option>
                                <?php foreach ($allmasterplan as $key => $value) {?>
                                 <option value="<?php echo $value->master_plan_id;?>"  <?php echo (isset($planDetail)&& $planDetail->plan_master_plan_id==$value->master_plan_id)?"selected='selected'":""  ?> ><?php echo $value->master_plan_name;?></option> 
                                <?php } ?>

                            </select>
                            <div class="invalid-tooltip" id="msg_plan_name"></div>
                        </div>                
                        
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                    </form>
                <?php } ?>
            </div>
        </div>
    </div>
</div>

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

<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
    });

</script>