<?php

require_once(dirname(dirname(__FILE__)) . '/app.php');
need_manager();

if($_GET){
    if(isset($_GET['id'])){
        $id = $_GET['id'];
    }else{
        Session::Set('error','没有选择任何用户！');
        redirect(WEB_ROOT."/manage/account.php");
    }
}

$user = Table::Fetch('user', $id);


if($_POST){
    
    if(isset($_POST['store']) && !empty($_POST['store']) && !empty($_POST['detail'])){
        $re = Table::UpdateCache('user', $id, array(
            'money' => $user['money'] + $_POST['store']    
        ));
        if($re == true){
            Session::Set('success','成功给 '.$user['username'].' 充值'.$_POST['store'].'元');
            OFlow::Create(array(
                'direction'     => 'income',
                'action'        => 'store',
                'money'         => $_POST['store'],
                'create_time'   => time(),
                'user_id'       => $id,
                'detail_id'     => '',
                'detail'        => $_POST['detail'],
                'admin_id'      => $login_user_id
            ));
        }
        redirect(WEB_ROOT."/manage/account_detail.php?id=".$id);
    }
    
    
    if(isset($_POST['withdraw']) && !empty($_POST['withdraw']) && !empty($_POST['detail'])){
        $re = Table::UpdateCache('user', $id, array(
            'money' => $user['money'] - $_POST['withdraw']   
        ));
        if($re == true){
            Session::Set('success','成功给 '.$user['username'].' 提现'.$_POST['withdraw'].'元');
            OFlow::Create(array(
                'direction'     => 'expense',
                'action'        => 'withdraw',
                'money'         => $_POST['withdraw'],
                'create_time'   => time(),
                'user_id'       => $id,
                'detail_id'     => '',
                'detail'        => $_POST['detail'],
                'admin_id'      => $login_user_id
            ));
        }
        redirect(WEB_ROOT."/manage/account_detail.php?id=".$id);
    }
    
    $u = array();
    if($_POST['password']!=$user['password']){
        $user['password'] = strval($_POST['password']);
    }else{
        unset($user['password']);
    }
    $user['email'] = strval($_POST['email']);
    $user['mobile'] = strval($_POST['mobile']);
    $user['realname'] = strval($_POST['realname']);
    $user['gender'] = strval($_POST['gender']);
    $user['qq'] = strval($_POST['qq']);
    $user['zipcode'] = strval($_POST['zipcode']);
    $user['address'] = strval($_POST['address']);
    $user['level'] = strval($_POST['level']);
    $user['manager'] = strval($_POST['manager']);
    if(empty($user['manager'])) $user['manager'] = 'N';
    $user['enable'] = strval($_POST['enable']);
    if(empty($user['enable'])) $user['enable'] = 'Y';


    if (!Utility::ValidEmail($user['email'], true)) {
        Session::Set('error', 'Email地址为无效地址');
        redirect(WEB_ROOT . '/manage/account_detail.php?id='.$id);
    }
    if ($_POST['password2'] == $_POST['password'] && $_POST['password']) {
        if ( OUser::Modify($id, $user) ) {
            Session::Set('success', '修改账户设置成功');
            redirect(WEB_ROOT . '/manage/account_detail.php?id='.$id);
        } else {
            Session::Set('error', '修改账户设置失败');
            redirect(WEB_ROOT . '/manage/account_detail.php?id='.$id);
        }
    }else{
        Session::Set('error', '密码输入错误！');
        redirect(WEB_ROOT . '/manage/account_detail.php?id='.$id);
    }
}



include template('manage_account_detail');
?>
