<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use App\Traits\ModelTimeFormat;
use Illuminate\Support\Facades\Date;

use App\Traits\ModelLogChanging;

class UserDutyCheck extends Model
{
    use ModelLogChanging;


    use ModelTimeFormat;
    public function __construct(array $attributes = [])
    {
        $this->timestamps=false;
        parent::__construct($attributes);
    }


    protected $fillable=[
        'user_id','checked_at','type','source','workgroup_id','verify_user_id'
    ];

    public function userDetail(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo('App\UserDetail','user_id','user_id');
    }

    public function userLabor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo('App\UserLabor','user_id','user_id');
    }
    public function userWorkgroup(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo('App\UserWorkgroup','workgroup_id','id');
    }
    public function laborReport(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo('App\LaborReport','id','user_duty_check_id');
    }

    //进场创建临时工报表信息
     function makeEnteringRecord(): LaborReport
     {
        $userDetail=UserDetail::find($this['user_id']);
        $name=$userDetail['full_name'];
        $mobile_phone=$userDetail['mobile_phone'];
        $identity_number=$userDetail['identity_number'];
        $labor_company_id=UserLabor::where('user_id',$this['user_id'])->value('labor_company_id');
        $laborReport=new LaborReport([
            'user_id'=>$this['user_id'],
            'name'=>$name,
            'mobile_phone'=>$mobile_phone,
            'identity_number'=>$identity_number,
            'labor_company_id'=>$labor_company_id,
            'user_duty_check_id'=>$this['id'],
            'created_at'=>$this['checked_at'],
        ]);
        $laborReport->save();
        $laborReportStatus=new LaborReportStatus([
            'labor_report_id'=>$laborReport['id'],
            'status'=>'未审核',
            'created_at'=>$this['checked_at'],
        ]);
        $laborReportStatus->save();
        $laborReport['enter_number']=$laborReport->makeOrGetEnteringNumber();
        $laborReport->update();
        return $laborReport;
    }
}
