<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">

<h1><?php  echo $page_title;?></h1>
 <?php  $table='crm_picklist';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;
 $modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$url="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=".$_REQUEST['route'].$url;
if($_REQUEST['Save'])
{
	if(empty($_REQUEST['picklist_title']))
	{
	   $msgErr[]='Please Enter Group Name';
	   $status=false;
	}
	 
	 
	 /*	$condition="picklist_title='".$_REQUEST['picklist_title']."'";
		$allready=$s->GetRecord($table,  $condition);
		$row=DB_query($allready,$db);
		if(DB_num_rows($row)>0 && $action!='Edit')
		{
			 $msgErr[]='All Ready Exist';
			 $status=false;
		}
		*/
		
} 

//************************Delete*********************************************** 
if( $_REQUEST['enable']==1)
{
	
 
		$darr['status']='N';
		$darr['picklist_id']=$_REQUEST['picklist_id'];
 		 $condition="picklist_id='".$_REQUEST['picklist_id']."'";
	  	  $s->EditRecord($table, $data, $cond);
	     $s->pageLocation($targetpage.'daction=Deleted');
 }
 
  
//************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="picklist_id='".$_REQUEST['picklist_id']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
  	  $picklist_title=$RData['picklist_title'];
	  $status=$RData['status'];
  $parent_id=$RData['parent_id'];
			 
}
 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{
 $data['picklist_title']=$_REQUEST['picklist_title'];
  $data['status']=$_REQUEST['status'];
  $data['parent_id']=$_REQUEST['parent_id'];
  $cond="picklist_id='".$_REQUEST['picklist_id']."'";
   

	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
  $s->pageLocation($targetpage.'&daction=Updated');
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{
 $data['picklist_title']=$_REQUEST['picklist_title'];
   $data['parent_id']=$_REQUEST['parent_id'];
   $data['status']=$_REQUEST['status'];
// $data['Predefined']=$_REQUEST['Predefined'];
	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
 $s->pageLocation($targetpage.'&daction=Inserted');
}
?>


 <form action="" method="post">
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
   
  <?php if(isset($daction))
  {?>
    <tr>
  
    <td colspan="4" ><div class="success">
	<?php 
	 if($_REQUEST['daction']!=1 && $_REQUEST['daction']!=2)
	 echo "Record ".$_REQUEST['daction']." Successfully";
	 elseif($_REQUEST['daction']==1)
	 	 echo "Can't Delete This Record because AccountName Attatch with LedgerDayBook table ";
	 elseif($_REQUEST['daction']==2)		 
		 echo "Can't Delete This Record. First Delete it's Parent Record   ";
		 ?>
	</div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>
  
    <td colspan="4" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
   
   
  <tr>
    <td class="tabledata" style="width:200px;">PickList Item </td>
    <td><input name="picklist_title" type="text" size="27"  value="<?php echo $picklist_title;?>" style="width:100%" ></td>

    <td class="tabledata">PickList Item Category </td>
    <td><?php 
	
 	$pcode= $parent_id;
 	function CategoryShow($pcode, $PcateID, $dashVal)
	{
	    global $db;
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from crm_picklist  where  parent_id = '".$PcateID."'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
				$sqlCateSel = "select * from crm_picklist where picklist_id  =".$rowCate['picklist_id']." ";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['picklist_id']==$pcode)
				 $px="selected";
 		?>
 				<option value="<?php echo $rowCate['picklist_id'];?>" <?php echo $px ?>><?php echo $dashVal;?>
				 <?php echo $rowCate['picklist_title'];?> </option>
	   <?php			
				   CategoryShow($pcode,$rowCate['picklist_id'], $dashVal);
			}
		}
	}   
   ?>  
   <select name="parent_id" id="parent_id" style="width:100%">	  
  <option value="000">----</option>
  <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  crm_picklist  where    parent_id   = 0";
	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 				$sqlCateSel = "select * from crm_picklist where parent_id   =".$rowCate['picklist_id']." ";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['picklist_id']==$pcode)
				 $px="selected";
			echo $dashVal;	
		  ?>
		<option value="<?php echo $rowCate['picklist_id'];?>"<?php echo $px ?>> 
		<?php echo  $rowCate['picklist_title'] ;?></option>
 		<?php			
			 CategoryShow($pcode, $rowCate['picklist_id'], $dashVal);
		}
	}
?></select>
	
	
	
	
	</td>
  </tr>
   <tr>
    <td>Status</td>
    <td colspan="4"> <input name="status" type="radio" value="Y" <?php  echo ($status=='Y'?'checked':'')?>>
	Active
	<input name="status" type="radio" value="N"<?php  echo ($status=='N'?'checked':'')?>>Inactive
	
     
    
    </td>
  </tr>

</table>

<p align="left" style="padding-left:10px;"><input type="submit" name="Save" value="Save"></p>


 </form>
 


<?php  include('ViewPickList.php') ?>

</div></div>
