<?php
namespace Card\Controller;
use Common\Controller\AdminbaseController;
class AdminCardController extends AdminbaseController {
	protected $card_obj;
	protected $cardtype_obj;
	protected $cardview_obj;
	protected $course_obj;
	function _initialize() {
		parent::_initialize();
		$this->card_obj = D("Common/Card");
		$this->cardtype_obj = D("Common/Cardtype");
		$this->cardview_obj = D("Common/CardView");
		$this->course_obj = D("Common/Course");
		$this->coupon_obj = D("Common/Coupon");
	}
	function index(){
		$this->_lists();
		$this->_getType();
		$this->display();
	}
   function index_cs(){
        $cs_id=intval(I("get.cs_id"));
        $cs_name=$this->course_obj->where("id=$cs_id")->getField('cs_name');
        $count=$this->card_obj->where("cs_id=$cs_id")->count();
		$page = $this->page($count, 50);
		$cardlist=$this->card_obj->where("cs_id=$cs_id")->order("id ASC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign("page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->assign("cardlist",$cardlist);
		$this->assign("cs_data",$cs_name);
		$this->display();
   }
	function add(){
		$type_id = intval(I("get.type_id"));
		$domain=sp_get_domain();
		$AuthoCode=C('authoCode');
		$this->assign("AuthoCode",$AuthoCode);
		$this->assign("domain",$domain);
		$this->assign("type_id", $type_id);
		$this->display();
	}
    function add_jihuo(){
    	$type_id = intval(I("get.type_id"));
		$domain=sp_get_domain();
		$AuthoCode=C('authoCode');
		$this->assign("AuthoCode",$AuthoCode);
		$this->assign("domain",$domain);
    	$this->_getCourse();
    	$this->assign("type_id", $type_id);
		$this->display();
	}
    function add_jihuo_cs(){
    	$type_id = intval(I("get.type_id"));
    	$cs_id = intval(I("get.cs_id"));
		$domain=sp_get_domain();
		$AuthoCode=C('authoCode');
		$this->assign("AuthoCode",$AuthoCode);
		$this->assign("domain",$domain);
    	$this->assign("type_id", $type_id);
    	$this->assign("cs_id", $cs_id);
		$this->display();
	}
    function add_tiyan(){
		$type_id = intval(I("get.type_id"));
		$domain=sp_get_domain();
		$AuthoCode=C('authoCode');
		$this->assign("AuthoCode",$AuthoCode);
		$this->assign("domain",$domain);
		$this->assign("type_id", $type_id);
		$this->display();
	}
    function add_vip(){
		$type_id = intval(I("get.type_id"));
		$domain=sp_get_domain();
		$AuthoCode=C('authoCode');
		$this->assign("AuthoCode",$AuthoCode);
		$this->assign("domain",$domain);
		$this->assign("type_id", $type_id);
		$this->display();
	}
	function add_post(){
		$i=1;
		if (IS_POST) {
			$type_id = intval(I("post.type_id"));
			$cs_id = intval(I("post.cs_id"));
			$viptime = intval(I("post.viptime"));
			$card_price= intval(I("post.card_price"));
			$numbers= intval(I("post.numbers"));
			$notice=$_POST['code'];
			$count=$_POST['count'];
			$data = $this->card_obj->create(); 
			$j=$this->card_obj->where(array('type_id'=>$type_id))->count();
		   while($i<=$numbers)
		{  
			if($notice=='sucess'){
			  $carddate=$this->sp_random_string_card(15);
		   	  $data['type_id']=$type_id;
			  $data['cs_id']=$cs_id;
			  $data['viptime']=$viptime;
			  $data['card_price']=$card_price;
	    	  $data['card_name']=$carddate;
			  $data['addtime']=date('Y-m-d H:i:s');	
			  $i++;
			  $result=$this->card_obj->add($data);
			}else{
				if($i<=$count-$j){
					$carddate=$this->sp_random_string_card(15);
		      	    $data['type_id']=$type_id;
			        $data['cs_id']=$cs_id;
			        $data['viptime']=$viptime;
			        $data['card_price']=$card_price;
	    	        $data['card_name']=$carddate;
			        $data['addtime']=date('Y-m-d H:i:s');
					$i++;
			        $result=$this->card_obj->add($data);
				}else{
					$this->error($notice);
					exit();
				}
			}
		}
			if ($result) {
					$this->success("添加成功！",U("AdminCard/index"));
				}else{
					$this->error("添加失败！");
				}			
		}
	}
	private  function _lists($status=1){
		$type_id=0;
		if(!empty($_REQUEST["type"])){
			$type_id=intval($_REQUEST["type"]);
		}
		$where_ands=empty($type_id)?array("card_state<=$status"):array("type_id = $type_id and card_state<=$status");
		$fields=array(
				'start_time'=> array("field"=>"addtime","operator"=>"="),
				'end_time'  => array("field"=>"addtime","operator"=>"<"),
				'keyword'  => array("field"=>"card_name","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);
		//dump($where);
		$count=$this->card_obj->where($where)->count();
		$page = $this->page($count, 100);
		$cardlist=$this->card_obj->where($where)->order("id DESC")->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign("page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->assign("cardlist",$cardlist);
	}
	private function _getType(){
		$typedata = $this->cardtype_obj->order(array("id"=>"asc"))->select();
		$this->assign("typedata", $typedata);
	}
    private function _getCourse(){
		$typedata = $this->course_obj->order(array("id"=>"asc"))->select();
		$this->assign("typedata", $typedata);
	}
	function sell(){
		if(isset($_POST['ids']) && $_GET["sell"]){
			$data["sale_state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->card_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("设置成功！",U("AdminCard/index"));
			} else {
				$this->error("设置失败！",U("AdminCard/index"));
			}
		}
		if(isset($_POST['ids']) && $_GET["unsell"]){
			$data["sale_state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->card_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("取消成功！",U("AdminCard/index"));
			} else {
				$this->error("取消失败！",U("AdminCard/index"));
			}
		}
	}
	function used(){
		if(isset($_POST['ids']) && $_GET["used"]){
			$data["use_state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->card_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("设置成功！",U("AdminCard/index"));
			} else {
				$this->error("设置失败！",U("AdminCard/index"));
			}
		}
		if(isset($_POST['ids']) && $_GET["unused"]){
			$data["use_state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->card_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("取消成功！",U("AdminCard/index"));
			} else {
				$this->error("取消失败！",U("AdminCard/index"));
			}
		}
	}
	function lock(){
		if(isset($_POST['ids']) && $_GET["lock"]){
			$data["card_state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->card_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("锁定成功！",U("AdminCard/index"));
			} else {
				$this->error("锁定失败！",U("AdminCard/index"));
			}
		}
		if(isset($_POST['ids']) && $_GET["unlock"]){
			$data["card_state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->card_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("解锁成功！");
			} else {
				$this->error("解锁失败！");
			}
		}
	}
    function sp_random_string_card($len) {
	$chars = array(
			"A", "B", "C", "D", "E", "F", "G",
			"H", "J", "K", "M", "N",  "P", "Q", "R",
			"S", "T", "U", "V", "W", "X", "Y", "Z",  "2",
			"3", "4", "5", "6", "7", "8", "9"
	);
	$charsLen = count($chars) - 1;
	shuffle($chars);    // 将数组打乱
	$output = "";
	for ($i = 0; $i < $len; $i++) {
		$output .= $chars[mt_rand(0, $charsLen)];
	}
	return $output;
}
   function randstr($len) {
		$chars='ABCDEFGHJKMNPQRSTUVWXYZ23456789';
		$chars1='023456789';
		// characters to build the password from
		mt_srand((double)microtime()*1000000*getmypid());
		// seed the random number generater (must be done)
		$password='';
		$cardname='';
		while(strlen($cardname)<$len)
		{
			$cardname.=substr($chars,(mt_rand()%strlen($chars)),1);
		}
		while(strlen($password)<($len-4))
		{
			$password.=substr($chars1,(mt_rand()%strlen($chars1)),1);
		}
		return $card=array($cardname,$password);
    }
	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->card_obj->delete($id)!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($this->card_obj->where("id in ($tids)")->delete()) {
				$this->success("删除成功！",U("AdminCard/index"));
			} else {
				$this->error("删除失败！",U("AdminCard/index"));
			}
		}
	}
	function discount(){
		$discount_obj=M('Discount');
		$course = $this->course_obj->order(array("id"=>"asc"))->select();
		$discount=$discount_obj->find();
		$discount['ids']=json_decode($discount['c_ids'],true);
		foreach ($course as $n=> $val) {
			$course[$n]['selected']=in_array($course[$n]['id'], $discount['ids'])?'selected="selected"':'';
		}
		$this->assign("course", $course);
		$this->assign('discount',$discount);
		$this->display();
	}
	function discount_post(){
		if(IS_POST){
			$discount_obj=M('Discount');
			$data['c_ids']=json_encode($_POST['term']);
			$data['discount']=$_POST['discount'];
			$data['starttime']=$_POST['starttime'];
			$data['endtime']=$_POST['endtime'];
			if(empty($data['c_ids'])){
				$this->error("请至少选择一个课程！");
			}
			if(empty($data['discount'])){
				$this->error("请填写打折设置！");
			}
			
			if($data['discount']>1){
				$this->error("打折设置不能大于1");
			}
			if(empty($data['starttime'])){
				$this->error("请填写开始时间");
			}
			if(empty($data['endtime'])){
				$this->error("请填写结束时间！");
			}
			if($discount_obj->select()){
				if($discount_obj->where(array("id"=>1))->save($data)){
					$this->success('设置成功');
				}else{
					$this->error('设置失败！');
				}
			}else{
				if($discount_obj->add($data)){
					$this->success('设置成功');
				}else{
					$this->error('设置失败！');
				}
			}
		}
	}
	function coupon(){
        $count=$this->coupon_obj->count();
		$page = $this->page($count, 50);
		$coupon=$_POST['coupon'];
		 if(empty($coupon)){
		  	$countlist=$this->coupon_obj->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
			}else{
				$countlist=$this->coupon_obj->order("id desc")->where(array('coupon'=>$coupon))->select();
			}
		foreach($countlist as $n=> $val){
			if(strtotime(date('Y-m-d H:i:s', time()))> strtotime($countlist[$n]['timelimit'])){
				$countlist[$n]['state']=2;
			}
		}	
		$this->assign("page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		$this->assign("countlist",$countlist);
		$this->display();
	}
	function couponadd(){
		
		$this->display();
    }
	function couponadd_post(){
		$i=1;
		if (IS_POST) {
			$timelimit =$_POST['timelimit'];
			$discount= $_POST['discount'];
			$numbers= intval(I("post.numbers"));
		   while($i<=$numbers)
			{  
				$carddate=$this->sp_random_string_card(15);
				$data['discount']=$discount;
				$data['coupon']=$carddate;
				$data['addtime']=date('Y-m-d H:i:s');
				$data['timelimit']=$timelimit;	
				$data['state']=1;		
				$i++;
				$result=$this->coupon_obj->add($data);
			}
			if ($result) {
				$this->success("添加成功！",U("AdminCard/coupon"));
			}else{
				$this->error("添加失败！");
			}			
		}
		
	}
	function couponsell(){
		if(isset($_POST['ids']) && $_GET["sell"]){
			$data["sales_state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->coupon_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("设置成功！",U("AdminCard/coupon"));
			} else {
				$this->error("设置失败！",U("AdminCard/coupon"));
			}
		}
		if(isset($_POST['ids']) && $_GET["unsell"]){
			$data["sales_state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->coupon_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("取消成功！",U("AdminCard/coupon"));
			} else {
				$this->error("取消失败！",U("AdminCard/coupon"));
			}
		}
	}
	function couponused(){
		if(isset($_POST['ids']) && $_GET["used"]){
			$data["used_state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->coupon_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("设置成功！",U("AdminCard/coupon"));
			} else {
				$this->error("设置失败！",U("AdminCard/coupon"));
			}
		}
		if(isset($_POST['ids']) && $_GET["unused"]){
			$data["used_state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->coupon_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("取消成功！",U("AdminCard/coupon"));
			} else {
				$this->error("取消失败！",U("AdminCard/coupon"));
			}
		}
	}
	function couponlock(){
		if(isset($_POST['ids']) && $_GET["lock"]){
			$data["state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->coupon_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("锁定成功！",U("AdminCard/coupon"));
			} else {
				$this->error("锁定失败！",U("AdminCard/coupon"));
			}
		}
		if(isset($_POST['ids']) && $_GET["unlock"]){
			$data["state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->coupon_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("解锁成功！",U("AdminCard/coupon"));
			} else {
				$this->error("解锁失败！",U("AdminCard/coupon"));
			}
		}
	}
	function coupondelete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->coupon_obj->delete($id)!==false) {
				$this->success("删除成功！",U("AdminCard/coupon"));
			} else {
				$this->error("删除失败！",U("AdminCard/coupon"));
			}
		}
		if(isset($_POST['ids'])){
			$tids=join(",",$_POST['ids']);
			if ($this->coupon_obj->where("id in ($tids)")->delete()) {
				$this->success("删除成功！",U("AdminCard/coupon"));
			} else {
				$this->error("删除失败！",U("AdminCard/coupon"));
			}
		}
	}
}