<?php

namespace App\Distributor\Repositories;

use Dcat\Admin\Traits\ModelTree;
use App\Models\SiteMenu as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class SiteMenu extends EloquentRepository
{

    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    /*
     * 只能显示2层
     */
    public static function selectOptions(\Closure $closure = null)
    {
        if (!$closure) {
            $closure = function ($query) {
                $query =  $query->where('dist_id', getDistributorId())->orderBy('order', 'asc');
                return $query;
            };
        }
        $selectOptions =  Model::class::selectOptions($closure);
        //只显示2层
        foreach ($selectOptions as $key => $value) {
            if (substr_count($value,'&nbsp;') > 8) {
                unset($selectOptions[$key]);
            }
        }
        return $selectOptions;
    }

    public static function showAllSelectOptions($siteDomain = '') {
        $model = new Model();
        $allMenu = $model->where('dist_id', getDistributorId())->orderBy('order', 'asc')->get();
        $arrayMenu = [];
        foreach ($allMenu as $menu) {
            $arrayMenu[$menu->id] = $menu;
        }

        $topSelectOptions =  Model::class::selectOptions(function ($query) {
            $query =  $query->where('menu_location',0)->where('dist_id', getDistributorId())->orderBy('order', 'asc');
            return $query;
        });

        $bottomSelectOptions =  Model::class::selectOptions(function ($query) {
            $query =  $query->where('menu_location',1)->where('dist_id', getDistributorId())->orderBy('order', 'asc');
            return $query;
        });

        $selectOptions = [];
        foreach ($topSelectOptions as $key => $value) {
            if ($key == 0) {
                $selectOptions[] = ['id'=>-1,'title'=>admin_trans_label('top_menu'),'url'=>''];
            } else {
                $url = $siteDomain == '' ? $arrayMenu[$key]['uri'] : $siteDomain.$arrayMenu[$key]['uri'];
                $selectOptions[] = ['id'=>$key,'title'=>$value,'url'=>$url];
            }
        }

        foreach ($bottomSelectOptions as $key => $value) {
            if ($key == 0) {
                $selectOptions[] = ['id'=>-2,'title'=>admin_trans_label('bottom_menu'),'url'=>''];
            } else {
                $url = $siteDomain == '' ? $arrayMenu[$key]['uri'] : $siteDomain.$arrayMenu[$key]['uri'];
                $selectOptions[] = ['id'=>$key,'title'=>$value,'url'=>$url];
            }
        }
        return $selectOptions;
    }

    /*
    * 获取一个标签
    */
    public static function getOneById($id)
    {
        return Model::where('id', $id)->where('dist_id', getDistributorId())->first();
    }

    public static function createMenuBatch($data) {
        $model = new Model();
        return $model->createMenuBatch($data);
    }

    //生成uri
    public static function generateUri($menuType,$menuConfig,$isId = true) {
        $distId = getDistributorId();
        $model = new Model();
        return $model->generateUri($menuType,$menuConfig,$distId,$isId);
    }

}
