<?php

namespace App\Models;

use App\Models\traits\Filter;

class BusinessOrder extends Model
{
    use Filter;

    public $fieldSearchable = [
        'participate_no',
        'shop_id',
        'activity_no',
        'shipping_status',
        'is_supplier',
        'cancel_status',
        'after_sales_status',
        'supply_participate_no',
        'confirm_at_start',
        'confirm_at_end',
        'goods_sku_num',
        'print_status',
        'ids',
        'pno',
    ];

    protected $fillable = [
        'shop_id',
        'receiver_address_detail',
        'receiver_address_province',
        'self_pick_site_no',
        'discount_amount',
        'theoretical_refund_amount',
        'receiver_address_district',
        'verification_status',
        'inner_transaction_id',
        'is_supplier',
        'service_amount',
        'supply_participate_no',
        'updated_at',
        'order_amount',
        'receiver_address_city',
        'receiver_name',
        'business_note',
        'buyer_memo',
        'logistics_type',
        'help_sell_nickname',
        'activity_title',
        'after_sales_status',
        'mall_activity_type',
        'transaction_id',
        'activity_no',
        'confirm_at',
        'platform_discount_amount',
        'participate_no',
        'receiver_mobile',
        'shipping_status',
        'shipping_amount',
        'cancel_status',
        'nick_name',
        'order_sn',
    ];

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

    public function getShippingStatusAttribute($value)
    {
        $map = ['未发货', '已发货', '部分发货', '' => ''];

        return $map[$value];
    }

    public function getIsSupplierAttribute($value)
    {
        $map = ['帮忙团订单', '自卖团订单', '' => ''];

        return $map[$value];
    }

    public function getCancelStatusAttribute($value)
    {
        $map = ['未取消', '已取消', '' => ''];

        return $map[$value];
    }

    public function getAfterSalesStatusAttribute($value)
    {
        return empty($value) ? '未售后' : '有售后';
    }

    public function items()
    {
        return $this->hasMany(BusinessOrderItem::class, 'business_order_id');
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id');
    }
}
