<?php
namespace User\Controller;
use Common\Controller\AdminbaseController;
class IndexadminController extends AdminbaseController {
    function index(){
		$where_ands=array("user_type >= 1");
		$fields=array(
				'start_time'=> array("field"=>"cs_addtime","operator"=>">"),
				'end_time'  => array("field"=>"cs_addtime","operator"=>"<"),
				'keyword'  => array("field"=>"mobile","operator"=>"like"),
		);
		if(IS_POST){
			foreach ($fields as $param =>$val){
				if (isset($_POST[$param]) && !empty($_POST[$param])) {
					$operator=$val['operator'];
					$field   =$val['field'];
					$get=$_POST[$param];
					$_GET[$param]=$get;
					if($operator=="like"){
						$get="%$get%";
					}
					array_push($where_ands, "$field $operator '$get'");
				}
			}
		}else{
			foreach ($fields as $param =>$val){
				if (isset($_GET[$param]) && !empty($_GET[$param])) {
					$operator=$val['operator'];
					$field   =$val['field'];
					$get=$_GET[$param];
					if($operator=="like"){
						$get="%$get%";
					}
					array_push($where_ands, "$field $operator '$get'");
				}
			}
		}
		$where= join(" and ", $where_ands);
    	$users_model=M("Users");
    	$count=$users_model->where($where)->count();
    	$page = $this->page($count, 50);
    	$lists = $users_model->where($where)->order("id DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
    	$vipuser=M('Vipuser');
    	foreach($lists as $n=> $val){
    		if($vipuser->where(array('uid'=>$lists[$n]['id']))->find()){
    			$lists[$n]['vip']=1;
    		}else{
    			$lists[$n]['vip']=0;
    		}
    	}
    	$this->assign('lists', $lists);
    	$this->assign("page", $page->show('Admin'));
    	$this->display(":index");
    }
    function  del(){
		$users_model=M("Users");
		
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			
			exit();
			$res=$users_model->where(array('id'=>$id))->delete();
			if($res){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($users_model->where("id in ($tids)")->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		} 
	}
    function ban(){
		$users_model=M("Users");
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			$res=$users_model->where(array('id'=>$id))->setField('user_status','0');
			if($res){
				$this->success('设置成功！');
			}else{
				$this->error('设置失败！');
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($users_model->where("id in ($tids)")->setField('user_status','0')) {
				$this->success("设置成功！");
			} else {
				$this->error("设置失败！");
			}
		}
		
		
    }
    
    function cancelban(){
    	$users_model=M("Users");
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			$res=$users_model->where(array('id'=>$id))->setField('user_status','1');
			if($res){
				$this->success('设置成功！');
			}else{
				$this->error('设置失败！');
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($users_model->where("id in ($tids)")->setField('user_status','1')) {
				$this->success("设置成功！");
			} else {
				$this->error("设置失败！");
			}
		}
    }
   function detailed(){
     	$id=I('get.id');
     	$users_model=M("Users");   	
    	$lists = $users_model->where(array("id"=>$id))->find();
    	$this->assign('lists', $lists);
        $this->display(":detailed");
   } 
   function buycourse(){
      $this->course_obj = D("Common/Course");
      $this->section_obj = D("Common/Section");
      $id=I('get.id');
      $model=M("Usercourse"); 
      $count=$model->where(array('user_id'=>$id))->count();
      $page = $this->page($count, 10);
      $lists= $model->where(array('user_id'=>$id))->limit($page->firstRow . ',' . $page->listRows)->select();
      foreach($lists as $n=> $val){
			 $lists[$n]['coursename']=$this->course_obj->where('id=\''.$val['course_id'].'\'')->getField('cs_name');
		     $urlArr = explode("|",$lists[$n]['studied']);  
             $jnum=count( $urlArr)-1;
             $cs_id=$lists[$n][course_id];
             $znum=$this->section_obj->where(array("cs_id"=>$cs_id))->count();
             $lists[$n]['bili'] = intval(($jnum/$znum)*100);
      }
     
      $this->assign('lists', $lists);
      $this->display(":buycourse");
   }
   function repass(){
       $id=I('get.id');
       $this->assign('id', $id);
       $this->display(":repass");
   }
   function repass_post(){
       $id=I('post.id');
       $pass=I('post.pass');
	   if(empty($pass)){
		   $this->error('请输入密码！');
	   }
       $newpass=sp_password($pass);
       $rst = M("Users")->where(array("id"=>$id))->setField('user_pass',$newpass);
    		if ($rst) {
    			$this->success("重置成功！");
    		} else {
    			$this->error('重置失败！');
    		} 
   }
   function addmoney(){
     $id=I('get.id');
     $this->assign('id', $id);
     $this->display(":addmoney");
   }
   function addmoney_post(){
       $id=I('post.id');
       $coin=I('post.coin');
	   if(empty($coin)){
		   
		   $this->error('金额不能为空！');
	   }
	   $ycoin=M("Users")->where(array("id"=>$id))->getField('coin');
       $rst = M("Users")->where(array("id"=>$id))->setField('coin',$coin+$ycoin);
    		if ($rst) {
    			$this->success("设置成功！");
    		} else {
    			$this->error('设置成功！');
    		}
   }
   function presentcourse(){
	    $this->course_obj = D("Common/Course");
		$this->coursetype_obj = D("Common/Coursetype");
		$this->courseview_obj = D("Common/CourseView");
	   $id=I('get.id');
	   $this->_getTree();
	   $term_id = intval(I("get.term"));
	   $term=$this->course_obj->where("ty_id=$term_id")->find();
	   $this->assign("term",$term);
	   $this->assign('id', $id);
	   $this->assign('course', $course);
	   $this->display(":presentcourse");
   }
   function presentcourse_post(){
	   $this->course_obj = D("Common/Course");
	   $this->usercourse = D("Common/Usercourse");
	   $userid=I('post.userid');
	   $courseid=I('post.courseid');
	   $courseinfo=$this->course_obj->where("id=$courseid")->find();
	   $data['user_id']=$userid;
	   $data['course_id']=$courseid;
	   $data['teacher_id']=$courseinfo['cs_teacher'];
	   $data['course_price']=0;
	   $data['addtime']=date('Y-m-d H:i:s');
	   
	   $check=$this->usercourse->where(array('user_id'=>$userid,'course_id'=>$courseid))->find();
	   if($check){
		   $this->error('该客户已经购买此课程，请不要重复赠送！');
	   }else{
		   if( $this->usercourse->add($data)){
		   
		      $this->success('赠送课程成功！');
	        }
		   
	   }
	   
	   
   }
   private function _getTree(){
		$term_id=empty($_REQUEST['term'])?0:intval($_REQUEST['term']);
		$result = $this->coursetype_obj->order(array("listorder"=>"asc"))->select();
		
		$tree = new \Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach ($result as $r) {
			$r['str_manage'] = '<a href="' . U("AdminCoursetype/add", array("parent" => $r['term_id'])) . '">添加子类</a> | <a href="' . U("AdminTerm/edit", array("id" => $r['term_id'])) . '">修改</a> | <a class="J_ajax_del" href="' . U("AdminTerm/delete", array("id" => $r['term_id'])) . '">删除</a> ';
			$r['visit'] = "<a href='#'>访问</a>";
			$r['taxonomys'] = $this->taxonomys[$r['taxonomy']];
			$r['id']=$r['term_id'];
			$r['parentid']=$r['parent'];
			$r['selected']=$term_id==$r['term_id']?"selected":"";
			$array[] = $r;
		}
		$tree->init($array);
		$str="<option value='\$id' \$selected>\$spacer\$name</option>";
		$taxonomys = $tree->get_tree(0, $str);
		$this->assign("taxonomys", $taxonomys);
	}
	function imporuser(){
		if (!empty($_FILES)) {
			
			$arr=0;
			vendor('Classes.PHPExcel');
			$upload = new \Think\UploadFile();
            $upload->maxSize = 3292200;
            $upload->allowExts = explode(',', 'xls,doc');
            $upload->savePath = './Uploads/';
			$upload->saveRule = time();
            if (!$upload->upload()) {
                $this->error($upload->getErrorMsg());
			} else {
				$info = $upload->getUploadFileInfo();
			}
			$file_name=$info[0]['savepath'].$info[0]['savename'];
			$objReader = \ PHPExcel_IOFactory::createReader('Excel5');
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); 
			$highestColumn = $sheet->getHighestColumn(); 
			for($i=2;$i<=$highestRow;$i++)
			{   
		    
			  $data['user_login'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); 
			  if(empty($data['user_login'])){
			  	break;
			  }
			  $user_nicename= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
			  if(empty($user_nicename)){
			  	 $data['user_nicename'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
			  	}else{
			  		$data['user_nicename'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
			  }
				
				$mobile=$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); 
				if(empty($mobile)){
						$data['mobile'] = 0;
					}else{
						$data['mobile'] =$mobile;
					}
				$coin=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				if(empty($coin)){
					$data['coin'] = 0;
					}else{
						$data['coin'] =$coin;
				  }
				$data['user_pass']   = sp_password($objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue());
				$data['user_type']=2;
				$data['create_time']=date("Y-m-d H:i:s");
				$data['last_login_time']=date("Y-m-d H:i:s");
				$courseidArray = explode(',', $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue());
				if(!$lastInsId =$users_model=M("Users")->add($data)){
					$arr++;
				}
				if(!empty($courseidArray)){
					for($j=0;$j<count($courseidArray);$j++){
						$usercouese['user_id']=$lastInsId;
						$usercouese['course_id']=$courseidArray[$j];
						$usercouese['state']=1;
						$usercouese['addtime']=date("Y-m-d H:i:s");
						if(!$usercouese_model=M("Usercourse")->add($usercouese)){
							$arr2++;
						}
					}
				}
				
			}  
			if($arr==0){
				$this->success('导入成功！');
			}else{
				$this->error($arr."个导入失败！");
			}
		 
        }else
            {
                $this->error("请选择上传的文件");
            }    
         
		
	}
	function regpresent (){
		$course_obj=M('Course');
		$course = $course_obj->order(array("id"=>"asc"))->select();
		$regpresent_obj=M('Regpresent');
		$regpresent_jinqian=$regpresent_obj->where(array('key'=>'present_jinqian'))->find();
		$regpresent_coupon=$regpresent_obj->where(array('key'=>'present_coupon'))->find();
		$regpresent_course=$regpresent_obj->where(array('key'=>'present_course'))->find();
	    $discount=json_decode($regpresent_course['parameter'],true);
		foreach ($course as $n=> $val) {
			$course[$n]['selected']=in_array($course[$n]['id'], $discount)?'selected="selected"':'';
		} 
		$this->assign("regpresent_jinqian", $regpresent_jinqian);
		$this->assign("regpresent_coupon", $regpresent_coupon);
		$this->assign("regpresent_course", $regpresent_course);
		$this->assign("course", $course);
		$this->display(":regpresent");
	}
	function regpresent_post(){
		$regpresent_obj=M('Regpresent');
		$data1['key']='present_jinqian';
		$data1['value']=$_POST['radio1'];
		$data1['parameter']=$_POST['jinqianNums'];
		
		$data2['key']='present_coupon';
		$data2['value']=$_POST['radio2'];
		$data2['parameter']=$_POST['couponNums'];
		
		$data3['key']='present_course';
		$data3['value']=$_POST['radio3'];
		$data3['parameter']=json_encode($_POST['term']);

		if($regpresent_obj->where(array('key'=>'present_jinqian'))->find()){
			  $regpresent_obj->where(array('key'=>'present_jinqian'))->save($data1);
		}elseif(!empty($data1['value'])){
			  $regpresent_obj->add($data1);
		}
		if($regpresent_obj->where(array('key'=>'present_coupon'))->find()){
			  $regpresent_obj->where(array('key'=>'present_coupon'))->save($data2);
		}elseif(!empty($data2['value'])){
			  $regpresent_obj->add($data2);
		}
		if($regpresent_obj->where(array('key'=>'present_course'))->find()){
			  $regpresent_obj->where(array('key'=>'present_course'))->save($data3);
		}elseif(!empty($data3['value'])){
			  $regpresent_obj->add($data3);
		}
		$this->success('设置成功！');
	}
  	
}