<style>
    /* enable absolute positioning */
    .inner-addon { 
        position: relative; 
    }

    /* style icon */
    .inner-addon .glyphicon {
        position: absolute;
        padding: 10px;
        pointer-events: none;
    }

    /* align icon */
    .left-addon .glyphicon  { left:  0px;}
    .right-addon .glyphicon { right: 0px;}

    /* add padding  */
    .left-addon input  { padding-left:  30px; }
    .right-addon input { padding-right: 30px; }

    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('<?php echo base_url(); ?>assets/images/page-loader-1.gif') 50% 50% no-repeat ;
        display: none;
    }
</style>
<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 id="btnPBooking" class="btn btn-primary waves-effect waves-light" onclick="open_PBooking();">
                                Add Booking
                            </button>
                        </div>
                        <h4 class="page-title">Bookings</h4>
                    </div>
                </div>
                
                 <div class="row">
                    <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().'ho_booking_list';?>' method='post'>
                            <div class="row m-b-40" id="filters">
                                <div class="col-md-12">
                                    <div class="row">
                                        <div class="col-md-2">
                                            <label class="form-label">Booking ID</label>
                                            <input name="filterbookingid" class="form-control" value="<?php echo $bookingid;?>">
                                        </div>
                                       
                                                                      
                                        <div class="col-md-2">
                                            <label class="form-label">Customer</label>
                                            <input id="filtercustomer" id="filtercustomer" name='filtercustomer' class="form-control" value="<?php echo $customer;?>">
                                        </div>
                                    
                                        <div class="col-md-2">
                                            <label class="form-label">Customer Cell</label>
                                            <input id="filtercustomercell" name="filtercustomercell" class="form-control" value="<?php echo $customercell;?>">
                                        </div>
                                        
                                        <div class="col-md-4"> 
                                             <label class="form-label">Visit Date</label>
                                            <div id="filtervisitdate" class="inner-addon left-addon">                                                
                                                <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
                                                <input style="background-color: white;" readonly class="form-control" type="text" name="visitrange" id="visitrange" />
                                                <input type="hidden" name="visitrangehidden" id="visitrangehidden" value="1" />
                                            </div>
                                        </div>
                                        
                                    </div>
                                    <div class='row'>
                                        
                                         <div class="col-md-2">
                                            <label class="form-label">Package</label>
                                            
                                            <select name="filterpackage" class="form-control">
                                                <option ></option>
                                                <?php foreach($packagetypes as $packagetype){ ?>
                                                <option <?php if($packagetype['id_package_type'] == $filterpackagetype){echo "selected='selected'";} ?> value="<?php echo $packagetype['id_package_type'];?>"><?php  echo $packagetype['service_type'];?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                        <div class="col-md-2">
                                            <label class="form-label">Booking For</label>
                                            
                                            <select name="filterbookingfor" class="form-control">
                                                <option ></option>
                                                <?php foreach($bookingfor as $package){ ?>
                                                <option <?php if($package['id_package_category'] == $filterpackagecategory){echo "selected='selected'";} ?> value="<?php echo $package['id_package_category'];?>"><?php  echo $package['business_name'].'-'.$package['service_category'];?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                        <div class="col-md-2">
                                            <label class="form-label">Advance Amount</label>
                                            <input id="advanceamount" name="advanceamount" class="form-control" value="<?php echo $advanceamount;?>">
                                        </div>
                                       
                                        <div class="col-md-4"> 
                                             <label class="form-label">Booking Date</label>
                                            <div id="filterbookingdate" class="inner-addon left-addon">
                                                
                                                <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
                                                <input style="background-color: white;" readonly class="form-control" type="text" name="bookingrange" id="bookingrange" />
                                                <input type="hidden" name="bookingrangehidden" id="bookingrangehidden" value="1" />
                                            </div>
                                        </div>
                                        <div class="col-md-2"> 
                                            <label class='form-label'>Action</label>
                                            <div class="">
                                            <input type='hidden' name='bookingstartdate' id='bookingstartdate'>
                                            <input type='hidden' name='bookingenddate' id='bookingenddate'>
                                             <input type='hidden' name='visitstartdate' id='visitstartdate'>
                                            <input type='hidden' name='visitenddate' id='visitenddate'>
                                            <button type='submit' class="btn btn-pink">Run</button>
                                            </div>
                                        </div>
                                   
                                    </div>
                                </div>
                            </div>
                                
                            </form>
                            <hr/>
                            <div class="row table-responsive m-t-20">
                                <table id="tblbookings" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>Booking ID</th>
                                            <th>Package</th>
                                            <th>Booking For</th>
                                            <th>Customer</th>
                                            <th>Cell</th>
                                            <th>Booking Date</th>
                                            <th>Visit Date</th>
                                            <th>Advance</th>
                                            <th>Discount</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php $customer=''; $totaladv=0;  $lastid=0; foreach ($bookings as $booking){
                                            $bookingid=$booking['id_bookings']; 

                                            if($lastid !== $bookingid) {
                                        ?>
                                        <?php if($lastid>0){?>
                                            <tr><td style="border-bottom: 1px solid #888"><?php echo $lastid; ?></td>
                                                <td style="border-bottom: 1px solid #888"></td>
                                                <td style="border-bottom: 1px solid #888"></td>
                                                <td style="border-bottom: 1px solid #888"><?php echo $customer; ?></td>
                                                <td style="border-bottom: 1px solid #888"></td>
                                                <td style="border-bottom: 1px solid #888"></td>
                                                <td style="border-bottom: 1px solid #888"><b>Total Advance:</b></td>                                                
                                                <td style="border-bottom: 1px solid #888"><b><?php echo $totaladv;?></b></td>
                                                <td style="border-bottom: 1px solid #888"><b></b></td>
                                                <td style="border-bottom: 1px solid #888"><button type="button" class="btn btn-sm btn-danger waves-effect m-b-5" onclick="cancelVisit(<?php echo $lastid; ?>);" id="btncancelvisit"><i class="ti ti-close"></i> Cancel</button>
                                                <button type="button" class="btn btn-sm btn-pink waves-effect m-b-5" onclick="duplicate_booking(<?php echo $lastid; ?>);" id="btnduplicatebooking"><i class="ti ti-copy"></i> Duplicate</button>
                                                <a target="_blank" href="<?php echo base_url().'ho_print_booking/'.$lastid; ?>" class="btn btn-sm btn-primary waves-effect waves-light m-b-5"> <i class="fa fa-print m-r-5"></i> <span>Print</span> </a>
                                                </td>
                                            </tr>
                                        <?php $totaladv=0;} ?>

                                        <tr>
                                            <td><?php echo $booking['id_bookings']; ?></td>
                                            <td><?php echo $booking['service_type']; ?></td>
                                            <td><?php echo $booking['service_category']; ?></td>
                                            <td><?php echo $booking['customer_name']; ?></td>
                                            <td><?php 
                                            
                                                    if( $this->session->userdata('hide_cell') && $this->session->userdata('hide_cell') === "No"){
                                                        echo $booking['customer_cell'];
                                                    } 
                                                ?></td>
                                            <td><?php echo $booking['booking_date']; ?></td>
                                            <td><?php echo $booking['visit_service_start']; ?></td>

                                            <td><?php echo $booking['advance_amount']; $totaladv=$totaladv+$booking['advance_amount'];?></td>
                                            <td><?php echo $booking['discount']; ?></td>
                                            <td>

                                                <button class="btn btn-sm btn-pink m-b-5" onclick="openEdit('<?php echo $booking['visit_id']; ?>')"><i class="ti ti-pencil"></i></button>
                                                <button onclick='showadvancemodal("<?php echo $booking['visit_id']; ?>");' class='btn btn-warning btn-sm m-b-5' id='btnadvance'><i class="ti ti-money"></i> Adv.</button>
                                            </td>
                                        </tr>

                                        <?php $lastid=$bookingid; $customer= $booking['customer_name']; } else { ?> 
                                            <tr>
                                            <td><?php echo $booking['id_bookings']; ?></td>
                                            <td><?php echo $booking['service_type']; ?></td>
                                            <td><?php echo $booking['service_category']; ?></td>
                                            <td><?php echo $booking['customer_name']; ?></td>
                                            <td><?php 
                                                    if( $this->session->userdata('hide_cell') && $this->session->userdata('hide_cell') == "No"){
                                                        $booking['customer_cell'];
                                                    } 
                                                ?></td>
                                            <td><?php echo $booking['booking_date']; ?></td>
                                            <td><?php echo $booking['visit_service_start']; ?></td>




                                            <td><?php echo $booking['advance_amount']; $totaladv=$totaladv+$booking['advance_amount'];?></td>
                                            <td><?php echo $booking['discount']; ?></td>
                                            <td>

                                                <button class="btn btn-sm btn-pink m-b-5" onclick="openEdit('<?php echo $booking['visit_id']; ?>')"><i class="ti ti-pencil"></i></button>
                                                <button onclick='showadvancemodal("<?php echo $booking['visit_id']; ?>");' class='btn btn-warning btn-sm m-b-5' id='btnadvance'><i class="ti ti-money"></i> Adv.</button>
                                            </td>
                                        </tr>

                                        <?php $lastid=$bookingid; $customer= $booking['customer_name']; }}?>
                                        <?php if(isset($booking['id_bookings'])){ ?>
                                        <tr>
                                            <td style="border-bottom: 1px solid #888"><?php echo $booking['id_bookings']; ?></td>
                                            <td style="border-bottom: 1px solid #888"></td>
                                            <td style="border-bottom: 1px solid #888"></td>
                                             
                                            <td style="border-bottom: 1px solid #888"><?php echo $booking['customer_name']; ?></td>
                                            <td style="border-bottom: 1px solid #888"></td>
                                            <td style="border-bottom: 1px solid #888"></td>
                                            <td style="border-bottom: 1px solid #888"><b>Total Advance:</b></td>                                            
                                            <td style="border-bottom: 1px solid #888"><b><?php echo $totaladv;?></b></td>
                                            <td style="border-bottom: 1px solid #888"></td>
                                            <td style="border-bottom: 1px solid #888">
                                                <button type="button" class="btn btn-sm btn-danger waves-effect m-b-5" onclick="cancelVisit(<?php echo $booking['id_bookings']; ?>);" id="btncancelvisit"><i class="ti ti-close"></i> Cancel</button>
                                                <button type="button" class="btn btn-sm btn-pink waves-effect m-b-5" onclick="duplicate_booking(<?php echo $lastid; ?>);" id="btnduplicatebooking"><i class="ti ti-copy"></i> Duplicate</button>
                                                <a target="_blank" href="<?php echo base_url().'ho_print_booking/'.$booking['id_bookings']; ?>" class="btn btn-sm btn-primary waves-effect waves-light m-b-5"> <i class="fa fa-print m-r-5"></i> <span>Print</span> </a>
                                            </td>
                                        </tr>
                                        <?php } ?>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <th>Booking ID</th>
                                            <th>Package</th>
                                            <th>Booking For</th>
                                            <th>Customer</th>
                                            <th>Customer Cell</th>
                                            <th>Booking Date</th>
                                            <th>Visit Date</th>
                                            <th>Advance</th>
                                            <th>Discount</th>
                                            <th>Action</th>


                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div><!-- end col -->
                </div>
                <!-- end row -->
                
                
                <!--modal start-->
        <div class="modal fade none-border" id="change_appointment_modal" tabindex="-1" role="dialog" aria-labelledby="Cash Register" aria-hidden="true">
            <div class="modal-dialog modal-md">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title">Update Reminder Status for visit ID:  <span id="customer_visit_id"></span></h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-inline m-b-10" >
                            <div class="form-group m-l-10">
                                <div class="checkbox checkbox-primary">
                                    <input id="reminder_sms" type="checkbox">
                                    <label for="reminder_sms">
                                        SMS
                                    </label>
                                </div>
                            </div>
                            <div class="form-group m-l-10">
                                <div class="checkbox checkbox-primary">
                                    <input id="reminder_email" type="checkbox">
                                    <label for="reminder_email">
                                        Email
                                    </label>
                                </div>
                            </div>
                            <div class="form-group m-l-10">
                                <div class="checkbox checkbox-primary">
                                    <input id="reminder_call" type="checkbox">
                                    <label for="reminder_call">
                                        Call
                                    </label>
                                </div>
                            </div>
                            <div class="form-group m-l-10">
                                
                                <div>
                                    <label for="visit_service_date">
                                       | Change Date
                                    </label>
                                    <input id='visit_service_date' class='form-control'>
                                </div>
                            </div>
                            
                        </div>
                        <form class="form-horizontal" role="form">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Customer Name</label>
                                        <div class="col-md-10">
                                            <input readonly="readonly" type="text" class="form-control" id="customername">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Customer Cell</label>
                                        <div class="col-md-10">
                                            <input readonly="readonly" type="text" class="form-control" id="customercell">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="col-md-2 control-label">Visit Discount</label>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" id="visitdiscount">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                   
                                        <label class="col-md-2 control-label">Services</label>
                                        <div class="col-md-10">
                                            <table class="table table-stripped" id="visitservices">
                                                <tbody></tbody>
                                            </table>
                                        </div>
                                   
                                </div>
                            </div>
                        </form>
                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button type="button" onclick="saveStatus();" class="btn btn-info waves-effect waves-light">Save changes</button>
                    </div>
                </div>
            </div>
        </div>
        <!--modal end-->
        
        <!---pick service modal-->
        <?php include 'modals/advance_modal.php'; ?>
        <!-- Pick service modal-->
        
    </div>
    <div class="modal fade none-border" id="duplicatemodal" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
        <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">&times;</button>
                    <h4 class="modal-title"><strong>Make Copies of the Selected Booking:</strong></h4>
                </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="form-label">Number of Copies:</label>
                            <input id="numofcopies" class="form-control numeric" type="number" max=10 min=1 value="1">
                            <input id="duplicatebookingid" class="form-control" type="hidden">
                        </div>                        
                    </div>
                    <div class="col-md-6">
                        <p class="text-primary" style="font-size:14px;">
                            This Action will make the entered number of COPIES of all the services within the booking.<br>
                            These services will be assigned to the same STAFF and same DATETIME as the original service.<br>
                            <span class="text-pink" style="font-size:16px;"><strong>NOTE: </strong>  ADVANCES WILL NOT BE COPIED WITH THE SERVICES</span>
                        </p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="row">
                    <div class="">
                    <button onclick="create_duplicates();" class="pull-right btn btn-pink">Go</button>
                    </div>                    
                </div>
            </div>
      </div>
                
                
                
    <script type="text/javascript">
        $(document).ready(function() {

        $(".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 != 46 && (e.which < 48 || e.which > 57)) {
                return false;
            }
        });


        <?php
                //$month_ini = new DateTime("first day of last month");
                //$month_end = new DateTime("last day of last month");
                
                if($bookingstartdate !== null && $bookingstartdate !==''){
                    $month_ini = $bookingstartdate;
                    $month_end = $bookingenddate;
                } else {
                    
                    
                    
                    $query_date = date('Y-m-d');
                    $date=date_create($query_date);
                    date_sub($date,date_interval_create_from_date_string("1 days"));
                    $query_date = date_format($date, 'Y-m-d');
                    
                    // First day of the month.
                    //$month_ini = date('Y-m-01', strtotime($query_date));
                    $month_ini = date('Y-m-d', strtotime($query_date));
                    // Last day of the month.
                    $month_end = date('Y-m-d', strtotime( date('Y-m-d')));
                }
                
               
                
                ?>
                <?php if($visitstartdate !== null && $visitstartdate !==''){?>        
                    var startmonth = '<?php echo date('F j, Y', strtotime($bookingstartdate));   ?>';
                    var endmonth = '<?php echo date('F j, Y', strtotime($bookingenddate));   ?>';
                    $('#filterbookingdate input').val(startmonth + ' - ' + endmonth);
                <?php } else {?>
                    var startmonth = '<?php echo date('F j, Y', strtotime($month_ini));   ?>';
                    var endmonth = '<?php echo date('F j, Y', strtotime($month_end));   ?>';
                    $('#filterbookingdate input').val(startmonth + ' - ' + endmonth);
                    
                <?php } ?>
                
                <?php if($visitstartdate !== null && $visitstartdate !==''){?>
                    var visitstartdate = '<?php echo date('F j, Y', strtotime($visitstartdate));   ?>';
                    var visitenddate = '<?php echo date('F j, Y', strtotime($visitenddate));   ?>';
                    $('#filtervisitdate input').val(visitstartdate + ' - ' + visitenddate);
                <?php } ?>
            $('#filterbookingdate').daterangepicker({
                    //format: 'MM/DD/YYYY',
                    startDate: moment().subtract(29, 'days'),
                    endDate: moment(),
                    minDate: '01/01/2012',
                    maxDate: '12/31/2099',
//                    dateLimit: {
//                        days: 60
//                    },
                    showDropdowns: true,
                    showWeekNumbers: true,
                    timePicker: false,
                    timePickerIncrement: 1,
                    timePicker12Hour: true,
                    ranges: {
                        'Today': [moment(), moment().add(1, 'days')],
                        'Yesterday': [moment().subtract(1, 'days'), moment()],
                        'Last 7 Days': [moment().subtract(6, 'days'), moment().add(1, 'days')],
                        'Last 30 Days': [moment().subtract(29, 'days'), moment().add(1, 'days')],
                        'This Month': [moment().startOf('month'), moment().endOf('month')],
                        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
                        'Last Two Months': [moment().subtract(2, 'month').startOf('month'), moment().subtract('month').endOf('month')]
                    },
                    opens: 'left',
                    drops: 'down',
                    buttonClasses: ['btn', 'btn-sm'],
                    applyClass: 'btn-success',
                    cancelClass: 'btn-default',
                    separator: ' to ',
                    locale: {
                        applyLabel: 'Submit',
                        cancelLabel: 'Cancel',
                        fromLabel: 'From',
                        toLabel: 'To',
                        customRangeLabel: 'Custom',
                        daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
                        monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                        firstDay: 1
                    }
                }, function(start, end, label) {
                   // console.log(start.toISOString(), end.toISOString(), label);
                    startdate = start;
                    enddate = end;
                    //$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                    $('#filterbookingdate input').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                    
                    $('#bookingstartdate').val(start.format('YYYY-MM-DD'));
                    $('#bookingenddate').val(end.format('YYYY-MM-DD'));
                    
                });
                 $('#filterbookingdate').on('cancel.daterangepicker', function(ev, picker) {
                    //do something, like clearing an input
                    $('#filterbookingdate input').val('');
                  });

                
                $('#filtervisitdate').daterangepicker({
                    //format: 'MM/DD/YYYY',
                    startDate: moment().subtract(29, 'days'),
                    endDate: moment(),
                    minDate: '01/01/2012',
                    maxDate: '12/31/2099',
//                    dateLimit: {
//                        days: 60
//                    },
                    showDropdowns: true,
                    showWeekNumbers: true,
                    timePicker: false,
                    timePickerIncrement: 1,
                    timePicker12Hour: true,
                    ranges: {
                        'Today': [moment(), moment().add(1, 'days')],
                        'Yesterday': [moment().subtract(1, 'days'), moment()],
                        'Last 7 Days': [moment().subtract(6, 'days'), moment().add(1, 'days')],
                        'Last 30 Days': [moment().subtract(29, 'days'), moment().add(1, 'days')],
                        'This Month': [moment().startOf('month'), moment().endOf('month')],
                        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                    },
                    opens: 'left',
                    drops: 'down',
                    buttonClasses: ['btn', 'btn-sm'],
                    applyClass: 'btn-success',
                    cancelClass: 'btn-default',
                    separator: ' to ',
                    locale: {
                        applyLabel: 'Submit',
                        cancelLabel: 'Cancel',
                        fromLabel: 'From',
                        toLabel: 'To',
                        customRangeLabel: 'Custom',
                        daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
                        monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                        firstDay: 1
                    }
                }, function(start, end, label) {
                   // console.log(start.toISOString(), end.toISOString(), label);
                    startdate = start;
                    enddate = end;
                    //$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                    $('#filtervisitdate input').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                   
                    $('#visitstartdate').val(start.format('YYYY-MM-DD'));
                    $('#visitenddate').val(end.format('YYYY-MM-DD'));
                    
                });

                
                  $('#filtervisitdate').on('cancel.daterangepicker', function(ev, picker) {
                    //do something, like clearing an input
                    $('#filtervisitdate input').val('');
                  });

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

             $('#tblbookings').DataTable({
                lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                stateSave: true,
                fixedHeader: {header: true},
                <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                    dom: "Bfrtlip",
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans", footer: true}, {
                        extend: "excel",
                        className: "btn-sm btn-warning btn-trans"
                    }, {extend: "pdf", className: "btn-sm btn-purple btn-trans", footer: true}
                , {extend: "print", className: "btn-sm btn-primary btn-trans", footer: true}],
                <?php } else { ?>
                    dom: "frtlip",
                <?php } ?> 
                responsive: !0,
                //"scrollX": true,
                ordering: false
            });
            
        });
        function open_PBooking(){
            window.location.assign('<?php echo base_url();?>ho_booking');
        }
        
        
        
        function openEdit(visit_id){
            $("#customer_visit_id").text(visit_id);
            $("#visitservices tbody").empty();
            
            
            $.ajax({
                type: 'POST',
                url: '<?php echo base_url();?>appointment_controller/get_appointment',
                data: {id_customer_visit: visit_id},
                dataType: "json",
                cache: false,
                async: true,
                success: function (data) {
                    console.log(data);
                    $("#customername").val(data[0]['customer_name']);
                    $("#customercell").val(data[0]['customer_cell']);
                    $("#visitdiscount").val(data[0]['discount']);

                    
                    if(data[0]['reminder_sms']=="Y"){$('#reminder_sms').prop( "checked", true );
                    } else {$('#reminder_sms').prop( "checked", false);}
                    if(data[0]['reminder_email']=="Y"){$('#reminder_email').prop( "checked", true );
                    } else {$('#reminder_email').prop( "checked", false);}
                    if(data[0]['reminder_call']=="Y"){$('#reminder_call').prop( "checked", true );
                    } else {$('#reminder_call').prop( "checked", false);}
                    
                    mhtml = "";
                    for (x = 0; x < data.length; x++) {
                        mhtml+="<tr>";
                        mhtml+="<td class='id_visit_service' id='" + data[x]['id_visit_services'] + "'>"+data[x]['visit_service_start']+"</td>";
                        mhtml+="<td>"+data[x]['service_type']+"</td>";
                        mhtml+="<td>"+data[x]['service_category']+"</td>";
                        mhtml+="<td>"+data[x]['service_name']+"</td>";
                        mhtml+="<td>"+data[x]['staff_name']+"</td>";
                        
                        mhtml+="</tr>";
                    }
                   // console.log(mhtml);
                    $("#visitservices tbody").append(mhtml);
                }
            });
                
            
            $("#cog").hide();
            
            $("#change_appointment_modal").modal("show");
        }
        function saveStatus(){
            var reminder_sms="N";        var reminder_email="N";        var reminder_call="N";
            if( $('#reminder_sms').is(':checked') ){reminder_sms = 'Y';}
            if( $('#reminder_email').is(':checked') ){reminder_email = 'Y';}
            if( $('#reminder_call').is(':checked') ){reminder_call = 'Y';}
        
            
            $.ajax({
                    type: "POST",
                    url: '<?php echo base_url(); ?>visits_controller/reminder_message_update',
                    data: {discount: $("#visitdiscount").val(), customer_visit_id: $("#customer_visit_id").text(), reminder_sms: reminder_sms, reminder_email: reminder_email, reminder_call: reminder_call},
                    success: function(data) {
                       if($('#visit_service_date').val()!==''){
                            
                            update_visit_date();
                            
                        } else {
                            toastr.success('Visit Updated!', 'Done!');    
                            $("#change_appointment_modal").modal("hide");
                        }
                    }
            });
            
            
            
        }
       
   
       
    function update_visit_date(){
        
        var visit_service_id=[];
        var visit_service_olddate=[];
        
        $('#visitservices tr').each(function(row, tr) {
           // console.log($(tr).find('td:eq(0)').attr('id'));
            visit_service_id.push($(tr).find('td:eq(0)').attr('id'));
            
        });
        
        $.ajax({
            type: "POST",
            url: '<?php echo base_url(); ?>ho_controller/change_visit_services_date',
            data: {customer_visit_id: $("#customer_visit_id").text(), visit_service_ids: visit_service_id, visit_service_date: $("#visit_service_date").val()},
            success: function(data) {
                toastr.success('Visit Updated!', 'Done!');    
                $("#change_appointment_modal").modal("hide");
                 window.location.href='<?php echo base_url(); ?>ho_booking_list';
            }
        });
      
    }
    
    
    function showadvancemodal(visitid){
    var myurl;
        
        myurl = '<?php echo base_url().'pos_controller/getVisitbyid'; ?>';

        $.ajax({
            type: 'POST',
            url: myurl,
            data: {id_customer_visit: visitid, anybusiness: true},
            dataType: "json",
            cache: false,
            async: true,
            success: function(data) {
                console.log(data);
                //$("#txt-customer-search").select2("val", data[0]['customer_name']);
                var mhtml = "";
                var rowcount=1;
                
                var advances=data.advances;
               
                 var totaladvance=0; var ahtml=""; var fhtml="";
                if(advances.length>0){
                    for (x = 0; x < advances.length; x++) {
                        ahtml +='<tr><td>Adv.</td><td>'+ advances[x].date +'</td><td style="text-align:right;">'+ advances[x].advance_amount +'</td><td><button type="button" class="btn btn-sm btn-default" onclick="removeadvance('+ advances[x].id_visit_advance +');" class="text-danger"><i class="ti ti-close"></i></button></td></tr>';
                        totaladvance= totaladvance+parseFloat(advances[x].advance_amount);
                    }
                    $("#adv_table tbody").html(ahtml);
                    fhtml +='<tr><td></td><td style="font-weight:bold;">Total Advance:</td><td style="font-weight:bold; text-align:right;">'+ totaladvance.toFixed(2) +'</td><td></td></tr>';
                    $("#adv_table tfoot").html(fhtml);
                }else{
                    $("#adv_table tbody").html('');
                    $("#adv_table tfoot").html('');
                }
                
                $("#visitid").val(visitid);
                $("#txtadvance").val(totaladvance);
            }
        });
    
        $("#cc_charge_setting").val($("#cc_charge").val());
        $("#advancemodal").modal("show");
    }
    
    
    function advancemodalclose(){
         window.location.href='<?php echo base_url(); ?>ho_booking_list';
           
    }
    
    
    function cancelVisit(visitid) {
       
        
        swal({
                title: "Are you sure?",
                text: "Give Reason For Cancelling This Booking:",
                type: "input",
                showCancelButton: true,
                closeOnConfirm: false,
                inputPlaceholder: "reason for cancelling"
              }, function (inputValue) {
                if (inputValue === false) return false;
                if (inputValue === "") {
                  swal.showInputError("You need to write something!");
                  return false
                } 

            $.ajax({
                //url: 'Scheduler_controller/cancelVisit',
                url: "<?php echo base_url() . 'ho_controller/cancelBooking'; ?>",
                //data: 'visitid=' +visitid,
                data: {
                    bookingid: visitid, cancelreason: inputValue
                },
                type: 'POST',
                success: function(response) {



                        swal(
                            'Canceled!',
                            'Booking ' + response + ' has been canceled.',
                            'success'
                            );
                        window.location.href='<?php echo base_url(); ?>ho_booking_list';
                    

                }
            });

        });
    }
    function removeadvance(advanceid){
         $.ajax({
            type: 'POST',
            //url: 'Scheduler_controller/active_staff_list',
            url: "<?php echo base_url() . 'visits_controller/remove_advance'; ?>",
            data:{
                
                advance_id:advanceid
                },
            dataType: "text",
            cache: false,
            async: true,
            success: function(data) {
                if(data === "Advance removed!"){
                    toastr.success(data, 'Done!');
                     window.location.href='<?php echo base_url(); ?>ho_booking_list';
                } else {
                    toastr.error(data);
                }
                
            }
        });
    }

function duplicate_booking(bookingid){

    $("#duplicatebookingid").val(bookingid);
    $("#duplicatemodal").modal('show');
}


function create_duplicates(){
    var numofcopies = $("#numofcopies").val();
    var bookingid = $("#duplicatebookingid").val();
    if(numofcopies > 0){

        $.ajax({
            type: 'POST',
            url: '<?php echo base_url(); ?>bookings_controller/duplicate_booking',
            data: { 'booking_id':bookingid, 'numofcopies':numofcopies },
            dataType: "text",
            cache: false,                    
            success: function (data) {
                location.reload();
            }
        });

}

}
    </script>
