<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable,HasDateTimeFormatter,SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'account',
        'name',
        'email',
        'avatar',
        'password',
        'mobile',
        'status',
        'online',
        'lang',
        'jpush_reg_id',
        'last_login_ip',
        'register_ip',
        'last_login_time',
        'is_counting',
        'is_cashier',
        'member_type',
        'company_image',
        'company_card_color',
        'company_name',
        'production_project',
        'company_url',
        'company_phone',
        'other_contacts',
        'start_time',
        'end_time',
        'member_type'
    ];
    public function memberStatus(){
        return [1 => '一般会员', 2 => '企业会员'];
    }

    protected $appends =[
        'member_type_text',
    ];
    public function getMemberTypeTextAttribute()
    {
        $list = $this->memberStatus();
        if(empty($this->attributes['member_type'])){
            return null;
        }
        return isset($list[$this->attributes['member_type']]) ? $list[$this->attributes['member_type']] : '';
    }

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

    public function getJWTIdentifier()
    {
        return $this->getKey(); // TODO: Implement getJWTIdentifier() method.
    }

    public function getJWTCustomClaims()
    {
        return [
            'role' => 'user'
        ];  // TODO: Implement getJWTCustomClaims() method.
    }

    /**
     * 设置密码加密
     * @param $value
     * @return string
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }


    public function member(){
        return $this->hasOne(UserMember::class,'id','member_id');
    }
}
