<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Traits\ModelTimeFormat;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class Waybill extends Model
{
    use ModelTimeFormat;
    use SoftDeletes;
    protected $fillable=[
        'status','type','waybill_number','owner_id','wms_bill_number','origination','destination','recipient','recipient_mobile','charge','ordering_remark',
        'carrier_id','carrier_bill','origination_city_id','destination_city_id','warehouse_weight','warehouse_weight_unit_id','carrier_weight','carrier_weight_unit_id','carType_id',
        'car_owner_info','fee','pick_up_fee','other_fee','collect_fee','dispatch_remark','waybill_price_model_id','warehouse_weight_other','warehouse_weight_unit_id_other'
        ,'carrier_weight_other','carrier_weight_unit_id_other','source_bill','mileage','amount','inquire_tel','amount_unit_id','other_charge','other_charge_remark','deliver_at'
    ];
    protected $appends=[
        'origination_city_name',
        'carrier_name',
        'owner_name',
        'destination_city_name',
        'warehouse_weight_unit_name',
        'carrier_weight_unit_name',
        'warehouse_weight_unit_other_name',
        'carrier_weight_unit_other_name',
        'amount_unit_name',
        'upload_file_url',
        'upload_file_type',
        'destination_province_name'
    ];

    public function owner(){
        return $this->belongsTo('App\Owner','owner_id','id');
    }
    public function carrier(){
        return $this->belongsTo('App\Carrier','carrier_id','id');
    }

    public function origination_city(){
        return $this->belongsTo('App\City','origination_city_id','id');
    }

    public function destination_city(){
        return $this->belongsTo('App\City','destination_city_id','id');
    }
    public function wmsCommodities(){
        return $this->hasMany('App\WMSWaybillOrder','OrderNo','wms_bill_number');
    }


    public function warehouse_weight_unit(){
        return $this->belongsTo('App\Unit','warehouse_weight_unit_id','id');
    }

    public function carrier_weight_unit(){
        return $this->belongsTo('App\Unit','carrier_weight_unit_id','id');
    }

    public function warehouse_weight_unit_other(){
        return $this->belongsTo('App\Unit','warehouse_weight_unit_id_other','id');
    }

    public function carrier_weight_unit_other(){
        return $this->belongsTo('App\Unit','carrier_weight_unit_id_other','id');
    }

    public function carType(){
        return $this->belongsTo('App\CarType','carType_id','id');
    }

    public function waybillAuditLogs(){
        return $this->hasMany('App\WaybillAuditLog','waybill_id','id');
    }
    public function uploadFile(){
        return $this->hasOne('App\UploadFile','table_id','id')->where('table_name','waybills');
    }

    public function waybill_on_top(){
        return $this->belongsTo('App\WaybillOnTop','waybill_id','id');
    }
    public function amount_unit(){
        return $this->belongsTo('App\Unit','amount_unit_id','id');
    }

    static public function setWeightByOrderCode($orderCode,$weight){
        if(empty($orderCode))return;
        $waybill=Waybill::where('wms_bill_number',$orderCode)->where('status','!=','已完结')
            ->where('status','!=','无模型')->first();
        if ($waybill){
            $waybill->warehouse_weight_other=$weight;
            $waybill->warehouse_weight_unit_id_other=1;
            $waybill->update();
        }
    }

    public function getUploadFileUrlAttribute(){
        return $this['uploadFile']? asset('/storage'.$this['uploadFile']['url']):null;
    }
    public function getUploadFileTypeAttribute(){
        return $this['uploadFile']? $this['uploadFile']['type']:null;
    }
    public function getOwnerNameAttribute(){
        return $this['owner']? $this['owner']['name']:null;
    }
    public function getCarrierNameAttribute(){
        return $this['carrier']? $this['carrier']['name']:null;
    }
    public function getOriginationCityNameAttribute(){
        return $this['origination_city']? $this['origination_city']['name']:null;
    }
    public  function  getDestinationCityNameAttribute(){
        return $this['destination_city']? $this['destination_city']['name']:null;
    }
    function getDestinationProvinceNameAttribute(){
        return $this['destination_city'] ? $this['destination_city']['province_name']:null;
    }
    public function getWarehouseWeightUnitNameAttribute(){
        return $this['warehouse_weight_unit']? $this['warehouse_weight_unit']['name']:null;
    }
    public function getCarrierWeightUnitNameAttribute(){
        return $this['carrier_weight_unit']? $this['carrier_weight_unit']['name']:null;
    }
    public function getWarehouseWeightUnitOtherNameAttribute(){
        return $this['warehouse_weight_unit_other']? $this['warehouse_weight_unit_other']['name']:null;
    }
    public function getCarrierWeightUnitOtherNameAttribute(){
        return $this['carrier_weight_unit_other']? $this['carrier_weight_unit_other']['name']:null;
    }
    public function getAmountUnitNameAttribute(){
        return $this['amount_unit']? $this['amount_unit']['name']:null;
    }


    public static function filterAuthorities(){
        $user = Auth::user();
        $owner_ids=$user->getPermittingOwnerIdsAttribute();
        return (new static)->newQuery()->whereIn('owner_id',$owner_ids);
    }


}
