package cn.stylefeng.team.form.modular.component.controller;

import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.team.form.modular.component.entity.FormComponent;
import cn.stylefeng.team.form.modular.component.pojo.request.FormComponentCategoryRequest;
import cn.stylefeng.team.form.modular.component.pojo.request.FormComponentRequest;
import cn.stylefeng.team.form.modular.component.pojo.response.SummaryFormComponent;
import cn.stylefeng.team.form.modular.component.service.FormComponentCategoryService;
import cn.stylefeng.team.form.modular.component.service.FormComponentService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiResource(name = "表单组件")
public class FormComponentController {

    @Resource
    private FormComponentService formComponentService;

    @Resource
    private FormComponentCategoryService formComponentCategoryService;

    @PostResource(name = "添加表单组件", path = "/formComponent/add")
    public ResponseData<FormComponent> add(@RequestBody @Validated(FormComponentRequest.add.class) FormComponentRequest formComponentRequest) {
        formComponentService.add(formComponentRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "删除表单组件", path = "/formComponent/delete")
    public ResponseData<?> delete(@RequestBody @Validated(FormComponentRequest.delete.class) FormComponentRequest formComponentRequest) {
        formComponentService.del(formComponentRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "批量删除表单组件", path = "/formComponent/batchDelete")
    public ResponseData<?> batchDelete(@RequestBody @Validated(BaseRequest.batchDelete.class) FormComponentRequest formComponentRequest) {
        formComponentService.batchDelete(formComponentRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "编辑表单组件", path = "/formComponent/edit")
    public ResponseData<?> edit(@RequestBody @Validated(FormComponentRequest.edit.class) FormComponentRequest formComponentRequest) {
        formComponentService.edit(formComponentRequest);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "查看表单组件详情", path = "/formComponent/detail")
    public ResponseData<FormComponent> detail(@Validated(FormComponentRequest.detail.class) FormComponentRequest formComponentRequest) {
        return new SuccessResponseData<>(formComponentService.detail(formComponentRequest));
    }

    @GetResource(name = "获取表单组件列表（带分页）", path = "/formComponent/page")
    public ResponseData<PageResult<FormComponent>> page(FormComponentRequest formComponentRequest) {
        return new SuccessResponseData<>(formComponentService.findPage(formComponentRequest));
    }

    @GetResource(name = "获取表单组件列表（带分组）", path = "/formComponent/list")
    public ResponseData<SummaryFormComponent> list(FormComponentCategoryRequest formComponentCategoryRequest) {
        SummaryFormComponent summaryFormComponent = formComponentCategoryService.findTotalList(formComponentCategoryRequest);
        return new SuccessResponseData<>(summaryFormComponent);
    }

    @GetResource(name = "获取代码生成用的表单组件列表", path = "/formComponent/getCodeGenerateComponentList")
    public ResponseData<List<FormComponent>> getCodeGenerateComponentList() {
        FormComponentRequest formComponentRequest = new FormComponentRequest();
        formComponentRequest.setCodeGeneratorFlag(true);
        List<FormComponent> list = formComponentService.findList(formComponentRequest);
        return new SuccessResponseData<>(list);
    }

}
