<?php
$this->load->view ( $left );
$this->load->view ( $header );
?>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2>Item List</h2>
    </div>
    <div class="col-sm-8">
        <div class="title-action">
            <a href="<?php echo site_url("items/newitem");?>" class="btn btn-primary addNewUser">
                <i class="fa fa-plus-square-o"></i>
                <span class="m-l-xs">Add Item</span>
            </a>
            <button class="btn btn-info" data-toggle="modal" data-target="#myModal">
                    <i class="fa fa-plus-square-o"></i>
                    <span class="m-l-xs">Add Stock</span>
                </button>
        </div>
    </div>
</div>
<div class="wrapper wrapper-content">
    <div class="row text-center animated fadeInRightBig">
        <table class="table" id="myTable">
        	<thead>
        		<tr>
        			<th>Name</th>
        			<th>Description</th>
                    <th>Rate Sale</th>
                    <th>Rate Purchase</th>
                    <th>GST %</th>
        			<th>HSN</th>
					<th>Edit/Delete</th>					
        		</tr>
        	</thead>
            <tbody>
                <?php if (count($item_list) > 0) {
                    foreach ($item_list as $key_item => $value_item) {?>
                        <tr>
                           <td><?php echo !empty($value_item->item_name) ? $value_item->item_name : '';?></td>
                           <td><?php echo !empty($value_item->item_description) ? $value_item->item_description : '';?></td>
                           <td><?php echo !empty($value_item->rate_sale) ? $value_item->rate_sale  : '';?></td>
                           <td><?php echo !empty($value_item->rate_purchase ) ? $value_item->rate_purchase : '';?></td>
                           <td><?php echo !empty($value_item->gst_rate ) ? $value_item->gst_rate : '0';?></td>
                           <td><?php echo !empty($value_item->hsn_code ) ? $value_item->hsn_code : '';?></td>
                           <td>
                                <a href="<?php echo site_url('items/print_barcode/'.$value_item->item_id);?>" target="_blank" ><button class="btn btn-info btn-xs"><i class="fa fa-print"></i></button></a>
                                <a href="<?php echo site_url('items/newitem/'.$value_item->item_id);?>"><button class="btn btn-info btn-xs"><i class="fa fa-edit"></i></button></a>
                           </td>
                        </tr>
                <?php }
                }?>
            </tbody>
        </table>
    </div>
    <div class="modal inmodal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
        <div class="modal-content animated bounceInRight">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <!-- <i class="fa fa-laptop modal-icon"></i> -->
                    <h4 class="modal-title">Add Stock</h4>
                    <input type="hidden" name="item_id_cur" class="item_id_cur" value="" >
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>Item</label> 
                        <input type="text" name="item_name" id="item_name" placeholder="Enter Item" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Sample Input</label> 
                        <input type="number" name="quantity" id="quantity" placeholder="Enter Quantity" class="form-control">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary add_stock">Save</button>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $this->load->view($footer); ?>
<script type="text/javascript" src="<?php echo js_url('jquery.scannerdetection');?>"></script>
<script type="text/javascript">
	$(document).ready(function(){
       // $('#myTable').DataTable();

        var items_autocomplete = JSON.parse('<?php echo $items_autocomplete;?>');
        var items_barcode = JSON.parse('<?php echo $items_barcode;?>');
        var item_id = 0;
        var quantity = 0;
        var $input = $("#item_name");
        $input.typeahead({
          source: items_autocomplete
          //autoSelect: true
        });

        $(document).scannerDetection({
            timeBeforeScanTest: 200, // wait for the next character for upto 200ms
            startChar: [120], // Prefix character for the cabled scanner (OPL6845R)
            endChar: [13], // be sure the scan is complete if key 13 (enter) is detected
            avgTimeByChar: 40, // it's not a barcode if a character takes longer than 40ms
            onComplete: function(barcode, qty){ 
                            // alert(barcode); 
                            // var rowid = $(document.activeElement).data('tr');
                            // items[rowid].item_id = items_barcode[barcode]['item_id'];
                            // items[rowid].item_name = items_barcode[barcode]['item_name'];
                            // items[rowid].hsn_code = item_hsn[items[rowid].item_id];
                            // items[rowid].item_gst = gst_list[items[rowid].item_id];
                            // items[rowid].unit_price = item_sale_price[items[rowid].item_id];
                            // items[rowid].uom = item_uom[items[rowid].item_id];
                            //alert(items_barcode[barcode]['item_id']);
                            $('.item_id_cur').val(items_barcode[barcode]['item_id']);
                            
                            //item_id = items_barcode[barcode]['item_id'];
                            $('#item_name').val(items_barcode[barcode]['item_name']);
                            
                        } // main callback function 
        });

        $(document).on('change','#item_name',function(){
            var current = $(this).typeahead("getActive");
            if (current) {
                // Some item from your model is active!
                if (current.name == $(this).val()) {
                  // This means the exact match is found. Use toLowerCase() if you want case insensitive match.
                    item_id = current.id;
                    $('.item_id_cur').val(current.id);
                } else {
                  item_id = 0;
                }
            } else {
              item_id = 0;
            // Nothing is active so it is a new value (or maybe empty value)
            }
       });

        setInterval(function(){
            
                $('.add_stock').unbind('click');
                $('.add_stock').bind('click',function(){
                    
               // alert("HI");    
                    
                quantity = $('#quantity').val();
                item_id = $('.item_id_cur').val();
                //alert(item_id+"---"+quantity);
                
                if ((item_id > 0) && (quantity > 0)) {
                    $.ajax({
                        url: '<?php echo site_url('items/add_stock');?>',
                        type: "POST",
                        data:{
                            'item_id'  : item_id,
                            'quantity'  : quantity
                        },
                        success: function(data){ 
                            item_id = 0;
                            quantity = 0;
                            $('#item_name').val('');
                            $('#quantity').val('');
                            $('#myModal').modal('toggle');
                        }
                    });
                }
            });
                
            },1000);
   
	});
</script>

<script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="//cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script> 	
	<script src="//cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
	<script src="//cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script> 
    <script src="//cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>

<script type="text/javascript">
	var jq = jQuery.noConflict();

	jq(document).ready(function() {
	jq('#myTable').DataTable( {
			dom: 'Bfrtip',
			buttons: [
				'copy', 'csv', 'excel', 'pdf', 'print'
			]
		} );
	} );

</script>	