<?php

namespace App\Http\Controllers\Admin;

use App\Models\Lang;
use App\Models\CoreEvent;
use Illuminate\Http\Request;

class CoreEventController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where    = $request->except('page');
        $events = CoreEvent::with('langs');
        $events = $request->has('lang') ? $events->where('lang', $where['lang']) : $events;
        $events = $request->has('event') ? $events->where('event', $where['event']) : $events;
        $events = $request->has('device') ? $events->where('device', $where['device']) : $events;

        $events = $events->latest()->paginate(20);
        $langs = Lang::where('buy', 1)->where('enabled', 1)->oldest('orders')->lists('name', 'slug')->toArray();

        return view('admin.core_event.event', compact('events', 'where', 'langs'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $ids    = array_str($id);
        $result = CoreEvent::whereIn('id', $ids)->delete();

        if ($result) {
            flash('success', '删除成功！');
            return response()->json(['code' => 200]);
        }

        return response()->json(['code' => 500]);
    }

}
