<?php

namespace App\Http\Controllers\Admin;

use App\Models\Page;
use App\Models\PageCate;
use Illuminate\Http\Request;
use App\Models\PageCateDescription as Description;

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

        return view('admin.page.page_cate', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.page.page_cate_create');
    }

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

        $result  = PageCate::create($request->all());

        if ($result) {
            Description::attachDescription($request->input('description'), $result->id, 'page_cid', true);
            event(new \App\Events\TranslateEvent('pageCate', $result->id));
            \App\Models\SystemLog::set("发布自定义页面分类成功[id=". $result->id ."]");
            return back()->with('success', '栏目添加成功');
        }

        return back()->withInput($request->all())->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)
    {
        $data     = PageCate::find($id);
        $descriptions = Description::where('page_cid', $id)->get()->groupBy('lang');

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

        return view('admin.page.page_cate_edit', compact('data', 'descriptions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->has('is_product_detail') ?: $request->merge(['is_product_detail' => 0]);
        $request->merge(['cname' => $request->input('description.' . config('setting.lang') . '.cname')]);
        
        $result = PageCate::find($id)->fill($request->all())->save();
        PageCate::attachSlug(PageCate::find($id), config('managerLang') == config('setting.lang') ? $request->input('description.' . config('setting.lang') . '.cname') : Description::where('page_cid', $id)->where('lang', config('setting.lang'))->value('cname'));

        if ($result) {
            $description = $request->input('description');
            $description[config('managerLang')]['img'] = $description[config('managerLang')]['img'] ?? null;
            Description::attachDescription($description, $id, 'page_cid');
            \App\Models\SystemLog::set("编辑自定义页面分类成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('pageCate', $id, true));
            return redirect('admin/page-cate')->with('success', '修改栏目成功');
        }

        return back()->withInput($request->all())->with('error', '修改栏目失败');
    }

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



}
