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

function defultmessage() 
{
    console.log('test');return false;
    $.ajax({
          url: "productadd.php",
          data: 'act=chkdefault&product_id='+product_id,
          dataType: 'json',
          cache: false,
          success: function(data)
          {
            console.log(data);
            return false;
              $("#appointment_sr_no").val(data.appointmentdata.pmsAppointment_sr_no);
              /*$("#manual_apppatient_id").val(data.appointmentdata.pmsAppointment_patient_id).trigger('change');*/
              $("#provider_id").val(data.appointmentdata.pmsAppointment_provider_id);
              $("#app_date").val(data.appointmentdata.pmsAppointment_date);
              $("#app_time").val(data.appointmentdata.pmsAppointment_time);
              $("#app_length").val(data.appointmentdata.pmsAppointment_length);
              $("#app_amount").val(data.appointmentdata.pmsAppointment_amount);
              $("#app_status").val(data.appointmentdata.pmsAppointment_status);
              
          }
      });
}
/*function validate_product(){
    
	var productname = $("#product_name");
    var pnamemsg = $("#msg_product_name");
	
    if(productname.val()=='')
    {
        productname.addClass('error');
        productname.focus();
        pnamemsg.html('Please enter product name').show();
        return false;
	}else
    {
        productname.removeClass('error');
        pnamemsg.hide();
    }

    return true;
}*/

</script>
<div class="breadcrumb">
    <h1>Products</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Product</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 id="results" >
      <div  id="msg_error_name"></div>
  </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 class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Add New Product</div>
                <form action="" id='frm-product' method="post" enctype="multipart/form-data">
                    <?php if(isset($productDetail['subscription_product_id'] ) && $productDetail['subscription_product_id'] !=0) { ?>
                       <input type="hidden" id="product_id" name="product_id" value="<?php echo $productDetail['subscription_product_id']; ?>" />
                       <input type="hidden" id="product_stripe_id" name="product_stripe_id" value="<?php echo $productDetail['subscription_product_stripe_id']; ?>" />
                       
                    <?php } ?>
                    <div class="row">
                        <?php if(!isset($productDetail['subscription_product_id'] ) ) { ?>
                        <div class="col-md-6 form-group mb-3">
                            <label for="product_name">Product Mode <span class="asterisk">*</span></label>
                            <select class="form-control" id="product_mode" name="product_mode">
                                <option value="">Select Product Mode</option>
                                <option value="allinone" <?php echo (isset($productDetail)&& $productDetail['subscription_product_mode']=='allinone')?"selected='selected'":""  ?> >All In One</option>
                                <option value="customization" <?php echo (isset($productDetail)&& $productDetail['plan_interval']=='customization')?"selected='selected'":""  ?> >Customization</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_product_mode"></div>
                        </div>
                    <?php } ?>
                        <div class="col-md-6 form-group mb-3" style="display: none;" id="div_plan_price">
                            <label for="plan_price">Price <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="plan_price" name="plan_price" placeholder="Enter Price" >
                            <div class="invalid-tooltip" id="msg_plan_price"></div>
                       </div>

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

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="clinics">Clinics </label>
                            <select class="form-control" id="clinics" multiple name="clinics[]">
                                <!-- <option value="">Select Clinic</option> -->
                                <?php foreach ($allClinic as $key => $value) {?>
                                 <option value="<?php echo $value->clinic_id;?>" ><?php echo $value->clinic_name;?></option> 
                                <?php } ?>

                            </select>

                            
                            <div class="invalid-tooltip" id="msg_clinic"></div>
                        </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='loader-bubble loader-bubble-secondary m-5' align="center" style="display: none;"></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,
        selectAll: true
    });


    $("#product_mode").on('change',function()
    {
        var product_mode = $("#product_mode").val();
        if(product_mode == 'allinone')
        {
            $("#div_plan_price").show();
        }else{
            $("#div_plan_price").hide();
        }

    });
    
    $( "form" ).on( "submit", function(e) {
      e.preventDefault();
      var product_name = $("#product_name").val();
      var product_mode = $("#product_mode").val();
      var plan_price = $("#plan_price").val();
      var clinics = $("#clinics").val();

      //console.log(product_mode);return false;
      <?php if(!isset($_REQUEST['subscription_product_id']))
      { ?>
          if($("#product_mode").val()=='')
          {
              $("#product_mode").addClass('error');
              $("#product_mode").focus();
              $("#msg_product_mode").html('Please select product mode').show();
              return false;
          }
          else if($("#product_mode").val() == 'allinone' && $("#plan_price").val() == '')
          {
              if($("#product_mode").val()!='')
              {
                    $("#product_mode").removeClass('error');
                    $("#msg_product_mode").hide();
              }
              
             if($("#plan_price").val() == '')
             {
                  $("#plan_price").addClass('error');
                  $("#plan_price").focus();
                  $("#msg_plan_price").html('Please enter price').show();
                  return false
             }
          }
          else if($("#product_name").val()=='')
           {
              
              if($("#product_mode").val()!='')
              {
                    $("#product_mode").removeClass('error');
                    $("#msg_product_mode").hide();
              }
              
              if($("#plan_price").val()!='')
              {
                    $("#plan_price").removeClass('error');
                    $("#msg_plan_price").hide();
              }


              $("#product_name").addClass('error');
              $("#product_name").focus();
              $("#msg_product_name").html('Please enter product name').show();
              return false;
           }
    <?php }else if(isset($_REQUEST['subscription_product_id'])){ ?>
            if($("#product_name").val()=='')
           {
              $("#product_name").addClass('error');
              $("#product_name").focus();
              $("#msg_product_name").html('Please enter product name').show();
              return false;
           }
    <?php } ?>
        else
        {
            if($("#product_name").val()!='')
            {
                $("#product_name").removeClass('error');
                $("#msg_product_name").hide();
            }
          <?php if(isset($_REQUEST['subscription_product_id']))
          { ?>
            
              $.ajax({
                  url: "productadd.php",
                  data: 'act=validate_defult_data&clinics='+clinics+'&product_id='+<?php echo $_REQUEST['subscription_product_id']; ?>,
                  method: 'POST',
                  success: function(msg){
                      if(msg!='success'){
                        
                          $("#alert_msg").show();
                          $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>"+msg+"</div>");
                          return false;
                      }else{
                          
                         $(".loader-bubble").show();
                          $.ajax({
                              url: "productadd.php",
                              data: { act:"load",
                                    product_name:product_name,clinics:clinics,product_id:<?php echo $_REQUEST['subscription_product_id'];?> },
                              method: 'POST',
                              success: function(msg){
                                  if(msg!=''){
                                     $(".loader-bubble").hide();
                                      window.location='productlist.php';
                                      //return false;
                                  }
                              }
                          });
                      }
                  }
              });
          <?php 
          }else
          {  ?>
            $(".loader-bubble").show();
            $.ajax({      
                    url: "productadd.php",
                    data: 
                    { 
                        act:"load",
                        product_name:product_name,clinics:clinics,product_mode:product_mode,
                        plan_price:plan_price 
                    },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            $(".loader-bubble").hide();
                           window.location='productlist.php';
                        }
                    }
                });
         <?php }
          ?>
      }

  });

</script>