<?php

namespace App\Http\Controllers;

use App\Models\SiteAlbumFolder;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    public $foldersTree =  [];

    public function __construct()
    {
        // 一次性查询所有文件夹
        $folders = SiteAlbumFolder::all();
        // 构建父子关系树
        $fid = isset($_GET['fid'])? $_GET['fid'] : 0;
        $result = SiteAlbumFolder::buildTree($folders);
        $result = $result->toArray();
        foreach ($result as $key => $item) {
            $checked = false;
            foreach ($item['children'] as $child) {
                if ($fid == $child['id']) {
                    $checked = true;
                }
                foreach ($child['children'] as $subChild) {
                    if ($fid == $subChild['id']) {
                        $checked = true;
                    }
                }
            }
            if ($fid == $item['id']) {
                $checked = true;
            }
            $result[$key]['checked'] = $checked;
        }

        //dd($result);
        $this->foldersTree = $result;




    }
}
