<div id="content-header" class="hidden-print receiving-header">
	<h1> <i class="fa fa-cloud-download"></i>
		<?php //echo lang('receivings_register'); 
		echo 'Material Request Order ';?> <span id="ajax-loader"><?php echo img(array('src' => base_url().'/img/ajax-loader.gif')); ?></span>
	</h1>    
</div>

		<div class="row">
			<!--Left small box-->
			<div class="sale_register_leftbox col-md-9 receiving_register_leftbox no-padd">
				<div class="row">
                                    <?php if ($mode != 'supplier_store_accounts') { ?>
					<div class="col-md-8 no-padd">
						<div class="input-append">
							<?php echo form_open("mros/add",array('id'=>'add_item_form', 'autocomplete'=> 'off')); ?>
								<?php echo form_input(array('name'=>'item','id'=>'item','size'=>'30','placeholder'=>'Enter item name or scan barcode'));?>
									<?php echo anchor("items/view/-1/1/receiving",
									"<div class='small_button'><span>".lang('sales_new_item')."</span></div>",
									array('class'=>'btn btn-primary none width_full','title'=>lang('sales_new_item')));?>
							</form>
						</div>
					</div>
                                    <?php } ?>
					<div class="col-md-4 no-padd">
						<!--mode dorp down area-->				
					</div>
				</div>
				<div class="row">
                                    <?php if ($mode != 'supplier_store_accounts') 
                                    { ?>
					<div class="table-responsive">
						<table id="register" class="table table-bordered ">
						<thead>
							<tr>
								<th id="reg_item_del" width="3%"></th>
								<th id="reg_item_name"><?php echo lang('receivings_item_name'); ?></th>
								<th id="reg_item_product_id"><?php echo lang('receivings_item_product_id'); ?></th>
								<th id="" style="display:none"><?php echo 'Lot#'; ?></th>
								<th id="" style="display:none"><?php echo 'Expiry Date'; ?></th>
								<th id="reg_item_price" style="display:none"><?php echo lang('receivings_cost'); ?></th>
								<th id="reg_item_qty" width="10%"><?php echo lang('receivings_quantity'); ?></th>
								<th id="reg_item_discount" style="display:none"><?php echo lang('receivings_discount'); ?></th>
								<th id="reg_item_total" style="display:none"><?php echo lang('receivings_total'); ?></th>
							</tr>
						</thead>
						<tbody id="cart_contents">
							<?php
							if(count($cart)==0)
							{
							?>
							<tr class="cart_content_area">
								<td colspan='6' style="height:60px;border:none;">
									<div  class='text-center text-warning' ><h3><?php echo lang('sales_no_items_in_cart'); ?></h3></div>
								</td>
							</tr>
							<?php
								}
								else
								{
									foreach(array_reverse($cart, true) as $line=>$item)
									{
										$cur_item_info = $this->Item->get_info($item['item_id']);?>		
										 
												
											<tr id="reg_item_top">
												<td id="reg_item_del"><?php //echo anchor("mros/delete_item/$line",lang('common_delete'), array('class' => 'delete_item'));?>
												<?php echo anchor("mros/delete_item/$line",'<i class="fa fa-trash-o fa fa-2x text-error"></i>', array('class' => 'delete_item'));?>
												</td>
												<td id="reg_item_name"><?php echo H($item['name']); ?></td>
												<td id="reg_item_product_id"><?php echo H($item['product_id']); ?></td>
												<td id="" style="display:none"><?php 
												echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));
												echo form_input(array('name'=>'lot_no','value'=>$item['lot_no'],'class'=>'input-small', 'id' => 'lot_no_'.$line));?> </form>
                                                </td>
												<td id="" style="display:none">
												<?php 
												echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));
												echo form_input(array('name'=>'expiry','value'=>$item['expiry'],'class'=>'input-small', 'id' => 'expiry_'.$line));?> </form>
                                                </td>
											<?php if ($items_module_allowed){ ?>
												
												<td id="reg_item_price" style="display:none">
													<?php
														echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));															   
															echo form_input(array('name'=>'price','value'=>to_currency_no_money($item['price'], 10),'class'=>'input-small', 'id' => 'price_'.$line));?>
														</form>
												</td>
											<?php }else{ ?>
												<td id="reg_item_price" style="display:none">
													<?php echo $item['price']; ?>
													<?php
													echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));
														echo form_hidden('price',$item['price']); ?>
													</form>
												</td>
												
											<?php }	?>
												<td id="reg_item_qty">
												<?php 
													echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));
														echo form_input(array('name'=>'selected_item_id','value'=>$item['item_id'],'class'=>'input-small', 'id' => 'selected_item_id_'.$line,'style'=>'display:none'));
														echo form_input(array('name'=>'mode','value'=>$mode,'class'=>'input-small', 'id' => 'mode_'.$line,'style'=>'display:none'));
														echo form_input(array('name'=>'quantity','value'=>$item['quantity'],'class'=>'input-small', 'id' => 'quantity_'.$line));?>
													</form>
												</td>
												<td id="reg_item_discount" style="display:none"><?php
													echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));
														echo form_input(array('name'=>'discount','value'=>$item['discount'],'class'=>'input-small', 'id' => 'discount_'.$line));?>
													</form>	
												</td>
												<td id="reg_item_total" style="display:none"><?php echo to_currency($item['price']*$item['quantity']-$item['price']*$item['quantity']*$item['discount']/100); ?></td>
											</tr>
											<tr id="reg_item_bottom"  >
												<td id="reg_item_descrip_label"><?php echo lang('sales_description_abbrv').':';?></td>
												<td id="reg_item_descrip" colspan="8">
											<?php 
												echo H($item['description']);
												 
													echo form_open("mros/edit_item/$line", array('class' => 'line_item_form', 'autocomplete'=> 'off'));
														echo form_hidden('description',$item['description']);
											?>		</form>	
												</td>
										 
									</tr>
				
									<?php
									}
								}
							?>
						</tbody>
					</table>
				</div>
                                <?php } ?>
			</div>
                            
                            
		</div>
		<!-- Right small box  -->
		<div class="col-md-3 sale_register_rightbox">
		<br />
		<ul class="list-group">
			<li class="list-group-item nopadding">
				<!-- Cancel button -->
				<div class="sale_form_main"   <?php if(count($cart) > 0){ echo "style='visibility: visible;'";}?>>
					<?php	if(count($cart) > 0){ ?>
						<?php echo form_open("mros/cancel_receiving",array('id'=>'cancel_sale_form', 'autocomplete'=> 'off')); ?>
							<div class='btn btn-danger width_full' id='cancel_sale_button'>
								<span>
									<?php echo 'Cancel Mro';  ?>
								</span>
							</div>
						</form>
					<?php } ?>
				</div>
			
		<li class="list-group-item nopadding">

			<!-- Location info starts here-->
					<div class="widget-box">
						<div class="widget-title">
							<span class="icon">
								<i class="fa fa-th-list"></i>
							</span>
							<h5><?php echo "Mro Details";?></h5>
						</div>
                        <div class="widget-title">
							<span class="icon">
								<i class="fa fa-th-list"></i>
							</span>
                            <h5>Location: <?php echo $this->Employee->get_current_location_name(); ?></h5>
						</div>
                        <div class="widget-title">
							<span class="icon">
								<i class="fa fa-th-list"></i>
							</span>
                            <h5>Employee: <?php 		$user_info = $this->Employee->get_info($employee_id);
 echo $user_info->first_name.' '.$user_info->last_name; ?></h5>
						</div>
                        <div class="widget-title">
							<span class="icon">
								<i class="fa fa-th-list"></i>
							</span>
                            <h5>Mro #:: <?php echo $mro_no; ?></h5>
						</div>
						<div class="widget-content">
							<div id="customer_info_shell">
									
									<?php echo 'Date: &nbsp; ';?>
									<?php echo form_input(array('name'=>'mro_date','value'=>date("d-m-Y"),'size'=>'5','style'=>'width:80%','placeholder'=> 'Mro Date','readonly'=>'readonly')); ?>
									
							</div>
						</div>
					</div>
					<!--Location info ends here-->
					<div id='sale_details'>
						<table id="sales_items_total" class="table">
							<tr class="success">
								<td class="left"><h4><?php echo 'Items added in cart'; ?>:</h4></td>
								<td class="right"><h4><?php echo $items_in_cart; ?></h4></td>
							</tr>
						</table>
					</div>

					

				<?php
				// Only show this part if there are Items already in the Table.
				if(count($cart) > 0){ ?>

					<div id="finish_sale">
						<?php echo form_open("mros/complete",array('id'=>'finish_sale_form', 'autocomplete'=> 'off')); ?>

						<div id="make_payment" >
							<table id="make_payment_table" class="table">
								
								<tr id="mpt_bottom" class="info">
									<td id="tender" colspan="2">
										<?php echo 'Purpose:   ';?>
										<?php echo form_input(array('name'=>'mro_purpos','value'=>'','size'=>'10','class'=>'width_full_always','placeholder'=> 'Mro Purpos')); ?>
									</td>
								</tr>
							</table>
						</div>
						
						<div style="padding: 0 10px 0 10px;">
							<label id="comment_label" for="comment"><?php echo lang('common_comments'); ?>:</label>
							<?php echo form_textarea(array('name'=>'comment', 'id' => 'comment', 'value'=>'','rows'=>'4'));?>
							
							<?php if ($mode!= 'transfer' || ($mode=='transfer' && isset($location_id)) || $mode== "transfer_return") { ?>
								<?php echo "<div class='btn btn-primary btn-block btn-large'  style=\"margin: 10px 0 10px 0;\" id='finish_sale_button' >".lang('receivings_complete_receiving')."</div>"; ?>
							<?php } ?>
						</div>
					</div>
				</form>
				<?php } ?>
			</div>
			</li>
		</ul>
		</div><!-- END OVERALL-->		
		</div>
		</div>
		
