<?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>
// +----------------------------------------------------------------------
// $Id$
class IndexAction extends Action{
    public function index(){
        $this->checkloginstatus();
        $info = file_get_contents("http://www.thinkgroupon.com/info.php"); //官方消息
        if($_SESSION['admininfo'])
        {
            $this->assign('info',$info);
            $this->display();
        }
        else{
            $this->error("请正确登录");
        }

    }
    public function left(){
        $this->display();
    }
    public function main_index(){
        $this->display();
    }
    public function login(){
        if($_POST['username'] && $_POST['password'])
        {
            $admin = M('Admin');
            $username = $_POST['username'];
            $password = $_POST['password'];
            $admininfo = $admin->where("username='$username' AND password='$password'")->find();
            if($admininfo)
            {
                $_SESSION['admininfo'] = $admininfo['username'];
                $this->assign('jumpUrl',__DOMAIN__.'/apps/admin/index.php');
                $this->success("登录成功");
            }
            else{
                $this->error("登录失败，请检查用户名或密码");
            }
        }
        $this->display();
    }
    public function logout() {
        $_SESSION['admininfo'] = '';
        $this->assign('jumpUrl',__DOMAIN__.'/apps/admin/index.php/index/login');
        $this->success("成功退出");
    }
    public function admin_list() {
        $this->checkloginstatus();
        $admin = M('Admin');
        $admininfo = $admin->order('id desc')->findAll();
        $this->assign('admininfo',$admininfo);
        $this->display();
    }
    public function admin_add() {
        if($_POST['username'] && $_POST['password'])
        {
            $admin = M('Admin');
            if($admin->create())
            {
                if($admin->add()){
                    $this->assign('jumpUrl','admin_list');
                    $this->success('添加成功');
                }
                else{
                    $this->error('添加失败');
                }
            }
        }
        $this->display();
    }

    public function delete() {
        $this->checkloginstatus();
        $admin = M('Admin');
        $id = $_GET['id'];
        if($admin->delete($id))
        {
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }
    }

    public function editinfo() {
        $this->checkloginstatus();
        $admin = M('Admin');
        $id = $_GET['id'];
        $admininfo = $admin->where("id=$id")->find();
        if($_POST['password'])
        {
            $admin->id = $id;
            $admin->password = $_POST['password'];
            if($admin->save())
            {
                $this->assign('jumpUrl','../../admin_list');
                $this->success('修改成功');
            }
            else{
                $this->error('修改失败');
            }
        }else{
            $this->assign('admininfo',$admininfo);
            $this->display('admin_add');
        }
    }


    //关联表结构
    public function groupon_list() {
        $this->checkloginstatus();
        import("ORG.Util.Page");
        $grouponinfo = D('Grouponinfo');
        $count = $grouponinfo->count();//查询总记录数字
        $Page = new Page($count,20); //传参，总记录数，每页显示记录数
        $data = $grouponinfo->relation(true)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show(); //分页输出
        $this->assign('show',$show);
        $this->assign('data',$data);
        $this->display();
    }


    public function groupon_del() {
        $this->checkloginstatus();
        $grouponinfo = D('Grouponinfo');
        $id = $_GET['id'];
        if($grouponinfo->relation(true)->delete($id))
        {
            $this->success('成功删除');
        }
        else{
            $this->error('删除失败');
        }
    }
    //读取团购编辑信息
    public function groupon_edit() {
        $this->checkloginstatus();
        $grouponclass = M('Grouponclass');
        $grouponinfo = D('Grouponinfo');
        $id = $_GET['id'];
        $grouponclass_info = $grouponclass->findAll();
        $data = $grouponinfo->relation(true)->find($id);
//更新
        $grouponinfo->id = $id;
        $grouponinfo->grouponclass_id = $_POST['classname'];
        $grouponinfo->title = $_POST['title'];
        $grouponinfo->content = stripslashes($_POST['content']);
        $grouponinfo->count_person = $_POST['count_person'];
        $grouponinfo->end_time = strtotime($_POST['end_time']);
        $grouponinfo->Contact = array(
            'shopname'=>$_POST['shopname'],
            'contact'=>$_POST['contact'],
            'phone'=>$_POST['phone'],
            'qq'=>$_POST['qq'],
            'msn'=>$_POST['msn'],
            'email'=>$_POST['email'],
            'address'=>$_POST['address'],
            'website'=>$_POST['website'],
            );

        if($grouponinfo->relation(true)->save())
        {
            $this->assign('jumpUrl','../../groupon_list');
            $this->success('更新成功');
        }
//更新操作结束
        $this->assign('grouponclass_info',$grouponclass_info);
        $this->assign('data',$data);
        $this->display('groupon_add');
    }

