<?php
/**
 * 管理员管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class AdminController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $map=array();
        $map['status']=1;
        $shop_info=M("Shops")->where($map)->field("id,name")->select();
        $shop_info=array_filter($shop_info);
        $info=array();
        foreach($shop_info as $key=>$val){
            $info[$val['id']]=$val['name'];
        }
        $this->assign("shop_info",$info);
    }
   public function add()
    {
        if (IS_POST) {
            $admin_mod = M('Admin');
            if (!isset($_POST['username']) || ($_POST['username'] == '')) {
                $this->error('用户名不能为空');
            }
            if ($_POST['password'] != $_POST['repassword']) {
                $this->error('两次输入的密码不相同');
            }
            $result = $admin_mod->where("username='" . $_POST['username'] . "'")->count();
            if ($result) {
                $this->error('管理员' . $_POST['username'] . '已经存在');
            }
            unset($_POST['repassword']);
            $_POST['salt'] = randStr();
            $_POST['password'] = creatpwd($_POST['password'], $_POST['salt']);
            $_POST['reg_time'] = $_POST['last_login_time'] = NOW_TIME;
            $_POST['reg_ip'] = $_POST['last_login_ip'] = get_client_ip(1);
            $admin_mod->create();
            $result = $admin_mod->add();
            if ($result) {
                    // D('AuthGroupAccess')->setAccess(array(0=>2), $result);
                if ($_POST['group']) D('AuthGroupAccess')->setAccess($_POST['group'], $result);//jxr start 2019.02.15
                $this->success(L('DO_OK'), '', '', 'add');
            } else {
                $this->error(L('DO_ERROR'));
            }
        } else {
            $this->getGroup(1);
            $this->display('edit');
        }
    }
    function edit()
    {
        $id = I('id');
        $admin_mod = D('Admin');
        if (IS_POST) {
            $count = $admin_mod->where("id!=" . $id . " and username='" . $_POST['username'] ."'")->count();
            if ($count > 0) {
                $this->error('用户名已经存在！');
            }
            if ($_POST['password']) {
                if ($_POST['password'] != $_POST['repassword']) {
                    $this->error('两次输入的密码不相同');
                }
                $adminsalt = $admin_mod->where("username='" . $_POST['username'] . "'")->
                    getField('salt');
                $_POST['password'] = creatpwd($_POST['password'], $adminsalt);
            } else {
                unset($_POST['password']);
            }
            unset($_POST['repassword']);
            if (false === $admin_mod->create()) {
                $this->error($admin_mod->getError());
            }
            $result = $admin_mod->save();
            if (false !== $result) {
                    // D('AuthGroupAccess')->setAccess(array(0=>2), $id);
                if ($_POST['group']) D('AuthGroupAccess')->setAccess($_POST['group'], $result);//jxr start 2019.02.15
                $this->success(L('DO_OK'), '', '', 'edit');
            } else {
                $this->error(L('DO_ERROR'));
            }
        } else {
            if (isset($id)) {
                $id = isset($id) && intval($id) ? intval($id) : $this->error('参数错误');
            }
            $admininfo = $admin_mod->where('id=' . $id)->field(true)->find();
            $this->assign($admininfo);
            $this->assign('group', M('AuthGroupAccess')->where("uid=$id")->getField('group_id', true));
            $this->getGroup(1);
            $this->display();
        }
    }
    function delete()
    {
        $id = I('id');
        if ((!isset($id) || empty($id)) && (!isset($id) || empty($id))) {
            $this->error('请选择要删除的会员！');
        }
        if (isset($id) && ((is_array($id) && in_array('1',$id)) || $id==1 )) {
            $this->error('不能删除管理员！');
        }
        $admin_mod = D('admin');
        if (isset($id) && is_array($id)) {
            $ids = implode(',', $id);
            $admin_mod->delete($ids);
        } else {
            $id = intval($id);
            $admin_mod->delete($id);
        }
        $this->success(L('DO_OK'));
    }
    public function reset(){//修改密码
        $admin_mod = D('Admin');
        if (IS_POST) {
            $postdata=I('post.');
            if(empty($postdata['password']) || empty($postdata['oldpassword']))$this->error('您没有输入密码');
            if ($postdata['password'] != $postdata['repassword']) {$this->error('两次输入的密码不相同');}
            $adminsalt = $admin_mod->where("id='" .UID . "'")->field('salt,password')->find();
            $oldpassword=creatpwd($postdata['oldpassword'], $adminsalt['salt']);
            if($oldpassword!=$adminsalt['password'])$this->error('旧密码错误!');
            $savedata['id']=UID;
            $savedata['password'] = creatpwd($postdata['password'], $adminsalt['salt']);
            $result = $admin_mod->save($savedata);
            if (false !== $result) {
                $this->success(L('DO_OK'));
            } else {
                $this->error(L('DO_ERROR'));
            }
        } else {
            $this->display();
        }
    }
    public function ajaxCheckUsername()
    {
        $id = I('id');
        $username = $_GET['username'];
        $id = isset($id) && intval($id) ? intval($id) : '';
        $where = "username='$username'";
        $id = D('Admin')->where($where)->getField('id');
        if (!$id) {
            //不存在
            echo '1';
        } else {
            //存在
            echo '0';
        }
        exit;
    }
    public function ajax_check_used()
    {
        $id = I('id');
        $admin_mod = D('admin');
        $count = $admin_mod->where("id!=".$id." and username='".$_get['username']."'")->count();
        if ($count > 0) {
            echo "0";
        } else {
            echo "1";
        }
    }
    /* 得到所属组 */
    private function getGroup($type = '')
    {
        $groupData = M('AuthGroup')->where('status=1')->field(true)->select();
        if ($type == 1) {
            foreach ($groupData as $v) {
                $newgroupData[$v['id']] = $v['title'];
            }
            $groupData = $newgroupData;
        }
        $this->assign('groupData', $groupData);
    }
}
?>