<?php

/***************************************************************\
|                                                               |
|   房佳（专业的新房分销ERP管理系统）                              |
|                                                               |
|   © 2019 http://fj.ytsofts.cn/ 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();
        $map3 = "gongsiid=".session('gongsiid');
        $map3 .= " and txuid=".session('uid');
        $map3 .= " and txshijian<".time();
        $map3 .= " and isyidu=0";
        $sqlcount3 = "select count(*) FROM __KYTIXING__ where {$map3}";
        $countb3=$Model->query($sqlcount3);
        $sh_count3=$countb3[0]['count(*)'];
        $arr['sh_count3']=$sh_count3;
        $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=I('ymima');
            $xmima=I('xmima');
            $qrmima=I('qrmima');
            $yymima=M('Yonghu')->where(array('id'=>session('uid')))->getField('mima');
            if ($yymima!=md5($ymima)) {
                $this->error('原密码错误!');
            }
            if ($xmima!=$qrmima) {
                $this->error('两次密码不匹配!');
            }
            $passwd = md5($xmima);
            $uid = session('uid');
            $r = M('Yonghu')->where(array('id'=>$uid))->setField(array('mima'=>$passwd));
            if ($r) {
                $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->useri=M('Yonghu')->where(array('id'=>session('uid')))->field('id,ygmingcheng,dianhua,touxiang,ygbianhao,bumen')->find();
        $this->remark=M('role')->where(array('id'=>session('role')))->getField('remark');
        $this->bmming=M('bumen')->where(array('id'=>$this->useri['bumen']))->getField('bmming');
        $this->jcpz=M('jcpz')->where('gongsiid=10000')->find();
        $kssj=time()-30*24*3600;
        $jssj=time();
        //个人最新预定
        $this->assign('yuding',$yuding);
        //门店最新预定
        $this->assign('mdyuding',$mdyuding);
        //个人最新成交
        $this->assign('chengjiao',$chengjiao);
        //门店最新成交
        $this->assign('mdchengjiao',$mdchengjiao);
        //准意向客户
        $tmb=time()-7*24*3600;
        $tme=time();
        $yixiang="select id,khxingming,xqbianhao,a.count from __KEYUAN__ b join (select count(*) as count,kyid,dkuid,dkshijian from __KYDAIKAN__ group by kyid) a on a.kyid=b.id where a.dkuid=".session('uid')." and a.dkshijian>{$tmb} and a.dkshijian<{$tme} order by a.count DESC limit 6";
        $yixiang=M('kydaikan')->query($yixiang);
        $this->assign('yixiang',$yixiang);
        $this->display();
    }
    //显示头像页面
    public function avatar(){
    	$this->display();
    }
}