<?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();?>Generate_bill/generate_bill" method="post">
     <div class="row"> 
       <div class="col-md-3">
           <div class="form-group">
               <label>Bill No</label>
                   <select class="form-control select2" name="bill_id" id="bill_id">
                   <option value=''>Bill No</option>
                   <?php foreach($billno as $billnos) {
                     $selected = $billnos['bill_id'] == $search_data['bill_id'] ? 'selected' : '';
                      $billno = sprintf("%04d",$billnos['bill_no']); ?>
                       <option value="<?php echo $billnos['bill_id'];?>"<?php echo $selected; ?>><?php echo $billnos['cmp_shrt_code']."/".$billnos['branch_shrt_code']."/".$billnos['port_of_dis_shrt_code']."/".$billno."-".date("y");?></option>
                       <?php } ?>
                   </select>
            </div>
          </div>
          <div class="col-md-3">
           <div class="form-group">
                  <label>Bill Date</label>
                  <input type="date" class="form-control" name="bill_created_date" value="<?php echo $search_data['bill_created_date'];?>">
            </div>
            </div>
            <div class="col-md-3">
              <div class="form-group">
               <label>Bill Status</label>
                   <select class="form-control" name="bill_status">
                   <option value=''>Choose One</option>
                   <?php $selected = 0 == $search_data['bill_status'] ? 'selected' : ''; ?>
                      <option value="0"<?php echo $selected;?>>Approved/Reject</option>
                      <?php $selected = 1 == $search_data['bill_status'] ? 'selected' : ''; ?>
                      <option value="1"<?php echo $selected;?>>Approved</option>
                      <?php $selected = 2 == $search_data['bill_status'] ? 'selected' : ''; ?>
                       <option value="2"<?php echo $selected;?>>Reject</option>
                       
                   </select>
            </div>
          </div>
        <div class="col-md-1">

        </div>
        
        <div class="col-md-2">
            <div class="form-group">
              	<button class="btn btn-block btn-warning" name="search" value="search"><b>Search</b></button> 
              	<a href="<?php echo base_url();?>Generate_bill/add_bill" class="btn btn-block btn-info"><b>Add Bill</b></a>
            </div>
        </div>
    </div> 
    </form>
</div>
<!-- /.box-header -->
<div class="box-body"> 
     <table id="example2" class="table table-bordered table-hover">
        <thead>
            <tr>
                <th style="width: 5%">#</th>
                <th style="width: 17%">Bill Of Landing No</th>
                
                <th style="width: 20%">Shipper/Exporter</th>
              
                <th style="width: 20%">Consignee</th>
                <th style="width: 38%"></th>
                
            </tr>
        </thead>
        <tbody> 
       <?php $page_num=0;if(count($bill_details)!=0)
        {
        	// $count	=	1;
           $count = $this->uri->segment(3);
        	foreach($bill_details as $row)
        	{
            $count++;
                $bill_no = sprintf("%05d",$row['bill_no']);
                ?> 

	    		<tr>
	    			<td><?php echo $count;?></td>
	    			<td><a href="<?php echo base_url();?>Generate_bill/view_bill/<?php echo $row['bill_id'];?>"><?php echo $row['cmp_shrt_code']."/".$row['branch_shrt_code']."/".$row['port_of_dis_shrt_code']."/".$bill_no."-".date("y");?></a></td>
	    			
	    			<td><?php echo $row['consignor_name'];?></td>
                    <td><?php echo $row['consignee_name'];?></td>
	    			<td class="status_text">
                    <?php if($row['bill_status'] == '0'){ 
                    ?>
                    <a href="#" class="approve_bill" id="<?php echo $row['bill_id'];?>">Approve</a>/<a href="#" class="reject_bill" id="<?php echo $row['bill_id'];?>">Reject</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <!-- <a style="cursor:pointer;" id="<?php echo $row['bill_id'];?>" class="printButton">Print</a> -->
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo base_url();?>Generate_bill/edit_bill/<?php echo $row['bill_id'];?>"><i class="fa fa-edit"></i></a>
                    <button style="height:40px;margin-left: 12px;width: 95px;padding-left: 1px;" id="pdfDownload" type="button" class="btn btn-info" onclick="window.location.href='<?php echo base_url();?>Generate_bill/pdf_new/<?php echo $row['bill_id'];?>'">Download PDF</button>
                    <?php } ?>
                    <?php if($row['bill_status'] == '1'){ 
                    ?>
                    <a id="<?php echo $row['bill_id'];?>">Approved</a>
                    <!-- <a style="margin-left: 52px;cursor:pointer;"  id="<?php echo $row['bill_id'];?>" class="printbutton_aftr_approval">Print</a> -->
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-primary bill_edit_popup" data-id="<?php echo $row['bill_id'];?>" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" style="background-color: white;color: #3c8dbc;border: none;width: 14px;margin-left: -15px;"><i class="fa fa-edit"></i></button>
                    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                        <div class="modal-content">
                          <div class="modal-body">
                            <form>
                              <div class="form-group">
                                <label for="recipient-name" class="form-control-label">Enter the Password:</label>
                                <input type="password" class="form-control" id="password">
                              </div>
                            </form>
                          </div>
                          <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary get_passwrd">Submit</button>
                            
                          </div>
                        </div>
                      </div>
                    </div>
                    
<input type="hidden" value="<?php echo $row['bill_id'];?>" id="hidden_bill_id">
<!--                     <a href="<?php echo base_url();?>Generate_bill/edit_bill/<?php echo $row['bill_id'];?>"><i class="fa fa-edit"></i></a>
 -->
                    <button style="height: 40px;margin: 0px -9px -2px 11px;width: 95px;padding-left: 1px;" id="pdfDownload" type="button" class="btn btn-info" onclick="window.location.href='<?php echo base_url();?>Generate_bill/pdf_after_approval_original/<?php echo $row['bill_id'];?>'">Original PDF</button>
                    <button style="height:40px;margin-left: 10px;padding-left: 8px;" id="pdfDownload" type="button" class="btn btn-info" onclick="window.location.href='<?php echo base_url();?>Generate_bill/pdf_after_approval_non_negotiable/<?php echo $row['bill_id'];?>'">NN PDF</button>
                    <?php } ?>



                    </td>
                    
                    

                    
	    			<!-- <td></td> -->
                </tr>
        	<?php }
        }?>
         </tbody>
    </table>
    <p><?php echo $links; ?></p>
