<?php

namespace App\Http\Controllers\Api;

use App\Models\MiniBlock;
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class MiniBlockController extends BaseController
{

    // 创建
    public function create(Request $request) {
        $data = MiniBlock::create([
            'title' => $request->input('title'),
            'description' => $request->input('description'),
            'image' => $request->input('image')
        ]);

        return $this->response($data);
    }

    public function getMiniBlock(Request $request) {
        $data = MiniBlock::findOrFail($request->input('id'));
        return $this->response($data);
    }

    // 列表
    public function list(Request $request) {
        $data = MiniBlock::query()->orderBy('id','desc')->paginate(10);
        return $this->response($data);
    }

    // 更新
    public function update(Request $request) {
        $model = MiniBlock::findOrFail($request->input('id'));

        if(!empty($request->input('title'))) $model->title =$request->input('title');
        if(!empty($request->input('description'))) $model->description =$request->input('description');
        if(!empty($request->input('image'))) $model->image =$request->input('image');

        $model->save();

        return $this->response($model);
    }

    // 禁用启用
    public function showHide(Request $request) {
        $model = MiniBlock::findOrFail($request->input('id'));

        $model->status = !$model->status;
        $model->save();

        return $this->response($model);
    }

    // 删除
    public function delete(Request $request) {
        $model = MiniBlock::findOrFail($request->input('id'));
        $model->delete();
        return $this->response($model);
    }
}

