<?php 
session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/admin.class.php');
	$admininfo = $admin->getinfo();
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	$opendb=new mydb();
	
	$ls=$_GET['ls'];
	$id=$_GET['id'];
	$modiid=$_POST['modiid'];
	$userName=trim($_POST['userName']);
	$userRealName=$_POST['userRealName'];
	$usercompany=$_POST['usercompany'];
	$userPwd=$_POST['userPwd'];
	$userEmail=strtolower(trim($_POST['userEmail']));
	$userWeb=strtolower(trim($_POST['userWeb']));
	$userMemo=$_POST['userMemo'];	
	$userTime=$_POST['userTime'];
	$userNum=$_POST['userNum'];
	$branchID=$_POST['branchID'];

	$lsc=$_GET['lsc'];
	$lsctype=$_GET['lsctype'];
	$searchproductName=trim($_POST['searchproductName']);
	if($searchproductName==''){$searchproductName=$_GET['searchproductName'];}// 分页传过来的变量

	function noticeemail($user,$email,$action)
	{
		require_once('inc/conn.php');
		$opendb=new mydb();
		$config_sql="select * from ls_config ";
		$config_query=$opendb->query($config_sql);
		$config_rows=mysql_fetch_assoc($config_query);

		if($action=="delete")
		{
			$Subject="delete your ".$config_rows['website']." account ";
			$Content1="Dear Sir,\r\n We are sorry for your account on the web of ".$config_rows['website']." has been deleted. For further information please contact webmaster Thanks.\r\n\r\nWith Best Regards,\r\n".$config_rows['website']."";
	
		}

		if($action=="active")
		{
			$Subject="Your ".$config_rows['website']." account actived ";
			$Content1="Dear Sir,\r\n We are delighted to inform you that your account on the web of ".$config_rows['website']." has been actived. You can use your username \"".$user."\" and password to login. For further information please contact webmaster, Thanks.\r\n\r\nWith Best Regards,\r\n".$config_rows['website']."";
	
		}

		if($action=="deactive")
		{
			$Subject="Your ".$config_rows['website']." account deactived ";
			$Content1="Dear Sir,\r\n We inform you that your account on the web of ".$config_rows['website']." has been deactived. Your username \"".$user."\" can not work. For further information please contact webmaster, Thanks.\r\n\r\nWith Best Regards,\r\n".$config_rows['website']."";
	
		}

		$Content1.="\r\n\r\n".date("j, n, Y");                       ;
		
		if(mail($email,$Subject,$Content1))
		{
			$ls_msg="已经给用户 ".$user." 发送 ".$action." 通知邮件"; 
		}
		else
		{
			$ls_msg="给用户 ".$user." 发送 ".$action." 通知邮件失败"; 
		}
		return $ls_msg;
	}
	
	$nowusernum=$_GET['userNum'];

	$ls_msg="客户管理操作";
	
	
	
	
	switch ($ls)
	{
		case "insert":
			$select_sql2="select * from ls_users where userName='".$userName."' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysql_num_rows($select_query2); //总数
			if($select_rows2>0)
			{ $ls_msg="用户名已经存在，请重新选择";}
			else{

				$select_sql2="select * from ls_users ";
				$select_query2=$opendb->query($select_sql2);
				$select_rows2=mysql_num_rows($select_query2); //总数
				$info = $admin->getinfo();
				$branchID = $info['branchID'];
				$insert_sql="insert into ls_users(userName,userRealName,usercompany,userPwd,userEmail,userWeb,userTime,userMemo,userNum,enable,branchID) values ('".$userName."','".$userRealName."','".$usercompany."','".$userPwd."','".$userEmail."','".$userWeb."','".date('Y-m-d')."','".$userMemo."',".($select_rows2+1).",1,$branchID)"; 
				//echo $insert_sql;
				$insert_query=$opendb->query($insert_sql);
				$ls_msg="添加操作成功！";
			}
			break;
		case "modi":
			if($_POST['branchID']!=''){
				$branchID=$_POST['branchID'];
			}else{
				$branchID=$admininfo['branchID'];	
			}
			$modi_sql="update ls_users set userName='".$userName."', userRealName='".$userRealName."', usercompany='".$usercompany."', userPwd='".$userPwd."', userEmail='".$userEmail."', userWeb='".$userWeb."', userMemo='".$userMemo."',userTime='".$userTime."', userNum='".$userNum."',branchID=$branchID where id=".$modiid." ";
			//echo $modi_sql;
			$modi_query=$opendb->query($modi_sql);
			$ls_msg="修改操作成功！";
			break;
		case "del":
			$user_sql2="select * from ls_users where id=".$id." ";
			$user_query2=$opendb->query($user_sql2);
			$user_rows2=mysql_fetch_assoc($user_query2);
			noticeemail($user_rows2['userName'],$user_rows2['userEmail'],"delete");

			$num_sql2="update ls_users set userNum=userNum-1 where userNum>".$user_rows2['userNum']." ";//all order number-1
			$down_query=$opendb->query($num_sql2);
		
			$del_sql="delete from ls_users where id=".$id." ";
			//echo $del_sql;
			$del_query=$opendb->query($del_sql);
			$ls_msg="删除操作成功！";
			
			break;
		case "unenable":
			$unenable_sql="update ls_users set enable=0 where id=".$id." ";
			//echo $unenable_sql;
			$unenable_query=$opendb->query($unenable_sql);
			$ls_msg="禁止操作成功！";
			$user_sql="select * from ls_users where id=".$id." ";
			$user_query=$opendb->query($user_sql);
			$user_row=mysql_fetch_assoc($user_query);

			$ls_msg.=noticeemail($user_row['userName'],$user_row['userEmail'],"deactive");
			
			 
			break;
		case "enable":
			$enable_sql="update ls_users set enable=1 where id=".$id." ";
			//echo $enable_sql;
			$enable_query=$opendb->query($enable_sql);
			$ls_msg="允许操作成功！";
			$user_sql="select * from ls_users where id=".$id." ";
			$user_query=$opendb->query($user_sql);
			$user_row=mysql_fetch_assoc($user_query);

			$ls_msg.=noticeemail($user_row['userName'],$user_row['userEmail'],"active");
			 
			break;
	}
	



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<script language="JavaScript" src="js/ls.js"></script>
		<title>无标题文档</title>
	</head>

	<body>
		<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
			<tr>
				<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>客户管理</strong></font></div>
				</td>
			</tr>
			
			<tr>
			  <td height="213" valign="top" bgcolor="#dee7ff"><br>
                  <?php 
	if( isset($_GET['page']) )
	{$page = intval( $_GET['page'] );	}
	else
	{$page = 1;	}
	
	if($_POST['gopage']!="")
	{$page =$_POST['gopage']; 
	}

	// 每页数量
	if($usersNum!=0)
	{	$PageSize = $usersNum;}
	else
	{	$PageSize = 20;}
	
	// 获取总数据量
	/********************************************************************************/
	
	$sql="select * from ls_users ";
	
	if($searchproductName!="")
		{
			if($admininfo['branchID'] == 0){
				$brsql .=  " ";
			}else{
				$brsql .= " branchID=".$admininfo['branchID']." and ";	
			}
		
			$sql.="where ".$brsql." ((userName like '%".$searchproductName."%' or userRealName like '%".$searchproductName."%') or (userEmail like '%".$searchproductName."%' or usercompany like '%".$searchproductName."%' ) )";
			$ls_msg="包含".$searchproductName."的用户";
		}
	else
	{
		if($admininfo['branchID'] == 0){
				$brsql =  " ";
			}else{
				$brsql = " where branchID=".$admininfo['branchID'];	
			}
		
		$sql .= $brsql;
	}
	//echo $sql;
	$result=$opendb->query($sql);
	
	/********************************************************************************/
	// 获取总数据量
	
	$row = mysql_num_rows($result);
	$amount = $row;
	
	// 记算总共有多少页
	if( $amount ){
	   if( $amount < $PageSize ){ $page_count = 1; }			   //如果总数据量小于$PageSize，那么只有一页
	   if( $amount % $PageSize ){								  //取总数据量除以每页数的余数
		   $page_count = (int)($amount / $PageSize) + 1;		   //如果有余数，则页数等于总数据量除以每页数的结果取整再加一
	   }else{
		   $page_count = $amount / $PageSize;					  //如果没有余数，则页数等于总数据量除以每页数的结果
	   }
	}
	else{
	   $page_count = 0;
	}
	
	// 翻页链接
	$page_string = '';
	$pagecatestr="";
	if( $page == 1 ){
	   $page_string .= '首页 | 上一页 |';
	}
	else{
	   $page_string .= '<a href=?&page=1&'.$pagecatestr.'><u><font color=\"#0000FF\">首页</font></u></a> | <a href=?&page='.($page-1)."&".$pagecatestr.'><u><font color=\"#0000FF\">上一页</font></u></a> | ';
	}
	if( ($page == $page_count) || ($page_count == 0) ){
	   $page_string .= ' 下一页 | 尾页';
	}
	else{
	   $page_string .= ' <a href=?&page='.($page+1).'&'.$pagecatestr.'><u><font color=\"#0000FF\">下一页</font></u></a> | <a href=?&page='.$page_count.'&'.$pagecatestr.'><u><font color=\"#0000FF\">尾页</font></u></a>';
	}
		 $page_string .= " &nbsp;&nbsp;页次：<font color='#FF0000'><b>".$page."</b></font>/<b>".$page_count."</b>页";
		 $page_string .= " &nbsp;&nbsp;<b>".$PageSize."</b>个客户每页";
	
	
 
