<?php

namespace App\Models;

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

class MstStore extends Model
{
    use SetConnection;

    protected $fillable = ['code', 'company_id', 'name', 'name_kana' ,
        'zip', 'address1', 'address2', 'address3', 'address4',
        'freedial', 'tel', 'fax', 'email', 'business_hours',
        'suumo_id', 'suumo_pw', 'homes_id', 'homes_pw', 'athome_id', 'athome_pw',
        'holiday', 'parking', 'comment', 'obi', 'disp', 'created_at', 'updated_at', 'last_user_edit',
        'suumo_linked', 'homes_linked', 'athome_linked', 'homes_org_name', 'homes_org_checked'
    ];

    public static function getDataList($cid, $getbyAuth = false)
    {
        $res[] = ['code' => '', 'name' => '未選択'];
        if(!$getbyAuth) {
            $data = MstStore::where('company_id', '=', $cid)->get();
        } else {
            $user = Auth::user();
            $data = MstStore::where('code', '=', $user->store_id)->get();
        }
        return array_merge($res, $data->toArray());
    }

    public static function getDataAll($cid, $getbyAuth = false)
    {
        $res[] = ['code' => '', 'name' => '未選択'];
        if(!$getbyAuth) {
            $data = MstStore::where('company_id', '=', $cid)->get();
        } else {
            $user = Auth::user();
            $data = MstStore::where('code', '=', $user->store_id)->get();
        }
        foreach($data as $row) {
            $res[] = ['code' => $row->code, 'name' => $row->name];
        }

        return $res;
    }

    public static function getDataByArea($cid, $area)
    {
        $res = [];
        $store = MstStore::query();
        $store->select('mst_stores.*', 'mst_prefectures.name as pref_name', 'mst_cities.name as city_name', 'mst_towns.name as town_name');
        $store->leftJoin('mst_prefectures', 'mst_prefectures.code', '=', 'mst_stores.address1')
                 -> leftJoin('mst_cities', 'mst_cities.code', '=', 'mst_stores.address2')
                 -> leftJoin('mst_towns', function($join) {
                     $join->on('mst_towns.pref_code', '=', 'mst_stores.address1')
                        ->on('mst_towns.city_code', '=', 'mst_stores.address2')
                        ->on('mst_towns.code', '=', 'mst_stores.address3');
                 });
        //$store->where('company_id', '=', $cid);
        if($area != 999) {
            $store->where('address2', '=', $area);
        } else {
            $store->whereNotIn('address2', [111, 115,107,108,105,204]);
            $store->orWhereNull('address2');
        }

        $res = $store->get();
        return $res;
    }

    public static function getStoreListToTop($cid, $pref, $city) {
        $store = MstStore::query();
        $store->where('company_id', $cid);
        $store->where('address1', $pref);
        if($city != 999) {
            $store->where('address2', $city);
        } else {
            $store->whereNotIn('address2', [111, 115, 107, 108, 105,204, 999]);
        }
        $store->orderby('id');
        $result = $store->get();

        return $result;
    }

    public static function getMaxNo($cid) {
        $result = MstStore::where('company_id', $cid)->max('code');

        return $result;
    }

    public static function getDataByCode($cid, $code)
    {
        $store = MstStore::query();
        $store->select('mst_stores.*', 'mst_prefectures.name as pref_name', 'mst_cities.name as city_name', 'mst_towns.name as town_name');
        $store->leftJoin('mst_prefectures', 'mst_prefectures.code', '=', 'mst_stores.address1')
                 -> leftJoin('mst_cities', 'mst_cities.code', '=', 'mst_stores.address2')
                 -> leftJoin('mst_towns', function($join) {
                     $join->on('mst_towns.pref_code', '=', 'mst_stores.address1')
                        ->on('mst_towns.city_code', '=', 'mst_stores.address2')
                        ->on('mst_towns.code', '=', 'mst_stores.address3');
                 });
        $result = $store->where('mst_stores.code', '=', $code)->first();
        return $result;
    }

