<?php

namespace App\Http\Controllers\Admin;

use App\Models\Tag;
use App\Models\DownloadCate;
use App\Http\Requests\CateRequest;
use App\Models\DownloadCateDescription;

class DownloadCateController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $downloadcates = DownloadCate::with(['pages','descriptions'])->get()->toHierarchy();
        
        return view('admin.download.download_cate', compact('downloadcates'));
    }

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

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

        if ($result) {
           // add 多语言
           DownloadCateDescription::attachDescription($request->input('description'), $id, 'download_cid', true);
            event(new \App\Events\TranslateEvent('downloadCate', $id));
            \App\Models\SystemLog::set("发布下载分类成功[id=". $id ."]");
//           return redirect('admin/download-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     = DownloadCate::find($id);
        $tags         = DownloadCate::tagCount();
        $categoryNest = DownloadCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);
        $descriptions = DownloadCateDescription::where('download_cid', $id)->get()->groupBy('lang');

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

        return view('admin.download.download_cate_edit', compact('category', 'categoryNest', '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      = DownloadCate::find($id);
        $result    = $cate->fill($data)->save();
        DownloadCate::find($id)->retag($request->tags);
        DownloadCate::attachSlug(DownloadCate::find($id), config('managerLang') == config('setting.lang') ? $request->input('description.' . config('setting.lang') . '.cname') : DownloadCateDescription::where('download_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;
            DownloadCateDescription::attachDescription($description, $id, 'download_cid');
            \App\Models\SystemLog::set("编辑下载分类成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('downloadCate', $id, true));

            return redirect('admin/download-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(DownloadCate::canDelete($id, 'download_cid'));
    }

}
