<?php  
include('includes/Class_www_users.php');
include('CRM/functions.php');

$targetpage='index.php?route=DealerDistrbutions/Setup/dealerusersindex';
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$url="&modid=".$modid."&frmid=".$frmid;

function CategoryShow($companyid, $PcateID, $dashVal)
{
global $db;
$dashVal = $dashVal . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
$s=new db_query();


$sqlCate = "select * from deptmaster_hierarchy  where  parentid  = '".$PcateID."' and companyid='".$companyid."'";

$rsCate  = DB_query($sqlCate,$db);	
if(DB_num_rows($rsCate)>0)
{
while($rowCate = DB_fetch_array($rsCate))
{
$px='';
?>
<option value="<?php echo $rowCate['id'];?>" <?php echo $px ?>><?php echo $dashVal;?>
<?php echo "-".$rowCate['deptname'];?> </option>
<?php 
CategoryShow($companyid,$rowCate['id'], $dashVal);
}
}
} 



$dashVal = "&nbsp;&nbsp;&nbsp;";

if($_REQUEST['action']=='del')
{
			$condition="id='".$_REQUEST['Id']."'";
			$s->DeleteRecord($table,$condition);
			$s->pageLocation($targetpage.$url);
}
 
 
//*************************Update DATA FROM DATA BASE***************************************
if($_REQUEST['Save'] && $_REQUEST['action']=='add')
{
	$rolepid=trim($_REQUEST['rolepid']); 
	$user_input=$_REQUEST['password'];
	$data_userid['userid']=$_REQUEST['userid'];
	$data_userid['parentid']=$_REQUEST['www_users'];
	$data_userid['mst_dpt_heirarchy']=$_REQUEST['heirarchy'];
	$data_userid['password']=CryptPass($_POST['password']);
 	$data_userid['email']=$_REQUEST['email'];		
	if($_REQUEST['assigned_sites']!='0' && $_REQUEST['assigned_sites']!='')
	{
	$data_userid['assigned_sites']=implode(",",$_REQUEST['assigned_sites']);
	}
	else
	{
	$data_userid['assigned_sites']=$_REQUEST['assigned_sites'];
	}
	$data_userid['mst_site']=implode(",",$_REQUEST['mst_site']);
	$data_userid['fullaccess']=8;
	$data_userid['theme']='professional';
	$data_userid['language']='en_US';
	$data_userid['Type']='Dealer';
  	$data_userid['modulesallowed']=implode(',',$_REQUEST['modulesallowed']);
  	$data_userid['realname']=$_REQUEST['realname'];
	$data_userid['rolepid']=$rolepid; 
  	$data_userid['companyid']=$_REQUEST['companyid']; 
	$data_userid['dashboard_assign']=$_REQUEST['dashboard'];
	$data_userid['dealer']=$_REQUEST['dealer'];
	$row_insert= $s->RecordInsert('www_users', $data_userid );
		$s->pageLocation($targetpage.$url);	 
 
 }
 
 if($_REQUEST['action']=='Edit'  && $_REQUEST['Id']!='' && !$_REQUEST['Save'])
{
 	$condition="id='".$_REQUEST['Id']."'";
	$query=DB_query("select * from www_users where $condition ",$db);
	$RData=DB_fetch_array($query);	
	$userid=$RData['userid'];
	$companyid=$RData['companyid'];
   	$mst_site=$RData['mst_site'];
	$mst_dpt_heirarchy=$RData['mst_dpt_heirarchy'];
	$assigned_sites=$RData['assigned_sites'];
	$dashboard=$RData['dashboard_assign'];
	$phone=trim($RData['phone']);
  	$realname=trim($RData['realname']);
	$email=trim($RData['email']);
	$fullaccess=$RData['fullaccess'];
 	$rolepid=$RData['rolepid'];  
	$dealer=$RData['dealer'];
 }



