<?php
namespace Teacher\Controller;
use Common\Controller\AdminbaseController;
class AdminCenterController extends AdminbaseController {
	function _initialize() {
		parent::_initialize();
		$this->users_obj = D("Common/users");
		$this->usercourse_obj = D("Common/usercourse");
		$this->course_obj = D("Common/course");
		$this->tixian_obj = D("Common/tixian");
		$this->application_obj = D("Common/application");
	} 
	function index(){
	   $count=$this->application_obj->count();
	   $page = $this->page($count, 10);
	   $teacher=$this->application_obj->limit($page->firstRow . ',' . $page->listRows)->order(array('id'=>'desc'))->select();
	   foreach($teacher as $n=> $val){
			$teacher[$n]['csnum']=$this->course_obj->order('id desc')->where('cs_teacher=\''.$val['user_id'].'\'')->count();
			$teacher[$n]['xynum']=$this->usercourse_obj->order('id desc')->where('teacher_id=\''.$val['user_id'].'\'')->count();
			$teacher[$n]['adminplate']=$this->users_obj->where('id=\''.$val['user_id'].'\'')->getField('adminplate');
			$teacher[$n]['folderid']=$this->users_obj->where('id=\''.$val['user_id'].'\'')->getField('folderid');
			$teacher[$n]['aliyuncategory']=$this->users_obj->where('id=\''.$val['user_id'].'\'')->getField('aliyuncategory');
		}
		$this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->assign("teacher",$teacher);
		$this->display();
	}
	function tcourse(){
	   $t_id = intval(I("get.t_id"));
	   $count=$this->course_obj->where('cs_teacher=\''.$t_id.'\'')->count();
	   $page = $this->page($count, 10);
	   $data=$this->course_obj->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->where('cs_teacher=\''.$t_id.'\'')->select();
       foreach($data as $n=> $val){
			$data[$n]['xynum']=$this->usercourse_obj->where('course_id=\''.$val['id'].'\'')->count();
		}
		$this->assign("Page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
	    $this->assign("data",$data);
	    $this->display();
	}
	function tbrief(){
	   $id = intval(I("get.t_id"));
	   $data= $this->users_obj->where(array('id'=>$id))->find();
	   $this->assign("data",$data);
	   $this->display();
	}
	function tbriefpost(){
		if (IS_POST){
		   $id = intval(I("post.id"));
		   $data=$this->users_obj->create();
		   $data['tcProfile'] =htmlspecialchars_decode($data['tcProfile']);
		   if($this->users_obj->where(array('id'=>$id))->save($data)){
			   $this->success("设置成功！");
		   }else{
			   $this->error("设置失败！");
		   }
	  }	
	}
	function tixian(){
		$tixian=$this->tixian_obj->select();
	    foreach($tixian as $n=> $val){
			$tixian[$n]['name']=$this->users_obj->where('id=\''.$val['u_id'].'\'')->getField('user_nicename');
		}
		 $this->assign("tixian",$tixian);
	     $this->display();
	}
	function trequery(){
	   $id = intval(I("post.id"));
	   $tixian=$this->tixian_obj->select();
	   $data['state']=1;
	   if($this->tixian_obj->where(array('id'=>$id))->save($data)) {
	    return json_encode($data);
	   }
	}
    function requery(){
	   $id = intval(I("get.t_id"));
	   $data= $this->application_obj->where(array('user_id'=>$id))->find();
	   $this->assign("data",$data);
	   $this->display();
	  }
    function requery_post(){
    if (IS_POST){
	   $id = intval(I("post.id"));
	   $tid = intval(I("post.tid"));
	   $radio=intval(I("post.radio"));
	   $state1['state']=$radio;
	   $state2['user_type']=3;
	   $state3['user_type']=2;
	   $folderName=$this->users_obj->where(array('id'=>$tid))->getField('mobile');
	   if($radio==1){
	   	 $this->users_obj->where(array('id'=>$tid))->save($state2);
	   	 $this->application_obj->where(array('id'=>$id))->save($state1);
         $this->tcreatfolder($folderName,$tid);
		 $message="恭喜您申请的教师账户已经通过审核！";
		 send_message($tid,'0','1',$message);
	   	 $this->success("设置成功！");
	   }else{
	   	 $this->users_obj->where(array('id'=>$tid))->save($state3);
		 $message="您申请的教师账户未通过审核，请修改资料重新提交！";
		 send_message($tid,'0','1',$message);
	     $this->application_obj->where(array('id'=>$id))->save($state1);
	     $this->success("设置成功！");
	   }
	  }	
    }
    function tcreatfolder($folderName,$tid)
    {
        $postdata = $this->Ali_info();
        $postdata['name'] = $folderName;
        $url = $postdata['server'] . "/api/educloud/addAliCategory";
        $restemp = post_curl($url, $postdata);
        $res = json_decode($restemp, true);
        $map['aliyuncategory'] = $res['Category']['CateId'];
        if ($map['aliyuncategory']) {
            $this->users_obj->where(array('id' => $tid))->save($map);
        }
    }
	function creatfolder(){
        $tid = intval(I("get.t_id"));
        $folderName=$this->users_obj->where(array('id'=>$tid))->getField('mobile');
        $postdata = $this->Ali_info();
        $postdata['name'] = $folderName;
        $url = $postdata['server'] . "/api/educloud/addAliCategory";
        $restemp = post_curl($url, $postdata);
        $res = json_decode($restemp, true);
        $map['aliyuncategory'] = $res['Category']['CateId'];
        if($map['aliyuncategory']){
            if($this->users_obj->where(array('id'=>$tid))->save($map)){
                $this->success("创建成功！");
            }else{
                $this->error('插入数据失败');
            }

        }else{
            $this->error($res['msg']);
        }
	}
	function folder(){
		$t_id = intval(I("get.t_id"));
		$user=$this->users_obj->where(array('id'=>$t_id))->find();
		$this->assign("user",$user);
		$this->display();
	}
	function setbili(){
		$alipaybili=C('alipaybili');
		$cardbili=C('cardbili');
		$this->assign("alipaybili",$alipaybili);
		$this->assign("cardbili",$cardbili);
		$this->display();
	}
	function setbili_post(){
	 if (IS_POST){
		$config['alipaybili']=I('post.alipaybili');
		$config['cardbili']=I('post.cardbili');
	    sp_set_dynamic_config($config);
	 }
	   $this->success("设置成功！");
	 }
	 function adminforum(){
		 $id=I('get.t_id');
		 $state=$this->users_obj->where(array('id'=>$id))->getField('adminplate');
		 if($state==0){
			 $data['adminplate']=1;
			 if($this->users_obj->where(array('id'=>$id))->save($data)){
				 $this->success("设置成功！");
			 }else{
				 $this->error("设置失败！");
			 }
			 
		 }else{
			  $data['adminplate']=0;
			 if($this->users_obj->where(array('id'=>$id))->save($data)){
				 $this->success("设置成功！");
			 }else{
				 $this->error("设置失败！");
			 }
		 }
		 
	 }
    function get_site_info(){
        $info['author_code']=C('authoCode');
        $info['server']=C('server');
        return $info;
    }
    function Ali_info(){
        $info['KeyID']=C('Ali_KeyID');
        $info['keySecret']=C('Ali_KeySecret');
        $info['Bucket']=C('Ali_Bucket');
        $info['EndPoint']=C('Ali_EndPoint');
        $info['UserId']=C('Ali_UserId');
        $info['server']=C('server');
        $info['domain']=$this->get_domain();
        $info['authorcode']=C('authoCode');
        return $info;
    }
    function get_domain(){
        return sp_get_domain();
    }
	
}