<?php

namespace App\Models;

use App\Models\traits\SetConnection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;

class MstPrefecture extends Model
{
    use SetConnection;

    public $timestamps = false;

    protected $guarded = [];

    public static function getData($code = null, $company_id = null)
    {
	$user = Auth::user();
    	$prefectures = MstPrefecture::query();

	if(!empty($code)) {
	    $prefectures->where('code', $code);
	}

	if($user) {
	   $company_id = $user->company_id;
	}

	if(!empty($company_id)) {
	    $prefectures->where('company_id', $company_id);
	}

	return $prefectures->select('code', 'name')->orderBy('order')
			->get()->toArray();
    }

    public static function getName($prefCode=null)
    {
        $city_name = "";
        if($prefCode != null){
            $city_name = MstPrefecture::select('name')->where('code', '=', $prefCode)->first();
        }
        return $city_name;
    }
}
