<?php

namespace App\Http\Controllers\Admin;

use App\Models\Tag;
use App\Models\CaseCate;
use App\Http\Requests\CateRequest;
use App\Models\CaseCateDescription;

class CaseCateController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $casecates = CaseCate::with(['pages','descriptions'])->get()->toHierarchy();

        return view('admin.case.case_cate', compact('casecates'));
    }

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

        return view('admin.case.case_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  = CaseCate::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 = CaseCate::attachSlug($result, $request->input('description.' . config('setting.lang') . '.cname'));

        if ($result) {
            // add 多语言
            CaseCateDescription::attachDescription($request->input('description'), $id, 'case_cid', true);
            event(new \App\Events\TranslateEvent('caseCate', $id));
            \App\Models\SystemLog::set("发布案例分类成功[id=". $id ."]");
//           return redirect('admin/case-cate')->with('success', '分类添加成功');
            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 = CaseCate::find($id);
        $tags     = Tag::lists('name', 'name');
        $tags     = CaseCate::tagCount($tags);
        $nest     = CaseCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);
        
        $descriptions = CaseCateDescription::where('case_cid', $id)->get()->groupBy('lang');

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

        return view('admin.case.case_cate_edit', compact('category', 'nest', 'descriptions', 'tags'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CateRequest $request, $id)
    {
        if (config('managerLang') == config('setting.lang')) {
            $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      = CaseCate::find($id);
        $result    = $cate->fill($data)->save();
        CaseCate::find($id)->retag($request->tags);

        CaseCate::attachSlug(CaseCate::find($id), config('managerLang') == config('setting.lang') ? $request->input('description.' . config('setting.lang') . '.cname') : CaseCateDescription::where('case_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;
           CaseCateDescription::attachDescription($description, $id, 'case_cid');
            \App\Models\SystemLog::set("编辑案例分类成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('caseCate', $id, true));
           
           return redirect('admin/case-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(CaseCate::canDelete($id, 'case_cid'));
    }

}
