<?php 
$this->load->view("partial/header");
?>

<div id="page_title" style="margin-bottom:8px;"><?php echo "Search Item"; ?></div>



<?php echo form_open("reports/product_list", array('id' => 'add_item_form')); ?>
<label id="item_label" for="item">
    <?php
    echo $this->lang->line('sales_find_or_scan_item');
    ?>
</label>

<?php
echo form_input(array('name' => 'item', 'id' => 'item', 'size' => '65'));
?>
</form>
<?php if(trim($search_item_code)){?>

<div id="page_subtitle" style="margin-bottom:8px;">

<table style='border-collapse: collapse; border: 1px solid #000000;width:100%;'>

<tr style="border: 0px solid #000000;border-collapse: collapse; color:#000000;background-color:#EFC8C8;">
		 <td colspan="2" style="border: 1px solid #000000;text-align:left;vertical-align: middle;width:100%">
			<font style="font-weight:bold;">Search Item Barcode : </font><font style="font-weight:normal;"><?php echo $search_item_code."@".$search_lot_no;?></font>
		 </td>
</tr>

<tr style="border: 0px solid #000000;border-collapse: collapse; color:#000000;">
		 <td style="border: 1px solid #000000;text-align:left;vertical-align: middle;width:50%">
			<font style="font-weight:bold;">Item Category : </font><font style="font-weight:normal;"><?php echo $cat_description;?></font>
		 </td>
		 <td style="border: 1px solid #000000;text-align:left;vertical-align: middle;width:50%">
			<font style="font-weight:bold;">Gender : </font><font style="font-weight:normal;"><?php echo $gender;?></font>
		 </td>
</tr>

<tr style="border: 0px solid #000000;border-collapse: collapse; color:#000000;">
		 <td style="border: 1px solid #000000;text-align:left;vertical-align: middle;width:50%">
			<font style="font-weight:bold;">Item Name : </font><font style="font-weight:normal;"><?php echo $item_description;?></font>
		 </td>
		 <td style="border: 1px solid #000000;text-align:left;vertical-align: middle;width:50%">
			<font style="font-weight:bold;">Item Size : </font><font style="font-weight:normal;"><?php echo $size;?></font>
		 </td>
</tr>

</table>

</div>

<div id="table-container" style="width:100%">
<table id="maintable" width="100%">
    <thead>
        <tr>
			<th  style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;height:20px;vertical-align: middle;">
			 Item Size 
			</th>
			<th  style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;vertical-align: middle;">
			 LOT No 
			</th>
			<th  style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;vertical-align: middle;">
			 Price 
			</th>
			<th  style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;vertical-align: middle;">
			 QOH 
			</th>
			<th  style="background-color: #207D44; color:#FFFFFF;font-size:12px; text-align:center;border: 1px solid #000000;border-collapse: collapse;vertical-align: middle;">
			 Item Barcode 
			</th>
		</tr>
	
    </thead>
    <tbody>
    
    <?php
    
    $size_row_span=array();
    
    foreach ($stock_id_array as $key => $value) {
    	foreach ($value as $lo_key => $lo_value) {
    		$size_row_span[$key]+=count($lo_value);
    	}
    }
    
    foreach ($stock_id_array as $size_key => $size_value) {
    	$row_span_size=count($size_value);
    	?>
    	<tr>
    	<?php  if($size_key==$size){?>
    	<td rowspan="<?php echo $size_row_span[$size_key];?>" style="background-color: #207D44;font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse; padding:10px;vertical-align: middle;"> <?php echo $size_key;?></td>
    	<?php }else{?>
    	<td rowspan="<?php echo $size_row_span[$size_key];?>" style="font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse; padding:10px;vertical-align: middle;"> <?php echo $size_key;?></td>
    	<?php }?>
           <?php
           $show_lot=0;
           foreach ($size_value as $lot_key => $lot_value) {

           	if($show_lot==0){
           		$show_lot=1;
           	?>
           	<?php  if($search_lot_no==$lot_key){?>
           	<td rowspan="<?php echo count($lot_value);?>" style="background-color: #207D44; font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $lot_key;?></td>
           	<?php }else{?>
           	<td rowspan="<?php echo count($lot_value);?>" style="font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $lot_key;?></td>
           	<?php }?>
           	 
           <?php
           	}else{ ?>
           		<tr>
           		<?php  if($search_lot_no==$lot_key){?>
           			<td rowspan="<?php echo count($lot_value);?>" style="background-color: #207D44; font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $lot_key;?></td>
           			<?php }else{?>
           			<td rowspan="<?php echo count($lot_value);?>" style="font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $lot_key;?></td>
           		<?php }?>
           	<?php	
           	}
           	$show_price=0;
           	foreach ($lot_value as $price_key => $price_value) {
           		if($show_price==0){
           			$show_price=1;
           		?>
           	<td style=" font-weight:bold;color:#000000;font-size:14px; text-align:right;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $price_key;?></td>
           	<td style=" font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $price_value->qoh;?></td>
           	<td style=" font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $price_value->stock_id."@".$price_value->lot_no;?></td>
           		</tr>
           	<?php }else{?>
           	<tr>	
           	<td style=" font-weight:bold;color:#000000;font-size:14px; text-align:right;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $price_key;?></td>
           	<td style=" font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $price_value->qoh;?></td>
           	<td style=" font-weight:bold;color:#000000;font-size:14px; text-align:center;border: 1px solid #000000;border-collapse: collapse;padding:10px;vertical-align: middle;"> <?php echo $price_value->stock_id."@".$price_value->lot_no;?></td>
           	</tr>
           	<?php }?>
           	
          <?php 	
           	}?>
           
          <?php 	
    }
           ?> 
        
   <?php 	
}
    ?>
    </table>

 <?php } ?>


