<?php

namespace App;

use App\Services\api\UserService;
use App\Traits\ModelTimeFormat;
use Carbon\Carbon;
use Illuminate\Support\Facades\Gate;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

use App\Traits\ModelLogChanging;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Procurement extends Model
{
    use ModelLogChanging;
    use ModelTimeFormat;

    const type=[
        0 => "采购单",
        1 => "询价单",
        2 => "打样单",
    ];

    const status=[
        0 => "待报价",
        1 => "已报价",
        2 => "待接单",
        3 => "已失效",
        4 => "生产中",
        5 => "待收货",
        6 => "待确定",
        7 => "待出账",
        8 => "已出账",
        9 => "完结",
        10 => "订单取消",
        11 => "下单失败",
    ];

    protected $fillable=[
        'code','owner_material_id', 'supplier_id', 'quantity','amount','unit_price','cost_price','status','initiator','type','is_enquiry','deadline'
    ];

    public  function ownerMaterial(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne('App\OwnerMaterial','id','owner_material_id');
    }

    public  function supplier(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne('App\Supplier','id','supplier_id');
    }

    public function initiator(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class,'initiator','id');
    }
    public function procurementDeliveries(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany('App\ProcurementDeliverie','procurement_id','id')->orderByDesc('id');
    }
    public function procurementQuotations(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany('App\ProcurementQuotation','procurement_id','id');
    }

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

//    protected static function booted()
//    {
//        /** @var User $user */
//        $user = Auth::user();
//        if (Gate::allows('供应商-可见全部'))return;
//        if ($user && !$user->isSuperAdmin()){
//            /** @var \stdClass $user */
//            $ids = array_column(DB::select(DB::raw("SELECT supplier_id FROM supplier_user WHERE user_id = ?"),[$user->id]),"supplier_id");
//            if (count($ids)>0){
//                static::addGlobalScope('supplier', function (Builder $builder)use ($ids) {
//                    $builder->where(function (Builder $query)use($ids){
//                        $query->where(function (Builder $query)use($ids){
//                            $query->whereNull("supplier_id")->orWhereIn('supplier_id',$ids)->orWhere("type",1);
//                        })->WhereHas("ownerMaterial",function (Builder $query)use($ids){
//                            $query->whereHas("material",function (Builder $query)use($ids){
//                                $query->whereHas("supplier",function (Builder $query)use($ids){
//                                    $query->whereIn('id',$ids);
//                                });
//                            });
//                        });
//                    });
//                });//采购单 供应商为空 -》 耗材的供应商存在于IDS   供应商存在 -》 供应商本身存在于IDS
//            }
//        }
//    }
//    protected static function booted()
//    {
//        /** @var User $user */
//        $user = Auth::user();
//        if ($user && !$user->isSuperAdmin()) {
//            /** @var \stdClass $user */
//            $ids = array_column(DB::select(DB::raw("SELECT supplier_id FROM supplier_user WHERE user_id = ?"),[$user->id]),"supplier_id");
//            if (count($ids)>0){
//                static::addGlobalScope('supplier', function (Builder $builder)use ($ids) {
//                    $builder->whereIn('supplier_id',$ids);
//                });
//            }
//        }
//    }
}
