<?php

namespace App\Http\Controllers\Admin;

use App\Models\Page;
use App\Models\PageCate;
use Illuminate\Http\Request;
use App\Http\Requests\PageRequest;
use App\Models\PageDescription as Description;

class PageController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = $request->except('page');
        $query = Page::with(['cates', 'description']);
        $categories = PageCate::lists('cname', 'id')->toArray();
        $pid = $request->input('pid') ?: ($categories ? PageCate::first()->value('id') : null);

        if ($pid) $query->where('pid', $pid);

        $data = $query->orderBy('orders', 'DESC')->latest()->paginate(20);

        return view('admin.page.page', compact('data', 'categories', 'pid', 'where'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $pid = $request->has('pid') ? $request->input('pid') : null;
        $category = PageCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);

        return view('admin.page.page_create', compact('pid', 'category'));
    }

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

        $result = Page::create($request->all());
   
        if ($result) {
            Page::attachSlug($result, $request->input('description.' . config('setting.lang') . '.title'));
            Description::attachDescription($request->input('description'), $result->id, 'page_id', true);
            event(new \App\Events\TranslateEvent('page', $result->id));
            \App\Models\SystemLog::set("发布自定义页面成功[id=". $result->id ."]");

            return redirect('admin/page/create?pid=' . $request->input('pid'))->with('success', '添加页面成功');
        }

        return back()->withInput($request->all())->with('error', '添加失败');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $data         = Page::find($id);
        $category     = PageCate::getNestedList('cname', 'id', $this->seperator, '├ ', true);
        $descriptions = Description::where('page_id', $id)->get()->groupBy('lang');
        $pid = $request->has('pid') ? $request->input('pid') : null;

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

        return view('admin.page.page_edit', compact('data','category', 'descriptions', 'pid'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param PageRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(PageRequest $request, $id)
    {
        $result = Page::find($id)->fill($request->all())->save();
        Page::attachSlug(Page::find($id), config('managerLang') == config('setting.lang') ? $request->input('description.' . config('setting.lang') . '.title') : Description::where('page_id', $id)->where('lang', config('setting.lang'))->value('title'));

        if ($result) {
            $description = $request->input('description');
            $description[config('managerLang')]['img'] = $description[config('managerLang')]['img'] ?? null;
            Description::attachDescription($description, $id, 'page_id');
            \App\Models\SystemLog::set("编辑自定义页面成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('page', $id, true));

            return redirect($request->input('referer') ?: 'admin/page')->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)
    {
        $result = Page::deletes($id, 'page_id');

        return response()->json(['status' => $result ? 'sucess' : 'error']);
    }


}
