<style>
    .modal-dialog {
        max-width: 50% !important;
    }
    .tooltip-inner {
    max-width: 400px; /* Adjust width */
    white-space: normal; /* Allow line breaks */
}
</style>


<div class="col-md-12 px-2">
    
   <!-- div for the wallet summarary  -->
   <?php if($this->session->flashdata('message_name')): ?>
      <p  class="py-2 text-success text-center ">
         <?php echo $this->session->flashdata('message_name');?>
      </p>
       <?php endif; ?>
   
   <div class="d-flex py-2 justify-content-between align-item-center">
      
      <h3 class="text-center py-3 m-0">Fare Management</h3>

      <span class="py-3">  <a href="<?=base_url()?>Admin/add_fare" role="button" class="btn btn-primary text-white text-decoration-none"  >Add Fare</a>
   </span>
    </div>

   
 
   
  
    <div class="tab-content" id="pills-tabContent">
         <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
            <caption >
           
                   
                   <div class="col-lg-7 row px-0 py-1">
                      <div class="col-lg-5">
                         <div class="d-flex justify-content-end align-items-center gap-2 ">
                            <i class="fa fa-filter text-primary fw-bolder" aria-hidden="true"></i>
                            <label class="fw-bolder">Filter</label>
                            <input type="text" name="daterange" id="daterange" class="form-control" value="" placeholder="Select Date" autocomplete="off">
                         </div>
                      </div>
                      <div class="d-flex gap-2 col-lg-7"> 
                         <input type="text" name="search_filter" id="search_filter" class="form-control" value="" placeholder="Search data" autocomplete="off">
                         <button class="btn btn-primary searchBtn">Search</button>
                         <a href="javascript:void(0)" class="btn btn-warning" onclick="resetfilter()">Reset</a>   
                      </div>
                   </div>
              </div>

                
             
            </caption>

            <!-- //////////// -->
         </div>
        
      </div>

   

   <div class="border rounded-2 p-3 bg-white " id="w-sum">

      <!-- ==========tab=========== -->
      <ul class="nav nav-pills mb-3 walletsummary" id="pills-tab" role="tablist">
         <li class="nav-item d-none" role="presentation">
            <button class="nav-link active" id="pills-home-tab" data-bs-toggle="pill" data-val="all" 
            data-title-name="all" data-bs-target="#pills-home" type="button" role="tab" 
            aria-controls="pills-home" aria-selected="true">All</button>
         </li> 
      </ul>
   

      <!-- ====Admin========== -->
      <div class="tab-content" id="pills-tabContent">
        
            <div class="table-responsive bg-white p-2">
               <table class="table align-center table-hover " id="add-table" style="width:100% !important">
               </table>
            </div>
            <!-- //////////// -->
        
      </div>
   </div>

   </div>