    public function groupon_add() {
        $this->checkloginstatus();
        $grouponclass = M('Grouponclass');
        $grouponinfo = D('Grouponinfo');
        $grouponclass_info = $grouponclass->findAll();
        if($_POST['title'] && $grouponinfo->create())
        {
            $grouponinfo->content = stripslashes($grouponinfo->content);
            $grouponinfo->end_time = strtotime($_POST['end_time']);
            $grouponinfo->Contact = array(
                'shopname'=>$_POST['shopname'],
                'contact'=>$_POST['contact'],
                'phone'=>$_POST['phone'],
                'qq'=>$_POST['qq'],
                'msn'=>$_POST['msn'],
                'email'=>$_POST['email'],
                'address'=>$_POST['address'],
                'website'=>$_POST['website'],
                );
            if($grouponinfo->relation(true)->add()){
                $this->assign('jumpUrl','groupon_list');
                $this->success('添加团购成功');
            }
            else{
                $this->error('添加团购失败');
            }
        }
        $this->assign('grouponclass_info',$grouponclass_info);
        $this->display();
    }
    //团购分类列表
    public function grouponclass_list()
    {
        $grouponclass_list = M("Grouponclass");
        $grouponclass_info_list = $grouponclass_list->order("id desc")->findAll();
        $this->assign('grouponclass_info_list',$grouponclass_info_list);
        $this->display();
    }
    //添加团购分类
    public function grouponclass_add() {
        $grouponclass_list = M("Grouponclass");
        if($_POST && $grouponclass_list->create())
        {
            if($grouponclass_list->add())
            {
                $this->assign("jumpUrl","grouponclass_list");
                $this->success("添加分类成功 ");
            }
        }
        $this->display();
    }
    //编辑团购信息
    public function grouponclass_edit() {
        $grouponclassinfo = M("Grouponclass");
        $id = $_GET['id'];
        $grouponclass = $grouponclassinfo->where("id=$id")->find();
        //写入数据
        if($_POST)
        {
            $grouponclassinfo->id = $id;
            $grouponclassinfo->classname = $_POST['classname'];
            if($grouponclassinfo->save())
            {
                $this->assign("jumpUrl","../../grouponclass_list");
                $this->success("修改分类成功");
            }
        }
        $this->assign("grouponclass",$grouponclass);
        $this->display("grouponclass_add");
    }
    //删除团购分类
    public function grouponclass_del() {
        $this->checkloginstatus();
        $grouponinfo = D('Grouponclass');
        $id = $_GET['id'];
        if($grouponinfo->delete($id))
        {
            $this->success('成功删除');
        }
        else{
            $this->error('删除失败');
        }
    }
    //seo信息
    public function seo_list() {
        $this->checkloginstatus();
        Load('extend');
        $seo = M('Seo');
        $seoinfo = $seo->order('id desc')->findAll();
        $this->assign('seoinfo',$seoinfo);
        $this->display();
    }
    //del seo
    public function seo_del() {
        $this->checkloginstatus();
        $seo = M('Seo');
        $id = $_GET['id'];
        if($seo->delete($id))
        {
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }
    }
    //add seo
    public function seo_add() {
        $this->checkloginstatus();
        $seo = M('Seo');
        if($_POST && $seo->create())
        {
            if($seo->add())
            {
                $this->assign('jumpUrl','seo_list');
                $this->success('添加成功');
            }
            else{
                $this->error('添加失败');
            }
        }
        $this->display();
    }
    //edit seo
    public function seo_edit() {
        $this->checkloginstatus();
        $seo = M('Seo');
        $id = $_GET['id'];
        $seoinfo = $seo->where("id=$id")->find();
        if($_POST)
        {
            $seo->id = $id;
            $seo->seo_title = $_POST['seo_title'];
            $seo->seo_keywords = $_POST['seo_keywords'];
            $seo->seo_description = $_POST['seo_description'];
            if($seo->save())
            {
                $this->assign('jumpUrl','../../seo_list');
                $this->success('编辑成功');
            }
            else{
                $this->error('编辑失败');
            }
        }
        $this->assign('seoinfo',$seoinfo);
        $this->display('seo_add');
    }

