<?php //print_r($supplier); exit(); ?>
<script type="text/javascript">
	$(document).ready(function(){
        $('select').select2();
		<?php if(isset($datatbls)): ?>
		//Data table configurations
	   	var oTable = $("#test").dataTable({
			"iDisplayLength": 10,
			"aLengthMenu": [5,10,25,50,100],
			"sPaginationType": "full_numbers",
			"aoColumns": [	null,
                            null,
							null,
							null,
							null,
							null,
							null,
							null,
							null,
							null
						] 
		});
		oTable.fnSort( [ [1,'asc'] ] );
		//Data table configurations end
		<?php endif; ?>
		
		var changeRow = new Array();
		/*Table editing start*/
		$("table.editable td").dblclick(function () {        
        
			var eContent = $(this).text();
			var eCell = $(this);
				
			if(eContent.indexOf('<') >= 0 || eCell.parents('table').hasClass('oc_disable')) return false;        
			
			if(eCell.hasClass('no-edit')){return false;}	
			eCell.addClass("editing");        
			eCell.html('<input type="text" value="' + eContent + '"/>');
			
			var eInput = eCell.find("input");
			eInput.focus();
	 
			eInput.keypress(function(e){
				if (e.which == 13) {
					var newContent = $(this).val();
					changeRow[eCell.parents('tr').find('#ID').text()] = newContent;
					calVariance(eCell.parents('tr'),newContent);
					eCell.html(newContent).removeClass("editing").animate({backgroundColor:'#b4e664'});
					// Here your ajax actions after pressed Enter button
					jQuery.ajax({
						url: "<?php echo base_url(); ?>itemcountadjust/editUpdate/",
						async: false,
						type: "POST",
						data: 'tracID='+eCell.parents('tr').find('#ID').text()+"&counted="+eCell.parents('tr').find('#counted').text(),
						dataType: "html",
						success: function(data) {
							if(data == 'success'){
								setTimeout(function(){
									location.reload();	
								},5000);
							}	
						}
					});
				}
			});
	 		
			eInput.focusout(function(){
				var newContent = $(this).val();
				changeRow[eCell.parents('tr').find('#ID').text()] = newContent;
				calVariance(eCell.parents('tr'),newContent);
				eCell.text($(this).val()).removeClass("editing").animate({backgroundColor:'#b4e664'});            
				// Here your ajax action after focus out from input
				jQuery.ajax({
					url: "<?php echo base_url(); ?>itemcountadjust/editUpdate/",
					async: false,
					type: "POST",
					data: 'tracID='+eCell.parents('tr').find('#ID').text()+"&counted="+eCell.parents('tr').find('#counted').text(),
					dataType: "html",
					success: function(data) {
						if(data == 'success'){
							setTimeout(function(){
								location.reload();	
							},5000);
						}	
					}
				});
			});
		});
		
		/*Table editing end*/
		/*$('.save').live('click',function(){
			var newArr = new Array();
			var i = 0;
			changeRow.forEach(function(val,key){
				newArr[i] = new Array(2);
				newArr[i][0] = key;
				newArr[i][1] = val;
				newArr[i][2] = <?php echo $locID?$locID:"''"; ?>;
				i++;
			});
			
			var data = JSON.stringify(newArr);
			$.ajax({
				url: "<?php echo base_url(); ?>itemcountadjust/save/",
				async: false,
				type: "POST",
				data: 'data='+data,
				dataType: "html",
				success: function(data) {
					location.reload();
				}
			});
		});*/
		
		//Locaiton change information
		$('.location').change(function(){
			if($(this).val() != 0 && $(this).val() != 'all'){
				window.location = "<?php echo base_url() ?>itemcountadjust/listing/"+$(this).val();
			}else if($(this).val() == 'all'){
				window.location = "<?php echo base_url() ?>itemcountadjust/listing/";
			}
			console.log($(this).val());
		});
	});
	
	function  calVariance(obj,nCont){
        var qty = accounting.unformat(obj.find('#quantity').text());
        var perItemPrice = accounting.unformat(obj.find('#costQty').text())/qty;
        var countPrice = perItemPrice*nCont;
        var costVariance = (qty*perItemPrice)-countPrice;
        var itemId = obj.find('#itemID').text();
        var itmDiff = qty - nCont;
        console.log(itmDiff);
		
        $.ajax({
            url: "<?php echo base_url(); ?>itemcountadjust/getcost/",
            async: false,
            type: "POST",
            data: 'itemID='+itemId+'&itmDiff='+itmDiff+'&locID='+$('.location').select2('val'),
            dataType: "html",
            success: function(data) {
                //location.reload();
                //Changeing HTML DOM values
                obj.find('#variance').text(qty - nCont).animate({backgroundColor:'#b4e664'});
                obj.find('#varCost').text(accounting.formatNumber(accounting.unformat(obj.find('#costQty').text())-data, 2)).animate({backgroundColor:'#b4e664'});
                obj.find('#costVar').text(accounting.formatNumber(data,2)).animate({backgroundColor:'#b4e664'});
            }
        });
		
	}
