<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <h4 class="page-title">Enroll Customer in Coaching Program:</h4>
            </div>
        </div>
        <?php if ($this->session->flashdata('Success') && $this->session->flashdata('Success') != "") { ?>
            <div class="row">
                <div class="col-sm-12">
                    <div class="alert alert-success">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <strong>Success!</strong> <?php echo $this->session->flashdata('Success'); ?>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if ($this->session->flashdata('Error') && $this->session->flashdata('Error') != "") { ?>
            <div class="row">
                <div class="col-sm-12">
                    <div class="alert alert-danger">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <strong>Error!</strong> <?php echo $this->session->flashdata('Error'); ?>
                    </div>
                </div>
            </div>
        <?php } ?> 



        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-30"></h4>
                    <form id="registerform" method="post" action="<?php echo base_url(); ?>coaching_controller/enroll_customer">
                        <input type="hidden" name="csrf_test_name" id="training_customer_csrf" value=""/>
                        <input type="hidden" name="program_type" value="<?php echo $program_type; ?>" />
                        <input type="hidden" name="program_id" value="<?php echo $program_id; ?>" />
                        <div class="form-group">
                             <div class="divider"></div>
                             <div class="row">
                                <div class="col-lg-12">
                                    <h3 class="title text-primary"><strong>Customer Details:</strong></h3>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <label for="txtcustomername" class="control-label"><span class='text-pink'>*</span> Name</label>
                                        <input type="hidden" class="form-control" placeholder="Customer ID" id="txtcustomerid" name="txtcustomerid" value="<?php if(isset($customer)){ echo $customer[0]['id_customers'];}?>">
                                        <input tabindex="1" type="text" class="form-control alpha" placeholder="Participant Name" id="txtcustomername" name="txtcustomername" value="<?php if(isset($customer)){ echo $customer[0]['customer_name'];}?>" required="required">
                                    </div>
                                </div>
                                <div class="col-lg-3">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="txtcustomercell" class="control-label"><span class='text-pink'>*</span> Cell</label>
                                            <input tabindex="2" type="text" class="form-control numeric" placeholder="Customer Cell" id="txtcustomercell" name="txtcustomercell" value="<?php if(isset($customer)){ echo $customer[0]['customer_cell'];}?>" requried='required'>
                                        </div> 
                                    </div> 
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomergender" class="control-label">Gender</label>
                                        <select tabindex="3" type="text" class="form-control numeric" placeholder="Gender" id="txtcustomergender" name="txtcustomergender" >
                                            <option value="F" <?php if(isset($customer) && $customer[0]['customer_gender']==="F"){ echo "selected";}?>>Female</option>
                                            <option value="M" <?php if(isset($customer) && $customer[0]['customer_gender']==="M"){ echo "selected";}?>>Male</option>
                                        </select>
                                    </div> 
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomertype" class="control-label">Type</label>
                                        <select tabindex="4" name="txtcustomertype" id="txtcustomertype" class="form-control">
                                           
                                            <option value=""></option>
                                            <option value="orange" <?php if(isset($customer) && $customer[0]['customer_type']=="orange"){echo "selected='selected'";}?>><i style="color:orange" class="ti ti-star"></i> (Orange) High Priority Customer</option>
                                            <option value="green" <?php if(isset($customer) && $customer[0]['customer_type']=="green"){echo "selected='selected'";}?>><i class="ti ti-face-smile"></i> (Green) General Customer</option>
                                            <option value="red" <?php if(isset($customer) && $customer[0]['customer_type']=="red"){echo "selected='selected'";}?>><i class="ti ti-hand-stop"></i> (Red) Flagged Customer</option>
                                                    
                                        </select>
                                    </div>
                                </div>
                            </div>
                            
                             
                             <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomerfathername" class="control-label">Father's Name</label>
                                        <input tabindex="5" type="text" class="form-control" placeholder="Fathers Name" id="txtcustomerfathername" name="txtcustomerfathername" value="<?php if(isset($customer)){ echo $customer[0]['father_name'];}?>">
                                    </div> 
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomerfathercell" class="control-label">Father's Cell</label>
                                        <input tabindex="6" type="text" class="form-control numeric" placeholder="Fathers Cell" id="txtcustomerfathercell" name="txtcustomerfathercell" value="<?php if(isset($customer)){ echo $customer[0]['father_cell'];}?>">
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomermohtername" class="control-label">Mother's Name</label>
                                        <input tabindex="7" type="text" class="form-control" placeholder="Mother Name" id="txtcustomermohtername" name="txtcustomermohtername" value="<?php if(isset($customer)){ echo $customer[0]['mother_name'];}?>">
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomermothercell" class="control-label">Mother's Cell</label>
                                        <input tabindex="8" type="text" class="form-control numeric" placeholder="Mother Cell" id="txtcustomermothercell" name="txtcustomermothercell" value="<?php if(isset($customer)){ echo $customer[0]['mother_cell'];}?>">
                                    </div> 
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomerfatherprofession" class="control-label">Father's Profession</label>
                                        <input tabindex="9" type="text" class="form-control" placeholder="Father Profession" id="txtcustomerfatherprofession" name="txtcustomerfatherprofession" value="<?php if(isset($customer)){ echo $customer[0]['father_profession'];}?>">
                                    </div> 
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomermotherprofession" class="control-label">Mother's Profession</label>
                                        <input tabindex="10" type="text" class="form-control" placeholder="Mother Profession" id="txtcustomermotherprofession" name="txtcustomermotherprofession" value="<?php if(isset($customer)){ echo $customer[0]['mother_profession'];}?>">
                                    </div>
                                </div>
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <label for="txtcustomerphone1" class="control-label">Phone 1</label>
                                        <input tabindex="11" type="text" class="form-control numeric" placeholder="Customer Phone 1" id="txtcustomerphone1" name="txtcustomerphone1" value="<?php if(isset($customer)){ echo $customer[0]['customer_phone1'];}?>">
                                    </div>                                    
                                </div>
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <label for="txtcustomerphone2" class="control-label">Phone 2</label>
                                        <input tabindex="12" type="text" class="form-control numeric" placeholder="Customer Phone 2" id="txtcustomerphone2" name="txtcustomerphone2" value="<?php if(isset($customer)){ echo $customer[0]['customer_phone2'];}?>">
                                    </div>
                                </div>
                                
                            </div>
                            <div class="row">
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <label for="txtcustomeremail" class="control-label">Email</label>
                                        <input tabindex="13" type="email" class="form-control " placeholder="Customer Email" id="txtcustomeremail" name="txtcustomeremail" value="<?php if(isset($customer)){ echo $customer[0]['customer_email'];}?>">
                                    </div>
                                </div>
                                
                                <div class="col-lg-6">
                                    <div class="form-group">
                                        <label for="txtcustomeraddress" class="control-label">Address</label>
                                        <input tabindex="14" type="text" class="form-control " placeholder="Customer Address" id="txtcustomeraddress" name="txtcustomeraddress" value="<?php if(isset($customer)){ echo $customer[0]['customer_address'];}?>">
                                    </div>
                                </div>
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <div class="col-sm-12">
                                            <label for="txtcustomeranniversary" class="control-label">Wedding Anniversary</label>
                                        </div>
                                        <div class="col-sm-12">
                                            <div class="input-group">
                                                <input tabindex="15" type="text" class="form-control" placeholder="yyyy/mm/dd" id="txtcustomeranniversary" name="txtcustomeranniversary" value="<?php if(isset($customer)){ echo $customer[0]['customer_anniversary'];}?>">
                                                <span class="input-group-addon bg-info b-0 text-white"><i class="ti ti-calendar"></i></span>
                                            </div><!-- input-group -->
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-lg-3">
                                     <div class="form-group">
                                        <div class="col-lg-12">
                                            <label for="txtcustomerbirthday" class="control-label">Birthday</label>
                                        </div>
                                        <div class="col-lg-4">
                                            <select tabindex="16" id="txtcustomerbirthday" name="txtcustomerbirthday" class="form-control">
                                                <option value=""></option>
                                                <?php $x=1; while($x <= 31) {?>
                                                    <option value='<?php echo $x;?>' <?php if(isset($customer) && $customer[0]['customer_birthday']==$x){echo "selected='selected'";}?>><?php echo $x;?></option>                                                    
                                                <?php $x++; } ?>
                                            </select>
                                        </div>
                                        <div class="col-lg-8">
                                            <select tabindex="17" id="txtcustomerbirthmonth" name="txtcustomerbirthmonth" class="form-control">
                                                <option value=""></option>
                                                <option value="January" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="January"){echo "selected='selected'";}?>>January</option>
                                                <option value="February" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="February"){echo "selected='selected'";}?>>February</option>
                                                <option value="March" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="March"){echo "selected='selected'";}?>>March</option>
                                                <option value="April" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="April"){echo "selected='selected'";}?>>April</option>
                                                <option value="May" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="May"){echo "selected='selected'";}?>>May</option>
                                                <option value="June" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="June"){echo "selected='selected'";}?>>June</option>
                                                <option value="July" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="July"){echo "selected='selected'";}?>>July</option>
                                                <option value="August" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="August"){echo "selected='selected'";}?>>August</option>
                                                <option value="September" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="September"){echo "selected='selected'";}?>>September</option>
                                                <option value="October" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="October"){echo "selected='selected'";}?>>October</option>
                                                <option value="November" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="November"){echo "selected='selected'";}?>>November</option>
                                                <option value="December" <?php if(isset($customer) && $customer[0]['customer_birthmonth']=="December"){echo "selected='selected'";}?>>December</option>
                                            </select>
                                        </div>

                                    </div>
                                </div>
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <label for="txtcustomerallergies" class="control-label text-danger">Allergies Alert</label>
                                        <input tabindex="18" type="text" class="form-control " placeholder="Note down any allergies the customer may have" id="txtcustomerallergies" name="txtcustomerallergies" value="<?php if(isset($customer)){ echo $customer[0]['customer_allergies'];}?>">
                                    </div> 
                                </div>
                                <div class="col-lg-3">
                                    <div class="form-group">
                                        <label for="txtcustomeralert" class="control-label text-warning">Customer Alert</label>
                                        <input tabindex="19" type="text" class="form-control " placeholder="Note down any other alerts you want to get when the customer visits . . ." id="txtcustomeralert" name="txtcustomeralert" value="<?php if(isset($customer)){ echo $customer[0]['customer_alert'];}?>">
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="txtcustomercard" class="control-label">Card #</label>
                                        <input tabindex="20" type="text" class="form-control numeric" placeholder="Customer Card#" id="txtcustomercard" name="txtcustomercard" value="<?php if(isset($customer)){ echo $customer[0]['customer_card'];}?>">
                                    </div>
                                </div>
                            </div>
                            <!----Coaching Items--->
                             <div class="divider"></div>
                             <div class="row">
                                <div class="col-lg-12">
                                    <h3 class="title text-primary"><strong>Coaching Related Info:</strong></h3>
                                </div>
                            </div>
                            <div class="row m-t-20 m-b-20">
                                <div class="col-lg-12">
                                   <h4 class="title txt-primary" ><strong style='border-bottom: #808080 solid 1px '>Basic Info:</strong></h4>
                                </div>
                            </div>
                            
                            <div class="row ">
                                <div class="col-md-6 form-horizontal">
                                    <div class="form-group">
                                        <label for="txtemergencyperson" class="col-md-4 control-label "><span class="text-pink">*</span> Emergency Contact Person</label>
                                        <div class="col-md-8">
                                            <input tabindex="20" type="text" class="form-control" placeholder="Emergency Contact Person" id="txtemergencynumber" name="txtemergencyperson" required="required" value="<?php if(isset($customer)){ echo $customer[0]['emergency_contact_person'];}?>">
                                        </div>
                                    </div>
                                    
                                    <div class="form-group">
                                        <label for="txtemergencynumber" class="col-md-4 control-label "><span class="text-pink">*</span> Emergency Contact Number</label>
                                        <div class="col-md-8">
                                            <input tabindex="21" type="text" class="form-control numeric" placeholder="Emergency Contact Number" id="txtemergencynumber" name="txtemergencynumber" required="required" value="<?php if(isset($customer)){ echo $customer[0]['emergency_contact_number'];}?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="txtage" class="col-md-4 control-label">Age</label>
                                        <div class="col-md-8">
                                            <input tabindex="22" type="text" class="form-control numeric" placeholder="Age" id="txtage" name="txtage" value="<?php if(isset($customer)){ echo $customer[0]['age'];}?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="txtgoals" class="col-md-4 control-label">Learning Goals</label>
                                        <div class="col-md-8">
                                            <textarea tabindex="23" type="text" class="form-control" placeholder="Goals" id="txtgoals" name="txtgoals" value="<?php if(isset($customer) && isset($customer[0]['goals'])){ echo $customer[0]['goals'];}?>"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-6 form-horizontal">
                                    
                                    <div class="form-group">
                                        <label for="txteducation" class="col-md-4 control-label ">Last Level of Education Completed</label>
                                        <div class="col-md-8">
                                            <input tabindex="24" type="text" class="form-control" placeholder="Education" id="txteducation" name="txteducation" value="<?php if(isset($customer)&& isset($customer[0]['education'])){ echo $customer[0]['education'];}?>">
                                        </div>
                                    </div>
                                    
                                    <div class="form-group">
                                        <label for="txtlanguages" class="col-md-4 control-label">Spoken Languages</label>                                    
                                        <div class="col-md-8">
                                            <select tabindex="25" id="txtlanguages" name="txtlanguages[]" multiple="multiple" class="form-control" >
                                                <option value="English">English</option>
                                                <option value="Urdu">Urdu</option>
                                                <option value="Sindhi">Sindhi</option>
                                                <option value="Pashto">Pashto</option>
                                                <option value="Punjabi">Punjabi</option>
                                                <option value="Balochi">Balochi</option>
                                            </select>          
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="txtexperience" class="col-md-4 control-label">Previous Experience</label>
                                        <div class="col-md-8">
                                            <input tabindex="26" type="text" class="form-control" placeholder="Experience" id="txtexperience" name="txtexperience" value="<?php if(isset($customer) && isset($customer[0]['experience'])){ echo $customer[0]['experience'];}?>">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <hr/>
                            <!----Training Items--->
                            <?php if($scheduler_style[0]['gym']=='Yes'){?> 
                            
                            <!----Gym Items--->
                             <div class="divider"></div>
                             <div class="row">
                                <div class="col-lg-12">
                                    <h3 class="title text-primary"><strong>Gym Related Info:</strong></h3>
                                </div>
                            </div>
                                                        
                            <div class="row">
                            
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtheight" class="control-label">Height</label>
                                        <input tabindex="17" type="text" class="form-control decimal" placeholder="Height" id="txtheight" name="txtheight" value="<?php if(isset($customer)){ echo $customer[0]['height'];}?>">
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtbmi" class="control-label ">BMI</label>
                                        <input tabindex="18" type="text" class="form-control decimal" placeholder="Body Mass Index" id="txtbmi" name="txtbmi" value="<?php if(isset($customer)){ echo $customer[0]['bmi'];}?>">
                                    </div>
                                </div>
                                
                            </div>
                            
                            <div class="row m-t-20 m-b-20">
                                <div class="col-lg-12">
                                   <h4 class="title txt-primary" ><strong style='border-bottom: #808080 solid 1px '>Customer's Current Status:</strong></h4>
                                </div>
                            </div>
                             
                            <div class="row">
                                 <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtweight" class="control-label ">Weight</label>
                                        <input tabindex="19" type="number" class="form-control decimal" placeholder="Current Weight" id="txtweight" name="txtweight">
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtarms" class="control-label">Arms</label>
                                        <input tabindex="21" type="number" class="form-control decimal" placeholder="Current Arms" id="txtarms" name="txtarms">
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtthighs" class="control-label">Thighs</label>
                                        <input tabindex="22" type="number" class="form-control decimal" placeholder="Current Thighs" id="txtthighs" name="txtthighs">
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txthips" class="control-label">Hips</label>
                                        <input tabindex="23" type="number" class="form-control decimal" placeholder="Current Hips" id="txthips" name="txthips">
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtchest" class="control-label">Chest</label>
                                        <input tabindex="24" type="number" class="form-control decimal" placeholder="Current Chest" id="txtchest" name="txtchest">
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="form-group">
                                        <label for="txtwaist" class="control-label">Waist</label>
                                        <input tabindex="25" type="number" class="form-control decimal" placeholder="Current Waist" id="txtwaist" name="txtwaist">
                                    </div>
                                </div>
                            </div>
                            <!--current-->
                            
                            
                            <?php } ?>
                            <hr/>
                            <div class="row m-t-20 m-b-20">
                                <div class="col-lg-12">
                                   <h4 class="title txt-primary" ><strong style='border-bottom: #808080 solid 1px '>Program Enrollment:</strong></h4>
                                </div>
                            </div>
                            <!---Registration Fee-->
                            <?php if(null!=$registration_fee){?>
                            <div class="row"> 
                                <div class="col-lg-3">
                                    <label for="txtRegistrationFee" class="control-label">Registration Fee:</label>
                                    <input class="form-control decimal" name="txtRegistrationFee" id="txtRegistrationFee" readonly="readonly" value="<?php echo $registration_fee; ?>" >                                           
                                </div>

                                <input type="hidden" class="form-control decimal" name="txtRegistrationFeePaid" id="txtRegistrationFeePaid" readonly="readonly" value="0.0" >

                                <div class="col-lg-3">
                                    <label for="txtRegistrationValidity" class="control-label">Valid Until:</label>
                                    <input class="form-control" name="txtRegistrationValidity" id="txtRegistrationValidity" >
                                </div>
                            </div>
                            <hr/>
                            <?php } ?>
                            <!---Registration Fee End--> 
                           
                            <div class="row">
                                <div class="col-lg-2">  
                                    <?php 
                            

                                    ?>
                                    <label for="txtprograms" class="control-label">Program</label>                                    
                                    <select tabindex="27" id="txtprograms" name="txtprograms" class="form-control" onchange="fillservicedetail(),get_group_by_program()" required="required">
                                        <option></option> 
                                        <?php foreach($services as $service){ ?>
                                        <?php if($service['service_mode']=='Subscription'){?>
                                        <option rate="<?php echo $service['service_rate']; ?>" value="<?php echo $service['id_business_services']; ?>"><?php echo $service['service_name']." (".$service['service_mode'].")"; ?></option>
                                        <?php } else { ?>
                                        <option rate="<?php echo $service['service_rate']; ?>" value="<?php echo $service['id_business_services']; ?>"><?php echo $service['service_name']; ?></option>
                                         <?php } ?>
                                        <?php } ?>
                                    </select>                                    
                                </div>  
                           
                                <div class="col-lg-2" <?php  if($flag_for_individual_or_group  == "individual_client"){ ?> style="display:none" <?php  }else{ ?> style="display:block" <?php }  ?> > 
                                    <label for="txtprograms" class="control-label">Program Groups</label>                                    
                                    <select tabindex="27" id="txtprogramsgroup" name="txtprogramsgroup" class="form-control" onchange="getting_group_sessions_and_set_in_total_session();">
                    
                                    </select>                                    
                                </div>
                                <div class="col-lg-2">
                                    <label for="txtpsessionprice" class="control-label">Price Per Session</label>
                                    <input tabindex="28" type="text" class="form-control numeric" placeholder="Session price" id="txtpsessionprice" name="txtpsessionprice" required="required" min="0" value="0">
                                </div>
                                <div class="col-lg-2">
                                    <label for="txttotalsessions" class="control-label">Total Session</label>
                                    <input tabindex="29" type="text" class="form-control numeric" placeholder="Total Sessions" oninput="calculate_total_program_price_with_session()" id="txttotalsessions" name="txttotalsessions" required="required" min="1" value="1" <?php  if($flag_for_individual_or_group  == "individual_client"){ ?>  <?php  }else{ ?> readonly <?php }  ?>> 
                                 
                                </div>
                                <div   <?php  if($flag_for_individual_or_group  == "individual_client"){ ?> class="col-lg-2"<?php  }else{ ?> class="col-lg-1"<?php }  ?>>
                                    <label for="txttotalsessions" class="control-label">Discount</label>
                                    <input tabindex="29" type="text" class="form-control numeric" placeholder="Total Sessions" id="program_discount_perc" name="program_discount" required="required" min="1" value="0" oninput="calculate_total_program_price_with_session()"> 
                                 
                                </div>
                                <div class="col-lg-2">
                                    <label for="txttotalsessions" class="control-label">Other Charges</label>
                                    <input tabindex="29" type="text" class="form-control numeric"  id="othercharges" name="othercharges" required="required" min="1" value="0" oninput="calculate_total_program_price_with_session()"> 
                                    <h4 class="text-pink text-right" id="price_with_session"></h4>
                                </div>
                               
                                <div class="col-lg-1">                                   
                                    <button tabindex="30" type="button" onclick="addOrderRows();" class='btn btn-sm btn-primary m-t-20'><i class="fa fa-arrow-down"></i> Add</button>                                    
                                </div>
                                
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <table id="tblservices" name="tblservices" class="table">
                                        <thead>
                                            <th class="hidden">service_id</th>
                                            <?php  if($flag_for_individual_or_group  == "individual_client"){ ?>    <th>Program</th>  <?php  }else{ ?>    <th>Program | Group</th> <?php }  ?>
                                            <th>Per Session Rate</th>                                            
                                            <th>Total Session</th>
                                            <th>Discount</th>
                                            <th>Other Charges</th>
                                            <th>Total</th><!-- comment -->
                                            <th></th>
                                        </thead>
                                        <tbody></tbody>
                                        <tfoot></tfoot>
                                    </table>
                                </div>
                            </div>
                                 
                            
                            
                            <div class="row m-t-20">
                                <div class="col-md-6">
                                    <div class="btn-group pull-right m-t-15">
                                        
                                        <button type="button" onclick="$('#training_customer_csrf').val($('#cook').val()); submit_form();" class="btn waves-effect waves-light btn-pink m-t-20">Save</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>    
    </div>