    //会员列表
    public function vip_list() {
        $this->checkloginstatus();
        $users = D('Users');
        $list = $users->relation(true)->order('id desc')->findAll();

        //循环记录
        for($i=0;$i<count($list);$i++)
        {
            $money = 0;
            $money_del = 0;
            //循环记录中的money
            for($j=0;$j<count($list[$i]['Money']);$j++)
            {
                if($list[$i]['Money'][$j]['money_type'] != 2)
                    $money = $money+$list[$i]['Money'][$j]['money'];
                else
                    $money_del = $money_del+$list[$i]['Money'][$j]['money'];
            }
            $list[$i]['money'] = $money-$money_del; //总数减去已经消费的

        }
        $this->assign('list',$list);
        $this->display();
    }
    //删除会员
    public function users_del() {
        $this->checkloginstatus();
        $users = M('Users');
        $id = $_GET['id'];
        if($users->delete($id))
        {
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }
    }
    //后台充值
    public function money_add() {
        $this->checkloginstatus();
        $users = M('Users');
        $money = M('Money');
        $username = $_POST['username'];
        $userinfo = $users->where("username='$username'")->find();
        if($userinfo && $_POST)
        {
            $data['users_id'] = $userinfo['id'];
            $data['money'] = $_POST['money'];
            $data['money_type'] = 1;
            $data['pubtime'] = time();
            if($money->add($data))
            {

                $this->success('充值成功');
            }
            else{
                $this->error('用户不存在');
            }
        }
        else if($_POST && !$userinfo){
            $this->error('用户不存在');
        }

        $this->display();
    }
    //网站设置
    public function settings() {
        $this->checkloginstatus();
        $setting = M('Setting');
        $siteInfo = $setting->find();
        if($_POST['action'] == 'ok')
        {
            $setting->id = $_POST['id'];
            $setting->site_name = $_POST['site_name'];
            $setting->domain = $_POST['domain'];
            $setting->create_time = strtotime($_POST['create_time']);
            $setting->email = $_POST['email'];
            $setting->logo = $_POST['logo'];
            if($setting->save())
            {
                $this->success('设置完成');
            }
            else{
                $this->error('设置失败');
            }
        }
        $this->assign('siteInfo',$siteInfo);
        $this->display();
    }

    //官网帮助
    public function helpview() {
        echo "如需官方帮助，请登录www.thinkgroupon.com";
    }

    //网站信息编辑
    public function website_edit(){
        $this->checkloginstatus();
        require_once(APP_PATH . '/common/Common.inc.php');
        $common = new Common();
        $classnamelist = $common->classname;
        $classname = $_GET['classname'];
        $website_edit = M('Common');
        if(!empty($_POST['id']))
        {
            $website_edit->id = $_POST['id'];
            $website_edit->content = $_POST['content'];
            if($website_edit->save())
            {
                $this->success('更新成功');
            }
            else{
                $this->error('更新失败');
            }
        }
        $data = $website_edit->where("classname='$classname'")->find();
        $data['classname'] = $classnamelist[$data['classname']];
        $this->assign('data',$data);
        $this->display();
    }

    //商家信息
    public function shoplist() {
        $this->checkloginstatus();
        $seller = M('Seller');
        $sellerinfo = $seller->order('id desc')->findAll();
        $this->assign('sellerinfo',$sellerinfo);
        $this->display();
    }

    //删除商家信息
    public function delseller() {
        $this->checkloginstatus();
        $id = $_GET['id'];
        $seller = M('Seller');
        if($seller->delete($id))
        {
            $this->success('删除成功');
        }
    }
    //验证页面登录状态
    public function checkloginstatus() {
        if(!$_SESSION['admininfo'])
        {
            $this->assign("jumpUrl",__DOMAIN__.'/apps/admin/index.php/index/login');
            $this->success("对不起，您未登录");
        }
    }
}
?>