<?php

namespace App\Models;

use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use App\Models\City;
use App\Utils;

class RegularType extends Model
{
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    const RegularClass = [
        '有効',
        '無効',
    ];

    protected $table = 'regular_type';
    protected $primaryKey = 'regular_type_id';

    protected $fillable = [
        'regular_type_id',
        'city_id',
        'regular_class_1',
        'regular_class_2',
        'regular_class_3',
        'regular_class_6',
        'regular_class_12',
        'memo',
        'operator_id',
    ];

    public static function boot()
    {
        parent::boot();
        self::creating(function (RegularType $model) {
            $model->operator_id = Auth::user()->ope_id;
        });
    }

    public static function search($inputs)
    {
        $list = self::query();

        if ($inputs['isMethodPost']) {
            // 検索条件
        }

        if ($inputs['sort']) {
            $list->orderBy($inputs['sort'], $inputs['sort_type']);
        }

        if ($inputs['isExport']) {
            $list = $list->get();
        } else {
            $list = $list->paginate(Utils::item_per_page);
        }

        return $list;
    }

    public static function getById($id)
    {
        return self::find($id);
    }

    public static function deleteById($id)
    {
        return self::where('regular_type_id', $id)->delete();
    }

    public function getCity()
    {
        return $this->belongsTo(City::class, 'city_id', 'city_id')->first();
    }
}
