<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Causal extends Component
{
    public $recordsIn, $recordsOut, $parent_id,  $name, $enabled, $corrispettivo_fiscale, $no_receipt, $money, $user_status, $no_first, $no_records, $no_reports, $type, $dataId, $update = false, $add = false;

    public $corrispettivo_causal_id = 0;

    public $parent = '';

    protected $rules = [
        'name' => 'required',
        'type' => 'required'
    ];

    protected $messages = [
        'name.required' => 'Il nome è obbligatorio'
    ];

    public function mount(){

        if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
            return redirect()->to('/dashboard');

        $fisc = \App\Models\Causal::where('corrispettivo_fiscale', true)->first();
        if ($fisc)
            $this->corrispettivo_causal_id = $fisc->id;

    }

    public function resetFields(){
        $this->name = '';
        $this->parent_id = null;
        $this->parent = '';
        $this->type = null;
        $this->money = false;
        $this->corrispettivo_fiscale = false;
        $this->no_receipt = false;
        $this->user_status = false;
        $this->no_first = false;
        $this->no_records = false;
        $this->no_reports = false;
        $this->enabled = true;
    }

    public function render()
    {
        $this->recordsIn = \App\Models\Causal::where('parent_id', null)->where('type', 'IN')->get();
        $this->recordsOut = \App\Models\Causal::where('parent_id', null)->where('type', 'OUT')->get();
        return view('livewire.causal');
    }

    public function add()
    {
        $this->resetFields();
        $this->add = true;
        $this->update = false;
    }

    public function addLevel($parent_id)
    {
        $this->resetFields();
        $this->parent_id = $parent_id;
        $p = \App\Models\Causal::findOrFail($parent_id);
        $this->type = $p->type;
        $this->parent = $p->name;
        $this->add = true;
        $this->update = false;
    }

    public function store()
    {
        $this->validate();
        try {
            \App\Models\Causal::create([
                'name' => $this->name,
                'type' => $this->type,
                'parent_id' => $this->parent_id,
                'money' => $this->money,
                'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                'no_receipt' => $this->no_receipt,
                'user_status' => $this->user_status,
                'no_first' => $this->no_first,
                'no_records' => $this->no_records,
                'no_reports' => $this->no_reports,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Causale creata');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function edit($id){
        try {
            $causal = \App\Models\Causal::findOrFail($id);
            if( !$causal) {
                session()->flash('error','Causale non trovata');
            } else {
                $this->name = $causal->name;
                $this->money = $causal->money;
                $this->no_receipt = $causal->no_receipt;
                $this->user_status = $causal->user_status;
                $this->no_first = $causal->no_first;
                $this->no_records = $causal->no_records;
                $this->no_reports = $causal->no_reports;
                $this->enabled = $causal->enabled;
                $this->type = $causal->type;
                $this->parent_id = $causal->parent_id;
                $this->dataId = $causal->id;
                $this->update = true;
                $this->add = false;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function update()
    {
        $this->validate();
        try {
            \App\Models\Causal::whereId($this->dataId)->update([
                'name' => $this->name,
                'type' => $this->type,
                'parent_id' => $this->parent_id,
                'user_status' => $this->user_status,
                'no_first' => $this->no_first,
                'no_records' => $this->no_records,
                'no_reports' => $this->no_reports,
                'money' => $this->money,
                'no_receipt' => $this->no_receipt,
                'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Tessera aggiornata');
            $this->resetFields();
            $this->update = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function cancel()
    {
        $this->add = false;
        $this->update = false;
        $this->resetFields();
    }

    public function delete($id)
    {
        try{
            \App\Models\Causal::find($id)->delete();
            session()->flash('success',"Tessera eliminata");
            return redirect(request()->header('Referer'));
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function duplicate($id)
    {
        $old = \App\Models\Causal::find($id);
        $new = $old->replicate();
        $new->name = $new->name . ' - COPIA';
        $new->save();

        $this->duplicateRecursive($old, $new);
    }

    public function duplicateRecursive($old, $new)
    {
        foreach($old->childs as $c)
        {
            $old1 = \App\Models\Causal::find($c->id);
            $new1 = $old1->replicate();
            $new1->parent_id = $new->id;
            $new1->save();

            $this->duplicateRecursive($old1, $new1);

        }
    }

    public function reorder()
    {

    }

}