<?php
$this->load->view("partial/footer");
?>

<script type="text/javascript" language="javascript">
$(document).ready(function ()
{
	$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	$("#find").blur(function () {
		var find_val = $(this).val();

		$("#button_find").attr('href', '<?php echo base_url(); ?>index.php/sales/globalsearch/' + find_val + '/width:480');
		$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	});

	$("#recept_id").blur(function () {
		var find_val = $(this).val();

		$("#button_find").attr('href', '<?php echo base_url(); ?>index.php/sales/global_delivery/' + find_val + '/width:480');
		$(this).attr('placeholder', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	});

	$('#item').bind('keyup', function() {

		var itemLength = $("#item").val().length;
		if(itemLength==14){
			if($("#item").val().match(/^\d+$/)) {
				$('#add_item_form').submit();
			}
		}
	});

	$("#item").autocomplete('<?php echo site_url("sales/item_search"); ?>',
	{
		minChars: 0,
		max: 100,
		selectFirst: false,
		delay: 10,
		formatItem: function (row) {
			//return row[1];
			return (row.length > 1 && row[1]) || row[0];
		}
	});


	$("#item").result(function (event, data, formatted)
	{
		$("#add_item_form").submit();
	});
	$('#item').focus();
	$('#find').focus();
	$('#recept_id').focus();

	$('#item').blur(function ()
	{
		$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	});

	$("#find").autocomplete('<?php echo site_url("sales/item_search"); ?>',
	{
		minChars: 0,
		max: 100,
		selectFirst: false,
		delay: 10,
		formatItem: function (row) {
			return row[1];
		}
	});

	$('#check_date').datePicker();


	$('#item,#customer,#find,#recept_id').click(function ()
	{
		$(this).attr('value', '');

	});

	$("#customer").autocomplete('<?php echo site_url("sales/customer_search"); ?>',
	{
		minChars: 0,
		delay: 10,
		max: 100,
		formatItem: function (row) {
			return row[1];
		}
	});

	$("#customer").result(function (event, data, formatted)
	{
		$("#select_customer_form").submit();
	});

	$('#customer').blur(function ()
	{
		$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_customer_name'); ?>");
	});

	$('#comment').change(function ()
	{
		$.post('<?php echo site_url("sales/set_comment"); ?>', {comment: $('#comment').val()});
	});

	$('#receive_location').change(function ()
	{
		$.post('<?php echo site_url("sales/set_receive_location"); ?>', {receive_location: $('#receive_location').val()});
	});

	$('#email_receipt').change(function ()
	{
		$.post('<?php echo site_url("sales/set_email_receipt"); ?>', {email_receipt: $('#email_receipt').is(':checked') ? '1' : '0'});
	});


	$("#finish_sale_button").click(function ()
	{
		if (confirm('<?php echo $this->lang->line("sales_confirm_finish_sale"); ?>'))
		{
			$('#finish_sale_form').submit();
		}
	});
	$("#add_new_card_button").click(function ()
	{
		if (confirm('<?php echo "Are you sure you want to add this card? This cannot be undone."; ?>'))
		{
			$('#select_new_card_form').submit();
		}
	});

	$("#suspend_sale_button").click(function ()
	{
		if (confirm('<?php echo $this->lang->line("sales_confirm_suspend_sale"); ?>'))
		{
			$('#finish_sale_form').attr('action', '<?php echo site_url("sales/suspend"); ?>');
			$('#finish_sale_form').submit();
		}
	});

	$("#cancel_sale_button").click(function ()
	{
		if (confirm('<?php echo $this->lang->line("sales_confirm_cancel_sale"); ?>'))
		{
			$('#cancel_sale_form').submit();
		}
	});

	$("#edit_item_shawpon_button").click(function ()
	{
		$('#edit_item_shawpon_form').submit();
	});

	$("#add_payment_button").click(function ()
	{
		$('#add_payment_form').submit();
	});



	$("#payment_types").change(checkPaymentTypeGiftcard).ready(checkPaymentTypeGiftcard)
});

function post_item_form_submit(response)
{
	if (response.success)
	{
		$("#item").attr("value", response.item_id);
		$("#add_item_form").submit();
	}
}

function post_person_form_submit(response)
{
	if (response.success)
	{
		$("#customer").attr("value", response.person_id);
		$("#select_customer_form").submit();
	}
}
</script>
