<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Campaign;

class CampaignController extends Controller
{

    /**
     * 创建活动
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $id = $request->input('id');
        $title = $request->input('title');
        $content = $request->input('content');
        $status = $request->input('status');
        if ($id) {
            $campaign = Campaign::find($id);
            $campaign->status = $status;
        } else {
            $campaign = new Campaign;
            $campaign->status = 0;
        }
        if ($title && $content) {
            $campaign->title = $title;
            $campaign->content = $content;
            $campaign->save();
            return Response()->json([
                "status" => 0,
                "message" => "保存成功!"
            ]);
        } else {
            return Response()->json([
                "status" => -1,
                "message" => "信息不能为空!"
            ]);
        }
    }

    /**
     * 每页20进行分页
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        return Campaign::paginate(20);
    }
}