</script>

<div id="content">                        
    <div class="wrap">
    	<!--Header start here-->
		<div class="head">
            <div class="info">
                <!--<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
                    <?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>-->
                <ul class="breadcrumb">
                    <li><a href="<?php echo base_url() . "home" ?>"><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                    <li><a href="<?php echo base_url() . "inventoryControl" ?>"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                    <li class="active"><?php echo $this->lang->line('titlelist') ?></li>
                </ul>
            </div>
            <div class="search">
                <form action="<?php echo base_url() ?>admin/search" method="post">
                    <input name="search_text" type="text" placeholder="search..."/>                                
                    <button type="submit"><span class="i-magnifier"></span></button>
                </form>
            </div>                        
        </div>
        <!--Header end here-->
        
        <!--Alert Message start-->
        <?php 	
			$message = $this->session->flashdata('msg');
			$alert = $this->session->flashdata('danger-msg');
			if(isset($message) && $message != ''): ?>
            <div class="content alertbox">                                                
                <div class="row-fluid">
                    <div class="span12">        
                        <div class="alert alert-success">            
                            <strong>Success! </strong><?php echo $message;?>
                        </div>       
                    </div>
                </div>
            </div>
        <?php	
			elseif(isset($alert) && $alert != ''): ?>
            <div class="content alertbox">                                                
                <div class="row-fluid">
                    <div class="span12">        
                        <div class="alert alert-error">            
                            <strong>Deleted! </strong><?php echo $alert;?>
                        </div>       
                    </div>
                </div>
            </div>
		<?php endif; ?>
        <!--Alert Message end-->
        
        <!--Main content start here-->
        <div class="content">
            <div class="wrap">                    
                <div class="row-fluid">
                    <div class="span12">
                    	<?php if(!empty($location)): ?>
                        <div class="block">
                        	<div class="head">
                            	<h2><?php echo $this->lang->line('selectLoc') ?></h2>
                            </div>
                            <div class="content">
                            	<div class="controls-row"><!-- Row 4-->
                                    <div class="span5 tac">
                                    	<div class="span5 tar"><?php echo form_label($this->lang->line('selectLoc'));?></div>
                                        <div class="span6">
                                            <?php
                                                $preselcat = $locID;
												$options[''] = '--Select Location--';
                                                foreach($location as $row){
                                                    $options[$row->fldid] = $row->code.' | '.$row->desc;
                                                }
                                                $js=' class="input-medium location" disabled="disabled"';
                                                echo form_dropdown('location', $options, $preselcat, $js);
                                            ?>
                                        </div>
                                	</div>
                                    <div class="span5 tac">
                                    	<div class="span4"><?php echo $this->lang->line('formNo') ?></div>
                                        <div class="span8">
                                            <?php
                                            $data = array(
                                                'value'=>$datatbls[0]->formNo,
                                                'name'=>'formNo',
                                                'id'=>'formNo',
                                                'style'=>'color: red;',
                                                'class'=>'span5',); 
                                            echo form_input($data);
                                            ?>
                                            <?php
                                            $data = array(
                                                'value'=>$datatbls[0]->ID,
                                                'name'=>'formID',
                                                'id'=>'formID',
                                                'style'=>'',
                                                'disabled'=>'disabled',
                                                'placeholder'=>'0000',
                                                'class'=>'span5 validate[required]',); 
                                            echo form_input($data);
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="footer"></div>
                        </div>
                        <?php endif; ?>
                        
                        <?php if(isset($datatbls)): ?>
                        <div class="block">
                            <div class="head">
                                <h2><?php echo $this->lang->line('titlelist'); ?></h2>
                                <!--<ul class="buttons">
                                    <li><a href="" class="" title="New"><span class="i-plus-2"></span></a></li>
                                </ul>-->                                        
                            </div>
                            <div class="content np table-sorting">
                                <table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable">
                                    <thead>
                                    <tr>
                                        <th style="display:none">AdjustID</th>
                                        <th style="display:none">itemID</th>
                                        <th><?php echo $this->lang->line('itemCode') ?></th>
                                        <th><?php echo $this->lang->line('itemName') ?></th>
                                        <th><?php echo $this->lang->line('quantityOnHand') ?></th>
                                        <th><?php echo $this->lang->line('costQtyOnHand') ?></th>
                                        <th style="background:#f4fcec;"><?php echo $this->lang->line('counted').'<br />('.$this->lang->line('dblClk').")"; ?></th>
                                        <th><?php echo $this->lang->line('variance') ?></th>
                                        <th><?php echo $this->lang->line('cost') ?></th>
                                        <th><?php echo $this->lang->line('costVariance') ?></th>
                                    </tr>
                                    </thead>
                                     <tbody>
										<?php $bil = 0; 
                                        foreach($datatbls as $datatbl):
                                        $bil++?>
                                        <tr>
                                            <td id="ID" style="display:none"><?php echo $datatbl->ID; ?></td>
                                            <td id="itemID" style="display:none"><?php echo $datatbl->itemID; ?></td>                                                                      
                                            <td id="itemCode" class="no-edit"><?php echo $datatbl->itemCode; ?></td>
                                            <td id="name" class="no-edit"><?php echo $datatbl->name; ?></td>
                                            <td id="quantity" class="no-edit" style="text-align:right"><?php echo $datatbl->qtyOnHand; ?></td>
                                            <td id="costQty" class="no-edit" style="text-align:right"><?php echo number_format($datatbl->qtyPrice,2,'.',','); ?></td>
                                            <td id="counted" class="samPopMsg" style="text-align:right"><?php echo $datatbl->counted; ?></td>
                                            <td id="variance" class="no-edit" style="text-align:right"><?php echo $datatbl->qtyOnHand-$datatbl->counted; ?></td>
                                            <td id="varCost" class="no-edit" style="text-align:right">
												<?php 
                                                    $costVariance = ($datatbl->qtyOnHand*$datatbl->onDatePrice)-(($datatbl->qtyOnHand-$datatbl->counted)*$datatbl->onDatePrice); 
                                                    echo number_format($costVariance,2,'.',','); 
                                                ?>
                                            </td>
                                            <td id="costVar" class="no-edit" style="text-align:right">
                                                    <?php echo number_format($datatbl->countedPrice,2,'.',','); ?>
												</td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>                                         
                        	</div>
                            <div class="footer">
                                <div class="side fr">
                                    <div class="btn-group">
                                        &nbsp;
                                    </div>
                                </div>
                            </div> 
                        </div>                          
                        <?php endif;?>                               
                    </div>
                </div>                                
            </div>
        </div>
        
	</div>
</div>
<div class="dialog" id="row_delete" style="display: none;" title="Are you sure to Delete?">
    <p><?php echo $this->lang->line('message1') ?></p>
</div>   
<div class="dialog" id="row_edit" style="display: none;" title="Are you sure to Edit ?">
    <p><?php echo $this->lang->line('message2') ?></p>
</div>