</div>

<script>
    $(document).ready(function () { 

        //  discount_percentage on each program  

        $("#txtprograms").select2();
        $("#txtprogramsgroup").select2(); 


        $(document).on('click', '#othercharges,#program_discount_perc', function() {
            this.select();
        });
        $(document).on('input', '#program_discount_perc', function() { 
             var program_discount = parseFloat($(this).val()) || 0; 
                if(program_discount >0 ) {
                  $("#othercharges").val(0);
                } 
             calculate_total_program_price_with_session();
        });
        $(document).on('input', '#othercharges', function() { 
            var othercharges = parseFloat($(this).val()) || 0; 
            if(othercharges >0){
             $("#program_discount_perc").val(0);
            } 
            calculate_total_program_price_with_session();
        });

        $(document).on("keyup",'.discount_on_each_program',function(){ 
            
          var each_session_price = parseFloat($(this).closest('tr').find('.each_session_price').text()); 
          var  each_session_qty = parseFloat($(this).closest('tr').find('.each_session_qty').text());  
         });

        toastr.options = {
            "closeButton": true,
            "debug": true,
            "newestOnTop": true,
            "progressBar": true,
            "positionClass": "toast-bottom-center",
            "preventDuplicates": true,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }

        $("#txtcustomeranniversary").datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd'
        });

        $("#txtstartdate").datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd'
        });

        $("#txtRegistrationValidity").datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd'
        }).datepicker('setDate', new Date('<?php echo date('Y-m-d', strtotime('+1 year'));?>'));        


        $(".numeric").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                return false;
            }
        });
        $(".decimal").keypress(function(e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && (e.which < 48 || e.which > 57) ) {
                
                return false;
            }
        });
    });

    function getprogramsessions(){
         $("#txtprogramsessions").html('<option></option>');
         
         $.ajax({
            type: 'POST',
            url: "<?php echo base_url() . 'programs_controller/get_active_sessions'; ?>",
            data: {programid: $("#txtprograms option:selected").val()},
            dataType: "json",
            cache: false,
            async: true,
            success: function(data) {
               
                var mhtml = '';
                for (x = 0; x < data.length; x++) {
                    mhtml+='<option value="'+ data[x]['id_program_sessions'] +'">'+ data[x]['start'] +' - ' + data[x]['end'] + '</option>'
                }
                
                $("#txtprogramsessions").append(mhtml);
            }
        });
    }

    function fillservicedetail(){
    
        $("#txtpsessionprice").val($("#txtprograms option:selected").attr('rate')); 
        calculate_total_program_price_with_session();
    }

    function addOrderRows() {

               
                var mhtml = "";
                var exists;
                var count = 1;

               if($('#txtprograms option:selected').val() == ''){
                 return false;   
                 } 
                if($("#txtprogramsgroup").val() == ''){
                  return false;   
                 } 
                 $('#tblservices tbody tr').each(function() { 
                        var idVal = $(this).find('.id').text(); 
                        var groupIdVal = $(this).find('.group_id').text();  
                        if(idVal == $("#txtprograms option:selected").val() && groupIdVal ==  $("#txtprogramsgroup").val()){
                             exists = 1;
                        }
                    });  

               var discount_amount =  parseFloat($("#program_discount_perc").val()) || 0;  
               var total_program_price = parseFloat($("#txtpsessionprice").val()); 
               var total_session =  parseFloat($("#txttotalsessions").val()); 
               var total_program_price_before_discount = total_program_price*total_session;
               var price_after_discount =  total_program_price_before_discount-discount_amount; 
                var othercharges = parseFloat($("#othercharges").val()) || 0; 
                if(othercharges > 0){
                    price_after_discount =  total_program_price_before_discount+othercharges; 
                }

                mhtml += '<tr>';
                mhtml += '<td class="id hidden">' + $("#txtprograms option:selected").val() + '</td>';
                if( $("#txtprogramsgroup").val() =="" ||  $("#txtprogramsgroup").val() ==0){
                    mhtml += "<td >" + $("#txtprograms option:selected").text() + " <input type='hidden' value='"+$("#txtprograms option:selected").text()+"'></td>";
                }else{
                    mhtml += "<td >" + $("#txtprograms option:selected").text() +" | "+$("#txtprogramsgroup option:selected").text() +" <input type='hidden' value='"+$("#txtprograms option:selected").text()+"'></td>";
                }
                mhtml += "<td class='each_session_price'>" + $("#txtpsessionprice").val() + "</td>";
                mhtml += "<td class='each_session_qty'>" + $("#txttotalsessions").val() + "</td>";
                mhtml += "<td>"+parseFloat(discount_amount).toFixed(2)+"</td>";
                mhtml += "<td>" + parseFloat(othercharges).toFixed(2) + "</td>";
                mhtml += "<td>" + parseFloat(price_after_discount).toFixed(2) + "</td>";
                mhtml += "<td class='hidden group_id'>" +  $("#txtprogramsgroup").val() + "</td>";
                mhtml += '<td><span class="label label-danger" onclick="removeservice(' + $("#txtprograms option:selected").val() + ');" style="cursor:pointer">x</span></td>';
                
                mhtml += "</tr>";
                //}
                if (exists !== 1) {
                    console.log('adding: ' + mhtml);
                    $("#tblservices tbody").append(mhtml); 

                    $("#othercharges").val(0);
                    $("#program_discount_perc").val(0);
                } else {
                    swal({
                        title: "Service already added!",
                        text: 'If you want to change this Service, please remove and add again.',
                        type: "info",
                        confirmButtonText: 'OK!'
                    });
                }
            }

    function removeservice(val) {
        $('#tblservices').find("td.id").each(function(index) {
            if ($(this).html() == val) {
                $(this).closest('tr').remove();
            }
        });
    }
    
    function submit_form(){
        
      var TableData;
        TableData = storeOTblValues();
        TableData = $.toJSON(TableData);
        
//        console.log(TableData.length);
//        return false;
        
       if(TableData.length <= 2){
           swal({
                title: 'Missing Data',
                text: "Add services first!",
                type: 'info',
                showCancelButton: false,
                confirmButtonColor: '#ff5b5b !important',
                confirmButtonText: 'Ok'
            });
            return false;
        } 
        if($("#txtcustomername").val() ==""){ 
            swal({
                title: 'Missing Data',
                text: "Name cannot be null the name is required.",
                type: 'info',
                showCancelButton: false,
                confirmButtonColor: '#ff5b5b !important',
                confirmButtonText: 'Ok'
             });
            return false;

        }else if($("#txtcustomername").val() ==""){ 

            swal({
                title: 'Missing Data',
                text: "Cell cannot be null cell is required.",
                type: 'info',
                showCancelButton: false,
                confirmButtonColor: '#ff5b5b !important',
                confirmButtonText: 'Ok'
             });
            return false;
        } 
        
        $.ajax({
                url: $("#registerform").attr('action') || window.location.pathname,
                type: "POST",
                data: $("#registerform").serialize()+ "&TableData=" + TableData, 
                
                cache: false,
                async: true,
                success: function(data) {
                    console.log(data);
                    var r=data.split(",");
                    if (r[0] == "Success"){
                        <?php if(COACHING_MODE=='session'){?>
                            location.href = '<?php echo base_url();?>coaching_invoice_list/'+r[1];
                        <?php }else{ ?>
                            location.href = '<?php echo base_url();?>open_receivable_invoices/'+r[1];
                        <?php } ?>
                    } else if(r[0] == "Warning_of_validation"){ 

                        swal({
                            title: 'One of the groups you have selected already has a student in it.',
                            text: "",
                            type: 'info',
                            showCancelButton: false,
                            confirmButtonColor: '#ff5b5b',
                            confirmButtonText: 'Ok'
                        }); 
                        return false; 

                    } else{
                        swal({
                            title: 'Error',
                            text: "Registration Failed!",
                            type: 'error'
                        });
                    }
                }
                
        });
                
        
    } 


    function calculate_total_program_price_with_session(){ 

       var price =  parseFloat($("#txtpsessionprice").val()) || 0; 
       var total_session =  parseFloat($("#txttotalsessions").val()) || 0;  
       var discount  =  parseFloat($("#program_discount_perc").val()) || 0;  
       var othercharges = parseFloat($("#othercharges").val()) || 0;  
       var price_with_sessions =  (price*total_session)-discount;  
        if(othercharges > 0){
            price_with_sessions =(price*total_session)+othercharges;
        }  
       if(discount > (price*total_session)){
            swal({
                title: 'We cannot offer a discount greater than the total price.',
                text: "",
                type: 'info',
                showCancelButton: false,
                confirmButtonColor: '#ff5b5b',
                confirmButtonText: 'Ok'
            }); 
            return false; 
            parseFloat($("#program_discount_perc").val((price*total_session)))              
        }
       $("#price_with_session").text("Final Price :"+ parseFloat(price_with_sessions).toFixed(1));
     } 

    function storeOTblValues(){
        var TableData = new Array();
        $('#tblservices  tr').each(function(row, tr) {
            TableData[row] = {
                "service_id": $(tr).find('td:eq(0)').text()
                , "service_name": $(tr).find('td:eq(1) input').val()
                , "service_rate": $(tr).find('td:eq(2)').text()
                , "total_sessions": $(tr).find('td:eq(3)').text()
                , "program_discount": $(tr).find('td:eq(4)').text()
                , "other_charges": $(tr).find('td:eq(5)').text()
                , "program_group_id": $(tr).find('td:eq(7)').text()
            }      
        });

        TableData.shift();  // first row will be empty - so remove
        //TableData.pop();    // last row will be empty - so remove
        console.log(TableData ,"table");
        return TableData;
        return false;
    } 


    
    function get_group_by_program(){
      var  program_id = parseFloat($("#txtprograms").val());  
        $.ajax({
                type: 'POST',
                //url: 'customer_controller/searchid',
                url: "<?php echo base_url() . 'coaching_controller/getting_groups_by_program'; ?>",
                data: {id_program: program_id},
                cache: false,
                async: true,
                success: function(data) {
                    var data = JSON.parse(data);
                    var options = '';   
                    if(data.groups.length > 0) { 
                        if ("<?php echo $flag_for_individual_or_group; ?>" === "individual_client") {
                            options += '<option tsession="1" value="0" selected>None</option>'; 
                        } 
                        $.each(data.groups, function(index, group) { 
                            options += '<option tsession="'+group.total_group_session+'" value="' + group.id_program_groups + '">' + group.program_group + '</option>';
                        }); 
                        $("#txtprogramsgroup").html(options); 
                        getting_group_sessions_and_set_in_total_session(); 
                        $("#txtprogramsgroup").select2();
                    } else {  
                        var options = ''; 
                        if ("<?php echo $flag_for_individual_or_group; ?>" === "individual_client") {  
                          options+='<option tsession="1" value="0" selected>None</option>'; 
                          $("#txtprogramsgroup").html(options); 
                          $("#txtprogramsgroup").select2();
                        }else{ 
                            options+='<option tsession="1" value="" selected>Not Available</option>'; 
                          $("#txtprogramsgroup").html(options);  
                          $("#txtprogramsgroup").select2();
                        }
                        getting_group_sessions_and_set_in_total_session();
                    }
                }
            });
    } 

    function getting_group_sessions_and_set_in_total_session(){
        $("#txttotalsessions").val($("#txtprogramsgroup option:selected").attr('tsession'));  
        calculate_total_program_price_with_session();
    } 

</script>