<?php

namespace App\Models;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class InnerDevice extends BaseModel
{

    protected $appends = ['inner_name'];
    use SoftDeletes;

    public function workPoint()
    {
        return $this->belongsTo('App\Models\WorkPoint', 'work_point_id');
    }

    public function project()
    {
        return $this->belongsTo('App\Models\Project', 'project_id');
    }

    public function spec()
    {
        return $this->belongsTo('App\Models\Spec', 'spec_id');
    }

    public function device()
    {
        return $this->belongsTo('App\Models\Device', 'device_id');
    }

//    public function device_name()
//    {
////        return $this->belongsTo('App\Models\DeviceName', 'device_name_id');
//        return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
//
//    }

    public function device_name(){
        return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
    }
    public function option()
    {
        return $this->belongsTo('App\Models\Option', 'status');
    }

    public function getValidator(Request $request, $type)
    {
        $validator = Validator::make($request->input('data'), [
            'number' => 'required'
        ], [
            'number.required' => '固定资产编号'
        ]);
//        $validator->after(function ($validator) use($request) {
//            if (!$request->input('device_name_id')) {
//                $validator->errors()->add('device_name_id', '设备名称必填');
//            }
//            if (!$request->input('spec_id')) {
//                $validator->errors()->add('spec_id', '规格型号必填');
//            }
//        });
        return $validator;
    }

    public function getInnerNameAttribute($value)
    {
        $id = $this->attributes['device_name_id'];
        return InnerDeviceNamesModel::where(['id'=>$id])->value('name');
    }
    public function getStatusLabel()
    {
        return $this['option'] ? '<div style="color: ' . $this['option']['color'] . '">' . $this['option']['name'] . '</div>' : '';
    }

    public function inner_device_names(){
        return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
    }
}
