<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class DatesFestius extends Model{
    protected $table = 'dates_festius';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'data_inici', 'data_fi'];
    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['data_inici', 'data_fi'];
    //per desactivar camps a la taula update at i created at
    public $timestamps = false;

    /** Insertar periode de dates
     * @param Collection $dades
     * @return $this
     */
    public function insertDatesFestius(Collection $dades){
        $this->data_inici = $dades->get('dataInici');
        $this->data_fi = $dades->get('dataFI');
        $this->descripcio = $dades->get('descripcio');
        $this->save();
        return $this;
    }

    /** Llistar dates emagatzemades
     * @return mixed
     */
    public function llistarDates(){
        return $this->get();
    }

    /** Cerca periode dates
     * @param $data
     * @return mixed
     */
    public function existeixDataBetwenDates($data){
        return $this->where('data_inici','<=',$data)->where('data_fi','>=',$data)->get();
    }

    /** Borrar periode dates
     * @param $id
     * @return mixed
     */
    public function borrarDatesFestius($id){
        return $this->where('id', $id)->delete();
    }
}

