<?php

namespace App\Http\Main\Users;

use App\Models\Alert;
use App\Models\Drive;
use App\Models\Team;
use Livewire\Component;

class Show extends Component
{

    public $drive;
    public $alert;
    public $exists;

    public function mount($id)
    {

        $this->alert = Alert::latest()->first();
        $drive = Drive::find($id);
        if ($drive->user_id == auth()->user()->id) {
            $this->drive = $drive;
        } else if (auth()->user()->currentTeam->id == 1 || auth()->user()->currentTeam->id == 2) {
            $this->drive = $drive;
        } else {
            return abort(404);
        }

        if (count(Drive::where('serial_no', $this->drive->serial_no)->get()) > 1)
            $this->exists = true;
    }

    public function render()
    {

        $drives = Drive::orWhere('user_id', $this->drive->user_id)->get();

        if (auth()->user()->currentTeam->id == 1) {
            return view('main.users.show', [
                'drives' => $drives
            ])->layout('layouts.app-engineers');
        } else {
            return view('main.users.show', [
                'drives' => $drives
            ])->layout('layouts.app-frontdesk');
        }
    }
}
