<?php

/*
 * https://www.we-rc.com/blog/2015/07/19/nested-set-model-practical-examples-part-i
 */
namespace App\Http\Controllers\Admin\Page;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use App\Models\Menu;

class MenuController extends Controller
{
    private Menu $menuModel;

    public function __construct(Menu $menu)
    {
        $this->menuModel = $menu;
    }

    /**
     * 메뉴 관리
     * @method GET
     * @see /admin/page/menu
     */
    public function index()
    {
        $menuData = $this->menuModel->data();

        if ($menuData->rows > 0) {
            foreach ($menuData->list as $i => $row) {
                $row->upURL = route('admin.page.menu.up', $row->id);
                $row->downURL = route('admin.page.menu.down', $row->id);

                $menuData->list[$i] = $row;
            }
        }

        return view('admin.page.menu.index', [
            'actionURL' => route('admin.page.menu.store'),
            'menuData' => $menuData
        ]);
    }

    /**
     * 메뉴 관리 저장
     * @method POST
     * @see /admin/page/menu
     */
    public function store(Request $request)
    {
        $rules = [
            'menu_id' => 'nullable|numeric',
            'name' => 'string',
            'link' => 'string',
            'target' => 'string|nullable',
            'desktop' => 'numeric|in:1,0',
            'mobile' => 'numeric|in:1,0',
            'custom' => 'string|nullable',
            'icon' => 'string|nullable',
            'depth' => 'numeric'
        ];

        $attributes = [
            'menu_id' => '메뉴 PK',
            'name' => '메뉴명',
            'link' => 'URL',
            'target' => '새창 여부',
            'desktop' => 'PC',
            'mobile' => '모바일',
            'custom' => '속성',
            'icon' => '아이콘',
            'depth' => '분류 깊이'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);

        $this->menuModel->register($posts);

        $message = '메뉴 정보가 저장되었습니다.';
        return redirect()->route('admin.page.menu.index')->with('message', $message);
    }

    /**
     * 메뉴 관리 수정
     * @method PUT
     * @see /admin/page/menu/{pk}
     */
    public function update(Request $request)
    {
        $posts = $request->all();

        if ($posts['chk']) {
            foreach ($posts['chk'] as $menuID) {
                $updateData = [
                    'menu_id' => $menuID,
                    'parent_id' => ($posts['parent_id'][$menuID] ?? ''),
                    'name' => ($posts['name'][$menuID] ?? ''),
                    'link' => ($posts['link'][$menuID] ?? ''),
                    'target' => ($posts['target'][$menuID] ?? ''),
                    'desktop' => ($posts['desktop'][$menuID] ?? ''),
                    'mobile' => ($posts['mobile'][$menuID] ?? ''),
                    'custom' => ($posts['custom'][$menuID] ?? ''),
                    'icon' => ($posts['icon'][$menuID] ?? ''),
                    'depth' => ($posts['depth'][$menuID] ?? '')
                ];

                $rules = [
                    'menu_id' => 'required|numeric|exists:tb_menu,id',
                    'parent_id' => 'required|numeric|exists:tb_menu,parent_id',
                    'name' => 'string',
                    'link' => 'string',
                    'target' => 'numeric|in:1,0',
                    'desktop' => 'numeric|in:1,0',
                    'mobile' => 'numeric|in:1,0',
                    'custom' => 'string|nullable',
                    'icon' => 'string|nullable',
                    'depth' => 'numeric'
                ];

                $attributes = [
                    'menu_id' => '메뉴 PK',
                    'parent_id' => '부모 메뉴 PK',
                    'name' => '메뉴명',
                    'link' => 'URL',
                    'target' => '새창 여부',
                    'desktop' => 'PC',
                    'mobile' => '모바일',
                    'custom' => '속성',
                    'icon' => '아이콘',
                    'depth' => '분류 깊이'
                ];

                $validator = Validator::make($updateData, $rules, [], $attributes);
                if ($validator->fails()) {
                    return back()->withErrors($validator)->withInput();
                }
                $this->menuModel->updater($updateData);
            }
        }

        $message = '메뉴 정보가 변경되었습니다.';
        return redirect()->route('admin.page.menu.index')->with('message', $message);
    }

    /**
     * 메뉴 관리 삭제
     * @method DELETE
     * @see /admin/page/menu/destroy
     */
    public function destroy(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $menuID) {
                $this->menuModel->remove($menuID);
            }
        }

        $message = '메뉴 정보가 삭제되었습니다.';
        return redirect()->route('admin.page.menu.index')->with('message', $message);
    }

    /**
     * 메뉴 한 단계 위로
     * @method GET
     */
    public function up(int $menuID)
    {
        $this->menuModel->orderUp($menuID);

        return redirect()->route('admin.page.menu.index');
    }

    /**
     * 메뉴 한 단계 아래로
     * @method GET
     */
    public function down(int $menuID)
    {
        $this->menuModel->orderDown($menuID);

        return redirect()->route('admin.page.menu.index');
    }
}
