<?php
namespace User\Controller;
use Common\Controller\AdminbaseController;
class PromotersController extends AdminbaseController {
    function index(){
		$where_ands=array("id >= 1");
		$fields=array(
				'start_time'=> array("field"=>"cs_addtime","operator"=>">"),
				'end_time'  => array("field"=>"cs_addtime","operator"=>"<"),
				'keyword'  => array("field"=>"tel","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);
		$Promoters_model=M("Promoters");
		$users_model=M("Users");
    	$count=$Promoters_model->where($where)->count();
    	$page = $this->page($count, 20);
    	$lists = $Promoters_model->where($where)->order("id DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach($lists as $n=> $val){
			$lists[$n]['PromoteCount']=$users_model->where(array('PromoterCode'=>$lists[$n]['code']))->count();
		 }
		$this->assign('lists', $lists);
    	$this->assign("page", $page->show('Admin'));
		$this->display();
    }
	function add(){
		
		$this->display();
	}
	function add_post(){
		$rules = array(
			array('name', 'require', '用户名不能为空！', 1 ),
			array('tel','require','手机号不能为空',1),
		);
		$this->Promoters_model=M("Promoters");
		if($date=$this->Promoters_model->validate($rules)->create()===false){
			$this->error($this->Promoters_model->getError());
		}else{
			$date['name']=I("post.name");
			$date['tel']=I("post.tel");
			$date['state']=1;
			$date['code']=sp_random_string();
			$date['addtime']=date('Y-m-d H:i:s');
		}
		if($this->Promoters_model->add($date)){
			$this->success('添加成功',U('User/Promoters/index'));
		}else{
			$this->error('添加失败',U('User/Promoters/add'));
		}
	}
	function  del(){
		$Promoters_model=M("Promoters");
		if(isset($_GET['id'])){
			$id = I("get.id");
			
			exit();
			$res=$Promoters_model->where(array('id'=>$id))->delete();
			if($res){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($Promoters_model->where("id in ($tids)")->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		} 
	}
	function edit(){
		$id=I('get.id');
		$Promoters_model=M("Promoters");
		$Promoter= $Promoters_model->where(array("id"=>$id))->find();
		$this->assign('lists', $Promoter);
        $this->display();
	}
	function detailed(){
     	$code=I('get.code');
     	$users_model=M("Users");   	
    	$lists = $users_model->where(array("PromoterCode"=>$code))->field('id,mobile,user_nicename,create_time')->select();
    	$this->assign('lists', $lists); 
        $this->display();
   } 
   function edit_post(){
	    $id=I('post.id');
	    $this->Promoters_model=M("Promoters");
		$date['name']=I("post.name");
		$date['tel']=I("post.tel");
		if($this->Promoters_model->where(array('id'=>$id))->save($date)){
			$this->success('添加成功',U('User/Promoters/index'));
		}else{
			$this->error('添加失败',U('User/Promoters/add'));
		}
   }
   function ban(){
		$users_model=M("Promoters");
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			$res=$users_model->where(array('id'=>$id))->setField('state','0');
			if($res){
				$this->success('设置成功！');
			}else{
				$this->error('设置失败！');
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($users_model->where("id in ($tids)")->setField('state','0')) {
				$this->success("设置成功！");
			} else {
				$this->error("设置失败！");
			}
		}
		
		
    }
    
    function cancelban(){
    	$users_model=M("Promoters");
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			$res=$users_model->where(array('id'=>$id))->setField('state','1');
			if($res){
				$this->success('设置成功！');
			}else{
				$this->error('设置失败！');
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($users_model->where("id in ($tids)")->setField('state','1')) {
				$this->success("设置成功！");
			} else {
				$this->error("设置失败！");
			}
		}
    }
	function set(){
		$PromoterMoney=C('PromoterMoney');
		$this->assign('PromoterMoney', $PromoterMoney);
		$this->display();
	}
	function set_post(){
		$money=$_POST['money'];
		$configs["PromoterMoney"]=$_POST['money'];
		sp_set_dynamic_config($configs);
	    $this->success("设置成功！");
	}
	
}