<?php
$PageSecurity = 3;
$title = _('View/Search Items') ;
include('includes/SQL_CommonFunctions.inc');
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);

//****************SAVE DATA IN TABLE***********START HERE**************************************************************
if($_REQUEST['save'])
{

	
	//echo "<pre>"; print_r($_REQUEST); exit;
	$data['kitid']=$_REQUEST['asset'];
	$row= $s->RecordInsert('spare_part_kits', $data);

	$lastid=DB_Last_Insert_ID($db,'spare_part_kits', 'id');

	for($i=0;$i<count($_REQUEST['itemid']);$i++)
	{
		if($_REQUEST['itemid'][$i]!='')
		{
			$data_arr['parant_id']=$lastid;
			$data_arr['item_id']=$_REQUEST['itemid'][$i];
			$row1= $s->RecordInsert('spare_part_kits_details', $data_arr);
		}
	}
	$s->pageLocation("index.php?route=Inventory/maintenance/spare_part_kit_index".$rurl);
}
//****************SAVE DATA IN TABLE***********END HERE**************************************************************
?>
<script src="js/jquery-1.11.1.min.js"></script>


<div id="PickedItems" style="display:none">
</div>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1 align="center">Spare Part Kits</h1>
<form name="" method="post" onsubmit="return valitation()">
<table width="100%" border="0" cellspacing="0" cellpadding="3" align="center">
  <tr class="row1">
    <td width="13%" align="right"><strong>Asset:</strong> <font color="#FF0000">*</font></td>
    <td width="30%"><select name="asset" id="asset" style="width:100%;">
      <option value="0">Select</option>
      <?php
      $usedkit_array=array();
      $sql_kit_used=DB_query("SELECT DISTINCT kitid FROM spare_part_kits",$db);
      while($row_used_kit=DB_fetch_array($sql_kit_used))
      {
      	$usedkit_array[]=$row_used_kit['kitid'];
      }
     $sql222= "select * from machine_center where 1";
      if(!empty($usedkit_array))
      {
      $notusedkit=implode(",",$usedkit_array);
       $sql222.= " and id NOT IN (".$notusedkit.") ";
  		} 
  		$sql222.= " and CompanyId='".$_SESSION['ACGCompanyId']."'";
      $sql_kit=DB_query($sql222,$db);
      while($row=DB_fetch_array($sql_kit))
      {
      ?>
      <option value="<?php echo $row['id']; ?>"><?php echo $row['name']."--".$row['code']; ?></option>
      <?php } ?>
     </select>
     </td>
     <td width="13%"></td>
     <td></td>
  </tr>  
</table>

<p align="left"><input type="button" class="search"   onclick="deleteRowOrder('dataTable')" value="Delete Rows"/>&nbsp;&nbsp;<input type="button" name="profile_popup" value="Pick Items" id="profile_popup" class="search" /></p>
<div id="showdata">
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid;">
	<tr>
		<th width="5%">S. No.</th>
		<th width="1"></th>
		<th width="15%">Item Code <font color="#FF0000">*</font></th>
		<th>Item Name <font color="#FF0000">*</font></th>
		<th width="20%">Unit <font color="#FF0000">*</font>lo</th>            
	</tr>			
<tbody id="dataTable">
	<tr bgcolor="#fff">
		<td><input name="delitem[]" id="delitem" type="checkbox" value="<?php echo $item_row['id'];?>" /></td>
		<td><input type="hidden" name="itemid[]" id="itemid" style="width:100%" /></td>	
		<td><input type="text" name="" value="" id="itemcode" style="width:100%" readonly /></td>
		<td><input type="text" name=""  value="" id="itemname" style="width:100%" readonly /></td>
		<td><input type="text" name="unit[]" value="" id="unit"  style="width:100%"readonly /></td>	
	</tr>
</tbody> 
</table>
</div>
 <p style="text-align:center;"><input type="submit" name="save" class="search" value="Save" /></p>
  <input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
  <input type="hidden" name="" value="0" id="edit_cnt" />
    <input type="hidden" name="" value="0" id="rowcount1" />

</form>
</div></div>

<script type="text/javascript" language="javascript">
         
//$.noConflict();
$(document).ready(function(){	
		//var cnt=document.getElementById("count").value;
		$('input[name=profile_popup]').click(function(){
		var tt=this.id;
		var tt1=tt.split("_");
		var tt2=tt1[1];
		//var site=document.getElementById("site").value;
		
		//var LeaveType=document.getElementById("LeaveType_"+tt2).value;
		$("#PickedItems").dialog({
				title:'Item List',
				modal: true,
				resizable: false,
				width:900,
				height:450,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: {
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
				
			});
		GetitemListForDirectPO();
		});
	});


	
function GetitemListForDirectPO()
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("PickedItems").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","direct_po_items2.php",true);
	xmlhttp.send();
}

function getdataforSearch()
      { 

                 $.ajax({
						url : "direct_po_items2.php",
						type : "POST",
						data : {
						mainCategoryid:$('#PickedItems').find('select[name=CategoryID]').val(),
						subCategoryid:$('#PickedItems').find('select[name=subcategory]').val(),
						itemName:$('#PickedItems').find('input[id=StockID]').val(),
						itemScode:$('#PickedItems').find('input[id=itemScode]').val()
						},
						success : function(data) {//alert(data);
						 $('#PickedItems').html(data);	
													}
					       });
	  
}