?>
	         <form action="<?php echo "?&lsc=".$lsc."&lsctype=".$lsctype."&page=".($page<=0?1:$page)."&searchproductName=".$searchproductName; ?>" method="post" name="searchfm" id="searchfm">
				<table border="0" width="94%" align="center">
						<tr>
							<td width="42%">提示：<font color="#FF0000"><?php echo $ls_msg; ?></font></td>
                            <td width="22%" align="right"><span style="padding-left:6px;"><strong>查找：</strong>
                              <input type="text" name="searchproductName" size="12" maxlength="50" />
                              <input type="submit"  value="查询" />
                            </span></td>
                            <td width="13%" align="right" style="padding-left:6px;"></td>
                            <td width="7%" align="right"><span style="padding-left:6px;"><a href="?lsc=all">全部</a></span></td>
                            <td width="16%" align="right">共 <b>
                              <?=$row?>
                            </b> 个客户</td>
                        </tr>
			   </table>
	    </form>	
					<table width="94%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
					 
<tr bgcolor="#B9DAFF">
  <td align="center" width="93">序号</td> 
						<td align="center" width="93" height="38">用户名</td>
					   <td align="center" width="91">密码</td>
                       <td width="108" align="center">客户姓名</td>
                       <td width="168" align="center">公司名</td>
					   <td width="100" align="center">日期</td>
					   <td width="140" align="center">email</td>
					   <td width="125" align="center">网址</td>
                       <td width="125" align="center">部门</td>
						<td width="52" align="center" >操作</td>
                        <td width="53" align="center" >删除</td>
						<td align="center" width="55">激活</td>
					  </tr>
					 <?php 
					 		//查询数据
					$user_sql=$sql." order by  ID  desc  limit ". ($page-1)*$PageSize .",".$PageSize." ";	

					$user_query=$opendb->query($user_sql);
			
					 $i=1;
					  while ($user_rows=mysql_fetch_assoc($user_query))
					  {
					  ?>
						<tbody id='ZH_<?=$user_rows['ID']?>' >
						<tr bgcolor='#f4f4f4' onMouseOver="this.style.backgroundColor='#CCFFCC'"
						onMouseOut="this.style.backgroundColor='#f4f4f4' ">
						
  <td align='left' style='padding-left:10px;' height='25' ><?=$user_rows['userNum'] ?></td>
  <td align='left' style='padding-left:10px;' height='25' ><?=$user_rows['userName'] ?></td>
  <td align='left' style='padding-left:10px;' height='25'> ****** </td>
  <td align='left' style='padding-left:10px;' height='25'><?=$user_rows['userRealName'] ?></td>
  <td align='left' style='padding-left:5px;' height='25'><?=trim($user_rows['usercompany'])?></td>
  <td align='left' style='padding-left:10px;' height='25'>
  </a><?=trim($user_rows['userTime'])?></td>
  <td align='left' style='padding-left:10px;'><a href="mailto:<?=$user_rows['userEmail']?>">
    <?=$user_rows['userEmail']?>
  </a></td>
  <td align='left' style='padding-left:10px;' height='25'><a href="<?=$user_rows['userWeb']?>">
    <?=$user_rows['userWeb']?>
  </a></td>
  <td align="center">
  <?
	 $sql = "select * from ls_branch";
  	$branchlist = $opendb->AllRows($sql);
  		
  		foreach($branchlist as $v){
			if($v['ID'] == $user_rows['branchID']){
				echo $v['branchName'];
			}	
		}
  ?>
  </td>
  
  <td align='center'><a href='javascript:secBoard(<?=$user_rows['ID']?>)' title='编辑'><img src='images/b_edit.png' alt="" border='0' /></a></td>
  <td align='center'><?
						if($user_rows['userAdmin']==1)
						{
						
							echo "	<img src='images/b_undrop.png' border='0'>";
						}
						if($user_rows['userAdmin']==0)
						{
						
							echo "		<a href='?page=$page&id=".$user_rows['ID']."&ls=del' onclick='return ConfirmDel();' title='删除'><img src='images/b_drop.png' border='0'></a>";
						 }?></td>
  <td align='center'><?  
						  if($user_rows['enable']==1)
							{
								echo "<a href='?page=$page&id=".$user_rows['ID']."&ls=unenable' title='显示'><img src='images/enable.gif' border='0'></a>";
							}
						  if($user_rows['enable']==0)
						  	{
								echo "<a href='?page=$page&id=".$user_rows['ID']."&ls=enable' title='不显示'><img src='images/unenable.gif' border='0'></a>";
							}
							?></td>

						</tr>
						</tbody>
						
						<tbody id='Z_<?=$user_rows['ID']?>' style='display:none;'>
					  <form name='fm_user' action='?ls=modi' method='post'> 
							<tr bgcolor='#CCCCCC'>
							<td align='left' style='padding-left:10px;' height='25' >
					  	<?=$user_rows['userNum'] ?>
							</td>
								<td style='padding-left:5px;'>
									<input type='text' value='<?=$user_rows['userName']?>' name='userName' class='inp'><input type='hidden' value='<?=$user_rows['userNum'] ?>' name='userNum'> </td>
  <td style='padding-left:5px;'><input type='password' value='<?=$user_rows['userPwd'] ?>' name='userPwd' class='inp' /></td>
  <td align='left' style='padding-left:5px;' height='25'><input type='text' value='<?=$user_rows['userRealName'] ?>' name='userRealName' class='inp' /></td>
  <td align='left' style='padding-left:5px;' height='25'><input type='text' value='<?=$user_rows['usercompany'] ?>' name='usercompany' class='inp' /></td>
  <td align='left' style='padding-left:5px;' height='25'><input type='text' value='<?=trim($user_rows['userTime']) ?>' name='userTime' class='inp' size="12" /></td>
  <td align='left' style='padding-left:5px;'><input type='text' value='<?=$user_rows['userEmail'] ?>' name='userEmail' class='inp' /></td>
  <td align='left' style='padding-left:5px;' height='25'><input type='text'  name='userWeb' class='inp' value="<?=trim($user_rows['userWeb'])==""?"http://":trim($user_rows['userWeb'])?>" /></td>
  <td>
  <?php
  if($admininfo['branchID'] == 0){
	  $sql = "select * from ls_branch";
	  $branchlist = $opendb->AllRows($sql);
	  echo "<select name='branchID'>";
				echo "<option value='0' >无</option>";
			foreach($branchlist as $v){
				if($v['ID'] == $user_rows['branchID']){
					echo "<option value='".$v['ID']."' selected='selected' >".$v['branchName']."</option>";	
				}else{
					echo "<option value='".$v['ID']."' >".$v['branchName']."</option>";
				}	
			}
	  echo "</select>";
  }else{
	 	echo $admininfo['branchName']; 
	 }
  ?>
  <td>
  <td colspan='5' align='center'><input value='<?=$user_rows['ID'] ?>' name='modiid' type='hidden' />
    <input type='submit' value='确定修改' />
    &nbsp;
    <input type='button' onclick='javascript:secBoard2(<?=$user_rows['ID'] ?>)' value='放弃修改' /></td>
</tr>
					  </form>
						</tbody>
					   <?
						$i++;
					  }
					 ?> 
                     

							<tr>
							<td align="center" colspan="13" bgcolor="#B9DAFF" height="30"><? echo $page_string; ?> </td>
					  </tr>
				</table>
		   <br />

					
					
	       <form name="fmadduser" action="?ls=insert" method="post" onSubmit="return checkSubmitForm('userEmail1,userPwd1,userName1','邮箱,密码,用户名');">
					 
        <table width="95%" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#FFFFFF">
          <tr bgcolor="#B9DAFF">
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td style='padding-left:20px;'>&nbsp;</td>
            <td align="right" style='padding-left:20px;'>&nbsp;</td>
            <td style='padding-left:20px;'>&nbsp;</td>
            <td align="right" style='padding-left:20px;'>&nbsp;</td>
            <td align="left"  style='padding-left:20px;'>&nbsp;</td>
          </tr>
          <tr bgcolor="#B9DAFF"> 
            <td width="8%" height="28" align="right" style='padding-left:20px;'>用户名:&nbsp;</td>
            <td width="18%" style='padding-left:20px;'><input type="text" name="userName" id="userName1" >            
            *</td>
            <td width="12%" align="right" style='padding-left:20px;'>密码:&nbsp;</td>
            <td width="20%" style='padding-left:20px;'><input type="text" name="userPwd" id="userPwd1">
