<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\traits\SetConnectionUser;

class User extends Authenticatable
{
    use Notifiable;

    use SetConnectionUser;

    const ROLE_ADMINISTRATOR = 1;
    const ROLE_WEBMASTER = 2;
    const ROLE_STAFF = 3;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'role', 'company_id', 'order', 'store_id', 'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'remember_token',
    ];


    public static function getData($cid){
        $res = User::where('company_id', '=', $cid)->get();
        $arrResult = array();
        foreach($res as $row){
            $arrResult[] = ['code' => $row->id, 'name' => $row->name];
        }
        return $arrResult;
    }


    public static function getDataStaffByIds($ids)
    {
        return User::whereIn('id', $ids)->get();
    }

    public static function getDataStaffById($uid)
    {
        $res = User::where('id', '=', $uid)->get()->toArray();
        return $res;
    }

    public function getCompanyNameAttribute()
    {
        $companyId = $this->attributes["company_id"];
        $company = MstCompany::find($companyId);

        return optional($company)->name;
    }

    public static function getDataStaff($cid, $sid, $request = null)
    {
        $res = User::where('users.company_id', '=', $cid)
            ->leftJoin('user_infos', 'user_infos.id', '=', 'users.id' )
        ->where('users.store_id', '=', $sid);
        /*->where(function($query) {
            $query->orWhere('role', '=', 1)
                  ->orWhere('role', '=', 2)
                  ->orWhere('role', '=', 3)
                  ->orWhereNull('role');
        });*/

        if (!empty($request->get('search_name'))) {
            $res = $res->where('name', 'like', '%'.$request->get('search_name').'%');
        }
        $res = $res->orderByRaw("case when `user_infos`.`order` is null then 1 else 0 end, `user_infos`.`order`")->get()->toArray();
        return $res;
    }

    public static function getDataByCompanyId($cid, $role = 3)
    {
        $res = User::where('company_id', '=', $cid)->where('role', '=', $role)->get();

        $arrResult = array();
        foreach($res as $row){
                $arrResult[] = ['code' => $row->id, 'name' => $row->name];
        }

        return $arrResult;
    }
    public static function getDataByAll($cid)
    {
        $res = User::where('company_id', '=', $cid)->get();

        $arrResult = array();
        foreach($res as $row){
            $arrResult[] = ['code' => $row->id, 'name' => $row->name];
        }

        return $arrResult;
    }
    public static function getDataByStoreId($cid, $sid)
    {
        $res = User::where('company_id', '=', $cid)->where('store_id', '=', $sid)->get();

        $arrResult = array();
        foreach($res as $row){
            $arrResult[] = ['code' => $row->id, 'name' => $row->name];
        }

        return $arrResult;
    }

    public function isOwnerOfSaiKyoto()
    {
        return $this->attributes["company_id"] == config("const.SAI_KYOTO_ID");
    }

    public function isAdministrator()
    {
        return $this->attributes["role"] == self::ROLE_ADMINISTRATOR;
    }

    public function isWebmaster()
    {
        return $this->attributes["role"] == self::ROLE_WEBMASTER;
    }

    public function isStaff()
    {
        return $this->attributes["role"] == self::ROLE_STAFF;
    }

    public function store()
    {
        return $this->belongsTo(MstStore::class, 'store_id', 'code');
    }
}
