<?php

namespace App;

use App\Traits\ModelTimeFormat;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use App\Traits\ModelLogChanging;

class OrderTracking extends Model
{
    use ModelLogChanging;
    use ModelTimeFormat;

    protected $fillable = [
        'order_package_commodity_id','owner_id','logistic_id',
        'web_order_number',
        'pick_up_at','sale','client','order_client_code','order_client_code_wms',
        'order_remark','pallet_total','planning_sent_at',
        'is_on_duty_shift','is_arrival','signed_at',
        'receive_bill_status','remark','gross_weight','bulk'];

    /*
     * order_package_commodity_id 订单商品id
     * owner_id 货主
     * web_order_number WEB+订单号
     * pick_up_at 提货时间
     * sale 销售
     * client 客户
     * order_client_code 订单号
     * order_remark 订单备注
     * pallet_total 托盘合计
     * planning_sent_at 应送达时间
     * is_on_duty_shift 是否赶上卡班
     * is_arrival 到货情况
     * signed_at 签收时间
     * receive_bill_status
     * remark 签收单情况
     * gross_weight 重量
     * bulk 体积
     * customer_order_number 客户订单号
     */

    protected $appends =[
        'ownerName','sku','packageWeight','packageBulk','orderCity','packageLogisticNumber','packageLogistic','orderClientNumber'
    ];
    public function commodities(){
        return $this->hasOne(OrderPackageCommodities::class,'id','order_package_commodity_id');
    }
    public function owner(){
        return $this->hasOne(Owner::class,'id','owner_id');
    }
    public function logistic(){
        return $this->hasOne(Logistic::class,'id','logistic_id');
    }
    public function uploadFile()
    {
        return $this->hasOne(UploadFile::class,'table_id','order_client_code')->where('table_name','order_trackings');
    }

    public function getOwnerNameAttribute(){
        return $this->owner['name'] ?? '';
    }
    public function getSkuAttribute(){
        return ['sku' => $this->commodities['commodity']['sku'] ?? '',
            'skuName' => $this->commodities['commodity']['name'] ?? '',
            'skuAmount' => $this->commodities['amount'] ?? ''];
    }
    public function getPackageWeightAttribute(){
        return $this->commodities->package['weight'] ?? '';
    }
    public function getPackageBulkAttribute(){
        return $this->commodities->package['bulk'] ?? '';
    }
    public function getOrderCityAttribute(){
        return $this->commodities->package->order['city'] ?? '';
    }
    public function getPackageLogisticNumberAttribute(){
        return $this->commodities->package['logistic_number'] ?? '';
    }
    public function getPackageLogisticAttribute(){
        return $this->commodities->package->order->logistic['name'] ?? '';
    }
    public function getOrderClientNumberAttribute(){
        return $this->commodities->package->order['client_code'] ?? '';
    }

    public function scopeFilter($query, $filters)
    {
        return $filters->apply($query);
    }

    public function scopeDefaultQuery($query): Builder
    {
        return $query->orderByDesc('order_trackings.pick_up_at')
            ->orderBy('order_trackings.order_client_code');
    }

    public function scopeDefaultWith($query)
    {
        return $query->with(['commodities'=>function($query){
            $query->with(['commodity.barcodes','serialNumbers','package.order']);
        },'owner','logistic','uploadFile']);
    }

}
