<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 模型层:管理端用户 模型类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\Admin
 * @package   App\Models\Admin
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年6月16日10:41:08
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\Admin;

use App\Models\User\User as ClientUser;
use App\Models\Area\Group;
use App\Models\Area\Village;
use App\Models\Traits\UserTrait;
use App\Models\User\Area as UserArea;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Arr;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * Class User
 *
 * @category  App\Models\Admin
 * @package   App\Models\Admin
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年6月16日10:41:08
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */

class User extends Authenticatable implements JWTSubject
{
//    use UserTrait;
    use SoftDeletes;

    // 指定数据库表
    const TABLE = 'admin_users';
    protected $table = self::TABLE;

    // 指定对象显示名称:方便系统统一查询和做其他处理
    const OBJ_NAME      = 'admin';
    const OBJ_NAME_ZH   = '管理用户';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id','username', 'password','name','avatar','mobile','remember_token', 'status','role', 'teacher_id', 'user_id'
    ];

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

    /**
     * 访问器被附加到模型数组的形式。
     *
     * @var array
     */
    protected $appends = ['status_show', 'role'];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return ['role' => 'admin'];
    }

    /**
     * A user has and belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function roles(): BelongsToMany
    {
        $pivotTable = config('admin.database.role_users_table');

        $relatedModel = config('admin.database.roles_model');

        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
    }

//    /**
//     * @return BelongsTo
//     */
//    public function role(): BelongsTo
//    {
//        $pivotTable = config('admin.database.role_users_table');
//
//        return $this->belongsTo(Role::class, 'user_id', 'role_id', $pivotTable);
//    }

    /**
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(ClientUser::class);
    }

    /**
     *
     * @return HasMany
     */
    public function areas(): hasMany
    {
        return $this->hasMany(UserArea::class);
    }

    /**
     * A user has and belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function villages(): BelongsToMany
    {
        return $this->belongsToMany(Village::class, UserArea::class, 'user_id', 'area_id')
            ->where('area_type', Village::OBJ_NAME);
    }

    /**
     * A user has and belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(Group::class, UserArea::class, 'user_id', 'area_id')
            ->where('area_type', Group::OBJ_NAME);
    }



    /**
     * 获取角色
     *
     * @return mixed
     */
    public function getRoleAttribute()
    {
        // 只获取最新的角色
        $role = $this->roles()->first();
        return optional($role)->slug;
        return $this->roles()->get();
    }

    /**
     * 获取角色
     *
     * @return mixed
     */
    public function getRoleNameAttribute()
    {
        // 只获取最新的角色
        $role = $this->roles()->first();
        return optional($role)->name;
    }

    /**
     * 获取状态的中文显示
     *
     * @return mixed
     */
    public function getStatusShowAttribute()
    {
        return Arr::get(\App\Models\User\User::STATUS_OPTIONS, $this->status ? : \App\Models\User\User::ENABLE);
    }
}