<?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(){
        header("Content-Type:text/html; charset=utf-8");
        echo "<div style='font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;font-size:14px;font-family:Tahoma'>^_^ Hello,欢迎使用<span style='font-weight:bold;color:red'>ThinkPHP</span></div>";
    }

    public function buy() {
        ob_start();
        if(!$_SESSION['uid'])
        {
            $_SESSION['backurl'] = 'buy';
            $_SESSION['shopid'] = intval($_GET['id']);
            $this->assign('jumpUrl',__DOMAIN__.'/apps/vip/index.php/index/login');
            $this->success("请您登录");
        }
        $id = intval($_GET['id']); //GET和SESSION冲突
        $grouponinfo = D('Grouponinfo');
         //团购产品信息
        $data = $grouponinfo->relation(true)->where("id=$id")->find();
        //用户相关信息
        $users = D('Users');
        $uid = $_SESSION['uid'];
        $userinfo = $users->relation(true)->where("id=$uid")->find();
       // print_r($userinfo);die;
        //循环记录
            $money = 0;
            $money_del = 0;
            //循环记录中的money
            for($i=0;$i<count($userinfo['Money']);$i++)
            {
                if($userinfo['Money'][$i]['money_type'] != 2)
                    $money = $money+$userinfo['Money'][$i]['money'];
                else
                    $money_del = $money_del+$userinfo['Money'][$i]['money'];
            }
        $data['remainder_money'] = $money-$money_del; //总数减去已经消费的
        $data['title'] = $data['title'];
        $data['price'] = $data['price']*($data['rebate']*0.1);
        $data['users_id'] = $uid;
        $data['back_url'] = $_SERVER['PHP_SELF'];
        $_SESSION['shop'] = $data;
        $this->assign('data',$data);
        //跳转到checkput
        $this->display();
        ob_end_flush();
    }

    //检测并付款
    public function checkput() {
        ob_start();
        if(!$_SESSION['uid'])
        {
            $this->assign('jumpUrl',__DOMAIN__.'/apps/vip/index.php/index/login');
            $this->success("请您登录");
        }
        $remark = $_POST['remark'];
        $data = $_SESSION['shop'];
        $data['remark'] = $remark;
        $data['shop_price'] = $data['price']-$data['remainder_money']; //余额不足情况下应付金额，产品打折后的总价减去账户剩余金额
        $_SESSION['shop'] = $data;
        //print_r($data);
        $this->assign('data',$data);
        $this->display();
        ob_end_flush();
    }

    //跳转到付款界面
    public function pay() {
        $shop = M('Shop');
        $money = M('Money');
        $data = $_SESSION['shop'];
        $money->users_id = $data['users_id'];
        $money->money = $data['price'];
        $money->money_type = 2;
        $money->pubtime = time();
        $shop->users_id = $data['users_id'];
        $shop->grouponinfo_id = $data['id'];
        $shop->shop_name = $data['title'];
        $shop->count = 1;
        $shop->price = $data['price'];
        $shop->remark = $data['remark'];
        $shop->pubtime = time();

        if($shop->add())
        {
             if($money->add())
            {
                $this->success('支付成功！');
            }
            else{
                $this->error('写入money信息出错');
            }
        }
        else{
            $this->error('写入shop信息出错!');
        }
    }
    public function seller() {
        $this->display();
    }
    //提交团购信息
    public function post_seller() {
        $seller = M('Seller');
        if($seller->create())
        {
            $seller->pubtime = time();
            if($seller->add())
            {
                $this->success('提交成功');
            }
        }
    }
}
?>