<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-04-15
 * Time: 11:29
 */

namespace Modules\Admin\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\View;
use Modules\Admin\Auxiliary\View\FromAuxiliary;
use Modules\Admin\Auxiliary\View\TableAuxiliary;
use Modules\Admin\Auxiliary\View\TreeAuxiliary;
use Modules\Admin\Services\MenusService;
use Modules\Admin\Services\SettingService;
use Spatie\Permission\Models\Permission;

class BaseController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 默认布局模版
     * @var string
     */
    protected $layout = 'admin::layouts.sub';

    /**
     * 是否开启布局模版
     * @var boolean
     */
    protected $isLayout = true;

    /**
     * 高亮菜单
     * @var array
     */
    public $menusActive = ['adminDashboard', 'adminDashboardWelcome', ''];

    /**
     * 视图方法
     * @var
     */
    protected $layoutView;

    public function __construct()
    {
//        $this->middleware(['auth:admin', 'roles']);
        $this->middleware(['auth']);
        $this->setLayout($this->layout);
    }

    /**
     * 视图函数
     * @param string $view
     * @param array  $date
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function view($view = '', $date = [], $layoutMode = true)
    {
        // 获取并将用户信息视图共享
        $user = Auth::guard('web')->user();
        View::share('user', $user);

//        if (Auth::guard('web')->user()->hasRole('superAdmin')) {
//            $permissionsList = Permission::all()->pluck('route')->all();
//        } else {
//            $permissionsList = Auth::guard('web')->user()->getAllPermissions()->pluck('route')->all();
//        }
        //        dump($permissionsList);
        $permissionsList = [];
        View::share('permissionsList', $permissionsList);
        $date['isAjax'] = 0;
        if (Request::ajax()) {  // 判断Ajax请求
            $date['isAjax'] = 1;
            return view($view, $date);
        }

        // 获取并共享可见菜单信息
        $menus = new MenusService();
        $menus = $menus->getMenusList(1);
        View::share('menus', $menus);
        // 获取配置信息
//        $setting = new SettingService();
//        $setting = $setting->getList();
//        View::share('setting', $setting->pluck('value', 'name')->all());
        // 共享菜单高亮信息
        View::share('menusActive', $this->menusActive);
        if (!is_null($this->layout) && $this->isLayout && $layoutMode) {
            return $this->layoutView->nest('content', $view, $date);
        }
        return view($view, $date);
    }

    /**
     * 地址跳转
     * @param        $path
     * @param string $data
     * @return array|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    protected function redirect($path, $msg = '', $tableId = '')
    {
        if (Request::ajax()) {
            return [
                'code'    => 0,
                'message' => 'SUCCESS',
                'data'    => [
                    'url'   => $path,
                    'msg'   => $msg,
                    'table' => $tableId,
                ],
            ];
        }
        return redirect($path);
    }

    /**
     * 树型视图
     * @param TreeAuxiliary $treeObj
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tree(TreeAuxiliary $treeObj)
    {
        $data['path']                = $treeObj->path;
        $data['items']               = $treeObj->items;
        $data['columns']             = $treeObj->columns;
        $data['topActions']          = $treeObj->topActions;
        $data['actionBtns']          = $treeObj->actionBtns;
        $data['displayActionOthers'] = $treeObj->displayActionOthers;
        return $this->view('admin::component.template_tree_list', $data);
    }

    /**
     * 表格列表视图
     * @param TreeAuxiliary $treeObj
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tableList(TableAuxiliary $tableObj)
    {
        $data['path']           = $tableObj->path;
        $data['items']          = $tableObj->items;
        $data['columns']        = $tableObj->columns;
        $data['searchs']        = $tableObj->searchColumns;
        $data['primaryKey']     = $tableObj->primaryKey;
        $data['displayActions'] = $tableObj->displayActions;
        if (is_string($data['displayActions']['value'])) {
            $data['displayActions']['value'] = [$data['displayActions']['value']];
        }
        $data['actionBtns']          = $tableObj->actionBtns;
        $data['actionBtnsAttribute'] = $tableObj->actionBtnsAttribute;
        $data['topActions']          = $tableObj->topActions;
        $data['displayActionOthers'] = $tableObj->displayActionOthers;
        $data['topActionOthers']     = $tableObj->topActionOthers;
        $data                        += Request::except('_token');
        return $this->view('admin::component.template_table_list', $data);
    }

    /**
     * 表格单独视图
     * @param TreeAuxiliary $treeObj
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function tableView(TableAuxiliary $tableObj)
    {
        $data['items']   = $tableObj->items;
        $data['columns'] = $tableObj->columns;
        $data['title']   = $tableObj->title;
        return $this->view('admin::component.template_table_view', $data);
    }

    /**
     * 表单视图
     * @param FromAuxiliary $fromObj
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function from(FromAuxiliary $fromObj)
    {

        $data['path']       = '/admin/' . $fromObj->path;
        $data['fromTitle']  = $fromObj->title;
        $data['columns']    = $fromObj->columns;
        $data['item']       = $fromObj->item;
        $data['hasHr']      = $fromObj->hasHr;
        $data['primaryKey'] = $fromObj->primaryKey;
        $data['footBtns']   = $fromObj->footBtns;
        $data['colWidth']   = $fromObj->colWidth;
        if(strpos($data['path'],'camera') !== false){
            return $this->view('camera::camera_add', $data);
        }elseif(strpos($data['path'],'mine') !== false){//path包含mine
            return $this->view('camera::mine_add', $data);
        }else{
            return $this->view('admin::component.template_from', $data);
        }

//
    }

    /**
     * 设置布局模版
     * @param null $layout
     */
    public function setLayout($layout = null)
    {
        if (!is_null($layout) && $this->isLayout) {
            $this->layoutView = view($layout);
        }
    }

    protected function error($code = 1, $msg = '', $url = '', $time = 3)
    {
        if (Request::ajax()) {
            return [
                'code'    => $code,
                'message' => $msg,
                'url'     => $url,
            ];
        }
        $data['msg'] = $msg;
        $data['url'] = $url;
        if (empty($url)) {
            $data['url'] = url()->previous();
        }
        $data['time'] = $time;
        return $this->view('admin::layouts.error', $data);
    }

    protected function success($data = '', $msg = null, $url = '', $time = 3)
    {
        if (Request::ajax()) {
            $result = [
                'code'    => 0,
                'message' => $msg ?? 'SUCCESS',
                'data'    => $data,
            ];
            if (!empty($url)) {
                $result['url'] = $url;
            }
            return $result;
        }
        $_data['url'] = $url;
        if (empty($url)) {
            $_data['url'] = url()->previous();
        }
        $_data['data'] = $data;
        $_data['time'] = $time;
        return $this->view('admin::layouts.success', $_data);
    }

}
