<?php

namespace App;

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

use App\Traits\ModelLogChanging;

class Process extends Model
{
    use ModelLogChanging;

    use ModelTimeFormat;
    use SoftDeletes;
    protected $fillable=[
        'code','owner_id','process_method_id','unit_price','status','remark','balance_remark','amount','completed_amount','created_at','updated_at','deleted_at',"designate_id"
    ];
    protected $appends=[
        'owner_name','process_method_name',
    ];

    public static function filterAuthorities(){
        $user=Auth::user();
        if(!$user){
            return (new static)->newQuery()->where('processes.owner_id','0');
        }
        $ownerIds=app('UserService')->getPermittingOwnerIds($user);
        return (new static)->newQuery()->whereIn('processes.owner_id',$ownerIds);
    }

    public function owner(){
        return $this->belongsTo('App\Owner','owner_id','id');
    }
    public function processMethod(){
        return $this->belongsTo('App\ProcessMethod','process_method_id','id');
    }
    public function tutorials(){
        return $this->belongsToMany('App\Tutorial','process_tutorial','process_id','tutorial_id');
    }
    public function processesContents(){
        return $this->hasMany('App\ProcessesContent','process_id','id');
    }
    public function processDailies(){
        return $this->hasMany('App\ProcessDaily','process_id','id');
    }
    public function operatorLogs(){
        return $this->hasMany('App\OperatorLog','operator_logable_id','id')
            ->where('operator_logable_type','processes')->orderBy('id','DESC');
    }
    public function signs(){
        return $this->hasMany('App\Sign','signable_id','id')
            ->where('signable_type','processes')->where('field','unit_price');
    }
    public function processStatistic()
    {
        return $this->hasOne(ProcessStatistic::class,"process_id","id");
    }

    public function automaticSupplementSign(){
        return $this->hasOne('App\Sign','signable_id','id')
            ->where('signable_type','processes')->where('field','automatic_supplement');
    }
    public function signUnitPrice(){
        return $this->hasOne('App\Sign','signable_id','id')
            ->where('signable_type','processes')->where('field','update_unit_price_value');
    }


    public function getOwnerNameAttribute()
    {
        return $this['owner']? $this['owner']['name']:null;
    }
    public function getProcessMethodNameAttribute()
    {
        return $this['processMethod']? $this['processMethod']['name']:null;
    }


    public function createOperatorLog($operation){
        OperatorLog::query()->create([
            'operator_logable_type'=>'processes',
            'operator_logable_id'=>$this->id,
            'user_id'=>Auth::id(),
            'operation'=>$operation
        ]);
    }

    public function createSign($mark){
        return Sign::query()->create([
            'signable_type'=>'processes',
            'signable_id'=>$this->id,
            'field'=>'unit_price',
            'mark'=>$mark
        ]);
    }

    public function createSignUnitPrice($unit_price){
        return Sign::query()->create([
            'signable_type'=>'processes',
            'signable_id'=>$this->id,
            'field'=>'update_unit_price_value',
            'mark'=>$unit_price
        ]);
    }

    public function destroySign(){
        Sign::query()->where('signable_type','processes')->where('signable_id',$this->id)->where('field','unit_price')
            ->orWhere('field','update_unit_price_value')->delete();
    }
}