if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{
    $rolepid=trim($_REQUEST['rolepid']); 
	if($_POST['password']!=''){
	$data['password']= CryptPass($_POST['password']);
	}
	$data['email']=$_REQUEST['email'];
	if($_REQUEST['assigned_sites']!='0' && $_REQUEST['assigned_sites']!='')
	{
	$data['assigned_sites']=implode(",",$_REQUEST['assigned_sites']);
	}
	else
	{
	$data['assigned_sites']=$_REQUEST['assigned_sites'];
	}
	$data['mst_site']=implode(",",$_REQUEST['mst_site']);
	$data['fullaccess']=8;
	$data['theme']='professional';
	$data['language']='en_US';
	$data['Type']='Dealer';
  	$data['modulesallowed']=implode(',',$_REQUEST['modulesallowed']);
  	$data['realname']=$_REQUEST['realname'];
	$data['rolepid']=$rolepid; 
  	$data['companyid']=$_REQUEST['companyid']; 
	$data['dashboard_assign']=$_REQUEST['dashboard'];
	$data['dealer']=$_REQUEST['dealer'];

		 
 	$cond="id='".$_REQUEST['Id']."'";
	$row_update= $s->RecordEdit('www_users', $data, $cond);
 	 if(!$row_update)
	{
	   die('Error'.DB_error());
	}  
 	
	 
	$s->pageLocation($targetpage.$url);
}







?>
<script language="javascript" >
 
