<?PHP
// +----------------------------------------------------------------------
// | ThinkGroupon
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.thinkgroupon.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Jeffxie <jeffxie@gmail.com>
// +----------------------------------------------------------------------
class IndexAction extends Action{
    public function index(){
        $this->checkloginstatus();
        $Users = M('Users');
        $username = $_SESSION['username'];
        if(preg_match('/^(.*)\@(.*)\.com|cn|net$/',$username))
        {
            $userinfo = $Users->where("email='$username'")->find();
        }
        else{
            $userinfo = $Users->where("username='$username'")->find();
        }
        $this->assign("userinfo",$userinfo);
        $this->display();
    }

    public function login(){
        if($_SESSION['uid'])
        {
            $this->assign('jumpUrl',__DOMAIN__.'/apps/vip/index.php');
            $this->success('您已经登录,请勿重复登陆');
        }
        $email = $_POST['email'];
        $password = $_POST['password'];
        $Users = M('Users');
        //以下作法，是为了判断EMAIL登录还是username登录
        if(preg_match('/^(.*)\@(.*)\.com|cn|net$/',$email))
        {
            $data = $Users->where("email='$email' AND password='$password'")->find();
        }
        else{
            $data = $Users->where("username='$email' AND password='$password'")->find();
        }
        if($data)
        {
            $_SESSION['username'] = $email;
            $_SESSION['uid'] = $data['id'];
            if($_SESSION['backurl'] == 'buy'){
                $shopid = $_SESSION['shopid'];
                $_SESSION['backurl'] = '';//清理
                $this->assign("jumpUrl",__DOMAIN__."/apps/team/index.php/index/buy/id/$shopid");
                $this->success("登录成功");
            }
            else{
                $this->assign('jumpUrl',__DOMAIN__.'/index.php');
                $this->success("登录成功");
            }
        }


        $this->display();
    }

    public function signup(){
        if($_POST['email'] && $_POST['password'])
        {
            $Form = M('Users');
            if($Form->create())
            {
                $Form->city = $_POST['city_id'];
                import('ORG.Net.IpLocation');
                Load('extend');
                $ip = get_client_ip();
                $Form->ip = ip2long($ip); //省略时表示查询客户端IP.$location可以直接输出使用，键名如上。
                if($Form->add())
                {
                    $this->success("添加成功");
                }
                else{
                    $this->error("数据失败");
                }
            }
            else{
                echo "创建form 失败";
            }
        }
        $this->display();
    }
    public function logout()
    {
        $_SESSION['username'] = '';
        $_SESSION['uid'] = '';
        $this->assign('jumpUrl',__DOMAIN__.'/index.php');
        $this->success("退出成功");
    }

    public function update_profile() {
        $this->checkloginstatus();
        $Users = M('Users');
        $username = $_SESSION['username'];
        if(preg_match('/^(.*)\@(.*)\.com|cn|net$/',$username))
        {
            $userinfo = $Users->where("email='$username'")->find();
        }
        else{
            $userinfo = $Users->where("username='$username'")->find();
        }
        //写数据
        if($_POST['email'] && $_POST['password'])
        {
            $data['id'] = $userinfo['id'];
            $data['email'] = $_POST['email'];
            $data['password'] = $_POST['password'];
            if($Users->save($data))
            {
                $this->success("更新成功");
            }
            else{
                $this->error("您未做更新");
            }
        }
        $this->assign('userinfo',$userinfo);
        $this->display();
    }
    public function team() {
        $grouponinfo = M('Grouponinfo');
        $data = $grouponinfo->where('1=1')->order('id desc')->findAll();
        for($i=0;$i<count($data);$i++)
        {
                    preg_match('/src=\"(.+?)\"/',$data[$i]['content'],$arr);
                    $data[$i]['img'] = $arr[1];
                    $data[$i]['now_price'] = $data[$i]['price']*($data[$i]['rebate']*0.1);
                    $data[$i]['small_price'] = $data[$i]['price']-$data[$i]['price']*($data[$i]['rebate']*0.1);
        }
        $this->assign('data',$data);
        $this->assign('css',__DOMAIN__.'/public/images/team.css');
        $this->display();
    }
    //我的订单
    public function shoplist() {
        $this->checkloginstatus();
        $shoplist = M('Shop');
        $users_id = $_SESSION['uid'];
        $myshoplist = $shoplist->where("users_id=$users_id")->order("id desc")->findAll();
        $this->assign('myshoplist',$myshoplist);
        $this->display();
    }
    //账户信息
    public function money() {
        $this->checkloginstatus();
        //用户相关信息
        $users = D('Users');
        $uid = $_SESSION['uid'];
        $userinfo = $users->relation(true)->where("id=$uid")->find();
        $money = 0; //已消费
        $money_add = 0; //充值金额
        for($i=0;$i<count($userinfo['Money']);$i++)
        {
            if($userinfo['Money'][$i]['money_type'] == 2)
            {
                $money = $money+$userinfo['Money'][$i]['money'];
            }
            else{
                $money_add = $money_add+$userinfo['Money'][$i]['money'];
            }
        }
        $money_now = $money_add-$money;//账户目前余额
        $this->assign('money_now',$money_now);
        $this->assign('userinfo',$userinfo);
        $this->display();
    }
    //页面授权验证
    public function checkloginstatus() {
        if(!$_SESSION['username'])
        {
            $this->assign("jumpUrl","login");
            $this->success("对不起，您未登录!");
        }
    }
}
?>