<?php
if($_GET)
{
    $bill_id     =   $_GET['bill_id'];
}
else
{
    $bill_id     =   '';
}
?>
<div style="color:green;text-align:center" class="success"><strong><?php if($this->session->flashdata('msg')){ echo $this->session->flashdata('msg');} ?></strong></div>
<div class="box-body">
	<form role="form" id="bill_search" name="bill_search" action="<?php echo base_url();?>Invoice/invoice_list" method="post">
     <div class="row"> 
       <div class="col-md-">
          <div class="col-md-3">
           <div class="form-group">
                  <label>From Date</label>
                  <input type="date" class="form-control" name="bill_from_date" value="<?php echo $bill_from_date;?>">
            </div>
            </div>
			
			<div class="col-md-3">
           <div class="form-group">
                  <label>To Date</label>
                  <input type="date" class="form-control" name="bill_to_date" value="<?php echo $bill_to_date;?>">
				  
            </div>
            </div>
			<div class="col-md-3">
           <div class="form-group">
              	<button class="btn btn-block btn-warning" id="search" name="search" value="search"><b>Search</b></button>    
            </div>
            </div>
			

        <div class="col-md-1">

        </div>
        
        <div class="col-md-3">
            
              
              	<!--<a href="<?php echo base_url();?>Invoice/add_invoice" class="btn btn-block btn-info"><b>Add Invoice</b></a>-->
				</br>
				 <button type='submit' id='submit' name="submit" class="btn btn-success btn-sm font-8" value="Submit"><i class="fa fa-play" aria-hidden="true"></i>Cancel Invoice</button>
				 </br></br>
				  <button type='submit' id='submits' name="submits" class="btn btn-success btn-sm font-8" value="Submit"><i class="fa fa-play" aria-hidden="true"></i>Update Invoice</button>
				  
            
        </div>
		</div>
		<div class="col-md-12 ">
		<div class="col-md-2 ">
		<input type="radio" class="raw_rate" id="r1" name="r" value="0"<?php if($rad=='0') echo'checked';?> checked> Active
              <input type="radio" class="raw_rate" id="r2" name="r" value="1" <?php if($rad=='1') echo'checked';?>> Deactive
			  </div>
			  <div class="col-md-4 ">
			<select class="form-control select2" name="bill_id" id="bill_id">
                   <option value=''>Invoice No</option>
                   <?php foreach($invno as $billnos) {

                     $selected = $billnos['id'] == $search_data['id'] ? 'selected' : '';
                      ?>
                       <option value="<?php echo $billnos['id'];?>"<?php echo $selected; ?>><?php echo $billnos['old_invoice_no'];?></option>
                       <?php } ?>
                   </select>
			  </div>
	</div>
	
	
	
	
		</div>
		
    <!-- <div class="col-md-12">
	 <div class="col-md-2">
	<label>Active</label>
	<input type="checkbox" class="checkbox" data-raw_prd_id="<?php echo $row['id']; ?>" data-raw_prd_inv="<?php echo $row['invoice_no']; ?>"/>
	</div>
	 <div class="col-md-2">
	<label>Deactive</label>
	<input type="checkbox" class="checkbox" data-raw_prd_id="<?php echo $row['id']; ?>" data-raw_prd_inv="<?php echo $row['invoice_no']; ?>"/>
	</div>
	</div>-->
    </form>
