<?php

/**
 * 后台管理员模块 Controller
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhaojianjun $  <zwttmac@qq.com>
 * $Id: manage.php 133 2012-01-16 03:21:12Z zhaojianjun $
 * http://un.zhuna.cn；
 */
class manageAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('manage');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('zhuna_admin');
    }

    /**
     * 管理员类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有管理员
        $rs = $this->tmp_model->getManageList();
        $this->assign($rs);
        $this->V('manage');
    }

    /**
     * 新增/修改管理员
     */
    public function add()
    {
        $id = !empty($_GET['id']) ? (int) $_GET['id'] : 0;
        $editinfo = array();
        $editinfo['id'] = '';
        $editinfo['rank'] = '';
        $editinfo['username'] = '';
        $editinfo['name'] = '';

        if ($id > 0) {
            $editinfo = $this->tmp_model->getManageInfo($id);
        }

        //取管理员类型option数组
        $admintype_ary = $this->tmp_model->getAdminType();
        $admin_type_option = Utility::Option($admintype_ary, $editinfo['rank'], 'rank,typename');

        $this->assign('editinfo', $editinfo);
        $this->assign('admin_type_option', $admin_type_option);
        $this->V('manage');
    }

    /**
     * 新增/修改管理员页面　保存　
     */
    public function save()
    {
        global $cfg_mid;
        if (empty($_POST) || count($_POST) < 3) {
            $this->redirect('don\'t be evil');
            exit;
        }
        
        $configcache = Tmac::config('configcache.config', APP_NAME, '.inc.php');
        $cfg_mid = $configcache['cfg_mid'];

        /* 初始化变量 */
        $id = !empty($_POST['id']) ? (int) $_POST['id'] : 0;
        $username = !empty($_POST['username']) ? trim($this->H($_POST['username'])) : '';
        $name = !empty($_POST['name']) ? trim($this->H($_POST['name'])) : '';
        $password = !empty($_POST['password']) ? md5($_POST['password'].'^'.$cfg_mid) : '';
        $rank = !empty($_POST['rank']) ? $_POST['rank'] : 0;

        /* 验证不能为空 */
        if (empty($username)) {
            $this->redirect('管理员登录不能为空');
            exit;
        }
        if (empty($name)) {
            $this->redirect('管理员真实姓名不能为空');
            exit;
        }
        if (!isset($rank)) {
            $this->redirect('选择管理员类型!');
            exit;
        }
        // TODO goon to verify
        //验证id和admin不能重复
        $manageinfo = $this->tmp_model->checkManageName($username, $id);

        $info = array(
            'username' => $username,
            'name' => $name,
            'password' => $password,
            'rank' => $rank,
            'loginip' => Functions::get_client_ip(),
            'logintime' => time()
        );

        if ($id > 0) {
            if (empty($password)) { //如果修改时密码为空 就不用改
                unset($info['password']);
            }
            $manage_page = HttpResponse::getCookie('manage_page');
            //update save article
            $where = 'id = ' . $id . '';
            $rs = $this->tmp_model->addManage($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改管理员成功', PHP_SELF.'?m=manage&page= ' . $manage_page . '');
            } else {
                $this->redirect('修改管理员失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addManage($info);
            if ($rs) {
                $this->redirect('添加管理员成功', PHP_SELF.'?m=manage');
            } else {
                $this->redirect('添加管理员失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = (int) $_GET['id'];
        $id_a = $_POST['id_a'];
        if (is_array($id_a)) {
            if (in_array(1, $id_a)) {
                $this->redirect('系统管理员不能删除!');
                exit;
            }
            $id = implode(',', $id_a);
        } elseif (!empty($do_id)) {
            $id = $do_id;
            if ($id == 1) {
                $this->redirect('系统管理员不能删除!');
                exit;
            }
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }

        $rs = $this->tmp_model->delmanage($id);
        // TODO DEL该分类下的所有管理员
        if ($rs) {
            $manage_page = HttpResponse::getCookie('manage_page');
            $this->redirect('删除管理员成功', PHP_SELF.'?m=manage&page=' . $manage_page . '');
        } else {
            $this->redirect('删除管理员失败，请重试！');
        }
    }

}