<?php
declare (strict_types = 1);
namespace app\system\controller\v1;

use app\BaseController;
use app\model\system\NewsCate as NewsCateModel;
use app\model\system\News as NewsModel;
use app\Request;
use library\services\UtilService;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class News extends  BaseController{
    /**
     * 获取新闻列表
     * @param Request $request
     * @return type
     */
    public function getNews(Request $request){
        $post = $request->post();
        $pageSize = 20;
//        $cate_id = $post['cate_id'];
        $is_show = $post['is_show'];
        $page = empty($post['page'])?1:$post['page'];
        $keyword = $post['keyword'];
        $news = new NewsModel();
        $where = array();
//        if(!empty($cate_id)){
//            $where[]=["n.cate_id","=",$cate_id];
//        }
        if($is_show==='0'){
            $where[]=["n.is_show","=",0];
        }
        if($is_show==='1'){
            $where[]=["n.is_show","=",1];
        }
        if(!empty($keyword)){
            $where[]=["n.title","like","%{$keyword}%"];
        }

        $data = $news
                ->field("n.*,c.title as cate_title")
                ->alias("n")
                ->join("news_cate c","c.id = n.cate_id","left")
                ->where($where)
                ->order("n.time desc")
                ->paginate(['list_rows'=>$pageSize,'page'=>$page])
                ->toArray();
        foreach($data['data'] as $k=>$v){
            $data['data'][$k]["time"] = date("Y-m-d H:i:s");
        }
        return app('json')->success([
            'data'      => $data['data'],
            'pageCount' => $data['total'],
            'pageSize'  => $pageSize
        ]);
    }
    /**
     * 新闻内容保存
     * @param Request $request
     */
    public function newsSave(Request $request){
        $news = new NewsModel();
        $post = UtilService::getMore([
            ['title','','empty','请输入标题'],
            ['stitle',''],
            ['cate_id',0,'empty','请选择分类'],
            ['content',''],
            ['imgAr',[]],
            ['is_tui',0],
            ['is_hot',0],
            ['id',0],
            ['is_show',0]
        ], $request);
        $post['img'] = join(',',$post['imgAr']);
        unset($post['imgAr']);
        $news->newsSave($post);
        return app('json')->success("数据保存成功");
    }
    /**
     * 获取新闻详情
     */
    public function getNewsItem(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','数据不存在']
        ],$request,true);
        $data = (new NewsModel())->find(compact('id'))->toArray();
        $data["time"] = date("Y-m-d H:i:s",$data["time"]);
        return app('json')->success($data);
    }
    /**
     * 删除新闻
     * @param Request $request
     */
    public function newsDel(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','删除数据不存在']
        ],$request,true);
        $news = new NewsModel();
        $news->newsDel($id);
        return app('json')->success("数据已删除");
    }
    /**
     * 新闻状态
     * @param Request $request
     * @return type
     */
    public function newsShowSet(Request $request){
        [$id,$is_show] = UtilService::getMore([
            ['id','0','empty','参数错误'],
            ['is_show','0']
        ],$request,true);
        $bool = (new NewsModel())->newsShowSet($id,$is_show);
        if($bool) {
            return app('json')->success('操作成功');
        } else {
            return app('json')->fail('提交失败');
        }
    }
    /**
     * 获取树级分类
     */
    public function getCateTree() {
        $cateAr = (new NewsCateModel())->order("seq","desc")->select()->toArray();
        $data = sort_list_tier($cateAr, '顶级', 'pid', 'id');
        return app('json')->success($data);
    }
    
    /**
     * 获取分类
     * @return type
     */
    public function getCate(){
        $newsCate = new NewsCateModel();
        $newsCateData = $newsCate->getArCate();
        return app('json')->success($newsCateData);
    }
    
    /**
     * 保存数据
     */
    public function cateSave(Request $request){
        $post = UtilService::getMore([
            ['title',''],
            ['pid','0'],
            ['seq','0'],
            ['is_show','0'],
            ['code',''],
            ['id','0']
        ],$request);
        (new NewsCateModel())->cateSave($post);
        return app('json')->success("数据保存成功");
    }
    
    public function getCateItem(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $data = (new NewsCateModel())->field("title,id,is_show,pid,code,seq")->find(compact('id'))->toArray();
        return app('json')->success($data);
    }
    
    public function cateDel(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $bool =   (new NewsCateModel())->cateDel($id);
        return app('json')->success("分类删除成功");
    }
    
    public function cateShowSet(Request $request){
        [$id,$is_show] = UtilService::getMore([
            ['id','0','empty','参数错误'],
            ['is_show','0']
        ],$request,true);
        $bool = (new NewsCateModel())->cateShowSet($id,$is_show);
        if($bool) {
            return app('json')->success('操作成功');
        } else {
            return app('json')->fail('提交失败');
        }
    }
}