<!-- Modal for edit fare -->
<div class="modal fade" id="editFareModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Edit Fare</h5>
        <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="<?=base_url()?>index.php/Admin/add_cabfare" method="POST">
      <div class="modal-body">
        <div class="row  mx-auto p-2 bg-white py-3 gy-3">
							<div class="form-group  col-md-6">
							<label for="gender" class=" control-label">Vehicle Type <em class="text-danger">*</em></label>
						<select class="form-control" name="veh_type" id="vehi_type" required="" fdprocessedid="q510s">
						  
						    
					                                   <option value="0" selected=""> Selected Vehicle Type</option>
 
                                                          
                                   <?php 
                        $rs = $this->Login_model->Select('tbl_vehicletype', '*', array());
                        foreach($rs as $r){?>
                        <option value="<?=$r->id;?>" ><?=$r->name;?></option>
                        <?php } ?>             
						    
                                     
						    
						    
						</select>
						</div>
						<div class="form-group  col-md-6">
							<label for="input_email" class="control-label">Per Kilometer Charge<em class="text-danger">*</em></label>
							<div class="col-sm-offset-1">
								<input class="form-control" id="per_km" placeholder="Per Kilometer Charge" name="km_charge" type="number" required="" fdprocessedid="oe8acu">
							
							</div>
						</div>
							<div class="form-group  col-md-6">
							<label for="input_status" class="control-label">Capacity </label>
							<div class="col-sm-offset-1">
								<input class="form-control" id="capa_city" placeholder="Capacity" name="capacity" type="number" required="" fdprocessedid="suh5lj">
								<span class="text-danger"></span>
							</div>
						</div>
						<div class="form-group  col-md-6">
							<label for="input_password" class="control-label">Waiting Time Limit (In Minutes)<em class="text-danger">*</em></label>
							<div class="col-sm-offset-1">
								<input class="form-control" id="wait_limit" placeholder="Waiting Time Limit in minute" name="wait_limit" type="number" required="" fdprocessedid="mgocw">
								
							</div>
						</div>
					
					
						<div class="form-group  col-md-6 ">
							<label for="input_status" class="control-label">Waiting Charges (Per Minute) <em class="text-danger">*</em></label>
							<div class="col-sm-offset-1">
								<input class="form-control" id="wait_charge" placeholder="Waiting Charges" name="wait_charge" type="number" required="required" fdprocessedid="a7cexj">
							
							</div>
						</div>
					
						<div class="form-group  col-md-6">
							<label for="input_status" class="control-label">Schedule Ride Fare* </label>
							<div class="col-sm-offset-1">
								<input class="form-control" id="sch_ride" placeholder="Schedule Ride Fare" name="sch_ride" type="number" required="" fdprocessedid="qld08m">
								<span class="text-danger"></span>
							</div>
						</div>
					
					
								<div class="form-group  col-md-6">
							<label for="gender" class="control-label">Apply Night Fare <em class="text-danger">*</em></label>
						<select class="form-control" id="night_fared" onchange="toggleDivVisibility()" name="night_fare_sts" required="" >
						   <option >Select Option</option>
						     <option value="1">Yes</option>
						      <option value="0">No</option>
						</select>
						</div>
						
					
				<div class="col-md-12" id="night_fare_div" style="display:none;">
                <div class="panel panel-info">
                  <div class="panel-header">
                    <h4> Night Fare Details </h4>
                  </div>
                  <div class="panel-body">
                    <div class="row">
                   
                      <div class="night-fare-row">
                        <div class="row">
                          <div class="col-md-4">
                            <div class="input-addon">
                              <label>
                                Start Time
                              </label>
                             <input type="time" id="strt_time" class="form-control" name="strt_time">
                              <span class="input-suffix"></span>
                            </div>
                           
                          </div>
                          <div class="col-md-4">
                            <div class="input-addon">
                              <label>
                                End Time
                              </label>
                              <input type="time" id="end_time" class="form-control" name="end_time">
                            </div>
                           
                          </div>
                          <div class="col-md-4 night-fare-wrap">
                            <div class="input-addon">
                              <label class="night-fare-label">
                                Night Fare x(%)
                                <i id="night-fare-tooltip" data-toggle="tooltip" data-html="true" class="glyphicon  glyphicon-info-sign admin_fare_tooltip" title="Normal fare = 100<br>Night Fare x = 2 <br>Night Time Pricing x2 = 100<br>sub total = 200"></i>
                              </label>
                              <input type="text" name="night_fare" id="nigt_fare_charge" class="form-control " placeholder="Enter nigh fare charges">
                           <span id="error-message" style="color: red; display: none;">Percentage cannot be greater than 100</span>
                            </div>
                            
                          </div>
                        </div>
                       
                      </div>
                    </div>
                  </div>
                </div>
              </div>
				<input type="hidden" name="auto_id" id="auto_ide">
			<div class="col-sm-6 ">
							<label class="control-label ng-binding"> Fare Status<em class="text-danger">*</em></label>
							<div class=" col-sm-offset-1">
								<select class="form-control" name="status" id="d_sts" required="" >
							
									<option value="0" selected="selected">Active</option>
									<option value="1">InActive</option>
								
								</select>
							</div>
						</div>
				
				
		
					</div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save Changes</button>
      </div>
      </form>
    </div>
  </div>
</div>


