<?php

namespace App\Http\Controllers;

use App\Models\Waste;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class WasteController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('waste.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $lastWaste = Waste::latest()->first();
        $wasteTypes = collect([
            ['id' => 'lost', 'title' => 'lost'],
            ['id' => 'destroyed', 'title' => 'destroyed'],
        ])->map(function ($item) {
            return (object) $item;
        });
        return view('waste.create', compact('lastWaste', 'wasteTypes'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'book_id'   => 'required|exists:books,id',
            'qty'   => 'required|numeric|min:1',
            'waste_type'   => 'required',
            'date'         => 'required|date',
            'details'    => 'nullable|string|max:500',
        ]);
        Waste::create($request->all());
        return redirect()->back()->with("msg", __('messages.record_submitted'));
    }

    /**
     * Display the specified resource.
     */
    public function show(Waste $waste)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($locale, Waste $waste)
    {
        $wasteTypes = collect([
            ['id' => 'lost', 'title' => 'lost'],
            ['id' => 'destroyed', 'title' => 'destroyed'],
        ])->map(function ($item) {
            return (object) $item;
        });
        return view('waste.edit', compact('waste', 'wasteTypes'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update($locale, Request $request, Waste $waste)
    {
        $waste->update($request->all());
        return redirect()->route('wastes.index')->with("msg", __('messages.record_updated'));
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($locale, Waste $waste)
    {
        $waste->delete();
        return redirect()->back()->with("msg", __('messages.record_deleted'));
    }

    public function dataTable()
    {
        $wastes = Waste::with(['book'])
            ->select('wastes.*')
            ->orderBy('wastes.id', 'desc');

        return DataTables::eloquent($wastes)
            ->filter(function ($query) {
                if (request()->has('search') && $search = request('search')['value']) {
                    $query->where(function ($q) use ($search) {
                        $q->where('wastes.waste_type', 'like', "%{$search}%")
                            ->orWhere('wastes.date', 'like', "%{$search}%")
                            ->orWhere('wastes.details', 'like', "%{$search}%")
                            ->orWhereHas('book', fn($q) => $q->where('title', 'like', "%{$search}%"));
                    });
                }
            })
            ->addColumn('action', function ($el) {
                return view('waste.partials.actions', compact('el'));
            })
            ->addColumn('book', function ($el) {
                return $el->book->title;
            })
            ->addColumn('waste_type', function ($el) {
                return $el->waste_type;
            })
            ->rawColumns(['action'])
            ->toJson();
    }
}
