<?php

namespace App\Http\Livewire;

use Livewire\Component;

class PaymentMethod extends Component
{
    public $records, $name, $enabled, $money, $type, $corrispettivo_fiscale, $dataId, $origin_id, $destination_id, $update = false, $add = false;

    public $origins = [];
    public $destinations = [];

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

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

    public $sortField ='name';
    public $sortAsc = true;

    public function sortBy($field)
    {
        if($this->sortField === $field)
        {
            $this->sortAsc = ! $this->sortAsc;
        } else {
            $this->sortAsc = true;
        }

        $this->sortField = $field;
    }

    public function resetFields(){
        $this->name = '';
        $this->money = false;
        $this->type = '';
        $this->corrispettivo_fiscale = false;
        $this->enabled = true;
        $this->emit('load-data-table');
    }

    public function mount(){

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

        $this->origins = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['OUT', 'ALL'])->get();
        $this->destinations = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['IN', 'ALL'])->get();
    }

    public function render()
    {
        $this->records = \App\Models\PaymentMethod::all();
        foreach($this->records as $r)
        {
            $r->origin = $r->origin ? $r->origin->name : '';
            $r->destination = $r->destination ? $r->destination->name : '';
        }
        /*if ($this->sortAsc)
            $this->records = $this->records->sortBy($this->sortField);
        else
            $this->records = $this->records->sortByDesc($this->sortField);*/
        return view('livewire.payment_method');
    }

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

    public function store()
    {
        $this->validate();
        try {
            \App\Models\PaymentMethod::create([
                'name' => $this->name,
                'origin_id' => $this->origin_id != "" ? $this->origin_id : null,
                'destination_id' => $this->destination_id != "" ? $this->destination_id : null,
                'money' => $this->money,
                'type' => $this->type,
                'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Metodo pagamento creato');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function edit($id){
        try {
            $payment_method = \App\Models\PaymentMethod::findOrFail($id);
            if( !$payment_method) {
                session()->flash('error','Metodo pagamento non trovato');
            } else {
                $this->name = $payment_method->name;
                $this->enabled = $payment_method->enabled;
                $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
                $this->money = $payment_method->money;
                $this->type = $payment_method->type;
                $this->origin_id = $payment_method->origin_id;
                $this->destination_id = $payment_method->destination_id;
                $this->dataId = $payment_method->id;
                $this->update = true;
                $this->add = false;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function update()
    {
        $this->validate();
        try {
            \App\Models\PaymentMethod::whereId($this->dataId)->update([
                'name' => $this->name,
                'origin_id' => $this->origin_id != "" ? $this->origin_id : null,
                'destination_id' => $this->destination_id != "" ? $this->destination_id : null,
                'money' => $this->money,
                'type' => $this->type,
                'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Metodo pagamento aggiornato');
            $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\PaymentMethod::find($id)->delete();
            session()->flash('success',"Metodo pagamento eliminato");
            return redirect(request()->header('Referer'));
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }
}
