<?php

/*******************************************************************\
|                                                                   |
|   房佳（新房报备ERP管理系统）                                     |
|                                                                   |
|   © 2020-2023 http://www.kaidandashi.com/ All rights reserved.    |
|                                                                   |
|   本系统由淮南市银泰软件科技有限公司提供技术支持                  |
|                                                                   |
|   QQ号：984784483                                                 |
|                                                                   |
\*******************************************************************/

namespace Admin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        $role_id=M('role_user')->where(array('user_id'=>session('uid')))->getField('role_id');
        $sql = "select a.* from  
            (select * from __MENU__ where status=1 ) as a , 
            (select * from __ACCESS__ where role_id = '".$role_id."') as b where a.node_id=b.node_id  ORDER BY a.listorder ASC";
        $Model = new \Think\Model();
        $smenu=$Model->query($sql);
        $pmenu=M('Menu')->where(array('type'=>array('neq',2),'status'=>1))->select();
        $menu=array_merge_recursive($smenu,$pmenu);
        $menu=array_sort($menu,'listorder','asc');
        //p($menu);die;
        if(session('username') == C('RBAC_SUPERADMIN')){
            $result=M('menu')->where(array('status'=>1))->order('listorder asc')->select();
        }else{
            $result=$menu;
        }
        foreach ($result as $n=> $r) {
            if ($r['type']==2) {
                $name = ucwords($r['app']);
                $model = ucwords($r['model']);
                $action = $r['action'];
                //附带参数
                $params = "";
                if ($r['data']) {
                    $params = "?" . htmlspecialchars_decode($r['data']);
                }
                $result[$n]['url'] = U("{$name}/{$model}/{$action}{$params}");
            }elseif ($r['type']==3) {
                if ($r['blank']) {
                    $params =  htmlspecialchars_decode($r['blank']);
                }
                $result[$n]['url'] = $params;
            }
        }
        $tree = new \Org\Util\Tree();
        $tree->init($result);
        $categorys = $tree->get_tree_array(0);
        foreach ($categorys as $key => $value) {
            foreach ($value['child'] as $key1 => $value1) {
                if ($value1['child']==null) {
                    unset($categorys[$key]['child'][$key1]);
                }
            }
        }
        foreach ($categorys as $key => $value) {
            if ($value['child']==null) {
                unset($categorys[$key]);
            }
        }
        $this->categorys = $categorys;
        $this->user = M('yonghu')->where('id='.session('uid'))->find();
        $this->display('index1');
    }
    public function xiaoxi(){
        $Model = new \Think\Model();
        $is_rbac = _rbac_node_show(990);
        if ($is_rbac) {
            $condition = "a.gongsiid = '".session('gongsiid')."' and e.gongsiid = '".session('gongsiid')."' and a.status = 3 and a.type = 1";
            $x = _rbac_lists_where(990,0);
            if ($x && $x != "all" && $x != "me") {
                $condition .= " and e.bumen in ({$x})";
            } elseif ($x == "me") {
                $condition .= " and e.id = '".session('uid')."'";
            } elseif ($x == false) {
                $condition .= " and e.id = 0";
            }
            $sh_count = M('shenhe')->alias('a')->join('jjrxt_yonghu e on a.adduid = e.id')->where($condition)->count();
            $arr['sh_count'] = $sh_count;
        }
        $data = array('data'=>$arr);
        header("Content-type: application/json");
        exit(json_encode($data));
    }
    public function validatepass(){
        if (trim(I('passwd'))=='') {
            $this->error('密码不能为空');
        }
        $passwd = md5(trim(I('passwd')));
        $uid = session('uid');
        $r = M('Yonghu')->where(array('id'=>$uid,'mima'=>$passwd))->getField('id');
        //返回查询结果到异步json
        $data=array('r'=>$r);
        header("Content-type: application/json");
        exit(json_encode($data)); 
    }
    public function updatepass(){
        if (IS_POST) {
            $ymima = trim(I('ymima'));
            $xmima = trim(I('xmima'));
            $qrmima = trim(I('qrmima'));
            if ($ymima == '' || $xmima == '') {
                $this->error('密码不能为空！');
            }
            $yymima = M('yonghu')->where(array('id'=>session('uid')))->getField('mima');
            if ($yymima != md5($ymima)) {
                $this->error('原密码错误!');
            }
            if ($xmima != $qrmima) {
                $this->error('两次密码不匹配!');
            }
            if ($ymima == $xmima) {
                $this->error('密码无变化！');
            }
            $passwd = md5($xmima);
            $uid = session('uid');
            $isok = M('yonghu')->where(array('id'=>$uid))->setField(array('mima'=>$passwd));
            if ($isok) {
                $data_rizhi['zy_type'] = 8;
                $data_rizhi['uid'] = session("uid");
                $data_rizhi['addtime'] = time();
                $data_rizhi['cz_type'] = 2;
                $neirong = "修改密码";
                $data_rizhi['neirong'] = $neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('成功');
            } else {
                $this->error('失败');
            }
        } else {
            $this->display();
        }
    }
    public function welcome(){
        //用户信息显示
        $this->yhtx=M('yonghu')->where(array('gongsiid'=>session("gongsiid"),'id'=>session('uid')))->getField('touxiang');
        //显示模板
        $this->display();
    }
    //显示头像页面
    public function avatar(){
        $yhtx = '/Upload/Avatar/'.session("gongsiid").'/'.session('username').'_200.jpg';
        if (file_exists('.'.$yhtx)){
            $this->touxiang = $yhtx;
        }
        $this->display();
    }
}