<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;
use App\Http\Middleware\TenantMiddleware;

class CourseSubscription extends Component
{
    public $records, $name, $months, $month_day, $when_start, $enabled, $dataId, $update = false, $add = false;

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

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

    public $sortField ='name';
    public $sortAsc = true;
    public function boot()
    {
        app(TenantMiddleware::class)->setupTenantConnection();
    }
    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->months = 0;
        $this->month_day = 0;
        $this->when_start = '';
        $this->enabled = true;
        $this->emit('load-data-table');
    }

    public function render()
    {
        $this->records = \App\Models\CourseSubscription::select('id', 'name', 'enabled', 'months')->get();
        return view('livewire.course_subscription');
    }

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

    public function store()
    {
        $this->validate();
        try {
            \App\Models\CourseSubscription::create([
                'name' => $this->name,
                'months' => $this->months,
                'month_day' => $this->month_day,
                'when_start' => $this->when_start,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Dato creato');
            $this->resetFields();
            $this->add = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function edit($id){
        try {
            $course_subscription = \App\Models\CourseSubscription::findOrFail($id);
            if( !$course_subscription) {
                session()->flash('error','Dato non trovato');
            } else {
                $this->name = $course_subscription->name;
                $this->months = $course_subscription->months;
                $this->month_day = $course_subscription->month_day;
                $this->when_start = $course_subscription->when_start;
                $this->enabled = $course_subscription->enabled;
                $this->dataId = $course_subscription->id;
                $this->update = true;
                $this->add = false;
            }
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function update()
    {
        $this->validate();
        try {
            \App\Models\CourseSubscription::whereId($this->dataId)->update([
                'name' => $this->name,
                'months' => $this->months,
                'month_day' => $this->month_day,
                'when_start' => $this->when_start,
                'enabled' => $this->enabled
            ]);
            session()->flash('success','Dato 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\CourseSubscription::find($id)->delete();
            session()->flash('success',"Dato eliminato");
        }catch(\Exception $e){
            session()->flash('error','Errore (' . $e->getMessage() . ')');
        }
    }
}
