<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">
                    <!-- <a type="button" href="<?php echo base_url(); ?>coachingsearch" class="btn btn-custom waves-effect waves-light" >Add New Enrollment <span class="m-l-5"><i class="fa fa-plus"></i></span></a>                     -->
                </div>
                <h4 class="page-title">Prospect Students (<?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_members/5" 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="coaching_service" name="coaching_service">
                                                <option >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">View:</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select class="form-control" id="filter_veiw" name="filter_veiw">                                               
                                                <option <?php if(isset($status)){if($status=='Active'){echo 'selected="selected"';}} ?> value="Active">Active</option>
                                                <option <?php if(isset($status)){if($status=='Completed'){echo 'selected="selected"';}} ?> value="Completed">Completed</option>
                                                <option <?php if(isset($status)){if($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 Members</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>
                                        <th width="2%">ID</th>
                                        <th width="28%">Name</th>
                                        <th width="5%">Gender</th>
                                        <th width="10%">Care Of</th>
                                        <th width="10%">Cell</th>
                                        <th width="10%">Email</th>
                                        <!--<th width="10%">Address</th>-->
                                        <th width="10%">Profession</th>
                                        <!-- <th width="5%">Ref Staff</th> -->
                                        <th width="5%"></th>                                     
                                    </tr>
                                </thead>
                                <tbody>  
         
                                     <?php  
                                          if(isset($prospect_students)){
                                             foreach($prospect_students  as $students){    
                                                ?> 
                                                <tr>
                                                    <td><?php echo  $students['id_customers']; ?></td>
                                                    <td><?php echo  $students['customer_name']; ?></td>
                                                    <td><?php echo  $students['customer_gender']; ?></td>
                                                    <td><?php echo  $students['customer_careof']; ?></td>
                                                    <td><?php echo  $students['customer_cell']; ?></td>
                                                    <td><?php echo  $students['customer_email']; ?></td>
                                                    <td><?php echo  $students['profession']; ?></td>
                                                    <!-- <td><?php echo  $students['referencing_staff']; ?></td> --> 
                                                    <td><span style="cursor:pointer;" class="text-primary" onclick="openupdate(<?php echo $students['id_customers'];  ?>);"><button class="btn btn-sm btn-primary">edit</button></span></td>
                                                </tr>
                                                <?php    
                                             }
                                            }
                                     ?>
                                   
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div> 

  <!--Edit Customer Modal-->
  <div id="editcustomer" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editsupplier" aria-hidden="true" style="display: none;">
            <div class="modal-dialog modal-lg">
                <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 Customer</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="txteditcustomername" class="control-label">Name</label>
                                            <input tabindex="1" type="text" class="form-control" placeholder="Customer Name" id="txteditcustomername" name="txteditcustomername">
                                        </div> 
                                    </div> 
                                </div>                               
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="txteditcustomerphone1" class="control-label">Phone 1</label>
                                            <input tabindex="3" type="text" class="form-control numeric" placeholder="Customer Phone 1" id="txteditcustomerphone1" name="txteditcustomerphone1">
                                        </div> 
                                    </div> 
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="txteditcustomertype" class="control-label">Type</label>
                                            <select tabindex="5" name="txteditcustomertype" id="txteditcustomertype" class="form-control">
                                                <option value=""></option>
                                                <option value="orange">Orange</option>
                                                <option value="green">Green</option>
                                                <option value="red">Red</option>
                                            </select>
                                        </div> 
                                    </div> 
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="txteditcustomerbalance" class="control-label">Allowed Credit</label>
                                            <input tabindex="5" type="text" class="form-control numeric" placeholder="Allowed Credit" id="txteditcustomerbalance" name="txteditcustomerbalance">
                                        </div> 
                                    </div> 
                                </div>
                            </div>

                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label for="txteditcustomercell" class="control-label">Cell</label>
                                            <input tabindex="2" type="text" class="form-control numeric" placeholder="Customer Cell" id="txteditcustomercell" name="txteditcustomercell">
                                        </div> 
                                    </div>
                                    <div class="col-md-3">
                                        <div class="form-group has-feedback">
                                            <label class="control-label">Area</label>
                                            <select class="form-control" id="txteditcustomercell" name="txteditcustomercell">
                                                <option>Gulshan</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="txteditcustomergender" class="control-label">Gender</label>
                                            <select tabindex="2" type="text" class="form-control numeric" placeholder="Gender" id="txteditcustomergender" name="txteditcustomergender">
                                                <option value="F">Female</option>
                                                <option value="M">Male</option>
                                                
                                            </select>
                                        </div> 
                                    </div>
                                </div>
                                <div class="row">
                                   
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="txteditcustomerphone2" class="control-label">Phone 2</label>
                                            <input tabindex="4" type="text" class="form-control numeric" placeholder="Customer Phone 2" id="txteditcustomerphone2" name="txteditcustomerphone2">
                                        </div> 
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="txteditcustomercard" class="control-label">Card #</label>
                                            <input tabindex="4" type="text" class="form-control numeric" placeholder="Customer Card#" id="txteditcustomercard" name="txteditcustomercard">
                                        </div>
                                    </div>
                                   
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="txteditcustomerprofession" class="control-label">Profession</label>
                                            <input tabindex="6" type="text" class="form-control" placeholder="Customer Profession" id="txteditcustomerprofession" name="txteditcustomerprofession">
                                        </div> 
                                    </div> 
                                    <div class="col-md-6">
                                        <div class="form-group has-feedback">
                                            <label class="control-label">Referencing Staff</label>
                                            <select id="txteditcustomerrefstaff" name="txteditcustomerrefstaff"  class="form-control" placeholder="Staff">
                                                 <?php foreach ($staff_list as $sl){ ?>
                                                <option value="<?php echo $sl['id_staff'];?>"><?php echo $sl['staff_fullname'];?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="txteditcustomeremail" class="control-label">Email</label>
                                    <input tabindex="7" type="text" class="form-control" placeholder="Email" id="txteditcustomeremail" name="txteditcustomeremail">
                                </div> 
                            </div> 
                             
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="txteditcustomercareof" class="control-label">Care of (<span id="labelcustomercareof"></span>)</label>
                                    <input tabindex="7" onchange="oncareofchange();" type="text" class="form-control" placeholder="Care Of" id="txteditcustomercareof" name="txteditcustomercareof">
                                </div> 
                            </div> 
                        </div>
                        
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditcustomeraddress" class="control-label">Address</label>
                                    <input tabindex="8" type="text" class="form-control" placeholder="Address" id="txteditcustomeraddress" name="txteditcustomeraddress">
                                </div> 
                            </div> 
                        </div>
                        <div class="row m-b-5">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <label for="txteditcustomeranniversary" class="control-label">Wedding Anniversary</label>
                                    </div>
                                    <div class="col-sm-12">
                                        <div class="input-group">
                                            <input tabindex="9" type="text" class="form-control" placeholder="yyyy/mm/dd" id="txteditcustomeranniversary" name="txteditcustomeranniversary">
                                            <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 class="col-md-6">
                                <div class="form-group">
                                    <div class="col-lg-12">
                                        <label for="txteditcustomerbirthday" class="control-label">Birthday</label>
                                    </div>
                                    <div class="col-lg-4">
                                        <select tabindex="10" id="txteditcustomerbirthday" name="txteditcustomerbirthday" class="form-control">
                                            <option value=""></option>
                                        </select>
                                    </div>
                                    <div class="col-lg-8">
                                        <select tabindex="11" id="txteditcustomerbirthmonth" name="txteditcustomerbirthmonth" class="form-control">
                                            <option value=""></option>
                                            <option value="January">January</option>
                                            <option value="February">February</option>
                                            <option value="March">March</option>
                                            <option value="April">April</option>
                                            <option value="May">May</option>
                                            <option value="June">June</option>
                                            <option value="July">July</option>
                                            <option value="August">August</option>
                                            <option value="September">September</option>
                                            <option value="October">October</option>
                                            <option value="November">November</option>
                                            <option value="December">December</option>
                                        </select>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="txteditcustomerallergies" class="control-label text-danger">Allergies Alert</label>
                                            <input tabindex="12" type="text" class="form-control " placeholder="Note down any allergies the customer may have" id="txteditcustomerallergies" name="txteditcustomerallergies">
                                        </div> 
                                    </div> 
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="txteditcustomeralert" class="control-label text-warning">Customer Alert</label>
                                            <input tabindex="13" type="text" class="form-control " placeholder="Note down any other alerts you want to get when the customer visits . . ." id="txteditcustomeralert" name="txteditcustomeralert">
                                        </div> 
                                    </div> 
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <input tabindex="14" type="hidden" id="txteditcustomerid" name="txteditcustomerid">
                        <button onclick="update();" type="button" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!--End Edit Customer Modal-->
    


<script>
    
    $('document').ready(function(){
        fillBday();
        jQuery('#txteditcustomeranniversary').datepicker({
                    autoclose: true,
                    todayHighlight: true,
                    format: 'yyyy/mm/dd'
                });

                jQuery('#txtcustomeranniversary').datepicker({
                    autoclose: true,
                    todayHighlight: true,
                    format: 'yyyy/mm/dd'
                });
       
       $('#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
		
            });
            //changeview();
    });
    
    function changeview(){
        $('#datatable-buttons tr').each(function(row, tr) {
            //$(this).css('display','');
            console.log( $(tr).find('td:eq(7)').text().trim());
            console.log( $(tr).find('td:eq(8)').text().trim());
            if(row>0){
                var total = $(tr).find('td:eq(7)').text().trim();
                var booked = $(tr).find('td:eq(8)').text().trim();
                var status='';

                if(parseInt(total) <= parseInt(booked)){status = 'Completed';}
                else if(total > booked) {status = 'Active';}

                if($("#filter_veiw").val() != 'All'){ 
                    if(status != $("#filter_veiw").val()){
                        $(this).css('display','none');            
                    }else{
                        $(this).css('display','');
                    }
                }else{
                    $(this).css('display','');
                }
            }
        });
    } 



    function openupdate(id_customers) {
                //var name = $('#customername' + id).val();
                //console.log($(this));
                
                
                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url(); ?>customer_controller/edit_customers',
                    data: {id_customers: id_customers},
                    dataType: "json",
                    cache: false,
                    async: true,
                    success: function(data){
                        
                        $("#txteditcustomerid").val(id_customers);
                        $("#txteditcustomername").val(data.customer_name);
                        $("#txteditcustomercell").val(data.customer_cell);
                        $("#txteditcustomergender").val(data.customer_gender);
                        $("#txteditcustomerphone1").val(data.customer_phone1);
                        $("#txteditcustomerbalance").val(data.allowed_balance);                        
                        $("#txteditcustomerphone2").val(data.customer_phone2);
                        $("#txteditcustomercard").val(data.customer_card);
                        $("#txteditcustomeraddress").val(data.customer_address);
                        $("#txteditcustomeremail").val(data.customer_email);
                        $("#labelcustomercareof").html(data.customer_careof);
                        enable_detailcareof();
                        $("#txteditcustomerbirthday").val(data.customer_birthday);
                        $("#txteditcustomerbirthmonth").val(data.customer_birthmonth);
                        $("#txteditcustomerallergies").val(data.customer_allergies);
                        $("#txteditcustomeranniversary").val(data.customer_anniversary);
                        $("#txteditcustomeralert").val(data.customer_alert);
                        $("#txteditcustomertype").val(data.customer_type);
                        $('#txteditcustomerprofession').val(data.profession);
                        $('#txteditcustomerrefstaff').val(data.referencing_staff);
                        $("#editcustomer").modal('show');
                        
                    }
                });
            }

            function update() {
                customer_submit_update();

            }
            
            function customer_submit_update(){
                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url(); ?>customer_controller/update_customer',
                    data: {id_customer: $("#txteditcustomerid").val(), 
                        customer_name: $("#txteditcustomername").val(), 
                        customer_cell: $("#txteditcustomercell").val(), 
                        customer_gender: $("#txteditcustomergender").val(), 
                        customer_phone1: $("#txteditcustomerphone1").val(), 
                        customer_phone2: $("#txteditcustomerphone2").val(), 
                        customer_card: $("#txteditcustomercard").val(), 
                        customer_address: $("#txteditcustomeraddress").val(), customer_birthday: $("#txteditcustomerbirthday").val(), customer_birthmonth: $("#txteditcustomerbirthmonth").val(), customer_email: $("#txteditcustomeremail").val(), customer_careof:$("#txteditcustomercareof").val(), customer_anniversary: $("#txteditcustomeranniversary").val(), customer_allergies: $("#txteditcustomerallergies").val(), customer_alert: $("#txteditcustomeralert").val(), customer_type: $("#txteditcustomertype").val(), profession: $('#txteditcustomerprofession').val(), 
                        referencing_staff: $('#txteditcustomerrefstaff').val(),
                        allowed_balance: $("#txteditcustomerbalance").val()
                    },
                    success: function(data) { 
                        console.log(data,"data");
                        var result = data.split("|");
                        if (result[0] === "success") {
                            toastr.success(data, 'Customer Updated');
                            location.reload();
                        }
                    }
                });
            }
            
            function oncareofchange(){
                var data = $("#txteditcustomercareof").select2('data');

                if($("#txteditcustomercell").val()===""){
                    $("#txteditcustomercell").val(data.customer_cell);
                }
            }

            function onaddcareofchange(){
                var data = $("#txtcustomercareof").select2('data');

                if($("#txtcustomercell").val()===""){
                    $("#txtcustomercell").val(data.customer_cell);
                }
            }    
            function enable_detailcareof(){
            $("#txteditcustomercareof").select2({
                 ajax: {
                   url: '<?php echo base_url();?>customer_controller/searchnameforco',
                   dataType: 'json',
                   quietMillis: 1000,
                   data: function (term, page) {

                     return {
                         customername: term, // search term
                         page_limit: 30, // page size
                         page: page // page number
                     };
                   },
                   results: function (data, page) {

                         var more = (page * 30) < data.length;
                         return {results: data, more: more};
                     }
                   },
                 escapeMarkup: function (m) { return m; }, // let our custom formatter work
                 minimumInputLength: 3,
                 formatResult: function (option) {
                    return option.customer_name ;
                 },
                 formatSelection: function (option) {
                     return option.customer_name;
                 }
               });

         }
         function enable_addcareof(){
            $("#txtcustomercareof").select2({
                 ajax: {
                   url: '<?php echo base_url();?>customer_controller/searchnameforco',
                   dataType: 'json',
                   quietMillis: 1000,
                   data: function (term, page) {

                     return {
                         customername: term, // search term
                         page_limit: 30, // page size
                         page: page // page number
                     };
                   },
                   results: function (data, page) {

                         var more = (page * 30) < data.length;
                         return {results: data, more: more};
                     }
                   },
                 escapeMarkup: function (m) { return m; }, // let our custom formatter work
                 minimumInputLength: 3,
                 formatResult: function (option) {
                    return option.customer_name ;
                 },
                 formatSelection: function (option) {
                     return option.customer_name;
                 }
               });

         }

        function customer_update_exist(customer_id, name, cell) {
                cell = cell || null;
                var res;
                $.ajax({
                    type: 'POST',
                    url: 'customer_controller/customer_update_exist',
                    data: {customer_name: name, customer_cell: cell, customer_id: customer_id},
                    async: false,
                    success: function(response) {
                        res = response;
                    }
                });
                return res;
            }
            
            
        function fillBday() {
                for (x = 1; x <= 31; x++) {
                    $("#txt-customer-bday").append('<option value=' + x + '>' + x + '</option>');
                    $("#txtcustomerbirthday").append('<option value=' + x + '>' + x + '</option>');
                    $("#txteditcustomerbirthday").append('<option value=' + x + '>' + x + '</option>');

                }
            }
            
     function previousdirect(id) {
        if(parseInt(id) > 1){
            var customer_id = id;
            <?php if( $this->session->userdata('role')=="Sh-Users"){ ?>
                window.open('<?php echo base_url();?>sh_customer_previous_visit/' + customer_id);
            <?php } else {?>
                 window.open('<?php echo base_url();?>customer_previous_visit/' + customer_id);
            <?php } ?>
        }

    }
    
</script>