function FProfileMaster()
{		myCheckboxArray = document.myform.elements["assigned_sites[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('assigned_site').checked==true)
	{	
	
		if(t==undefined)
	
		document.getElementById('assigned_sites_'+1).checked=true;		
			for (var i = 0; i <= t; i++)
	{
	document.getElementById('assigned_sites_'+i).checked=true;	
	}	
	}
	else
	{

	if(t==undefined)		
		document.getElementById('assigned_sites_'+1).checked=false;		
			
	for (var i = 0; i <= t; i++)
	{
	document.getElementById('assigned_sites_'+i).checked=false;	
	}
	}
	

	
}

function CompanyRoles(str,hiddiv, url)
{

	document.getElementById("txtfiles").style.display  = "none";;
  if (str=="")
  {
  document.getElementById(hiddiv).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(hiddiv).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",url+"?q="+str,true);
xmlhttp.send();
}

function getZones(str)
{
	var cnt=document.getElementById("cnt").value;
	var arr=Array();
	for(var i=0; i<cnt; i++)
	{
		if(document.getElementById("mst_site_"+i).checked==true)
		{
			arr.push(document.getElementById("mst_site_"+i).value);
		}
	}
//alert(arr);
	
	if(str=="")
  {
  document.getElementById('hid-zone-div').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('hid-zone-div').innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ZonePrefix.php?arr="+arr,true);
xmlhttp.send();
	
}
</script> 
<link href="css/pagnation.css" rel="stylesheet" type="text/css">
<script language="javascript" src="js/function-ajax.js"></script>
<p align="center"><h2>Dealer Users</h2></p>	


<table width="60%" border="0" cellspacing="0" cellpadding="0" align="center" style="border:#999999 solid 2px">
  
   
   <?php
//**************************Display Mess********************************************     
   
     if(isset($daction))
	 { ?>
	  <tr>
    <td colspan="3" ><div align="left" class="success" >Record <?php echo $_REQUEST['daction']; ?> Successfully </div>    </td>
	   </tr>
	 <?php 
	 }
	 
	 
//**************************Error Section*********************************************     
	
	 
if(isset($msgErr)&&$msgErr!='' && count($msgErr)>0 )
{?>
    <tr>
     <td colspan="3" ><div class="error">
  <?php 
	for($kp=0;$kp<count($msgErr);$kp++)
	 {
		echo  $msgErr[$kp].'<br>';
	 }
  ?></div></td>
  </tr>
  
   <?php } 
 //**************************Error Section*********************************************     
 ?>
 
 
  <?php  if($_REQUEST['action']==''){?>	

 <tr><td colspan="3" >
 
 </td>
</tr>
<?php } else{ ?>





<tr><td colspan="3" >
<form action="" method="post" name="myform" >
		<table width="100%" border="0" cellspacing="2" cellpadding="2">
		<tr>
		<th width="50%"><strong>User Id</strong></th>
		<input name="fullacces" type="hidden" value="8" />
		<td width="50%"><input name="userid" type="text" value="<?php echo $userid;?>"  style="width:100%"/></td>
		</tr>
		<tr>
		<th><strong>Password:</strong></th>
		 <td> <input type='password' name='password' style="width:100%" maxlength=20 value='<?php echo $password;?>'></td>
		 </tr>
		 
		 <th><strong>Name:</strong></th>
		 <td> <input type='realname ' name='realname' style="width:100%" value='<?php echo $realname ;?>' onblur="ShowDiv(this.value,'hid-EmployeeID','EmpCodePrefix.php')"></td>
		 </tr>	  
	
		<tr>
		<th><strong>Company Name</strong></th>
		<td colspan="2"> <!--onchange="CompanyRoles(this.value,'txtCompanyRole','CompanyRole.php')"-->
		<select name="companyid" id='companyid' onchange="ShowDiv(this.value,'hid-site-div','Dealerprefix.php?type=Companyusers')" style="width:100%">
 <option value="0">--</option>
 		<?php //CompanyId='".$_SESSION['ACGCompanyId']."'
		  $query=DB_query("select * from companies where 1=1",$db);
		  while($row=DB_fetch_array($query))
		  {
 		  	$p='';
			if($row['CompanyId']==$companyid)
			{
			   $p="selected";
			}
 		 ?>
		<option value="<?php echo $row['CompanyId'] ?>" <?php echo $p; ?>><?php echo $row['CompanyName'] ?></option>
		<?php } ?>
		</select></td></tr></table>		
		 <div id='hid-site-div'>
		 <table width="100%" border="0">
		 <tr>
		<th><strong>Dealer</strong></th>
		<td colspan="2"> 		
		<select name="dealer" id='dealer'  style="width:100%" onchange="showbranch(this.value)">
           <option value="0">--Select--</option>
 		<?php //CompanyId='".$_SESSION['ACGCompanyId']."'
		  $query=DB_query("select * from dbl_dealer where company='".$companyid."'",$db);
		  while($row=DB_fetch_array($query))
		  {
 		  	$p='';
			if($row['id']==$dealer)
			{
			   $p="selected";
			}
 		 ?>
		<option value="<?php echo $row['id'] ?>" <?php echo $p; ?>><?php echo $row['dealer_name'] ?></option>
		<?php } ?>
		</select>		 </td>
		</tr>
		<tr>
	<th width="50%"><strong>Heirarchy</strong></th>
	<td><div style="overflow:auto; height:150px; width:100%; border:1px solid; background-color:#FFFFFF" >
   	 <select name="heirarchy" id="heirarchy"  multiple="multiple" style="height:200px; width:100%">
 	       <?php 
	         $dashVal = "&nbsp;&nbsp;&nbsp;";			
			 $query_comprole=DB_query("select * from deptmaster_hierarchy where parentid=0 and companyid='".$companyid."'",$db);
 			  while($row_comprole=DB_fetch_array($query_comprole))
			  {			  
			  ?>
		<option value="<?php echo $row_comprole['id']; ?>" selected="selected"><?php echo $row_comprole['deptname']; ?></option>
  			<?php  CategoryShow($row_comprole['CompanyId'], $row_comprole['id'], $dashVal);	  }	 ?>
	     </select>
		
		</div></td></tr>   
        <tr>
		<th width="50%"><strong>Roles <br /></strong></th>
		<td colspan="2" width="50%"> 
		<?php 
		$pcode= $rolepid;
		function CategoryShow1($pcode, $PcateID, $dashVal)
		{
			global $db;
			$dashVal = $dashVal . "&nbsp;&nbsp;&nbsp;&nbsp;";
			$sqlCate = "select * from securityroles  where  rolepid  = '".$PcateID."'";
			$rsCate  = DB_query($sqlCate,$db);	
			 
			if(DB_num_rows($rsCate)>0)
			{
				while($rowCate = DB_fetch_array($rsCate))
				{
					$sqlCateSel = "select * from securityroles where rolepid =".$rowCate['rolepid']." ";
					$rsCateSel	= DB_query($sqlCateSel,$db);
					$px='';
					 if($rowCate['secroleid']==$pcode)
					 $px="selected";
			?>
					<option value="<?php echo $rowCate['secroleid'];?>" <?php echo $px ?>><?php echo $dashVal;?>
					 <?php echo $rowCate['role_name'];?> </option>
		   <?php			
					   CategoryShow1( $pcode,$rowCate['secroleid'], $dashVal);
				}
			}
		} 
		
		$dashVal = "&nbsp;&nbsp;&nbsp;";
		$query=DB_query("select * from securityroles where rolepid=0 and companyid ='".$companyid."'",$db);
		?>
		   <strong>(Select only one Role)</strong>
		   <div id='txtCompanyRole'>
		 <select name="rolepid" multiple="multiple" style="height:200px; width:100%">
            <?php
		if(DB_num_rows($query)>0){
		 
		  while($row=DB_fetch_array($query))
		  {		  
		  	$p='';
			if($row['secroleid']==$rolepid)
			{
			   $p="selected";
			}
		 ?>
            <option value="<?php echo $row['secroleid'] ?>" <?php echo $p; ?> ><?php echo $row['role_name'] ?></option>
            <?php 
		   CategoryShow1($rolepid, $row['secroleid'], $dashVal);
		}} ?>
          </select>
		</div></td></tr>  
       	</table>
		
		 </div> 
		 <div id='hid-zone-div'  >
		<table border="0" width="100%">
		<tr>
		<th width="50%"><strong>Dealer Branch</strong></th>
		<td width="50%" colspan="2">
		<div style="overflow:auto; height:150px; width:100%; border:1px solid; background-color:#FFFFFF"  >
	    <?php 
		if($mst_site!='0'){
		$sites=explode(",",$mst_site);}
		else
		{$sites=$mst_site;}
		?>
  		<?php $c3=0;
		//echo "select * from locations where mst_companyid='".$companyid."'";
		  $query=DB_query("select * from locations where mst_companyid='".$companyid."'",$db);
		  while($row=DB_Fetch_array($query))
		  {
		  if($sites!='0')
		{
				$c='';
				for($s=0;$s<count($sites);$s++)
				{
					if($sites[$s]==$row['loccode'])
					$c="checked";
				}
		}
 		 ?>
		 <input type="checkbox" name="mst_site[<?php echo $c3;?>]" id="mst_site_<?php echo $c3;?>" 
		 <?php echo $c;?> value="<?php echo $row['loccode'];?>" 
	  <!--onclick="getZones(this.value)"--> <!--ShowDiv(this.value,'hid-zone-div','ZonePrefix.php?type=Companyusers')-->
	<?php echo $row['loccode']." - ".$row['locationname'];?><br />

	 
		<?php
		$c3++; } ?>
		<input type="hidden" name="" id="cnt" value="<?php echo $c3;?>" />
	</div>
		
		
		
	   </td></tr>
		
		
		
		<tr>
		<th width="50%"><strong>Area</strong></th>
		<td width="50%" colspan="2"> 
		<div style="overflow:auto; height:150px; width:100%; border:1px solid; background-color:#FFFFFF" >
 	<?php  
	 if($dealer!='')	{
	 $query=DB_query("select * from dbl_dealer where id='".$dealer."'",$db);
		  $row2=DB_Fetch_array($query);
	 	  $area_code=$row2['area_code'];

		if($assigned_sites!='0'){
		$sites=explode(",",$assigned_sites);}
		else
		{$sites=$assigned_sites;}
 	  $c3=0;$j=1;
	$site1=implode("','",explode(",",$mst_site));
	  $query=DB_query("select * from zone_master where id='".$area_code."' and  status='Y'",$db);
 	 ?>
  <?php 
 
		  while($row=DB_fetch_array($query))
		  {
		  	
				if($sites!='0')
				{
						$c='';
						for($s=0;$s<count($sites);$s++)
						{
							if($sites[$s]==$row['id'])
							$c="checked";
						}
				}
	?>
	 
	<?php 
?>
 		 <input type="checkbox" name="assigned_sites[]" id="assigned_sites_<?php echo $c3;?>" <?php echo $c;?> value="<?php echo $row['id'];?>" 
	   />
	<?php echo $j." - ".$row['ZoneTitle'];?><br />

 <?php  $c3++; $j++;} ?>
<?php } ?>
		 </div>
		
		
		
			  </td>
		</tr>
		</table>
		</div>
		 
		<table width="100%" border="0">
		
		  <tr><th width="50%"><strong>DashBoard </strong></th>
<td><div style="overflow:auto; height:150px; width:100%; border:1px solid; background-color:#FFFFFF" >	
	<?php
	$module1=explode(",",$dashboard); 
	$sqldashboard=DB_query("select * from dashboard_master",$db);
	while($sqldash=DB_fetch_array($sqldashboard))
	{
		$p1='';
 
if(in_array($sqldash['id'], $module1))
$p1='checked';
	?>	
	<!--<input type="checkbox" name="dashboard[]" value="<?php echo $sqldash['id'];?>" <?php echo $p1?>>--><input type="radio" name="dashboard" value="<?php echo $sqldash['id'];?>" <?php echo $p1?>>	<?php echo $sqldash['dashboard_name']."<br>"; ?>
	<?php	 } ?>		
</div></td>
	
	
</tr>
	
		
		  
		  
		   <tr>
		    <th colspan="3">
			
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td colspan="2">&nbsp;</td></tr>
		  <tr>
			<td valign="top"> 
			
			<fieldset>
			<legend> <strong>User Contact Details</strong></legend>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td><strong>Contact Number:</strong></td>
				<td><input name="phone" type="text" value="<?php  echo $phone;?>" />&nbsp;</td>
			  </tr>
			  <tr>
			    <td><strong>Email Address:&nbsp;</strong></td>
			    <td><input name="email" type="text" value="<?php echo $email ?>" /></td>
			    </tr>
			
			  <tr>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    </tr>
			</table>
			</fieldset></td>
			
			
			<td valign="top"> 
			<fieldset>
			<legend> <strong>Security&nbsp; Setting </strong></legend>
			<?php
$ModuleList = array('Orders','Receivables', 'Payables','Purchasing', 'Inventory', 'Manufacturing', 'General Ledger', 'Setup');
					
					?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php 
foreach($ModuleList as $ModuleName){

	echo '<tr><td>'  . $ModuleName . ' ' . _('options') . ": </td><td><select name='modulesallowed[]'>";
 	 	echo '<option selected value=1>' . _('Yes');
		echo '<option value=0>' . _('No');
	 }
	echo '</select></td></tr>';

?>			</table>
			</fieldset>			</td>
		  </tr>
		</table>			</th>
		    </tr>
		  <tr>
		    <th colspan="2"><div align="center"><input type="submit" name="Save" value="Save" /></div></th>
		    </tr>
		</table>
</form>


</td>
</tr>
<?php } ?>

</table>

<script>

function showbranch(str)
{

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("hid-zone-div").innerHTML=xmlhttp.responseText;
    }
  }
  var company=document.getElementById("companyid").value;
xmlhttp.open("GET","dealerbranchprefix.php?country="+str+"&company="+company,true);
xmlhttp.send();
}

</script>


	
    