</div> 
<!-- /.box-body -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">

var billNumber = 0;

$('.bill_edit_popup').click(function(){
  billNumber  = $(this).attr('data-id');
})


$('.get_passwrd').on("click",function(){

  var password = $('#password').val();
  var bill_id  = $('#hidden_bill_id').val();

  var bill_id = $(this).attr('id');
  var original_password = '201622';
  if(password == original_password){
    url = "<?php echo base_url();?>Generate_bill/edit_bill/"+billNumber;
     location.href = url;
  }
  else{
    alert('Invalide Password');
  }
  
});
    // $(function()
    // {
    //     setTimeout(port_of_dis_select);
    // });
    // function port_of_dis_select()
    // {
    //     $("#bill_id").select2({
    //         ajax: { 
    //             dataType: 'json', 
    //             url: '<?php echo base_url();?>General_masters/get_port_of_dis',
    //             cache: true,
    //             data: function (params) 
    //                 {
    //                     return {
    //                         q: params.term
    //                     }
    //                 }
    //         }
    //     });
    // }
jQuery(function()
  {
    eventHandler();      
  });
    $('.approve_bill').on("click",function(e){

        var bill_id = $(this).attr('id');
        console.log(bill_id);
        var $this = $(this);
          if (confirm('Are you sure ?')) {
              $.ajax({
            type: "POST",
            url: "<?php echo base_url();?>Generate_bill/changestatus",
            data: "bill_id="+bill_id,
            success: function(msg){



            // $("#"+loc_id).remove();
            // $("#"+loc_id).parent('div').remove();
            alert('status updated');
            $($this).closest('tr').find('.status_text').html('Approved');
            location.reload();


         }
   });
          }
     
     });



     $('.reject_bill').on("click",function(e){

        var bill_id = $(this).attr('id');
        console.log(bill_id);
        var $this = $(this);
        $.ajax({
            type: "POST",
            url: "<?php echo base_url();?>Generate_bill/rejectstatus",
            data: "bill_id="+bill_id,
            success: function(msg){



            // $("#"+loc_id).remove();
            // $("#"+loc_id).parent('div').remove();
            alert('Status updated');

            //$($this).closest('tr').find('.status_text').html('Rejected');
            $($this).closest('tr').remove();


         }
   });
     });


var eventHandler = function()
  {
      jQuery(document).on('click','.printButton',printButton);
      jQuery(document).on('click','.printbutton_aftr_approval',printbutton_aftr_approval);
  };
  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);
    });
  }

   
    
   //      $(".btnPrint").live("click", function () {


   //          var print_bill_id = $(this).attr('id');
   //          console.log(print_bill_id);
   //          $.ajax({
   //          type: "POST",
   //          url: "<?php echo base_url();?>Generate_bill/get_bill_details",
   //          data: "print_bill_id="+print_bill_id,
   //          success: function(msg){

   //          //       data = JSON.parse(msg);



   //          // // $("#"+loc_id).remove();
   //          // // $("#"+loc_id).parent('div').remove();
   //          // alert('Status updated');

   //          // //$($this).closest('tr').find('.status_text').html('Rejected');
   //          // $($this).closest('tr').remove();


   //       }
   // });

   //          // var divContents = $("#dvContainer").html();
   //          // var printWindow = window.open('', '', 'height=600,width=1000');
   //          // printWindow.document.write('<html><head><title>DIV Contents</title>');
   //          // printWindow.document.write('</head><body >');
   //          // printWindow.document.write(divContents);
   //          // printWindow.document.write('</body></html>');
   //          // printWindow.document.close();
   //          // printWindow.print();
   //      });
  





</script>