<?php

namespace App\Distributor\Repositories;

use App\Models\DistProductCategory as Model;

use App\Models\DistProductParameter;
use App\Traits\DistSlugTrait;
use Dcat\Admin\Repositories\EloquentRepository;
use Illuminate\Support\Facades\Session;
use function Symfony\Component\Translation\t;

class DistProductCategory extends EloquentRepository
{
    use DistSlugTrait;
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    public function __construct()
    {

    }

    // 调用模型方法
    public static function selectMainOptions(\Closure $closure = null)
    {
        // 获取当前登录的分销商/经销商
        $distributor=Session::get('distributor');

        // 过滤分销商
        if (!$distributor)
        {
            return false;
        }

        $query = Model::query();

        //通过传入条件限制
        if ($closure) {
            $closure($query);
        }

        // 确保查询构造器不为空
        if (!$query) {
            return [];
        }

        // 过滤分销商
        $query->where('dist_id', getDistributorId());

        // 返回键值对数组，并按 'name' 和 'order' 字段排序
        $results = $query->orderBy('order', 'desc')->orderBy('id', 'desc')->pluck('name', 'id');

        // 在每个 name 前面加上 '├─'
        $results = $results->map(function ($name) {
            return '├─' . $name;
        });

        $all = array_replace([0 => admin_trans_label('root')],$results->all());
        return $all;
    }

    // 调用模型方法
    public static function selectOptions(\Closure $closure = null)
    {
        if (!$closure) {
            $closure = function ($query) {
                $query = $query->where('dist_id', getDistributorId());
                $query = $query->where('enabled', 1);
                $query =  $query->orderBy('order', 'desc')->orderBy('id', 'desc');
                return $query;
            };
        }

        $selectOptions = Model::class::selectOptions($closure);
        return $selectOptions;
    }

    public static function selectOptionsShowTow(\Closure $closure = null)
    {
        if (!$closure) {
            $closure = function ($query) {
                $query = $query->where('dist_id', getDistributorId());
                $query = $query->where('enabled', 1);
                $query =  $query->orderBy('order', 'desc')->orderBy('id', 'desc');
                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 getParameter($categoryId)
    {
        $categoryRow =  Model::Class::find($categoryId);
        if ($categoryRow->baseProductParameter) {
            return $categoryRow->baseProductParameter->content;
        } else {
            return "[]";
        }
    }

    /**
     * @param Grid\Model|\Dcat\Admin\Grid\Model $model
     * @return array
     */
    public function get(Grid\Model|\Dcat\Admin\Grid\Model $model)
    {

        // 获取模型对象
        $obj = $this->model();

        $obj = $obj->where('dist_id', getDistributorId())->orderBy('order', 'desc')->orderBy('id', 'desc');

        // 获取数据
        $tree = $obj->get();

        // 构建树形结构
        $data=buildTree($tree->toArray());
        // 转换成一维数组
        $data=flattenTree($data);

        return $model->makePaginator(
            count($data), // 传入总记录数
            $data // 传入数据二维数组
        );
    }



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