<!-- main -->
<script>
	function loadsubcat(catid)
	{
		$('#itemsubcategory').empty();
		var div_data=" <option value='0'>-- Select --</option>";
		$(div_data).appendTo('#itemsubcategory'); 
		var _token = $("input[name='_token']").val();
		var scat_id = $('#scat_id').val();
		$.ajax({
            type: "GET",
            url:"/loadsubcategory",
            dataType: "json",
			data:{
				_token:_token, 
				'catid':catid	
			},
            success: function (data) {
                $.each(data,function(i,obj)
                {
					if(scat_id ==0)
					{
						var div_data="<option value="+obj.cat_id+">"+obj.cat_name+"</option>";	
					}
					else 
					{
						if(scat_id == obj.cat_id)
						{
							var div_data="<option value="+obj.cat_id+" selected>"+obj.cat_name+"</option>";	
						}
						else{
							var div_data="<option value="+obj.cat_id+">"+obj.cat_name+"</option>";	
						}
					}
					
					$(div_data).appendTo('#itemsubcategory'); 
				});  
			}
		});
	}
</script>
<div class="content">
	
	<div class="main-content">
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-user"></i> Item</h3> <em> - Add Item record</em>
				<div class="btn-group widget-header-toolbar">
					<a class="btn-borderless btn-focus clickme" href="viewitem" title="Focus" ><i class="fa fa-plus"></i></a>
					<!--<a href="#" title="Expand/Collapse" class="btn-borderless btn-toggle-expand"><i class="fa fa-chevron-up"></i></a>
					<a href="#" title="Remove" class="btn-borderless btn-remove"><i class="fa fa-times"></i></a>-->
				</div>
			</div>
			<div class="widget-content">
				<form id="frmmain" name="frmmain" data-parsley-validate novalidate>
					<input type="hidden" name="_token" value="{{ csrf_token() }}">						
					
					<div class="alert  alert-info " >
						<ul><li class="fa fa-cog  fa-1x fa-fw"></li>Kindly enter Item Details..</ul>
					</div>						
					<div class="alert alert-success print-success-msg" style="display:none">
						<ul></ul>
					</div>	
					<div class="alert alert-danger print-error-msg" style="display:none">
						<ul></ul>
					</div>
					
					<div class="col-md-6">
						<!-- ADVANCED VALIDATION -->
						<div class="widget-content">  
							<div class="form-group">
								<input type="hidden" class="form-control" id="itm_id" name="itm_id" value="{{ isset($data) ? $data[0]->itm_id : '0'  }}">
								<input type="hidden" id="cat_id" name="cat_id" value="{{ isset($data) ? $data[0]->category : '0'  }}">
								<label for="multiselect1">Item Category*</label>
                                <select name="itemcategory" id="itemcategory" class="form-control" required onchange="loadsubcat(this.value)">
									<option value="0">-- Select --</option>
								</select>
								<small class="text-danger" id="erritemcategory"></small>
								
								<script>
									loadsubcat(cat_id.value);
								</script>
							</div> 
							<div class="form-group">
								<label for="multiselect1">Item Sub Category*</label>
								<input type="hidden" id="scat_id" name="scat_id" value="{{ isset($data) ? $data[0]->subcategory : '0'  }}">
                                <select name="itemsubcategory" id="itemsubcategory" class="form-control" required>
									<option value="0">-- Select --</option>
								</select>
								<small class="text-danger" id="erritemsubcategory"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Unit*</label>
								<input type="hidden" id="unt_id" name="unt_id" value="{{ isset($data) ? $data[0]->unt_id : '0'  }}">
                                <select name="unit" id="unit" class="form-control" required>
                                    <option value="">-- Select --</option>
								</select>
								<small class="text-danger" id="errunit"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Item code*</label>
								<input type="text" name="itemcode" id="itemcode" class="form-control" value="{{ isset($data) ? $data[0]->itm_code : ''}}" onchange="validatecode(this.value)">
								<small class="text-danger" id="erritemcode"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Item name*</label>
								<input type="text" name="itemname" id="itemname" class="form-control" value="{{ isset($data) ? $data[0]->itm_name : ''}}">
								<small class="text-danger" id="erritemname"></small>
							</div>
							
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>
					
					<div class="col-md-6">
						<!-- ADVANCED VALIDATION -->
						<div class="widget-content">
							<div class="form-group">
								<label for="text-input1">Item Type*</label>
                                <select name="itemtype" id="itemtype" class="form-control" required>
                                    <option value="">-- Select --</option>
                                    <option value="Purchase"
									@if(isset($data))
									@if($data[0]->itm_type == "Purchase")
										selected='selected' 
										@endif
										@endif
									>Purchase</option>
                                    <option value="Production"
									@if(isset($data))
									@if($data[0]->itm_type == "Production")
										selected='selected' 
										@endif
										@endif
									>Production</option>
                                    <option value="Sales"
									@if(isset($data))
									@if($data[0]->itm_type == "Sales")
										selected='selected' 
										@endif
										@endif
									>Sales</option>
									<option value="Both"
									@if(isset($data))
									@if($data[0]->itm_type == "Both")
										selected='selected' 
										@endif
										@endif
									>Both</option>
								</select>
								<small class="text-danger" id="erritemtype"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Purchase cost*</label>
								<input type="text" name="purchasecost" id="purchasecost" class="form-control" value="{{ isset($data) ? $data[0]->itm_purchasecost : ''}}">
								<small class="text-danger" id="errpurchasecost"></small>
							</div>
							<div class="form-group">
								<label for="text-input1">Sales cost*</label>
								<input type="text" name="salescost" id="salescost" class="form-control" value="{{ isset($data) ? $data[0]->itm_salescost : ''}}">
								<small class="text-danger" id="errsalescost"></small>
							</div>
							<div class="form-group">
								<label>Note (Any note)</label>
								<textarea class="form-control" name="description" id="description" rows="4" cols="30" >{{ isset($data) ? $data[0]->itm_note : ''}}</textarea>
								<small class="text-danger" id="errdescription" ></small>
							</div>
							<div class="form-group">
								<label for="multiselect1">Status*</label>
								<br/>
                                <select name="status" id="status" class="form-control" required>
                                    <option value="Active" 
									@if(isset($data))
									@if($data[0]->itm_status == "Active")
										selected='selected' 
										@endif
										@endif
									> Active</option>
                                    <option value="Inactive"
									@if(isset($data))
									@if($data[0]->itm_status == "Inactive")
										selected='selected' 
										@endif
										@endif
									>Inactive</option>
								</select>
								<small class="text-danger" id="errstatus"></small>
							</div>
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>
					<div class="col-md-12">
						<div class="form-group">
                            <center><button type="submit" id="submit" name="submit" class="btn btn-primary">Add Item record</button></center>
						</div>
					</div>                        
				</form>
				
			</div>
		</div>
		<!-- END DATA EXPORT -->
		
	</div>
