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

use app\BaseController;
use app\model\api\AdverPage;
use app\model\api\Advert;
use app\model\api\Upsource;
use app\Request;
use library\services\UtilService;
use library\utils\Qiniu;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | [ 广告管理 ]
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class Adver extends  BaseController{

    /**
     * 分类列表数据
     */
    public function page(Request $request){
        [$cate_name,$pid,$is_show] = UtilService::getMore([
            ['cate_name',''],
            ['pid','0'],
            ['is_show','0']
        ],$request,true);
        $menuMenu = new AdverPage();
        $menus = $menuMenu->getArMenu($cate_name,$pid,$is_show);
        return app('json')->success($menus);
    }

    /**
     * 获取树级分类
     */
    public function page_tree() {
        $menuAr = (new AdverPage())->order("sort","desc")->select()->toArray();
        $data = sort_list_tier($menuAr, '顶级', 'pid', 'id');
        return app('json')->success($data);
    }

    /**
     * 获取详情栏目数据
     */
    public function page_info(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $data = (new AdverPage())->find(compact('id'))->toArray();
        return app('json')->success($data);
    }

    /**
     * 保存数据
     */
    public function page_save(Request $request){
        $post = UtilService::getMore([
            ['cate_name','','empty','商品分类'],
            ['pid','0'],
            ['sort','0'],
            ['id','0'],
            ['type','1'],
            ['max_count',0],
            ['json','[]'],
            ['mono','']
        ],$request);
        (new AdverPage())->savePage($post);
        return app('json')->success("数据保存成功");
    }

    /**
     * 栏目删除
     * @param Request $request
     */
    public function page_del(Request $request) {
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $bool =   (new AdverPage())->delAdver($id);
        return app('json')->success("栏目删除成功");
    }




    public function list(Request $request) {
        $pageSize = 50;
        [$page,$page_id,$title,$sassid] =  UtilService::getMore(
            [
                ['page',1],
                ['page_id','0'],
                ['title',''],
                ['sassid','0']
            ],$request,true
        );
        $where = compact('page_id','title','sassid');
        list($pageCount,$data) = (new Advert)
            ->getList($page,$where,$pageSize,'id desc');
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page
        ]);
    }



    /**
     * 获取详情栏目数据
     */
    public function info(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $data = (new Advert())->find(compact('id'))->toArray();
        return app('json')->success($data);
    }

    /**
     * 保存数据
     */
    public function save(Request $request){
        $post = UtilService::getMore([
            ['title','','empty','广告'],
            ['page_id','0'],
            ['sort','0'],
            ['sex',0],
            ['id','0'],
            ['text',''],
            ['url',''],
            ['code',''],
            ['img',''],
            ['data',''],
            ['is_show','0']
        ],$request);
        (new Advert())->savePage($post);
        return app('json')->success("数据保存成功");
    }

    /**
     * 栏目删除
     * @param Request $request
     */
    public function del(Request $request) {
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $bool =   (new Advert)->where("id",$id)->delete();
        return app('json')->success("栏目删除成功");
    }
    
    
    
    
    
    public function upsourceList(Request $request) {
        $post =  UtilService::getMore([
            ['page',1],
            ['pageSize', 50],
            ['title',''],
            ['type',''],
            ['status',''],
        ],$request);
        $data = (new Upsource)->getListData($post);
        return app('json')->success([
            'list'      => $data["list"],
            'pageCount' => $data["totalCount"],
            'pageSize'  => $data["pageSize"],
            'page'      => $data["page"],
        ]);
    }
    
    
    public function upsourceSave(Request $request){
       $post = UtilService::getMore([
            ['id',''],
            ['url',''],
            ['title',''],
            ['type','img'],
        ],$request);
        $id = $post["id"];
        unset($post["id"]);
        $r=0;
        if(empty($id) || $id=="0"){
            $post["time"]   = time();
            $post["status"] = 1;
            $r = (new Upsource)->insert($post);
        }else{
            $r = (new Upsource)->where("id",$id)->update($post);
        }
        if($r){
            return app('json')->success("数据保存成功");
        }else{
            return app('json')->fail("数据保存失败");
        }
    }
    /**
     * 删除资源图片
     * @param Request $request
     */
    public function upsourceDel(Request $request){
        $post = UtilService::getMore([
            ['id','',"empty","参数错误"],
        ],$request);
        $data = (new Upsource)->where("id",$post["id"])->find();
        if(empty($data)){
            return app('json')->fail("数据错误");
        }
        //        $qiniu = new Qiniu;
        //        $qiniu->
        (new Upsource)->where("id",$post["id"])->update(["is_del"=>1]);
        return app('json')->success("删除成功");
    }
    
    

}