<script type="text/javascript">
	<?php
	if(isset($error))
	{
		echo "gritter(".json_encode(lang('common_error')).",".json_encode($error).",'gritter-item-error',false,false);";

	}

	if (isset($warning))
	{
		echo "gritter(".json_encode(lang('common_warning')).",".json_encode($warning).",'gritter-item-warning',false,false);";

	}

	if (isset($success))
	{
		echo "gritter(".json_encode(lang('common_success')).",".json_encode($success).",'gritter-item-success',false,false);";

	}
	?>
</script>

<script type="text/javascript" language="javascript">
var submitting = false;
$(document).ready(function()
{		
	//Here just in case the loader doesn't go away for some reason
	$("#ajax-loader").hide();
	
	if (last_focused_id && last_focused_id != 'item' && $('#'+last_focused_id).is('input[type=text]'))
	{
		$('#'+last_focused_id).focus();
		$('#'+last_focused_id).select();
	}

	$(document).focusin(function(event) 
	{
		last_focused_id = $(event.target).attr('id');
	});
	
	$('#mode_form, #select_supplier_form,#select_location_form,.line_item_form').ajaxForm({target: "#register_container", beforeSubmit: receivingsBeforeSubmit});
	$('#add_item_form').ajaxForm({target: "#register_container", beforeSubmit: receivingsBeforeSubmit, success: itemScannedSuccess});
	
	$( "#item" ).autocomplete({
		source: '<?php echo site_url("mros/item_search"); ?>',
		delay: 10,
		autoFocus: false,
		minLength: 1,
		select: function(event, ui)
		{
 			event.preventDefault();
 			$( "#item" ).val(ui.item.value);
			$('#add_item_form').ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit, success: itemScannedSuccess});
		},
		change: function(event, ui)
		{
			if ($(this).attr('value') != '' && $(this).attr('value') != <?php echo json_encode(lang('sales_start_typing_item_name')); ?>)
			{
				$("#add_item_form").ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit});
			}
	
    		$(this).attr('value',<?php echo json_encode(lang('sales_start_typing_item_name')); ?>);
		}
	});
	
	$("#cart_contents input").change(function()
	{
		$(this.form).ajaxSubmit({target: "#register_container",beforeSubmit: receivingsBeforeSubmit});
	});
	
	$('#item,#supplier,#location').click(function()
    {
    	$(this).attr('value','');
    });

	$('#mode').change(function()
	{
		$('#mode_form').ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit});
	});

	$( "#supplier" ).autocomplete({
		source: '<?php echo site_url("mros/supplier_search"); ?>',
		delay: 10,
		autoFocus: false,
		minLength: 1,
		select: function(event, ui)
		{
                    $( "#supplier" ).val(ui.item.value);
                    $('#select_supplier_form').ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit, success: itemScannedSuccess});		
		}
	});

	$( "#location" ).autocomplete({
		source: '<?php echo site_url("mros/location_search"); ?>',
		delay: 10,
		autoFocus: false,
		minLength: 1,
		select: function(event, ui)
		{
			$( "#location" ).val(ui.item.value);
			$('#select_location_form').ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit});			
		}
	});



    $('#supplier').blur(function()
    {
    	$(this).attr('value',<?php echo json_encode(lang('receivings_start_typing_supplier_name')); ?>);
    });

    $('#location').blur(function()
    {
    	$(this).attr('value',<?php echo json_encode(lang('receivings_start_typing_location_name')); ?>);
    });


    $("#finish_sale_form").submit(function()
	{
		<?php if($mode=="transfer" and !isset($location)) { ?>
			alert(<?php echo json_encode(lang("receivings_location_required")); ?>);
			$('#location').focus();
			return;
			<?php } ?>
			
			if (confirm(<?php echo json_encode("Are you sure to finish this Mro?"); ?>))
    		{
				//Prevent double submission of form
				$("#finish_sale_button").hide();
				return true;
    		}
			else {
				return false;
			}
	});
    $("#finish_sale_button").click(function()
    {
		$('#finish_sale_form').submit();
	});

    $("#cancel_sale_button").click(function()
    {
    	if (confirm(<?php echo json_encode("Are you sure to cancel this Mro?"); ?>))
    	{
			$('#cancel_sale_form').ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit});
    	}
    });

	$('.delete_item, #delete_supplier, #delete_location').click(function(event)
	{
		event.preventDefault();
		$("#register_container").load($(this).attr('href'));	
	});

	$("input[type=text]").click(function() {
		$(this).select();
	});
	
	$("#new-supplier").click(function()
	{
		$("body").mask(<?php echo json_encode(lang('common_wait')); ?>);			
	});
});

	function chnage_location(){
		//$( "#location" ).val(ui.item.value);
			$('#select_location_form').ajaxSubmit({target: "#register_container", beforeSubmit: receivingsBeforeSubmit});	
	}

function receivingsBeforeSubmit(formData, jqForm, options)
{
	if (submitting)
	{
		return false;
	}
	submitting = true;
	
	$("#ajax-loader").show();
	$("#finish_sale_button").hide();
}

function itemScannedSuccess(responseText, statusText, xhr, $form)
{
	setTimeout(function(){$('#item').focus();}, 10);
}

</script>