<?php

namespace App\Http\Controllers\Admin;

use App\Models\Tag;
use App\Models\VideoCate;
use App\Http\Requests\CateRequest;
use App\Models\VideoCateDescription;
use Illuminate\Support\Facades\Validator;

class VideoCateController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $videocates = VideoCate::with(['pages','descriptions'])->get()->toHierarchy();

        return view('admin.video.video_cate', compact('videocates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response    
     */
    public function create()
    {
        $categoryNest = VideoCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);
        $tags         = VideoCate::tagCount();

        return view('admin.video.video_cate_create', compact('categoryNest', 'tags'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CateRequest $request)
    {
        if (config('managerLang') != config('setting.lang')) {
            return back()->withInput()->with('error', '请在主语言添加内容！');
        } else {
            $request->merge(['cname' => $request->input('description.' . config('setting.lang') . '.cname')]);
        }

        $result  = VideoCate::create($request->except('description'));
        $result->tag($request->tags);
        $id = $result->id;
        
        $result  = $request->input('parent_id') ? $result->makeChildOf($request->input('parent_id')) : $result->makeRoot();
        $result = VideoCate::attachSlug($result, $request->input('description.' . config('setting.lang') . '.cname'));

        if ($result) {
           // add 多语言
           VideoCateDescription::attachDescription($request->input('description'), $id, 'video_cid', true);
            event(new \App\Events\TranslateEvent('videoCate', $id));
            \App\Models\SystemLog::set("发布视频分类成功[id=". $id ."]");

            return back()->with('success', '分类添加成功');
        }

        return back()->withInput()->with('error', '分类添加失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category     = VideoCate::find($id);
        $categoryNest = VideoCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);
        $tags         = VideoCate::tagCount();
        $descriptions = VideoCateDescription::where('video_cid', $id)->get()->groupBy('lang');

        if (!isset($descriptions[config('managerLang')])) $descriptions[config('managerLang')] = $descriptions[config('setting.lang')];

        return view('admin.video.video_cate_edit', compact('category', 'categoryNest', 'descriptions', 'tags'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param CateRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(CateRequest $request, $id)
    {
        $request->merge(['cname' => $request->input('description.' . config('setting.lang') . '.cname')]);
        $data      = $request->except('description');

        if ($data['parent_id'] == $id) {
            return back()->withInput()->with('error', '上级分类不能是自身');
        }

        $cate      = VideoCate::find($id);
        $result    = $cate->fill($data)->save();
        VideoCate::find($id)->retag($request->tags);
        VideoCate::attachSlug(VideoCate::find($id), config('managerLang') == config('setting.lang') ? $request->input('description.' . config('setting.lang') . '.cname') : VideoCateDescription::where('video_cid', $id)->where('lang', config('setting.lang'))->value('cname'));

        if ($result) {
            $data['parent_id'] ? $cate->makeChildOf($data['parent_id']) : $cate->makeRoot();
            // 添加多语言
            $description = $request->input('description');
            $description[config('managerLang')]['img'] = $description[config('managerLang')]['img'] ?? null;
            VideoCateDescription::attachDescription($description, $id, 'video_cid');
            \App\Models\SystemLog::set("编辑视频分类成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('videoCate', $id, true));
            
            return redirect('admin/video-cate')->with('success', '分类修改成功');
        }

        return back()->withInput()->with('error', '分类修改失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return response()->json(VideoCate::canDelete($id, 'video_cid'));
    }


}
