<?php

namespace App\Http\Main\Engineers\Drive;

use App\Models\Team;
use App\Models\User;
use Livewire\Component;
use Livewire\WithFileUploads;

class Create extends Component
{
    use WithFileUploads;

    public $firstname;
    public $lastname;
    public $companyname;
    public $phone;
    public $email;
    public $reason;
    public $amount_paid;
    public $drive_name;
    public $serial_no;
    public $model_no;
    public $status = 'Yet to assess';
    public $iaea_no;
    public $users;
    public $new_user;
    // public $image;

    protected $messages = [
        'serial_no.unique' => 'This serial no already exists',
        
    ];
    

    public $step = 1;




    protected function rules()
    {
        if ($this->new_user != '[New User]' && $this->new_user != null) {
            return [
                'email' => 'required|email',
                // 'image' => 'image|max:1024',
                'serial_no' => 'unique:drives'

            ];
        } else {
            return [
                'email' => 'required|email',
                // 'image' => 'image|max:1024',
                'phone' => 'required|unique:users',
                'serial_no' => 'unique:drives'

            ];
        }
    }



    public function mount()
    {
        $this->iaea_no = bin2hex(random_bytes(6));
        $this->users = Team::find(3)->users;
    }

    public function updated()
    {

        $this->validate();
    }

    public function check()
    {
        $this->render();
    }

    public function updatedNewUser()
    {
        if ($this->new_user != '[New User]' && $this->new_user != null) {
        } else {
            $this->firstname = '';
            $this->lastname = '';
            $this->companyname = '';
            $this->phone = '';
            $this->email = '';
        }
    }

    public function render()
    {


        if ($this->new_user != '[New User]' && $this->new_user != null) {

            $user = User::find($this->new_user);
            $this->firstname = $user->first_name;
            $this->lastname = $user->last_name;
            $this->companyname = $user->company_name;
            $this->phone = $user->phone;
            $this->email = $user->email;
        }
        return view('main.engineers.drive.create')->layout('layouts.app-engineers');
    }
}
