<?php

class adminMod extends commonMod
{

    // 管理员首页
    public function index()
    {
        $sql = "SELECT A.*,B.name as gname FROM {$this->model->pre}admin A LEFT JOIN {$this->model->pre}admin_group B ON A.gid = B.id ORDER BY A.id ASC";
        $list = $this->model->query($sql);
        $this->assign('list', $list);
        $this->display();
    }

    // 管理员添加
    public function add()
    {
        if (empty($_POST['psot_do'])) {
            $list = $this->model->table('admin_group')->select();
            $this->assign('list', $list);
            $this->display();
            return;
        }
        $data = array();
        $data['user'] = in($_POST['user']);
        $data['name'] = in($_POST['name']);
        $data['gid'] = intval($_POST['gid']);
        $data['status'] = intval($_POST['status']);

        if (empty($data['gid'])) {
            $this->jserror('管理组未选择！');
            return;
        }

        if (empty($data['user'])) {
            $this->jserror('帐号不能为空！');
            return;
        }

        if (empty($data['name'])) {
            $this->jserror('用户名不能为空！');
            return;
        }

        $condition['user'] = $data['user'];
        $user_info = $this->model->table('admin')->where($condition)->find();

        if ($user_info['user'] == $data['user']) {
            $this->jserror('帐号不能重复！');
            return;
        }

        $condition2['name'] = $data['name'];
        $name_info = $this->model->table('admin')->where($condition2)->find();

        if ($name_info['name'] == $data['name']) {
            $this->jserror('用户名不能重复！');
            return;
        }

        if (empty($_POST['password'])) {
            $this->jserror('密码不能为空！');
            return;
        }

        if ($_POST['password'] == $_POST['password2']) {
            $data['password'] = md5($_POST['password']);
        } else {
            $this->jserror('两次输入密码错误！');
            return;
        }

        // 数据库操作
        $this->model->table('admin')->data($data)->insert();
        $this->jssuccess('管理员添加成功！');
    }

    // 管理员修改
    public function edit()
    {
        if (empty($_POST['psot_do'])) {
            $id = intval($_GET[0]);
            if (empty($id)) {
                $this->error('参数传递错误！');
                return;
            }
            $condition['id'] = $id;
            $info = $this->model->table('admin')->where($condition)->find(); //获取当前用户信息
            if (empty($info)) {
                $this->error('该帐号不存在！');
                return;
            }
            $list = $this->model->table('admin_group')->select();
            $this->assign('list', $list);
            $this->assign('info', $info);
            $this->display();
            return;
        }
        $data = array();
        $data['user'] = in($_POST['user']);
        $data['name'] = in($_POST['name']);
        $data['gid'] = intval($_POST['gid']);
        $data['status'] = intval($_POST['status']);

        $condition3 = 'user="'.$data['user'].'" AND id<>'.intval($_POST['id']);
        $info = $this->model->table('admin')->where($condition3)->find();

        if (!empty($info)) {
            $this->jserror('用户名不能重复！');
        }

        if (empty($_POST['id'])) {
            $this->jserror('管理员ID错误！');
            return;
        }
        if (empty($data['gid'])) {
            $this->jserror('管理组未选择！');
            return;
        }
        if (empty($data['name'])) {
            $this->jserror('用户名不能为空！');
            return;
        }

        if (!empty($_POST['password'])) {
            $data['password'] = md5($_POST['password']);
            if ($_POST['password'] <> $_POST['password2']) {
                $this->jserror('两次密码输入错误！');
                return;
            }
        }

        // 数据库操作
        $condition = array();
        $condition['id'] = intval($_POST['id']);
        $this->model->table('admin')->data($data)->where($condition)->update();
        $this->jssuccess('管理员修改成功！');
    }

    // 删除管理员
    public function del()
    {
        $id = intval($_GET[0]);
        if (empty($id)) {
            $this->jserror('ID传输错误！');
            return;
        }

        if ($id == 1) {
            $this->jserror('帐号保护无法删除！');
            return;
        }
        $condition['id'] = $id;
        $this->model->table('admin')->where($condition)->delete();        
        $this->jssuccess('管理员删除成功！');
    }

}

?>