<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Models\Course;
use Illuminate\Http\Request;

class BannerController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $query = Banner::get();
            return datatables($query)->toJson();
        }
        return view('admin.banner.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $lastBannerIndex = Banner::max('order');
        $lastBannerIndex = $lastBannerIndex ? $lastBannerIndex + 1 : 1;
        $courses = Course::get();
        return view('admin.banner.create', compact('lastBannerIndex', 'courses'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // return $request->all();
        $attributes = $request->validate([
            // 'screen' => 'required|string|max:255', 
            'type' => 'required|in:empty,course,lecture',
            'course_id' => 'nullable|exists:courses,id',
            'lecture_id' => 'nullable|exists:lectures,id',
            'order' => 'required|integer',
            'img' => 'required|string|max:255',
        ]);

        // Banner::create($attributes); // always create the order value "1" 
        $banner = new Banner();
        $banner->type = $request->type;
        $banner->order = $request->order;
        $banner->img = $request->img;

        if ($banner->type == 'empty') {
            $banner->course_id = null;
            $banner->lecture_id = null;
        } elseif ($banner->type == 'course') {
            $banner->course_id = $request->course_id;
            $banner->lecture_id = null;
        } elseif ($banner->type == 'lecture') {
            $banner->course_id = $request->course_id;
            $banner->lecture_id = $request->lecture_id;
        }

        $banner->save();

        return to_route('banners.index')->with('success', __('trans.alert.success.done_create', ['name' => 'Banner']));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Banner $banner)
    {
        $courses = Course::get();
        return view('admin.banner.edit', compact('banner', 'courses'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Banner $banner)
    {
        $attributes = $request->validate([
            // 'screen' => 'required|string|max:255',
            'type' => 'required|in:empty,course,lecture',
            'course_id' => 'nullable|exists:courses,id',
            'lecture_id' => 'nullable|exists:lectures,id',
            'order' => 'required|integer',
            'img' => 'required|string|max:255',
        ]);

        if ($request->type == 'empty') {
            $course_id = null;
            $lecture_id = null;
        } elseif ($request->type == 'course') {
            $course_id = $request->course_id;
            $lecture_id = null;
        } elseif ($request->type == 'lecture') {
            $course_id = $request->course_id;
            $lecture_id = $request->lecture_id;
        }

        Banner::where('id', $banner->id)->update([
            // 'screen'      => $request->screen,
            'type'      => $request->type,
            'course_id'      => $course_id,
            'lecture_id'      => $lecture_id,
            'img'        => $request->img,
            'order'          => $request->order,
        ]);
        return to_route('banners.index')->with('success', __('trans.alert.success.done_update', ['name' => 'Banner']));
    }
    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Banner $banner)
    {
        $banner->delete();
        return to_route('banners.index')->with('success', __('trans.alert.success.done_delete', ['name' => 'Banner']));
    }
}
