<?php
/**
 * 广告管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Admin\Controller;
class PosterController extends BaseController{
    public function index(){
        $name = CONTROLLER_NAME;
        $map=array();
        $aid=I('id');
        $keyword=I('keyword');
        if($aid)$map['position_id']=$aid;
        if($keyword)$map['name']=array('like',"%".$keyword."%");
        $map['device']=0;
        $list = $this->lists($name,$map);
        $this->assign('list', $list);
        $this->assign('id', $aid);
	    $this->getCate();
        $this->display();
    }
    public function mobileindex(){
        $name = CONTROLLER_NAME;
        $map=array();
        $aid=I('id');
        $keyword=I('keyword');
        if($aid)$map['position_id']=$aid;
        if($keyword)$map['name']=array('like',"%".$keyword."%");
        $map['device']=1;
        $list = $this->lists($name,$map);
        $this->assign('list', $list);
        $this->assign('id', $aid);
        $big_menu = array('添加广告' => U('add'));
        $this->assign('big_menu', $big_menu);
	    $this->getCate(1);
        $this->display();
    }
    public function wxsmallindex(){
        $name = CONTROLLER_NAME;
        $map=array();
        $aid=I('id');
        $keyword=I('keyword');
        if($aid)$map['position_id']=$aid;
        if($keyword)$map['name']=array('like',"%".$keyword."%");
        $map['device']=1;
        $map['position_id'] = idtoname('wxsmall_poster_position','PosterPosition','id','code',false);
        $list = $this->lists($name,$map);
        $this->assign('list', $list);
        $this->assign('id', $aid);
        $big_menu = array('添加广告' => U('add'));
        $this->assign('big_menu', $big_menu);
	    $this->getCate(1);
        $this->display();
    }
    public function _before_add(){
		$is_mobile=I('is_mobile');
		$is_wxsmall=I('is_wxsmall');
        $this->getCate($is_mobile);
		$this->getcateTree();
		if($is_mobile==1)$this->assign('device',1);
		if($is_wxsmall==1)$this->assign('is_wxsmall',$is_wxsmall);
    }
    public function _before_edit(){        
        $pid=idtoname(I('id'),CONTROLLER_NAME,'catid');
        $positionid=idtoname(I('id'),CONTROLLER_NAME,'position_id');
        $is_mobile=idtoname(I('id'),CONTROLLER_NAME,'device');
        $this->getCate($is_mobile);
        $is_wxsmall=I('is_wxsmall');
        if($is_wxsmall==1)$this->assign('is_wxsmall',$is_wxsmall);
        if($positionid==63){
            $this->getcateTree($pid,'',1);
        }else{
            $this->getcateTree($pid);
        }

    }
    public function insert() {
		$name = CONTROLLER_NAME;
		$model = D ($name);
        switch(I('type')){
            case '1':$_POST['code']=I('code_flash');break;
            case '2':$_POST['code']=I('code_code');break;
            default:$_POST['code']=I('code_image');break;
        }
        if(I('start_time'))$_POST['start_time']=strtotime(I('start_time'));
        if(I('end_time'))$_POST['end_time']=strtotime(I('end_time'));
        $data=I('post.');
		if (false === $model->create ($data)) {
			$this->error ( $model->getError () );
		}
        $data['url'] = return_urlrules($data['url']);
		$id = $model->add($data);
		if ($id !==false) {
			if($_POST['isajax'])$this->assign('dialog','1');
            $forward=U(CONTROLLER_NAME.'/index');
            if($data['device']==1){$forward=U(CONTROLLER_NAME.'/mobileindex');}
			$jumpUrl = $_POST['forward'] ? $_POST['forward'] : $forward;
			$this->success (L('ADD_OK'),$jumpUrl);
		} else {
			$this->error (L('ADD_ERROR').': '.$model->getDbError());
		}
	}
    public function update (){
		if(!$model)$model=CONTROLLER_NAME;
        switch(I('type')){
            case '1':$_POST['code']=I('code_flash');break;
            case '2':$_POST['code']=I('code_code');break;
            default:$_POST['code']=I('code_image');break;
        }
        if(I('start_time'))$_POST['start_time']=strtotime(I('start_time'));
        if(I('end_time'))$_POST['end_time']=strtotime(I('end_time'));
		if(!$data)$data=D($model)->create ();
        $data['url'] = return_urlrules($data['url']);
        $forward=U(CONTROLLER_NAME.'/index');
        if($data['device']==1){$forward=U(CONTROLLER_NAME.'/mobileindex');}
        $jumpUrl = $_POST['forward'] ? $_POST['forward'] : $forward;
        $id    = array_unique((array)I('id',0));
        $id    = is_array($id) ? implode(',',$id) : $id;
        $where = array_merge( array('id' => array('in', $id )) ,(array)$where );
        $msg   = array_merge( array('success'=>L('edit_ok'), 'error'=>L('edit_error'), 'url'=>$jumpUrl ,'ajax'=>IS_AJAX) , (array)$msg );
        if( M($model)->where($where)->save($data)!==false ) {
            $this->success($msg['success'],$msg['url'],$msg['ajax']);
        }else{
            $this->error($msg['error'],$msg['url'],$msg['ajax']);
        }
    }
    /* 得到节点树 */
	private function getCate($is_mobile=0) {
        if($is_mobile == 1){$wh['device']=1;}else{$wh['device']=0;}
        $allCate = M('PosterPosition')->where($wh)->field('id,name')->cache(true)->select();
		foreach($allCate as $k =>$v){
		   $linkcat[$v['id']]=$v['name'];
		}
		$adtype=array('图片','FLASH','代码','文本');
		$this->assign('adtype',$adtype);
		$this->assign('linkcat',$linkcat);
	}
    public function pgetcateTree(){
        $param = I('post.type');
        $return['select_categorys']=$this->getcateTree('',1,$param);
        $this->ajaxReturn($return);
    }
    private function getcateTree($pid = '',$assign='',$type=0) {
        if($type==1){
            $GoodsCategory = getcache('PurchaseCategory');
        }else{
            $GoodsCategory = getcache('GoodsCategory');
        }

		$Tree = new \Common\Vendor\Tree();
		$select_categorys='';
		$treemini=$Tree->treemini($GoodsCategory);
		$select_categorys.= $Tree->treeminioption($treemini,$pid);
		if($assign)return $select_categorys;
		else $this->assign('select_categorys',$select_categorys);
	}
}