<?php

namespace App\Models;

use App\Models\traits\Filter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Shop extends Model
{
    use Filter,SoftDeletes;

    public static $PLAT_KTT = 1;
    public static $PLAT_MX = 0;

    public static $STATUS_UNAUTHORIZED = 0;
    public static $STATUS_AUTHORIZED = 1;
    public static $STATUS_NO_AUTHORIZED = 2;
    public static $STATUS_STOP = 3;

    //查询字段
    public $fieldSearchable = [
        'plat_id',
    ];

    protected $hidden = [
        'access_token',
        'expires_in',
        'refresh_token',
        'refresh_token_expires_in',
        'pop_auth_token_create_response',
    ];

    protected $fillable = [
        'access_token', 'expires_at', 'expires_in', 'owner_id', 'owner_name', 'refresh_token', 'refresh_token_expires_at', 'refresh_token_expires_in', 'scope', 'pop_auth_token_create_response', 'status'
    ];

    public function getStatusAttribute($value)
    {
        $map = [
            0 => '未授权',
            1 => '已授权',
            2 => '无需授权',
            3 => '停用',
            '重新授权' => '重新授权',
        ];

        return $map[$value];
    }

    public function getPlatList()
    {
        return ['妙选', '快团团'];
    }

    public function getPlatIdAttribute($value)
    {
        $map = $this->getPlatList();

        return $map[$value];
    }

    public function getRefreshTokenExpiresAtAttribute($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }

    public function ship()
    {
        return $this->hasOne(ShopShip::class, 'shop_id', 'id');
    }
}
