<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <button type="button"  onclick="$('#addgroupmodal').modal('show');" class="btn btn-primary waves-effect waves-light" >Add <span class="m-l-5"><i class="fa fa-plus"></i></span></button>
                </div>
                <h4 class="page-title">Groups : (<?php echo $business[0]['business_name'];?>):</h4>
            </div>
        </div>

        
        <div class="row" id="divselection"> 
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-30">Filters:</h4>
                    <form action="<?php echo base_url();?>coaching_groups" method="post">
                        <input type="hidden" name="csrf_test_name" id="members_csrf" value=""/>
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="row">
                                    <div class="col-lg-2">
                                        <div class="row">
                                            <div class="col-lg-12"> <label class="control-label">Select Program Type:</label></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-12"> 
                                                <select  class="form-control" id="service_id" name="service_id">
                                                    <option value="0">All</option>
                                                    <?php foreach ($coaching_services as $cs) { ?>
                                                        <option <?php if($coaching_service===$cs['id_business_services']){echo 'selected="selected"';}?> value="<?php echo $cs['id_business_services']; ?>"><?php echo $cs['service_name']; ?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-2">
                                        <div class="row">
                                            <div class="col-lg-12"> <label class="control-label">Select Status:</label></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-12"> 
                                                <select  class="form-control" id="group_status" name="group_status">
                                                    <option  <?php if($group_status==='Active'){echo 'selected="selected"';}?> value="Active">Active</option>
                                                    <option  <?php if($group_status==='InActive'){echo 'selected="selected"';}?> value="InActive">InActive</option>
                                                    <option  <?php if($group_status==='All'){echo 'selected="selected"';}?> value="All">All</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-2">
                                        <div class="row">
                                            <div class="col-lg-12"> <label class="control-label">Get Groups</label></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" > 
                                                <button onclick="$('#members_csrf').val($('#cook').val());"  type="submit" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                            </div>
                                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><i id="cog" class="fa fa-spin fa-cog" style="display:none; font-size:26px;width: auto;margin-right: 10px;"></i></div>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
        <div class="row" >
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-30"></h4>
                    <div class="row">
                        <div class="col-lg-12">
                            <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <td>ID</td>
                                        <td>Group</td>
                                        <td>Total Group Session</td>
                                        <td>Total Schedule</td>
                                        <td>For</td>
                                        <td>Created On</td>
                                        <td>Created By</td>
                                        <td>Status</td>
                                        <td>Members</td>
                                        <td>Edit</td>
                                        <td>Schdule</td>
                                    </tr>
                                </thead>
                                <tbody> 
                                    <?php foreach($groups as $group){?>
                                    <tr>
                                        <td><?php echo $group->id_program_groups;?></td>
                                        <td><?php echo $group->program_group;?></td>
                                        <td><?php echo $group->total_group_session;?></td>
                                        <td><?php echo   $group->total_schedule > 0 ?  $group->total_schedule : 0;?></td>
                                        <td><?php echo $group->program_service_name;?></td>
                                        <td><?php echo $group->created_on;?></td>
                                        <td><?php echo $group->created_by;?></td>
                                        <td class='<?php if($group->group_status == 'InActive') {echo "text-danger"; }else{echo "text-success";}?>'><?php echo $group->group_status;?></td>
                                        <td>
                                            <a class="btn btn-sm btn-warning" href="<?php echo base_url();?>coaching_group_members/<?php echo $group->id_program_groups ;?>">
                                                <?php echo $group->members;?> Members                                               
                                            </a>                                            
                                        </td>
                                        <td> 
                                           
                                            <button onclick="openedit(<?php echo $group->id_program_groups;?>,'<?php echo $group->program_group;?>', <?php echo $group->program_service_id;?>, '<?php echo $group->group_status;?>' , <?php echo $group->total_group_session;?>);" class="btn btn-sm btn-primary"><i class="fa fa-icon fa-keyboard-o"></i></button>
                                        <td> 
                                        <?php 
                                        if($group->total_schedule < $group->total_group_session){ 
                                           ?>
                                           <a href="<?php echo base_url().'coaching_group_scheduling/'.$group->id_program_groups.'/'.$group->program_service_id.'/0';?>" class="btn btn-sm btn-icon waves-effect waves-light btn-pink m-b-5"> <i class="fa fa-calendar"></i> Schedule</a> 
                                           <?php } ?> 
                                         </td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="addgroupmodal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addgroupmodal" aria-hidden="true" style="display: none;">
            <div class="modal-dialog modal-md"  >
                <form id="addgroupform" method="POST" action="<?php echo base_url();?>coaching_controller/addgroup">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Add Group</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Group Name:</label>
                                    </div>
                                    <div class="col-md-8">
                                       <input class="form-control" name="groupname" id="groupname" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Service Name:</label>
                                    </div>
                                    <div class="col-md-8">                                        
                                        <select class="form-control" name="serviceid" id="serviceid" >
                                            <?php foreach($coaching_services as $service){?>
                                            <option <?php if($coaching_service = $service['id_business_services']){echo "selected='selected'";}?> value="<?php echo $service['id_business_services'];?>"><?php echo $service['service_name'];?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div> 
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Total Sessions</label>
                                    </div>
                                    <div class="col-md-8">
                                       <input type="number" class="form-control" name="total_group_session" id="total_group_session" min="0" value="0" />
                                    </div>
                                </div>
                            </div>
                        </div> 
                       
                    </div>
                    <div class="modal-footer">
                        <div class="row">
                            <div class="col-md-12">
                                <button type="button" onclick="$('#addgroupmodal').modal('hide');"  class="btn btn-default waves-effect waves-light pull-right m-l-5"><i class="fa fa-close"></i> Close</button>
                                <button type="button" onclick="addgroup();" class="btn btn-custom waves-effect waves-light pull-right">Save</button>
                            </div>
                        </div>
                    </div>
                </div>
                </form>
            </div>
        </div>
                            
        <div id="editgroupmodal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editgroupmodal" aria-hidden="true" style="display: none;">
            <div class="modal-dialog modal-md"  >
                <form id="editgroupform" method="POST" action="<?php echo base_url();?>coaching_controller/editgroup">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Edit Group</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Group Name:</label>
                                    </div>
                                    <div class="col-md-8">
                                       <input class="form-control" name="editgroupname" id="editgroupname" />
                                       <input type="hidden" name="id_program_groups" id="id_program_groups">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Service Name:</label>
                                    </div>
                                    <div class="col-md-8">                                        
                                        <select class="form-control" name="editserviceid" id="editserviceid" >
                                            <?php foreach($coaching_services as $service){?>
                                            <option value="<?php echo $service['id_business_services'];?>"><?php echo $service['service_name'];?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div> 
                        <div class="row">
                            <div class="col-lg-12"> 
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Tota Sessions :</label>
                                    </div>
                                    <div class="col-md-8">      
                                      <input type="number" class="form-control" name="edit_total_group_sessions" id="edit_total_group_sessions" min="0" />
                                    </div>
                            </div>
                         </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12"> 
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <label class="form-label">Group Status:</label>
                                    </div>
                                    <div class="col-md-8">      
                                        <select  class="form-control" id="editgroupstatus" name="editgroupstatus">
                                            <option value="Active">Active</option>
                                            <option value="InActive">InActive</option>                                                   
                                        </select>
                                    </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <div class="row">
                            <div class="col-md-12">
                                <button type="button" onclick="$('#editgroupmodal').modal('hide');"  class="btn btn-default waves-effect waves-light pull-right m-l-5"><i class="fa fa-close"></i> Close</button>
                                <button type="button" onclick="editgroup();" class="btn btn-custom waves-effect waves-light pull-right">Save</button>
                            </div>
                        </div>
                    </div>
                </div>
                </form>
            </div>
        </div>  
        
<script>

     $('document').ready(function(){
       
       
       
       $('#datatable-buttons').DataTable({
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "stateSave": true,
                "dom": "Bfrtlip",
                "buttons": [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"}, {
                        extend: "excel",
                        className: "btn-sm btn-warning btn-trans"
                    }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                "responsive": !0,
		"order": [[ 1, 'asc' ]],
		"displayLength": 25
		
            });
    });

    function addgroup(){
        
        $.ajax({
                url: $("#addgroupform").attr('action') || window.location.pathname,
                type: "POST",
                data: $("#addgroupform").serialize(),                 
                cache: false,
                async: true,
                success: function(data) {
                    //console.log(data);
                    if (data != "error"){
                        //location.href = '<?php echo base_url();?>coaching_members/5';
                        location.reload();
                    }
                }
                
        });
        

    }

    function openedit(id,name, serviceid, status ,total_group_session){
        
        $("#id_program_groups").val(id);
        $("#editgroupname").val(name);
        $("#editserviceid").val(serviceid);
        $("#editgroupstatus").val(status);
        $("#edit_total_group_sessions").val(total_group_session);
        
        $("#editgroupmodal").modal('show');
        
    }

    function editgroup(){
        
        $.ajax({
                url: $("#editgroupform").attr('action') || window.location.pathname,
                type: "POST",
                data: $("#editgroupform").serialize(),                 
                cache: false,
                async: true,
                success: function(data) {
                    //console.log(data);
                    if (data != "error"){
                        //location.href = '<?php echo base_url();?>coaching_members/5';
                        location.reload();
                    }
                }
                
        });
        

    }

</script>