<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller\v1;


use app\BaseController;
use app\model\system\Site as SiteModel;
use app\Request;
use library\services\UtilService;
use library\utils\MakeSite;

class Site extends  BaseController
{

    public function list(Request $request) {
        $pageSize = 50;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['name',''],
                ['i_sassid',''],
                ['artType','all']
            ],$request
        );
        $where = [];
        $where['artType'] =$post['artType'];
        $where['name'] = $post['name'];
        $where['i_sassid'] = $post['i_sassid'];
        list($pageCount,$data) = (new SiteModel)
            ->getList($post['page'],$where,$pageSize,'id desc');
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

    /**
     * 获取管理数据
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request) {
        [$sassid] = UtilService::getMore([
            ['sassid','','empty','参数错误']
        ],$request,true);
        $info = SiteModel::get(compact('sassid'))->toArray();
        return app('json')->success($info);
    }

    /**
     * 顶部数据
     * @param Request $request
     */
    public function top(Request $request){
      $allCount =   SiteModel::count();
      $openCount  = SiteModel::where("status",1)->count();
      $stopCount = SiteModel::where("status",0)->count();

      return app('json')->success(['allCount'=>$allCount,'openCount'=>$openCount,'stopCount'=>$stopCount]);
    }

    /**
     * 站点保存和修改
     */
    public function save(){
        $post = UtilService::getMore([
            ['logo',''],
            ['name',''],
            ['sassid','0'],
            ['domain',''],
            ['sub_domain','','empty','二级域名前缀不能为空'],
            ['lx_name',''],
            ['lx_tel',''],
            ['sys_seo_title',''],
            ['sys_seo_keyword',''],
            ['sys_seo_description',''],
            ['record',''],
            ['custom_qq',''],
            ['custom_tel',''],
            ['weixin_logo',''],
            ['icon_img',''],
            ['mono',''],
            ['status',0],
            ['is_transmatic',0],
            ['is_www',0]
        ]);
        $bool = (new SiteModel())->saveSite($post);
        if($bool)
            return app('json')->success("操作成功", []);
        else
            return app('json')->fail(SiteModel::getErrorInfo());
    }

    /**
     * 停运数据
     * @param Request $request
     */
    public function stop(Request $request) {
        [$sassid,$status] = UtilService::getMore(
            [
                ['sassid','0'],
                ['status',0]
            ],
            $request,true
        );
        SiteModel::where('sassid',$sassid)->save(['status'=>$status]);
        return app('json')->success("停运成功", []);
    }

    /**
     * 更新产品数据
     * @param Request $request
     */
    public function updatePro(Request $request) {
        [$sassid] = UtilService::getMore([
                ['sassid','0']
            ],
            $request,true
        );
        $site = (new \app\model\system\Site)->where('sassid',$sassid)->find();
        if(empty($site)) {
            return app('json')->fail('找不到站点');
        }
        $mSite = new MakeSite($sassid);
        $mSite->makeProduct();
        return app('json')->success("操作成功", []);
    }

    /**
     * 推荐
     * @param Request $request
     */
    public function tj(Request $request) {
        $post = UtilService::getMore(
            [
                ['sassid','0'],
                ['i_sassid','0','empty','参数错误']
            ]
            ,$request);

        if($post['sassid'] == $post['i_sassid']) {
            return app('json')->fail('自己无法挂载自己下面');
        }

        (new  \app\model\system\Site)->where('sassid',$post['i_sassid'])->save(['tj_sassid' =>$post['sassid']]);
        return app('json')->success('操作成功');
    }
}