<?php
use Addons\QyWechat\Helper;
use Addons\QyWechat\Services\ProvidersApi;
if(Cookie::get(ts)){
    $notice = new Model('notice');
    $notice->find();
    if($notice->switch == 1) {
        $content = $notice->content;
        Cookie::remove(ts);
    }else{
        $content = false;
    }
}
if(!Session::get('mainuid')){
	Redirect::index();
}
$user = new Model('user');
$pub = new Model('pubs');
if(Request::get(1)){
	$uid = Request::get(1);
	$user->find($uid);
	if($user->id==Session::get('mainuid') || Session::get('mainuid')==$user->pid){
		$pub->find(array('uid'=>$uid));
		$user->headpic = $pub->headpic;
	}else{
		die();
	}
}else{
	$user->find(Session::get('mainuid'));
	$user_type = new Model('user_type');
    $user_type->find($user->level_id);
    $start  = date_create($user->mendtime);
    $end   = date_create(date('Y-m-d')); // Current time and date
    $diff  	= date_diff( $start, $end );
    $dqtime = $diff->y . '年'.$diff->m . '月'.$diff->d . '日';
	$pub->find(array('uid'=>Session::get('mainuid')));
	$user->headpic = $pub->headpic;
}

Session::set('wid',$pub->id);
if(!$user->agid){
	$admin = (new Model('user_agency'))->find(['isadmin'=>1]);
	$user->agid = $admin->id;
	$user->save();
}
//是否授权
$thirdappsetting = (new Model('qy_agency_thirdappsetting'))->find(['pid'=>$user->agid]);
//$qyConfig = (new Model('qy_wechat_config'))->find(['wid'=>$pub->id]);
$ppc = (new Model('qy_providers_permanent_code'))->find(['wid'=>$pub->id]);
if(!$ppc->permanent_code && $thirdappsetting->is_type==1){
	$redirect_uri = urlencode(Helper::getInstance()->get_http_server_name().'/Addons/QyWechat/Admin/Providers/finish_auth.html');
	try{
		$url = ProvidersApi::getInstance()->get_rebind_url($redirect_uri);
	}catch(\Exception $e){
		$url = '';
	}
	//echo "<script>location.href='" . $url . "'</script>";
	if($url){
		Redirect::to($url);
	}else{
		die('企业微信第三方应用配置错误，请检查后再试');
	}
}
/**$ppc = (new Model('qy_providers_permanent_code'))->find(['wid'=>$pub->id]);
if(!$ppc->permanent_code){
	$redirect_uri = urlencode(Helper::getInstance()->get_http_server_name().'/Addons/QyWechat/Admin/Providers/finish_auth.html');
	$url = ProvidersApi::getInstance()->get_rebind_url($redirect_uri);
	echo "<script>location.href='" . $url . "'</script>";
}*/
$_SESSION['imgid']=$pub->id;
$_SESSION['imgurl']=conf::$http_path_static;
$initifurl = '';
if(Request::get('page')){
	$initifurl = '/'.str_replace('@', '/', Request::get('page')).'.html';	
}
$ag = new Model('user_agency');
$ag->id = $user->agid;
if($ag->is_real('id')){
	$ag->find($user->agid);
	$user->logo = !stristr($ag->logo,'http') ? conf::$http_path_static.$ag->logo : $ag->logo;
	$user->webname = $ag->webname;
} else {
	$ag->find();
	$user->logo = conf::$http_path_static . $ag->logo;
	$user->webname = $ag->webname;
}
$un = $user->un;
$level = $user->level_id;
$level_name = translate_level($level);
set_user_login($user);
include another('index');
$auth_check = new Model('auth_check');
$auth_check->find(array('auth_id'=>$level));
$nopages = explode(',',$auth_check->controllers);
foreach($nopages as $value){
	foreach($index as $key=>$value1){
		foreach($value1['sub'] as $key1=>$value2){
		if($value==$value2['file']){
			unset($index[$key]['sub'][$key1]);
		}
		}
	
		if(count($index[$key]['sub'])==0){
			unset($index[$key]);
		}	
		
		
	}
}

function set_user_login($mu){
	Auth::im_user();
	Session::set('uid',$mu->id);
	$uid = Session::get('uid');
	user_upgrade($mu);
	if(!Session::has('level_id')){
		Session::set('level_id',$mu->level_id);
	}
	Session::set('un',$mu->un);
	Session::set('ue',$mu->email);
	Session::set('ut',$mu->telephone);

	if($mu->isadmin=='a'){
		Auth::im_admin('admin');
	}
	Session::set('upath','/ups/'.date('Y/m',strtotime($mu->rtime)).'/'.$mu->id.'/');
	File::creat_dir(DOXCX_FRAME_PATH.DOXCX_PUB.'/'.Session::get('upath'));
	Session::set('headpic',trim($mu->headpic));
	Cookie::set('uid',$mu->id);
	Cookie::set('uuid',md5($mu->un.Request::ip().$mu->pwd));
}
//会员升级或降级操作
function user_upgrade($user){
	Session::remove('level_id');

	if(strtotime($user->mendtime)<time()){
		$user->level_id = 1;
		Session::set('level_id',1);
		$user->mendtime = '';
		$user->save();
	}else{
		Session::set('level_id',$user->level_id);
	}
}

$help = new Model('help_center');
$help->find(1);
$kefu = (new Model('qy_kefu'))->find(['mid' => $user->agid]);
//$menu = new Model('vshopadmin_indexmenu');
//$res = $menu->where(array('wid'=>Session::get('wid')))->list_all();

