<?php

namespace App\Admin\Actions\Course;

use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\App;

class CourseFieldForm extends Form implements LazyRenderable
{
    use LazyWidget;

    //弹窗表单
    public function form()
    {
        $id = isset($this->payload['id']) ? $this->payload['id'] : 0;
        $this->hidden('id')->value($id);
        $this->text('name', '场地名称');
    }

    //点击表单处理
    public function handle(array $input)
    {
        try {
            if(\App\Models\CourseField::query()->where('course_id', $input['id'])->where('name', $input['name'])->first()){
                return $this->response()->error('已存在');
            };
            $courseField = App::make('getCourseFieldInstance');
            $courseField->course_id = $input['id'];
            $courseField->name = $input['name'];
            $courseField->save();
        } catch (\Exception $exception) {
            return $this->response()->error($exception->getMessage());
        }
        return $this->response()->success('success')->refresh();
    }


}
