<?php

namespace App;

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

class Order extends Model
{
    use ModelLogChanging;
    use ModelTimeFormat;

    protected $fillable = [
        'id',
        'batch_id',
        'owner_id',
        'status',
        'created_at',
        'code',
        'shop_id',
        'client_code',
        'logistic_id',
        'consignee_name',
        'consignee_phone',
        'province',
        'city',
        'district',
        'address',
        'warehouse_id',
        'wms_edittime',
        'wms_status',
        'order_type',
        'frozen'
    ];

    /*
     * wms订单号             code=>DOC_ORDER_HEADER[orderno]
     * wms客户订单号         client_code=>DOC_ORDER_HEADER['soreference1']
     * wms订单状态           wms_status=>
     * 仓库                 warehouse_id=>DOC_ORDER_HEADER['WAREHOUSEID']
     */

    public function cancel(){
        $this['status'] = '取消';
        $this->update();
    }

    public function logistic()
    {
        return $this->hasOne(Logistic::class, 'id', 'logistic_id');
    }

    public function issue()
    {
        return $this->belongsTo(OrderIssue::class, 'id', 'order_id');
    }

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

    public function owner()
    {
        return $this->hasOne(Owner::class, 'id', 'owner_id');
    }

    public function packages()
    {
        return $this->hasMany(OrderPackage::class,'order_id','id');
    }

    public function warehouse()
    {
        return $this->belongsTo(Warehouse::class);
    }
    public function orderCommodities(){
        return $this->hasMany('App\OrderCommodity','order_id','id');
    }
    public function bin(){
        return $this->hasOne('App\OrderBin','order_id','id');
    }
    public function batch(){
        return $this->belongsTo('App\Batch', 'batch_id','id');
    }

    public function getLogisticNumbersAttribute()
    {
        $packages = $this->packages;
        if (!$packages) {
            return [];
        } else {
            $arr = [];
            foreach ($packages as $package) {
                array_push($arr, $package->logistic_number);
            }
            return $arr;
        }
    }

    public function getAmountAttribute()
    {
        $packages = $this->packages;
        if (!$packages) {
            return 0;
        } else {
            $count = 0;
            foreach ($packages as $package) {
                foreach ($package->commodities as $item) {
                    $count += $item->amount;
                }
            }
            return $count;
        }
    }

    public function getCommodityPackagesAttribute()
    {
        $packages = $this->packages;
        if (!$packages) {
            return 0;
        } else {
            $count = 0;
            foreach ($packages as $package) {
                foreach ($package->commodities as $item) {
                    $count++;
                }
            }
            return $count;
        }
    }

    public function delete()
    {
        $this->packages()->delete();
        return parent::delete();
    }
    public function deleteSafe()
    {
        return parent::delete();
    }

    public function isEquals($order)
    {
        return $this['wms_edittime'] == $order['wms_edittime']
            && $this['frozen'] == $order['frozen'];
    }

    public function assignValueByOrder($order)
    {
        $this['code'] = $order['code'] ;
        $this['batch_id'] = $order['batch_id'] ;
        $this['warehouse_id'] = $order['warehouse_id'] ;
        $this['owner_id'] = $order['owner_id'] ;
        $this['shop_id'] = $order['shop_id'] ;
        $this['logistic_id'] = $order['logistic_id'] ;
        $this['consignee_name'] = $order['consignee_name'] ;
        $this['consignee_phone'] = $order['consignee_phone'] ;
        $this['province'] = $order['province'] ;
        $this['city'] = $order['city'] ;
        $this['district'] = $order['district'] ;
        $this['address'] = $order['address'] ;
        $this['client_code'] = $order['client_code'] ;
        $this['wms_status'] = $order['wms_status'] ;
        $this['wms_edittime'] = $order['wms_edittime'];
        $this['frozen'] = $order['frozen'];
        $this['order_type'] = $order['order_type'];
        $this['created_at'] =$order['created_at'];
    }

    public static function getFrozen($string): string
    {
        $arr = [
            'H' => '是',
            'Y' => '否',
            'N' => '否'
        ];
        return $arr[$string] ?? '否';
    }
}
