<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\equipment;
use DB;

class EquipmentController extends Controller
{
    public function create(){

        if(auth()->user() != null && auth()->user()->fireStationID != null ){
            $equipment = DB::table('equipment')->where("fireStationID", '=', auth()->user()->fireStationID)
                ->whereNull('deleted_at')->paginate(10);
            return view("equipment", ["equipment" => $equipment]);
        } else{
            return view('equipment');
        }

    }

    public function addForm(){
        if(auth()->user() != null && auth()->user()->accessLevel() == 50 ){ //prezes,naczelnik
                return view('equipmentAdd');

        } else return redirect()->to('/sprzet');
    }

    public function editForm($id)
    {
        if(auth()->user() != null && auth()->user()->accessLevel() == 50 ) //prezes,naczelnik
        {

            $equipment = DB::table('equipment')->where("id", $id)->first();

                return view('equipmentEdit', ["equipment" => $equipment]);
        }
        else
            return redirect()->to('/sprzet');

    }

    public function store(){
        $this->validate(request(), [

            'name' => 'required|max:45',
            'amount' => 'required|numeric|digits_between:1,10',
            'parameter' => 'max:45|nullable',
        ],
            [
                'required' => ':attribute jest wymagany(a).',
                'name.required' => 'nazwa jest wymagany(a).',
                'name.max' => 'nazwa musi mieć nie więcej niż :max znaków.',
                'max' => ':attribute musi mieć nie więcej niż :max znaków.',
                'numeric' => ':attribute powinna zawierać tylko cyfry.',
                'digits_between' => ':attribute jest za duży(a)'
            ]);


        $request = request();
        $equipment = equipment::create([
            'fireStationID' => auth()->user()->fireStationID,
            'name' => $request-> name,
            'amount' => $request-> amount,
            'parameter' => $request-> parameter,
        ]);

        return redirect()->to('/sprzet');
    }

    public function update(){
        $this->validate(request(), [

            'name' => 'required|max:45',
            'amount' => 'required|numeric|digits_between:1,10',
            'parameter' => 'max:45|nullable',
        ],
            [
                'required' => ':attribute jest wymagany(a).',
                'name.required' => 'nazwa jest wymagany(a).',
                'name.max' => 'nazwa musi mieć nie więcej niż :max znaków.',
                'max' => ':attribute musi mieć nie więcej niż :max znaków.',
                'numeric' => ':attribute powinna zawierać tylko cyfry.',
                'digits_between' => ':attribute jest za duży(a)'
            ]);


            $request = request();
            $equipment = equipment::find( $request->equipmentID);
            $equipment-> name = $request-> name;
            $equipment-> amount = $request-> amount;
            $equipment-> parameter = $request-> parameter;
            $equipment->save();

        return redirect()->to('/sprzet');
    }

    public function destroy($id)
    {
        equipment::where('id',$id)->delete();

        return response()->json([
            'success' => 'Record deleted successfully!'
        ]);
    }

    public function createAllEquipmentPDF(){
        if (auth()->user() != null && auth()->user()->fireStationID != null) {
            $request = request();
            $test = new documentCreators();
            $test->createAllEquipmentPDF($request->fireStationID);
        }
    }
}