</div>
<!-- /.box-header -->
<div class="box-body"> 
     <table id="table_rows" class="table table-bordered table-hover">
        <thead>
            <tr>
                <th style="width: 10%">#</th>
                <th style="width: 26%">Invoice No</th>               
                <th style="width: 27%">Shipper/Exporter</th>              
                <th style="width: 27%">Customer</th>
                <th style="width: 5%">PDF</th>
			
            </tr>
        </thead>
        <tbody> 
       <?php 
	    $j=1;
	   $page_num=0;if(count($invoice_details)!=0)
        {        	
          $count = $this->uri->segment(3);
        	foreach($invoice_details as $row)
        	{
            $count++;
          ?> 

	    		<tr>
	    			<!--<td><?php echo $count;?></td>-->
					
					<td>
					<input type="checkbox" class="checkbox" data-raw_prd_id="<?php echo $row['id']; ?>" 
						data-raw_prd_inv="<?php echo $row['invoice_no']; ?>"/></td>
					
					  
	    			<td><?php echo $row['invoice_no'];?></td>	    			
	    			<td><?php echo $row['consignor_name'];?></td>
            <td><?php echo $row['customer_name'];?></td>
	    	<td class="status_text">
               <?php if($row['type']==0) { ?>
			   <button  id="pdfDownload" type="button" class="btn btn-primary btn-xs" onclick="window.location.href='<?php echo base_url();?>Invoice/pdf_Invoice/<?php echo $row['id'];?>'"><i class="fa fa-file-pdf-o" aria-hidden="true"></i>&nbsp;PDF</button>
				
			   <?php } else { ?>
				  <button  id="pdfDownload" type="button" class="btn btn-success btn-xs" onclick="window.location.href='<?php echo base_url();?>Invoice/pdf_Invoice_imp/<?php echo $row['id'];?>'"><i class="fa fa-file-pdf-o" aria-hidden="true"></i>&nbsp;PDF</button>
				 <?php }  ?>
            </td>
		

          </tr>
        	<?php $j++;}
        }?>
         </tbody>
    </table>
    <p><?php echo $links; ?></p>
</div> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">

$(function()
{
setTimeout(driver_select);	
});
/*jQuery(function()
  {
    eventHandler();      
  });*/
jQuery(document).on('change','.raw_rate',reload);

function reload()
	{
		//alert("r");
		//var r = $('input[name=r]:checked').val();
		
		//jQuery(document).on('click','#search');
		$('#search').trigger('click');
		
		//var radi=$('.raw_rate').val();
		//alert(r);
	}

jQuery(document).on('click','#submit',price_add);
	

    function price_add(e){
    	e.preventDefault();
        dataString = []; 
        checked=0;
 
 
        $('#table_rows input:checked').each(function() {
            checked=1;
            var data = {};
            data.raw_prd_id=$(this).attr('data-raw_prd_id');
            data.raw_prd_inv=$(this).attr('data-raw_prd_inv');
        
           
	    //alert(data.category_id);	
            //data.customer_type=$('#customer_type').val();
           // data.purchase_rate=$(this).closest('tr').find('td .pur_rate').val();
            //data.sales_rate=$(this).closest('tr').find('td .sales_rate').val();
            dataString.push(data);

          

        });

        if(checked==1){
		
     rowCount1 = $('#table_rows tr').length;
	//alert(rowCount1);
				var i=rowCount1-1;
				$("#tot_count").val(i);
			var count=$('#tot_count').val();

         $.ajax({
            
            url: '<?php echo base_url();?>Invoice/up_invoice/',
            type: 'post',

            data: {ids : JSON.stringify(dataString)},
            cache:false,    
            dataType: 'JSON',
            success: function (response) {
            	if(response.status='success'){
            		alert('Successfully Updated');
					
					//$('#driver_id').html(response.d);
            		//$('#driver_id').trigger('change');
            	}
				window.location='<?php echo base_url("Invoice/invoice_list")?>';
				//$('#driver_id').html(response.d);
            		//$('#driver_id').trigger('change');
            }
            });  
        }
        else{
            alert('Please Select Checkbox');
        }

    }	
	
	jQuery(document).on('click','#submits',rename);
	

    function rename(e){
		alert("Do you want replace");
		var bill   = $("#bill_id").val();
		
		if(bill=='')
		{
			alert("please select invoice number");
		}
		else{
		
		
		//alert(bill);
    	e.preventDefault();
        dataString = []; 
        checked=0;
 
 
        $('#table_rows input:checked').each(function() {
			
            checked=1;
            var data = {};
            data.raw_prd_id=$(this).attr('data-raw_prd_id');
            data.raw_prd_inv=$(this).attr('data-raw_prd_inv');
        
           
	   // alert(data.category_id);	
            //data.customer_type=$('#customer_type').val();
           // data.purchase_rate=$(this).closest('tr').find('td .pur_rate').val();
            //data.sales_rate=$(this).closest('tr').find('td .sales_rate').val();
            dataString.push(data);

          

        });

        if(checked==1){
		
     rowCount1 = $('#table_rows tr').length;
	//alert(rowCount1);
				var i=rowCount1-1;
				$("#tot_count").val(i);
			var count=$('#tot_count').val();

         $.ajax({
            
            url: '<?php echo base_url();?>Invoice/rename_invoice/',
            type: 'post',

            data: {ids : JSON.stringify(dataString),bill:bill},
            cache:false,    
            dataType: 'JSON',
            success: function (response) {
            	if(response.status='success'){
            		alert('Successfully Updated');
					window.location='<?php echo base_url("Invoice/invoice_list")?>';
					//$('#driver_id').html(response.d);
            		//$('#driver_id').trigger('change');
            	}
				
				//$('#driver_id').html(response.d);
            		//$('#driver_id').trigger('change');
            }
            });  
        }
        else{
           alert('Please Select Checkbox');
        }
		}

    }	