</div>
<!-- /main -->

<script type="application/javascript">
	i = 0;
	$(document).ready(function()
	{
		$('#frmmain').on('submit',function()
		{
			$(".alert-info").find("ul").html('<li class="fa fa-cog fa-spin fa-1x fa-fw"></li>Inserting record...');
			
			var _token = $("input[name='_token']").val();
			var itm_id = $("#itm_id").val();
			var catid = $("#itemcategory").val();
			var subcatid =  $('#itemsubcategory').val();
			var unit =  $('#unit').val();
			var itemcode =  $('#itemcode').val();
			var itemname =  $('#itemname').val();
			var itemtype =  $('#itemtype').val();
			var purchasecost =  $('#purchasecost').val();
			var salescost =  $('#salescost').val();
			
			var description = $('#description').val();
			var status = $('#status').val();
			
			
			document.getElementById("erritemcategory").innerHTML = "";
			document.getElementById("erritemsubcategory").innerHTML = "";
			document.getElementById("errunit").innerHTML = "";
			document.getElementById("erritemcode").innerHTML = "";
			document.getElementById("erritemname").innerHTML = "";
			document.getElementById("erritemtype").innerHTML = "";
			document.getElementById("errpurchasecost").innerHTML = "";
			document.getElementById("errsalescost").innerHTML = "";
			
			if(catid == "0")
			{
				document.getElementById("erritemcategory").innerHTML = "Category should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(subcatid == "0")
			{
				document.getElementById("erritemsubcategory").innerHTML = "Sub Category should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(unit == "0")
			{
				document.getElementById("errunit").innerHTML = "Unit should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(itemcode == "")
			{
				document.getElementById("erritemcode").innerHTML = "Item Code should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(itemname == "")
			{
				document.getElementById("erritemname").innerHTML = "Item Name should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(itemtype == "")
			{
				document.getElementById("erritemtype").innerHTML = "Item Type should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(purchasecost == "")
			{
				document.getElementById("errpurchasecost").innerHTML = "Purchase Cost should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			else if(salescost == "")
			{
				document.getElementById("errsalescost").innerHTML = "Sales Cost should not be empty...";
				$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
			}
			
			else
			{						   
				if(i == 0)
				{
					e.preventDefault();
					i++;
					$.ajax({
						url: "/item",
						type:'POST',
						data: {_token:_token, 
							'itm_id':itm_id,
							'catid':catid, 
							'subcatid':subcatid, 
							'unit':unit, 
							'itemname':itemname,
							'itemtype':itemtype, 
							'itemcode':itemcode, 
							'purchasecost':purchasecost, 
							'salescost':salescost, 
							'description':description,
							'status':status
						},
						success: function(data)
						{
							$('#sideMenu').load('viewitem');
						}
					});	
				}
				
			}
			return false;
		});
		
	});
	function printErrorMsg (msg) {				
		$(".alert-info").css('display','none');
		$(".print-success-msg").css('display','none');
		$(".print-error-msg").find("ul").html('');
		$(".print-error-msg").css('display','block');
		$(".print-error-msg").find("ul").append('<li class="fa fa-times-circle fa-1x fa-fw"></li>'+msg);
	}
	
	function printSuccessMsg (msg) {
		$(".alert-info").css('display','none');
		$(".print-error-msg").css('display','none');
		$(".print-success-msg").find("ul").html('');
		$(".print-success-msg").css('display','block');
		$(".print-success-msg").find("ul").append('<li class="fa fa-check  fa-1x fa-fw"></li>'+msg);
	}	
	
	
	$(document).ready(function(){
		var unt_id = $('#unt_id').val();
		$.ajax({
			type: "GET",
			url:"/loadunit",
			dataType: "json",
			success: function (data) {
				$.each(data,function(i,obj)
				{
					if(unt_id ==0)
					{
						var div_data="<option value="+obj.unt_id+">"+obj.unt_type+"</option>";	
					}
					else 
					{
						if(unt_id == obj.unt_id)
						{
							var div_data="<option value="+obj.unt_id+" selected>"+obj.unt_type+"</option>";	
						}
						else{
							var div_data="<option value="+obj.unt_id+">"+obj.unt_type+"</option>";	
						}
					}
					
					$(div_data).appendTo('#unit'); 
				});  
			}
		});
	});
	$(document).ready(function(){
		var cat_id = $('#cat_id').val();
		$.ajax({
			type: "GET",
			url:"/loadcategory",
			dataType: "json",
			success: function (data) {
				$.each(data,function(i,obj)
				{
					if(cat_id ==0)
					{
						var div_data="<option value="+obj.cat_id+">"+obj.cat_name+"</option>";	
					}
					else 
					{
						if(cat_id == obj.cat_id)
						{
							var div_data="<option value="+obj.cat_id+" selected>"+obj.cat_name+"</option>";	
						}
						else{
							var div_data="<option value="+obj.cat_id+">"+obj.cat_name+"</option>";	
						}
					}
					
					$(div_data).appendTo('#itemcategory'); 
				});  
			}
		});
	});
	
	function validatecode(code)
	{
		document.getElementById("erritemcode").innerHTML = "";
		var _token = $("input[name='_token']").val();
		$.ajax({
			type: "GET",
			url:"/validateitem",
			dataType: "json",
			data:{
				_token:_token, 
				'code':code	
			},
			success: function (data) {
				if(data.length > 0)
				{
					$('#itemcode').val('');
					document.getElementById("erritemcode").innerHTML = "Item Code Already Exists..";
					$(".alert-info").find("ul").html('<li class="fa fa-cog fa-1x fa-fw"></li>Kindly check the error...');
				}
			}
		});
	}	
</script>
<script>
	$('.clickme').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
</script>			