<?php


namespace App\Http\Requests\Demand;

use App\Traits\RequestApiFormValidation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;

class DemandRequest extends FormRequest
{
    use RequestApiFormValidation;

    protected $storeApiRules = [
        'description' => 'required',
        'type' => 'required|integer',
    ];

    protected $storeApiMessage = [
        'description.required' => '需求描述为必填项',
        'type.required' => '需求类型为必填项',
    ];

    protected $updateApiRules = [];

    protected $updateApiMessage = [];

    protected $uploadFileApiRules = [
        'file' => 'required|file|image'
    ];

    protected $uploadFileApiMessage = [
        'file.required' => '请选择上传文件',
        'file.file' => '未选择上传文件',
        'file.image' => '上传文件需为图片',
    ];

    protected $destroyApiRules = ['id' => 'required'];

    protected $destroyApiMessage = ['id.required' => 'id不能为空'];

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $routeName = Route::currentRouteName();
        switch ($routeName) {
            case 'demand.storeApi':
                return $this->storeApiRules;
            case 'demand.updateApi':
                return $this->updateApiRules;
            case 'demand.uploadFileApi':
                return $this->uploadFileApiRules;
            case 'demand.destroyApi':
                return $this->destroyApiRules;
            default :
                return [];
        }
    }

    public function messages(): array
    {
        $routeName = Route::currentRouteName();
        switch ($routeName) {
            case 'demand.storeApi':
                return $this->storeApiMessage;
            case 'demand.updateApi':
                return $this->updateApiMessage;
            case 'demand.uploadFileApi':
                return $this->uploadFileApiMessage;
            case 'demand.destroyApi':
                return $this->destroyApiMessage;
            default :
                return [];
        }
    }
}