*</td>
            <td width="13%" align="right" style='padding-left:20px;'>Email:&nbsp;</td>
            <td width="29%" align="left"  style='padding-left:20px;'>
              <input type="text" name="userEmail"  id="userEmail1">
*</td>
          </tr>
          <tr bgcolor="#B9DAFF"> 
            <td height="28" align="right" style='padding-left:20px;'>姓名:&nbsp;</td>
            <td style='padding-left:20px;'><input type="text" name="userRealName" id="userRealName"></td>
            <td align="right" style='padding-left:20px;'>公司:&nbsp;</td>
            <td style='padding-left:20px;'><input type="text" name="usercompany"  id="usercompany"></td>
            <td align="right" style='padding-left:20px;'>网址:&nbsp;</td>
            <td align="left"  style='padding-left:20px;'><input name="userWeb" type="text"  value="http://" /></td>
          </tr>
          <tr bgcolor="#B9DAFF"> 
            <td height="28" align="right" style='padding-left:20px;'>备注:&nbsp;</td>
            <td height="28" align="left" style='padding-left:20px;'><input type="text" name="userMemo"  id="userMemo1"></td>
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="left" style='padding-left:20px;'><input type="submit" value="添加客户">
              <label>
                <input type="reset" name="button" id="button" value="重置">
            </label></td>
          </tr>
          <tr bgcolor="#B9DAFF">
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="left" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="right" style='padding-left:20px;'>&nbsp;</td>
            <td height="28" align="left" style='padding-left:20px;'>&nbsp;</td>
          </tr>
        </table>
	       </form>
					
				<br /></td>
			</tr>
		</table>
	</body>
</html>