<?php

namespace App;

use App\Http\Controllers\MqttSubscribeController;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * App\VizitkaDefine
 *
 * @property int $idVizitky
 * @property int|null $id_stanice
 * @property string|null $sloupec_velicina
 * @property int|null $zobrazit
 * @property int|null $luser
 * @property string|null $zmena
 * @method static Builder|VizitkaDefine newModelQuery()
 * @method static Builder|VizitkaDefine newQuery()
 * @method static Builder|VizitkaDefine query()
 * @method static Builder|VizitkaDefine whereIdStanice($value)
 * @method static Builder|VizitkaDefine whereIdVizitky($value)
 * @method static Builder|VizitkaDefine whereLuser($value)
 * @method static Builder|VizitkaDefine whereSloupecVelicina($value)
 * @method static Builder|VizitkaDefine whereZmena($value)
 * @method static Builder|VizitkaDefine whereZobrazit($value)
 * @mixin Eloquent
 */
class VizitkaDefine extends Model
{
    protected $table = 'vizitky';
    protected $primaryKey = 'idVizitky';
    protected $fillable = ['id_stanice', 'sloupec_velicina','zobrazit', 'luser'];
    public $timestamps = false;

    public static function getDataVizitky($id_stanice, $sloupec_velicina)
    {
        return self::where(
                                [
                                    ['id_stanice','=', $id_stanice],
                                    ['sloupec_velicina', '=', $sloupec_velicina]
                                ]
                            )->first();
    }

    public static function saveDataVizitky($id_stanice,$sloupec_velicina): void
    {
        $db = new self(['id_stanice'=> $id_stanice,'sloupec_velicina' => $sloupec_velicina, 'zobrazit' => 1, 'luser' => MqttSubscribeController::SUBSCRIBEUSER]);
        $db->save();
    }

    public static function getModalSelect($idStanice)
    {
        $data = self::where(
                                [
                                    ['id_stanice','=',$idStanice],
                                    ['zobrazit','=',1]
                                ]

                            )->get();
        $dataSelect = [];

        foreach ($data as $id => $dataVizitky)
        {
            $d = PrevodHodnot::getVelicinaAJendotky($idStanice, $dataVizitky['sloupec_velicina']);
            if(empty($d))
            {
                $dataSelect[] = $dataVizitky->toArray();
            }
            else
            {
                $dataSelect[] = (array)$d[0];
            }
        }
        return $dataSelect;
    }

}
