<?php
/*
 *	www.myucms.com
 *  作者：梦雨
 *  @ QQ50361804
*/
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Links as LinksModel;
class Links extends Common
{
    
	
	public function index()
    {
        $links=new LinksModel();
      
      
      $type=input('type');

      if ($type == 'index' || $type == '') {	
 
       $gzc=$links->order('id desc')->where('fl=1')->paginate(15);

      } elseif ($type == '2') {	
        
        $gzc=$links->order('id desc')->where('fl=2')->paginate(15);
        

      } elseif ($type == '3') {	
        
        $gzc=$links->order('id desc')->where('fl=3')->paginate(15);

      } elseif ($type == '4') {	
        
        $gzc=$links->order('id desc')->where('fl=4')->paginate(15);
        
      }
      
        $this->assign('gzc',$gzc);
       print s(); return view();
    }
  
 

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

	public function edit()
    {
        $links=new LinksModel();
        if(request()->isPost()){
            $data=input('post.');
            if($links->edit($data)){
                return json(array('code'=>200,'msg'=>'修改成功'));
            }else{
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
        }
		$gzc=$links->find(input('id'));
        $this->assign('gzc',$gzc);
         print s();return view();
    }

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

	public function delss(){
		$links=new LinksModel();
		$params = input('post.');
		$ids = implode(',', $params['ids']);
		$result = $links->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('links')->field('show')->where('id',$change)->find();
			$show=$show['show'];
			if($show==1){
				db('links')->where('id',$change)->update(['show'=>0]);
				echo 1;
			}else{
				db('links')->where('id',$change)->update(['show'=>1]);
				echo 2;
			}
		}else{
            $this->error('非法操作');
		}
    }

}
