<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Supply;
use App\Models\Family;
use App\Models\familyUnit;
use App\Models\suppliesUnit;

class Supplies extends Component
{
    public $suppliers, $search = '', $data, $families;
    public $name, $code, $family, $location, $file, $image, $description, $family_id, $supplier_id, $supply_id;
    public $familyunits, $units, $listunit;
    public $updateMode = false;
    public $imputActive = false;
    protected $rules = ['family_id' => "required"];

    public function render()
    {
        $this->families = Family::All();
        $this->data = ($this->search)
        ? Supply::where('id', 'like', '%'.$this->search.'%')
            ->orWhere('name', 'like', '%'.$this->search.'%')
            ->orWhere('code', 'like', '%'.$this->search.'%')
            ->orWhere('location', 'like', '%'.$this->search.'%')
            ->get()
        : Supply::orderBy('id', 'DESC')->get();
        return view('livewire.supplies');
    }

    public function resetInput()
    {
        $this->name         = null;
        $this->code         = null;
        $this->family      = null;
        $this->location       = null;
        $this->description = null;
        $this->image = null;
        $this->file = null;
        $this->updateMode = false;
        // $this->emitUpdates();
    }

    public function save()
    {
        if ($this->updateMode)
            return $this->update();

        return $this->store();
    }

    public function store()
    {
        $this->validate([
            'name'   => 'required',

        ]);
        Supply::create([
            'name'          => $this->name,
            'code'          => $this->code,
            'family_id'     => $this->family_id,
            'location'      => $this->location,
            'description'   => $this->description
        ]);
        $this->emit('notify:toast', ['type'  => 'success', 'name' => 'Registro creado...']);
        $this->resetInput();
    }

    public function edit($id)
    {
        $record             = Supply::findOrFail($id);
        $this->supply_id    =  $id;
        $this->name         = $record->name;
        $this->code         = $record->code;
        $this->family_id         = $record->family_id;
        $this->location        = $record->location;
        $this->description         = $record->description;
        if(count($record->units)){
            $this->familyunits = $record->units;
        }else{
            $this->familyunits = Family::find($record->family_id)->units;
        }
        // $this->file = $record->file;
        // $this->image = $record->image;

        // $this->emitUpdates();

        $this->updateMode = true;

        $this->emit('notify:toast', ['type'  => 'success', 'name' => 'Registro cargado...']);
    }

    public function update()
    {
        if ($this->supply_id) {
            $record = Supply::find($this->supply_id);
            $record->update([
                'name' => $this->name,
                'code' => $this->code,
                'family_id' => $this->family_id,
                'location' => $this->location,
                'description' => $this->description
            ]);
            suppliesUnit::where('supplies_id', $record->id)->delete();
            foreach($this->listunit as $key => $unit){
                suppliesUnit::create([
                    'supplies_id' => $record->id,
                    'unit_id'   => $key,
                    'valor' => $unit
                ]);
            }
            return redirect()->back()->with('message', 'Registro actualizado...');
            $this->resetInput();
            $this->updateMode = false;
        }
    }

    public function destroy($id)
    {
        if ($id) {
            $record = Supply::find($id);
            $record->delete();

            $this->emit('notify:toast', ['type'  => 'success', 'name' => 'Registro eliminado...']);
            //$this->resetInput();
        }
    }

    public function generatecode()
    {
        $supply = Supply::findOrFail($this->supply_id);
        $mycode = '';
        foreach($supply->units as $units){
            $familyUnit = familyUnit::where('family_id', $supply->family_id)->where('unit_id', $units->id)->first();
            if($familyUnit->skuy == 1) {
                $mycode .= $units->name.$units->pivot->valor;
            }
        }
        $this->code = $mycode;
    }
}