    public static function getDataByCodeFront($cid, $code)
    {
        $store = MstStore::query();
        $store->select('mst_stores.*', 'mst_prefectures.name as pref_name', 'mst_cities.name as city_name', 'mst_towns.name as town_name');
        $store->leftJoin('mst_prefectures', 'mst_prefectures.code', '=', 'mst_stores.address1')
                 -> leftJoin('mst_cities', 'mst_cities.code', '=', 'mst_stores.address2')
                 -> leftJoin('mst_towns', function($join) {
                     $join->on('mst_towns.pref_code', '=', 'mst_stores.address1')
                        ->on('mst_towns.city_code', '=', 'mst_stores.address2')
                        ->on('mst_towns.code', '=', 'mst_stores.address3');
                 });
        $store->where('disp', '=', 1);
        $result = $store->where('mst_stores.code', '=', $code)->first();
        return $result;
    }

    public static function getDataByCodeFrontByKana($cid, $code)
    {
        $store = MstStore::query();
        $store->select('mst_stores.*', 'mst_prefectures.name as pref_name', 'mst_cities.name as city_name', 'mst_towns.name as town_name');
        $store->leftJoin('mst_prefectures', 'mst_prefectures.code', '=', 'mst_stores.address1')

                 -> leftJoin('mst_cities', 'mst_cities.code', '=', 'mst_stores.address2')
                 -> leftJoin('mst_towns', function($join) {
                     $join->on('mst_towns.pref_code', '=', 'mst_stores.address1')
                        ->on('mst_towns.city_code', '=', 'mst_stores.address2')
                        ->on('mst_towns.code', '=', 'mst_stores.address3');
                 });
        $store->where('disp', '=', 1);
        $result = $store->where('mst_stores.name_kana', '=', $code)->first();
        return $result;
    }

    public static function getDataById($id)
    {
        $res = MstStore::where('id', '=', $id)->first();
        return $res;
    }

    public static function getStoreCode($id)
    {
        $res = MstStore::where('code', '=', $id)->first();
        return $res;
    }

    public static function getDataByCompany($cid) {
        $store = MstStore::query();
        $store->select('mst_stores.*', 'mst_prefectures.name as pref_name', 'mst_cities.name as city_name', 'mst_towns.name as town_name');
        $store-> leftJoin('mst_prefectures', 'mst_prefectures.code', '=', 'mst_stores.address1')
                 -> leftJoin('mst_cities', function($join) {
                    $join->on('mst_cities.pref_code', '=', 'mst_stores.address1')
                       ->on('mst_cities.code', '=', 'mst_stores.address2');
                })
                 -> leftJoin('mst_towns', function($join) {
                     $join->on('mst_towns.pref_code', '=', 'mst_stores.address1')
                        ->on('mst_towns.city_code', '=', 'mst_stores.address2')
                        ->on('mst_towns.code', '=', 'mst_stores.address3');
                 });
        $store->where('mst_stores.company_id', $cid);
        $result = $store->orderby('mst_stores.code')->get();
        return $result;
    }

    public static function getData($cid) {
        $store = MstStore::query();
        $result = $store->where('disp', '=', 1)->orderby('code', 'desc')->get();

        return $result;
    }

    public static function getIdByName($name) {
        $store = MstStore::query();
        $result = $store->where('name', '=', $name)->first();

        return $result;
    }

    public function prefecture()
    {
        return $this->belongsTo(MstPrefecture::class, 'address1',  'code');
    }

    public function city()
    {
        return $this->belongsTo(MstCity::class, 'address2',  'code')
            ->where('pref_code', $this->attributes["address1"]);
    }

    public function town()
    {
        return $this->belongsTo(MstTown::class, 'address3',  'code')
            ->where('pref_code', $this->attributes["address1"])
            ->where('city_code', $this->attributes["address2"]);
    }

    public function getFirstLineObi(){
        if (!isset($this->obi)){
            return '';
        }
        $arr = explode('<br />', nl2br($this->obi));
        return $arr[0] ?? '';
    }

    public function getAddressInfoAttribute(){
        $address = "";
        if (isset($this->prefecture)) {
           $address .= $this->prefecture->name;
        }
        if (isset($this->city)) {
            $address .= $this->city->name;
        }
        if (isset($this->town)) {
            $address .= $this->town->name.$this->town->chome_name.$this->town->koaza_name;
        }
        if (isset($this->address4)) {
            $address .= $this->address4;
        }
        if ($address === ''){
            $address = '-/-';
        }
        return $address;
    }
}
