<?php 
defined('IN_HS') or exit('Access Denied');

require_once MOD_ROOT.'/include/member.class.php';
require_once MOD_ROOT.'/admin/include/member_admin.class.php';

if(!isset($username)) $username = '';
$member = new member_admin($username);


$GROUP = cache_read('member_group.php');

$submenu = array
(
	array($LANG['member_manage'], '?mod='.$mod.'&file='.$file.'&action=manage'),
	array($LANG['search_member'], '?mod='.$mod.'&file='.$file.'&action=search'),
	array($LANG['add_member'], '?mod='.$mod.'&file='.$file.'&action=add'),
);

$menu = adminmenu($LANG['member_manage'], $submenu);

switch($action)
{
    case 'edit':
		if($dosubmit)
		{
			if(!is_email($email)) showmessage($LANG['input_valid_email'],"goback");
			$gender = $gender==1 ? 1 : 0;

			if(!empty($qq) && (!is_numeric($qq) || strlen($qq)>20 || strlen($qq)<5)) showmessage($LANG['input_correct_qq'],"goback");
			if(strlen($truename)>50 || strlen($telephone)>50 || strlen($address)>255 || strlen($homepage)>100) showmessage($LANG['truename_telephoe_etc_not_too_long'],"goback");

            $arrgroupid = isset($arrgroupid) ? ','.implode(',', $arrgroupid).',' : '';
            

			$sql = $password ? "password='".hsmd5($password)."'," : "";


			$db->query("UPDATE ".TABLE_MEMBER." SET $sql email='$email',answer='$answer',money='$money',showemail='$showemail',groupid='$groupid',arrgroupid='$arrgroupid',question='$question',point='$point',chargetype='$chargetype',begindate='$begindate',enddate='$enddate' WHERE userid='$userid'");
			$db->query("UPDATE ".TABLE_MEMBER_INFO." SET truename='$truename',gender='$gender',idtype='$idtype',idcard='$idcard',telephone='$telephone',qq='$qq' WHERE userid=$userid");
		    

			showmessage($LANG['operation_success'], $forward);
		}
		else
		{
			$memberinfo = $member->view('m.userid='.$userid);
			if(!$memberinfo) showmessage($LANG['account_not_exist_or_delete'], $forward);

			extract(new_htmlspecialchars($memberinfo));


            $groups = $arrgroups = array();
            foreach($GROUP as $id=>$g)
            {
				$groups[] = $g;
				if($g['grouptype'] != 'system')
				{
					$arrgroups[] = $g;
				}
            }

			$arrgroupid = $arrgroupid ? array_filter(explode(',', $arrgroupid)) : array();


			include admintpl('member_edit');
		}
		break;

    case 'delete':

		if(!isset($userid)) showmessage($LANG['select_account'], $forward);

		$member->delete($userid);
		showmessage($LANG['operation_success'], $forward);

		break;

    case 'manage':

		$page = isset($page) ? intval($page) : 1;
		$pagesize = $HS['pagesize'] ? $HS['pagesize'] : 30;
		$offset = ($page-1)*$pagesize;
		$frommoney = isset($frommoney) ? intval($frommoney) : 0;
		$tomoney = isset($tomoney) ? intval($tomoney) : 0;
		$frompayment = isset($frompayment) ? intval($frompayment) : 0;
		$topayment = isset($topayment) ? intval($topayment) : 0;
    $groupid = isset($groupid) ? intval($groupid) : 0;

        if(!isset($username)) $username = '';
        if(!isset($occupation)) $occupation = '';

        if(!isset($qq)) $qq = '';
        if(!isset($email)) $email = '';

		$condition = '';
		$condition .= $username ? " and m.username like '%$username%'" : '';
		$condition .= $groupid ? " and (m.groupid=$groupid or m.arrgroupid like '%,$groupid,%')" : '';
		$condition .= $email ? " and m.email='$email'" : '';
		$condition .= $truename ? " and i.truename like '%$truename%'" : '';
		$condition .= $qq ? " and i.qq='$qq'" : '';
		$condition .= $occupation ? " and i.occupation='$occupation'" : '';
		$condition .= $frommoney ? " and m.money>=$frommoney" : '';
		$condition .= $tomoney ? " and m.money<=$tomoney" : '';
		$condition .= $frompayment ? " and m.payment>=$frompayment" : '';
		$condition .= $topayment ? " and m.payment<=$topayment" : '';


		$r = $db->get_one("SELECT count(*) as num FROM ".TABLE_MEMBER." m,".TABLE_MEMBER_INFO." i WHERE m.userid=i.userid $condition");
		$pages = phppages($r['num'], $page, $pagesize);

		$members = $member->get_list($condition, $page, $pagesize);

		$groupids = showgroup('select', 'groupid', $groupid);

		include admintpl('member_manage');

		break;

    case 'lock':

		if(!isset($userid)) showmessage($LANG['select_account'], $forward);

		$member->lock($userid, $val);
		showmessage($LANG['operation_success'], $forward);

		break;

	case 'checkuser':
		if(strtolower($CONFIG['charset']) != 'utf-8' && preg_match("/^([\s\S]*?)([\x81-\xfe][\x40-\xfe])([\s\S]*?)/", $username))
		{
			include HS_ROOT.'/include/charset.func.php';
			$username = convert_encoding('utf-8', $CONFIG['charset'], $username);
			$member->set_username($username);
		}
		if(strlen($username) < 2 || strlen($username) > 20)
		{
			echo 1;
		}
		elseif($member->is_badword($username))
		{
			echo 2;
		}
		elseif($member->get_info())
		{
			echo 3;
		}
		elseif($member->get_info())
		{
			echo 4;
		}
		else
		{
			echo 0;
		}
	break;
	case 'search':
		$groupids = showgroup('select', 'groupid', $groupid);

    include admintpl('member_search');
		break;

	case 'move':
		$userids = is_array($userid) ? implode(',', $userid) : $userid;
	    if(!$userids) showmessage($LANG['select_account'], $PHP_REFERER);

		if($dosubmit)
	    {
			$groupid = intval($groupid);
	        if(!$groupid) showmessage($LANG['select_group'], $PHP_REFERER);

		    $sql = '';
		    if($ischargebynewgroup)
			{
				@extract($member->group($groupid));
				$begindate = date('Y-m-d');
				$date->dayadd($defaultvalidday);
				$enddate = $defaultvalidday == -1 ? '0000-00-00' : $date->get_date();
				$point = $defaultpoint;
				$sql = ",point='$point',chargetype='$chargetype',begindate='$begindate',enddate='$enddate'";
			}
            $db->query("UPDATE ".TABLE_MEMBER." SET groupid=$groupid $sql WHERE userid IN($userids)");
			showmessage($LANG['operation_success'], $forward);
		}
		else
	    {
			$member = array();
			$result = $db->query("SELECT userid,username FROM ".TABLE_MEMBER." WHERE userid IN($userids)");
			while($r = $db->fetch_array($result))
			{
				$member[$r['userid']] = $r['username'];
			}
			$groupids = showgroup('select', 'groupid', $groupid);
			include admintpl('member_move');
		}
		break;

    default :
}
?>