function getItemsList(id,code,name,unit,ctr)
{  
	
  var dup='empty';
	var itemcount=parseInt(document.getElementById('rowcount1').value)+1;
	
	var edit_cnt=document.getElementById('edit_cnt').value;
	//alert(edit_cnt);
	if(edit_cnt=='')
	{
		var counter=1;
	} 
	else if(edit_cnt>itemcount)
	{
		var counter=edit_cnt;
		var itemcount=parseFloat(counter)+parseFloat(itemcount);
	}
	else
	{
		var counter=edit_cnt;
	}
	
	
	
	
	//alert(counter);
//IF WE CHECK THE CHECK BOX FOR SELECT ANY ITEM***********************START HERE******************************************
	if(document.getElementById("row_id_"+ctr).checked==true)
	{
	    
       
		if(document.getElementById('itemid') && document.getElementById('itemid').value=='')
		{
			if(edit_cnt=='')
			{
				var counter=0;
			}
		
			for(var j=0; j<counter; j++)
			{
          
           if(document.getElementById('itemid1_'+j))
            {
				if(document.getElementById('itemid1_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
            }
			}
			
			if(dup=='empty')
			{	
				var data=addRow('dataTable');
				document.getElementById('itemid').value = id;
				document.getElementById('itemcode').value = code;
				document.getElementById('itemname').value = name;
				document.getElementById('unit').value = unit;	
								
				//document.getElementById('instrumentname').value = desc;	
				
				
			}
		} 
		
		else
		{
		
		
			for(var i=counter; i<=itemcount; i++)
			{	
			
			
				if(edit_cnt=='')
				{
					var counter=0;
				}
				for(var j=0; j<counter; j++)
				{}
				if(document.getElementById('itemid'))
				{
				if(document.getElementById('itemid').value==id)
				{
					alert("Duplicate Value");
					document.getElementById("row_id_"+ctr).checked=false;
					break;
				}
				}
				if(document.getElementById('itemid1_'+i))
				{
				if(document.getElementById('itemid_'+i).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					break;
				}
				}
				
				if(dup=='empty')
				{
				if(!document.getElementById('itemid_'+i))
				{	
					
				var data= addRow('dataTable');	
				}
					if(document.getElementById('itemid_'+i))
					{	
					
					if(document.getElementById('itemid_'+i).value=='')
					{	
						document.getElementById('itemid_'+i).value = id;
						document.getElementById('itemcode_'+i).value = code;
						document.getElementById('itemname_'+i).value = name;
						document.getElementById('unit_'+i).value = unit;
						
						//document.getElementById('instrumentname_'+i).value = desc;	
												
						break;
					}
					}
					else
					{
						continue;
					}
				}
			}
		}
	}
	
//IF WE CHECK THE CHECK BOX FOR SELECT ANY ITEM***********************END HERE******************************************


//IF WE UNCHECK THE CHECK BOX FOR DESELECT ANY ITEM***********************START HERE******************************************
	else
	{
		if(document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
			document.getElementById('itemid').value = '';
			document.getElementById('itemcode').value = '';
			document.getElementById('itemname').value = '';
			document.getElementById('unit').value = '';
			
			//document.getElementById('instrumentname').value = '';
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
					document.getElementById('itemid_'+i).value = '';
					document.getElementById('itemcode_'+i).value = '';
					document.getElementById('itemname_'+i).value = '';
					document.getElementById('unit_'+i).value = '';
					
					//document.getElementById('instrumentname_'+i).value = '';
				}
			}
		}
	}
//IF WE UNCHECK THE CHECK BOX FOR DESELECT ANY ITEM***********************END HERE******************************************
}
	


function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var row1=document.getElementById("rowcount1").value;
	var rr=parseInt(row1)+1;
	document.getElementById("rowcount1").value=rr;
	document.getElementById("edit_cnt").value=rr;
	var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
			//newcell.className='tabledata';
           			// newcell.style.background='#ffffff';
			
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
			 

				//alert(newcell.childNodes[0].type);
				switch(newcell.childNodes[0].type) {

				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	

			}
			
			

		}
		//document.getElementById(countid).innerHTML=rowCount;

	}
function getSubCategory(str)
  {
      if (str=="")
  {
  document.getElementById("srch_subcateory").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {  
    document.getElementById("srch_subcateory").innerHTML=xmlhttp.responseText;
    }
  }
  //alert("mrn_item_list.php.php?str1="+str);
  xmlhttp.open("GET","mrn_item_list.php?str1="+str,true);
xmlhttp.send();

  }
function deleteRowOrder(tableID) 
{
	 var len=$("input[id^='delitem']:checkbox:checked").length;
	 if(len==0)
	 {
	 alert("Please select at least one record to delete");
	 return false;
	}
		try 
		{
			var table = document.getElementById(tableID);
			//alert(table);
			var rowCount = table.rows.length;
			//alert(rowCount);
			for(var i=0; i<rowCount; i++) 
				{
					var row = table.rows[i];
					var chkbox = row.cells[0].childNodes[0];
					if(null != chkbox && true == chkbox.checked) 
					{
						if(rowCount <= 1) 
						{
							alert("Cannot delete all the rows.");
							break;
						}
					table.deleteRow(i);
					rowCount--;
					i--;
					}
					
				}
		document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
		}
		catch(e) 
		{
			alert(e);
		}

}
function valitation()
{
	if(document.getElementById('asset').value=="0")
	{
		alert("Please select Asset");
		document.getElementById('asset').focus();
		return false;
	}
	else if(document.getElementById('itemcode').value=="")
	{
		alert("Please select Item");
		document.getElementById('itemcode').focus();
		return false;
	}
}
</script>

