<script type="text/javascript">
$(document).ready(function(){
	$("#frm-coupon").submit(function(e){
        if(!validate_coupon()){
            e.preventDefault();
        }
	});
        
    $("#amount_off_value").hide();
    $("#percent_off_value").hide();
    $("#duration_month").hide();

    $("#datewise_coupon_redeemed").hide();
    $("#timewise_coupon_redeemed").hide();
    $("#total_coupon_redeemed").hide();
    $("#total_number_ofcupon").hide();

    $('input[type=radio][name=amount_off]').change(function() {
        if(this.value == 'is_amount') {
            $("#amount_off_value").show();
            $("#percent_off_value").hide();
            $("#percent_off_value").val('');
        }
        else{
            $("#amount_off_value").hide();
            $("#amount_off_value").val('');
            $("#percent_off_value").show();
        }
    });

    $("#datarage_redeemed").click(function () 
    {   
        if ($(this).is(":checked")) {
            $("#datewise_coupon_redeemed").show();
            $("#timewise_coupon_redeemed").show();
            $('#timewise_coupon_redeemed').after("<br />");
        } else {
            $("#datewise_coupon_redeemed").hide();
            $("#timewise_coupon_redeemed").hide();
            $('#timewise_coupon_redeemed').next('br').remove();
            $("#datewise_coupon_redeemed").val('');
            $("#timewise_coupon_redeemed").val('');
        }
    });

    $("#coupon_redeemed").click(function () 
    {   
        if ($(this).is(":checked")) {
            $("#total_coupon_redeemed").show();
            //$('#total_coupon_redeemed').after("<br />");
            $("#total_number_ofcupon").show();
        } else {
            $("#total_coupon_redeemed").hide();
            //$('#total_coupon_redeemed').next('br').remove();
            $("#total_number_ofcupon").hide();
            $("#total_coupon_redeemed").val('');
        }
    });

    $("#duration_id").change(function() {
        if(this.value == 'repeating')
        {
            $("#duration_month").show();
        }else{
            $("#duration_month").val('');
            $("#duration_month").hide();
        }
    });
});
function validate_coupon(){
	var couponname = $("#coupon_name");
    var couponnamemsg = $("#msg_coupon_name");
	var duration = $("#duration_id");
    var durationmsg = $("#msg_duration_id");
	var clinic = $("#clinics");
    var clinicmsg = $("#msg_clinic");

    if(couponname.val()=='')
    {
        couponname.addClass('error');
        couponname.focus();
        couponnamemsg.html('Please enter coupon name').show();
        return false;
	}else
    {
        couponname.removeClass('error');
        couponnamemsg.hide();
    }
    if(duration.val()=='')
    {
        duration.addClass('error');
        duration.focus();
        durationmsg.html('Please select duration').show();
        return false;
	}else
    {
        duration.removeClass('error');
        durationmsg.hide();
    }
    if(clinic.val()=='')
    {
        clinic.addClass('error');
        clinic.focus();clinicmsg.html('Please select clinics ').show();
        return false;
	}
    return true;
}

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
</script>
<div class="breadcrumb">
    <h1>Coupons</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Coupon</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 Coupon</div>
                <form action="" id='frm-coupon' method="post" enctype="multipart/form-data">
                    <?php if(isset($couponDetail->coupon_id) && $couponDetail->coupon_id!=0) { ?>
                       <input type="hidden" id="coupon_id" name="coupon_id" value="<?php echo $couponDetail->coupon_id ?>" />
                    <?php } ?>
                    <div class="row">
                    	<div class="col-md-6 form-group mb-3">
                            <label for="coupon_name">Coupon name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="coupon_name" name="coupon_name" value="<?php echo $couponDetail->coupon_name; ?>" placeholder="Enter coupon name">
                            <div class="invalid-tooltip" id="msg_coupon_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="duration_id">Duration <span class="asterisk">*</span></label>
                            <select class="form-control" id="duration_id" name="duration_id">
                                <option value="">Select Duration</option>
                                <option value="forever" <?php echo ($couponDetail->coupon_duration=='forever') ? "selected='selected'" : "" ?>>Forever</option>
                                <option value="once" <?php echo ($couponDetail->coupon_duration=='once') ? "selected='selected'" : "" ?>>Once</option>
                                <option value="repeating" <?php echo ($couponDetail->coupon_duration=='repeating') ? "selected='selected'" : "" ?>>Multiple Months</option>
                            </select>    
                            
                            <div class="invalid-tooltip" id="msg_duration_id"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3" id="duration_month">
                            <label for="number_of_months">Number of months </label>
                            <input type="text" class="form-control" placeholder="number of months" name="duration_month" id="duration_month"  value="">
                            <div class="invalid-tooltip" id="msg_number_of_months"></div>
                        </div>


                        <div class="col-md-6 form-group mb-3">
                          <label class="control-label">Type</label>
                          <div class="controls">
                            <label class="radio radio-outline-primary inline-radio">
                                <input type="radio" name="amount_off" id="amount_off"  value="is_amount" <?php echo ($couponDetail->coupon_amount_off!='') ? "checked='checked'" : "" ?>>
                                <span>Amount Off</span>
                                <span class="checkmark"></span>
                            </label>                     
                            <input type="text" class="form-control" name="amount_off_value" id="amount_off_value"  value="" onkeypress="return isNumber(event)" >
                           </div>
                           <div class="controls">
                            <label class="radio radio-outline-primary inline-radio">
                                <input type="radio" name="amount_off" id="percent_off"  value="is_percent">
                                <span>Percent Off</span>
                                <span class="checkmark"></span>
                            </label>                     
                            <input type="text" class="form-control" name="percent_off_value" id="percent_off_value"  value="" onkeypress="return isNumber(event)">
                           </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-6 form-group mb-3">
                          <label class="control-label">Redemption limits</label>
                          <div class="controls">
                            <label class="checkbox checkbox-outline-primary inline-checkbox">
                                <input type="checkbox" name="datarage_redeemed" id="datarage_redeemed"  value="is_daterang"  <?php echo ($couponDetail->coupon_amount_off!='') ? "checked='checked'" : "" ?>>
                                <span>Limit the data range when customers can redeem this coupon</span>
                                <span class="checkmark"></span>
                            </label>
                            <br/>                    
                            <!-- <input type="text" class="form-control" name="datewise_coupon_redeemed" id="datewise_coupon_redeemed"  value=""> -->
                            
                            <input type="date" class="form-control" name="datewise_coupon_redeemed" id="datewise_coupon_redeemed"  />
                            <br/>
                            <input type="time" class="form-control" id="timewise_coupon_redeemed" name="timewise_coupon_redeemed" >
                           </div>
                           <div class="controls">
                            <label class="checkbox checkbox-outline-primary inline-checkbox">
                                <input type="checkbox" name="coupon_redeemed" id="coupon_redeemed"  value="is_coupon_redeemed">
                                <span>Limit the total number of times this coupon can be redeemed</span>
                                <span class="checkmark"></span>
                            </label>

                            <input type="text" class="form-control" name="total_coupon_redeemed" id="total_coupon_redeemed"  value="" onkeypress="return isNumber(event)">
                            <span id="total_number_ofcupon" name="total_number_ofcoupon">This limit applies across customers so it won't prevent a single customer from redeeming multiple times.</span>
                           </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>

<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  $couponDetail->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>