<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

class ProductType extends Model
{
	use HasDateTimeFormatter,ModelTree;
    protected $table = 'product_types';

    // 父级ID字段名称，默认值为 parent_id
    protected $parentColumn = 'pid';
    // 排序字段名称，默认值为 order
    protected $orderColumn = 'sort';

    // 标题字段名称，默认值为 title
    protected $titleColumn = 'zh_name';
//    public function __construct()
//    {
//        $lang = request()->session()->get('admin.config.lang');
//        if($lang){
//            dd($lang);
//            switch ($lang){
////            case 'en':// 英文
////                $lang = 'en';
////                break;
//                case 'ko':// 韩语
//                    $this->titleColumn = 'ko_name';
//                    break;
//                default:// 中文
//                    $this->titleColumn = 'zh_name';
//                    break;
//            }
//        }
//    }

    /**
     * @param $pid
     * @return array
     * 获取父级ID
     */
    public static function level($pid = 0){
            $list = self::query()->orderByDesc('sort')->orderByDesc('id')->pluck('pid','id');
            return self::superior($list,$pid);
    }
    /**
     * 得到当前位置父辈数组
     * @param int
     * @return array
     */
    public static function superior( $column = null, $id = 0 ){
        $list = [];
        foreach($column as $k=>$v){
            if($k == $id){
                $list[] = $k;
                $list = array_merge(self::superior($column,$v),$list);
            }
        }
        return $list;
    }

    protected $fillable =[
        'icon',
        'pid',
        'level',
        'zh_name',
        'ko_name',
        'sort',
        'route_id',
        'status',
        'is_upload_display',
        'is_filter_display',
        'zh_alias',
        'ko_alias',
        'fill_id'
    ];
}