var eventHandler = function()
  {
      jQuery(document).on('click','.printButton',printButton);
      jQuery(document).on('click','.printbutton_aftr_approval',printbutton_aftr_approval);
  };
  
  function driver_select()
    {
		
        $("#driver_id").select({
            ajax: { 
                dataType: 'json', 
                url: '<?php echo base_url();?>invoice/get_driver',
                data: function (params) 
                    {
                        return {
                            q: params.term
                        }
                    }
            },
            initSelection: function(element, callback) 
            {
            	callback({ 'id': '<?php echo $driver_id; ?>', 'text':'<?php echo $driver_name; ?>' });
			}
        });
    }
    $('#driver_id').on('change', function() {
	  	$("#driver_name").val( $("#driver_id option:selected").text());
	});
	
  
  
  function printButton()
  {
    var count  = this.id;
    $.get('<?php echo base_url();?>Generate_bill/get_bill_details/'+count, function(data)
    {
        var contents = data;
        var frame1 = $('<iframe />');
        frame1[0].name = "frame1";
        frame1.css({ "position": "absolute", "top": "-1000000px" });
        $("body").append(frame1);
        var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
        frameDoc.document.open();
        frameDoc.document.write(contents);
        frameDoc.document.close();
        setTimeout(function () 
        {
            window.frames["frame1"].focus();
            window.frames["frame1"].print();
            frame1.remove();
        }, 1000);
    });
  }

   function printbutton_aftr_approval()
  {
    var count  = this.id;
    $.get('<?php echo base_url();?>Generate_bill/get_bill_details_after_approval/'+count, function(data)
    {
        var contents = data;
        var frame1 = $('<iframe />');
        frame1[0].name = "frame1";
        frame1.css({ "position": "absolute", "top": "-1000000px" });
        $("body").append(frame1);
        var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
        frameDoc.document.open();
        frameDoc.document.write(contents);
        frameDoc.document.close();
        setTimeout(function () 
        {
            window.frames["frame1"].focus();
            window.frames["frame1"].print();
            frame1.remove();
        }, 1000);
    });
  }

</script>

<script type="text/javascript">
       // $("#selectall").click(function(){
            //$('input:checkbox').not(this).prop('checked', this.checked);
        //});
		
		
		$(document).on('click','#update_btn',function()
    {
        var i=0;
        dataString = [];
         var house_id=$('#house_id').val();
		// alert(house_id);
		 
        $('input.checkBoxclass:checkbox:checked').each(function()
        {
             i=1;
            var data = {};
            data.ids = $(this).val();
         
            data.student_id = $('#std_id'+data.ids).val();
            data.admission_no = $('#admission'+data.ids).val();
            //data.birthdate = $('#birthdate'+data.ids).val();
            //data.admdate = $('#admdate'+data.ids).val();
            //data.rollno = $('#rollno'+data.ids).val();
            
           // data.univ_reg_no = $('#univ_reg_no'+data.ids).val();
            //data.father_phone = $('#father_phone'+data.ids).val();
            dataString.push(data);

          
        })
        if (i==0) 
        {
            alert("Please check one");
        }

    if (dataString!='') 
    {
        console.log(JSON.stringify(dataString));

        $.ajax({
                    type : "post",
                    url:'<?php echo base_url();?>student/house_group1', 
                    data:  {ids : JSON.stringify(dataString),house_id:house_id},
                    dataType: "JSON",
                    success:  function(pdata)
                    {
                        
                        if(pdata.status == "success")
                        {
                                alert("Successfully Added");
                                location.reload();
                        }
                        
                    }
             
                });
         
    } 
        

    });
		

    </script>