<script>
   /*****************start for admin***************/
   $('body').on('click','.walletsummary .nav-link',function(){
       	$('#add-table').DataTable().destroy();
       	loadData();
   });
   
   $('#daterange').daterangepicker({
       locale: {
       format: 'DD/MM/YYYY'
       },
       maxDate: new Date(),
       
   });
   
    $('input[name="daterange"]').val('');
    loadData();
   function loadData()
   {
       var data_type=$('.walletsummary .nav-link.active').attr('data-val');   

       $('#print_All').data('data-type', data_type);       
       var searchvalue = $('#search_filter').val();
      
       var table = 'add-table' ;
        $('#'+table).DataTable({
         "ordering": false,
       'order': [[0, 'desc']], 
       'retrieve': true,
        'processing': true,
        'serverSide': true,
        'searching': false,
        //'serverMethod': 'post',
        'ajax': {
         'url':'<?php echo base_url();?>index.php/Admin/get_farelist',
          "type": "POST",
          "data": {
       		"daterange": $('#daterange').val(),
       		"data_type":data_type,
            'searchvalue':searchvalue,
       	}
        },

        'columns': [
         { title:'Sr.No',data: 'i' },
         { title:'Date',data: 'date' },
         { title:'Vehicle Type',data: 'veh_type' },         
         { title:'Per Km(in Rs)',data: 'per_km' }, 
         { title:'Capacity',data: 'cap' }, 
         { title:'Wait Limit(in Min)',data: 'wait_limit' },        
           { title:'Wait Charge(Per Min)',data: 'wait_Charge' }, 
         { title:'Sch Fare(in Rs)',data: 'sch_fare' }, 
         { title:'Night Fare',data: 'night_status' }, 
         { title:'Night fare Info',data: 'info' }, 
         { title:'Status',data: 'status' },
    { title:'Action',data: 'act' }
       
        ]
       });
       
       }
       
       $('body').on('click','.applyBtn',function(){
       $('#add-table').DataTable().destroy();
       loadData();
       });

       $('body').on('click','.searchBtn',function(){
         $('#add-table').DataTable().destroy();
         loadData();
         });

       
       $('body').on('click','.cancelBtn',function(){
            $('#daterange').val('');
            $('#daterange').daterangepicker({
       		locale: {
       			format: 'DD/MM/YYYY'
       		 },
       		 maxDate: new Date(),
            });
           
       
       	$('#add-table').DataTable().destroy();
       	loadData();
       });
   
       function resetfilter()
      {
            $('input[name="daterange"]').val('');
            $('input[name="search_filter"]').val('');
             $('#add-table').DataTable().destroy();
           loadData();
      }
   
</script>


<script>
$(document).ready(function () {
    $('.dropdown').hover(
        function () {
            $(this).find('.dropdown-menu').stop(true, true).slideDown(200);
        },
        function () {
            $(this).find('.dropdown-menu').stop(true, true).slideUp(200);
        }
    );
});

 </script>



<script>
       	//edit driver
	function edit_fare(id)
	{
	    
	    
	     $('#editFareModal').modal('show');
	    
	    
	    
	    
		
		$.ajax({
			url:  "<?php echo base_url()?>Admin/edit_fare_data", 
			data: {id:id},
			type: "POST", 
			dataType: 'json',
			success: function (data) {
			   var json = JSON.stringify(data);
			   var resultdata=JSON.parse(json);
			   if(resultdata.status==0)
			   {
				  //alertify.error(resultdata.message);
			   }
			   else if(resultdata.status==1)
			   {
			       
			       if(resultdata.night_fare_status == 1){
			           
			           const optionSelect = document.getElementById('night_fared');
  
  const hiddenDiv = document.getElementById('night_fare_div');

  hiddenDiv.style.display = 'block';
    optionSelect.value = 1 ;

			           
			       }
			       
			        $('#auto_ide').val(resultdata.id);
			        $('#night_fared').val(resultdata.night_fare_status);
			        $('#end_time').val(resultdata.end_time);
			        $('#strt_time').val(resultdata.strt_time);
			        $('#nigt_fare_charge').val(resultdata.nightfare_charge);
			        $('#sch_ride').val(resultdata.sch_fare);
			        $('#wait_charge').val(resultdata.wait_charge);
			        
			        $('#wait_limit').val(resultdata.wait_time_limit);
			        $('#capa_city').val(resultdata.capacity);
			        $('#per_km').val(resultdata.per_km);
			        $('#vehi_type').val(resultdata.vehicle_type);
			       
            
			       
				
			   } 
			}
		}); 
	}
    
    
     function toggleDivVisibility() {

  const optionSelect = document.getElementById('night_fared');
  
  const hiddenDiv = document.getElementById('night_fare_div');

  
    if (optionSelect.value === '1') {
      hiddenDiv.style.display = 'block';
    } else {
      hiddenDiv.style.display = 'none';
    }
}
    
    
</script>



<script>
$(document).ready(function () {
  
    function validateNightFare() {
        let value = parseFloat($('#nigt_fare_charge').val());
        if (isNaN(value) || value < 0 || value > 100) {
            $('#error-message').show(); 
            $('#nigt_fare_charge').val(''); 
            $('#nigt_fare_charge').addClass('is-invalid');
        } else {
            $('#error-message').hide(); 
            $('#nigt_fare_charge').removeClass('is-invalid'); 
        }
    }

   
    $('#nigt_fare_charge').on('input', function () {
        validateNightFare();
    });
});

</script>



  <script>            
      $('#print_All').on('click', function () {                
          var data_type = $(this).data('data-type');
          if (typeof data_type === 'undefined' || data_type === null || data_type.trim() === '') {
            data_type = 'all';
          }               
          var url = $(this).data('base-url') + data_type;
          window.open(url, '_blank');
      });

   </script>