<?php

namespace App\Models;


use Carbon\Carbon;
use Dcat\Admin\Traits\HasDateTimeFormatter;

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

class SiteMenu extends Model
{
	use HasDateTimeFormatter;
    use ModelTree;

    protected $table = 'site_menu';

    protected $parentColumn = 'parent_id';

    // 排序字段名称，默认值为 order
    protected $orderColumn = 'order';

    // 标题字段名称，默认值为 title
    protected $titleColumn = 'title';

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'menu_config' => 'json', // 将 menu_config 字段转换为数组
    ];

    protected $fillable = [
        'parent_id',
        'order',
        'title',
        'uri',
        'show',
        'menu_type',
        'menu_config',
        'menu_location',
        'dist_id',
    ];


    /**
     * @return 批量生成菜单
     */
    public function createMenuBatch($data) {
        $parentIdArray = [];
        $i = 0;
        foreach ($data as $item) {
            $parentId = $item['parentId'] == 0 ? 0 : $parentIdArray[$item['parentId']];
            $i++;
            $row = $this->create([
                'parent_id' => $parentId,
                'title' => $item['title'],
                'order' => $i,
                'dist_id'=>$item['distId'],
                'uri' => $item['uri'],
                'menu_type' => $item['menuType'],
                'menu_config' => $item['menuConfig'],
                'menu_location' => $item['menuLocation'],//0顶部 1底部
                'show'  => 1,
                'created_at'=>Carbon::now(),
                'updated_at'=>Carbon::now(),
            ]);
            $parentIdArray[$i] = $row->id;
        }
    }


    public  function generateUri($menuType,$menuConfig,$distId,$isId = true) {
        switch ($menuType) {
            case 0: //选择产品
                $uri ="/";
                break;
            case 1: //集合
                if ($menuConfig['collections_type'] == 0) {
                    $distProductCategory = new DistProductCategory();
                    $row = $distProductCategory->where('dist_id', $distId)->where('id', $menuConfig['product_category'])->first();
                    //产品分类
                    $slug = $isId ? $row->id : $row->slug;
                    $uri = $row ? config('dictionary.site_url.product_category').'/'.$slug : "/";
                } else if ($menuConfig['collections_type'] == 1) {
                    $sitePagesTag = new SitePagesTag();
                    $row = $sitePagesTag->where('dist_id', $distId)->where('id', $menuConfig['pages_tag'])->first();
                    $slug = $isId ? $row->id : $row->slug;
                    //文章标签
                    $uri = $row ? config('dictionary.site_url.tags').'/'.$slug : "/";
                } else if ($menuConfig['collections_type'] == 2) {
                    $distProductCategory = new DistVideoCategory();
                    $row = $distProductCategory->where('dist_id', $distId)->where('id', $menuConfig['video_category'])->first();
                    $slug = $isId ? $row->id : $row->slug;
                    $uri = $row ? config('dictionary.site_url.video_category').'/'.$slug : "/";
                }
                break;
            case 2: //选择产品
                $distProduct = new DistProduct();
                $row = $distProduct->where('dist_id', $distId)->where('id', $menuConfig['product'])->first();
                $slug = $isId ? $row->id : $row->slug;
                $uri = $row ? config('dictionary.site_url.products').'/' .$slug : "/";
                break;
            case 3: //选择页面
                $sitePages = new SitePages();
                $row = $sitePages->where('dist_id', $distId)->where('id', $menuConfig['pages'])->first();
                $slug = $isId ? $row->id : $row->slug;
                $uri = $row ? config('dictionary.site_url.pages').'/'.$slug : "/";
                break;
            case 4: //选择页面
                $sitePages = new SitePages();
                $row = $sitePages->where('dist_id', $distId)->where('id', $menuConfig['landing_page'])->first();
                $slug = $isId ? $row->id : $row->slug;
                $uri = $row ? config('dictionary.site_url.pages').'/'.$slug : "/";
                break;
            case 5: //视频
                $distVideo = new DistVideo();
                $row = $distVideo->where('dist_id', $distId)->where('id', $menuConfig['videos'])->first();
                $slug = $isId ? $row->id : $row->slug;
                $uri = $row ? config('dictionary.site_url.video').'/'.$slug : "/";
                break;
            case 10: //url
                $uri = $menuConfig['url'];
                break;
        }
        return $uri;
    }



}
