<?php

namespace App\Http\Controllers\Admin;

use App\Models\Block;
use App\Http\Requests\BlockRequest;
use App\Models\BlockDescription as Description;

class BlockController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $blocks = Block::latest()->paginate(20);

        return view('admin.block.block', compact('blocks'));
    }

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

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

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

        if ($result) {
            Description::attachDescription($request->input('description'), $result->id, 'block_id', true);
            event(new \App\Events\TranslateEvent('block', $result->id));
            \App\Models\SystemLog::set("发布说明页成功[id=". $result->id ."]");
            return redirect('admin/block')->with('success', '说明页添加成功');
        }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $block = Block::find($id);
        $descriptions = Description::where('block_id', $id)->get()->groupBy('lang');

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

        return view('admin.block.block_edit', compact('block', 'descriptions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param BlockRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(BlockRequest $request, $id)
    {
        $result = Block::find($id)->update($request->except('slug'));

        if ($result) {
            $description = $request->input('description');
            $description[config('managerLang')]['imgs'] = $description[config('managerLang')]['imgs'] ?? null;
            Description::attachDescription($description, $id, 'block_id');
            \App\Models\SystemLog::set("编辑说明页成功[id=". $id ."]");
            event(new \App\Events\TranslateEvent('block', $id, true));
            return redirect($request->input('referer') ?: 'admin/block')->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 = Block::deletes($id, 'block_id');

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

}
