<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Banner as BannerModel;
use think\Db;
class Banner extends Common
{
    public function guojiz($gj_name)
    {
        $url = DS . 'application/template/pc/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $gj_name . DS)) {
            $info = (include ROOT_PATH . $url . $gj_name . DS . 'Guojiz.php');
            $info['gj_name'] = $gj_name;
            return $info;
        }
    }
    public function guojizw($gj_name)
    {
        $url = DS . 'application/template/wap/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $gj_name . DS)) {
            $info = (include ROOT_PATH . $url . $gj_name . DS . 'Guojiz.php');
            $info['gj_name'] = $gj_name;
            return $info;
        }
    }
    public function cate()
    {
        $muban = array();
        $mubans = get_guojiz(ROOT_PATH . 'application/template/pc/' . DS);
        foreach ($mubans as $gj_name) {
            $muban[] = $this->guojiz($gj_name);
        }
        $this->assign('muban', $muban);
        $mubanw = array();
        $mubansw = get_guojiz(ROOT_PATH . 'application/template/wap/' . DS);
        foreach ($mubansw as $gj_name) {
            $mubanw[] = $this->guojizw($gj_name);
        }
        $this->assign('mubanw', $mubanw);
        return view();
    }
    //添加分类
    public function insertcate()
    {
        $data = $this->request->post();
        if (fun_W('banner_cate','in',input('in'))!=0) {
            $this->error('当前模板的广告位存在');
        }
         
        $data['time'] = time();
        if (!Db::name('banner_cate')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新分类
    public function updatecate()
    {
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('banner_cate')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //删除分类
	public function dels_cate(){
        $id = input('id');
        $data = $this->request->post();
        if (Db::name('banner_cate')->where('id', $id)->data($data)->delete($data)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
	}
	public function index()
    {
        $banner=new BannerModel();
        $tptc=$banner->where('in', input('in'))->order('fl desc')->paginate(15);
        $this->assign('tptc',$tptc);
        return view();
    }

	public function add()
    {
        $banner=new BannerModel();
		if(request()->isPost()){
			$data = input('post.');
			$data['time']=time();
			if($banner->add($data)){
				return json(array('code'=>200,'msg'=>'添加成功'));
			}else{
				return json(array('code'=>0,'msg'=>'添加失败'));
			}
		}
		$tptc=$banner->select();
        $this->assign('tptc',$tptc);
		return view();
    }

	public function edit()
    {
        $muban = array();
        $mubans = get_guojiz(ROOT_PATH . 'application/template/pc/' . DS);
        foreach ($mubans as $gj_name) {
            $muban[] = $this->guojiz($gj_name);
        }
        $this->assign('muban', $muban);
        $mubanw = array();
        $mubansw = get_guojiz(ROOT_PATH . 'application/template/wap/' . DS);
        foreach ($mubansw as $gj_name) {
            $mubanw[] = $this->guojizw($gj_name);
        }
        $this->assign('mubanw', $mubanw);
        $banner=new BannerModel();
        if(request()->isPost()){
            $data=input('post.');
            if($banner->edit($data)){
                return json(array('code'=>200,'msg'=>'修改成功'));
            }else{
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
        }
		$tptc=$banner->find(input('id'));
        $this->assign('tptc',$tptc);
        return view();
    }

	public function dels(){
		$banner=new BannerModel();
		if($banner->destroy(input('post.id'))){
			return json(array('code'=>200,'msg'=>'删除成功'));
		}else{
			return json(array('code'=>0,'msg'=>'删除失败'));
		}
	}

	public function delss(){
		$banner=new BannerModel();
		$params = input('post.');
		$ids = implode(',', $params['ids']);
		$result = $banner->batches('delete',$ids);
		if($result){
			return json(array('code'=>200,'msg'=>'批量删除成功'));
		}else{
			return json(array('code'=>0,'msg'=>'批量删除失败'));
		}
	}

	public function changeshow(){
        if(request()->isAjax()){
			$change=input('change');
			$show=db('banner')->field('show')->where('id',$change)->find();
			$show=$show['show'];
			if($show==1){
				db('banner')->where('id',$change)->update(['show'=>0]);
				echo 1;
			}else{
				db('banner')->where('id',$change)->update(['show'=>1]);
				echo 2;
			}
		}else{
            $this->error('非法操作');
		}
    }


}
