package com.yida.data.form.customForm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.customform.CoreCustomForm;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.form.customForm.service.CoreCustomFormItemChildrenService;
import com.yida.data.form.customForm.service.CoreCustomFormItemsService;
import com.yida.data.form.customForm.service.CoreCustomFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * 自定义表单 Controller
 *
 * @author wjm
 * @date 2021-08-11 20:55:19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/coreCustomForm")
@RequiredArgsConstructor
@Api(tags = "后台--表单管理")
public class CoreCustomFormController {

    private final CoreCustomFormService coreCustomFormService;
    private final CoreCustomFormItemsService coreCustomFormItemsService;
    private final CoreCustomFormItemChildrenService coreCustomFormItemChildrenService;


    /***
     * @Author jianMingWang
     * @Description //查询所有表单
     * @Date 14:33 2021/8/9
     * @Param [schoolId]
     * @return com.liuliu.data.custom.entity.ResultBean<java.util.List < com.liuliu.data.custom.entity.CoreCustomFormType>>
     **/
    @ApiOperation("表单分页查询")
    @GetMapping("/findList")
    public ResultBean<IPage<CoreCustomForm>> fondType(@ApiParam(value = "学校ID", required = true, type = "Long") Long schoolId,
                                                      @ApiParam(value = "表单类型ID", required = true, type = "Long") Long formTypeId,
                                                      @ApiParam(value = "是否为模板：1=是，0=否", required = false, type = "String") @RequestParam(value = "model", required = false) String model,
                                                      @ApiParam(value = "当前页码", required = false, defaultValue = "1", type = "Integer") @RequestParam(defaultValue = "1") Integer pageNum,
                                                      @ApiParam(value = "页面大小", required = false, defaultValue = "10", type = "Integer") @RequestParam(defaultValue = "10") Integer pageSize) {

        IPage<CoreCustomForm> customFormPageList = coreCustomFormService.findCustomFormPageList(new Page(pageNum, pageSize),
                schoolId, formTypeId, model, FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess(customFormPageList);

    }


    /***
     * @Author jianMingWang
     * @Description //获取当前表单
     * @Date 11:22 2021/8/10
     * @Param [schoolId, formTypeId]
     * @return com.liuliu.data.custom.entity.ResultBean<com.liuliu.data.custom.entity.CoreCustomForm>
     **/
    @ApiOperation("表单详情")
    @GetMapping("/findById")
    public ResultBean<CoreCustomForm> findById(@ApiParam(value = "主键ID", required = true, type = "Long") Long id) {

        return ResultBean.buildSuccess(coreCustomFormService.findById(id));

    }


    /***
     * @Author jianMingWang
     * @Description //删除表单
     * @Date 14:29 2021/8/9
     * @Param [coreCustomForm]
     * @return com.liuliu.data.custom.entity.ResultBean
     **/
    @ApiOperation("删除表单")
    @GetMapping("/delForm")
    public ResultBean delForm(@ApiParam(value = "主键ID", required = true, type = "Long") Long formId) {

        coreCustomFormService.delForm(formId);
        //删除表单对应附表
        return ResultBean.buildSuccess();
    }

    /***
     * @Author jianMingWang
     * @Description 创建、编辑表单
     * @Date 14:29 2021/8/9
     * @Param [coreCustomForm]
     * @return com.liuliu.data.custom.entity.ResultBean
     **/
    @ApiOperation("创建表单")
    @PostMapping("/createForm")
    public ResultBean createForm(@RequestBody CoreCustomForm dto) throws Exception {

        Integer state = coreCustomFormService.saveForm(dto);
        if (state==1){
            return ResultBean.buildError("已发布的表不能修改");
        }
        return ResultBean.buildSuccess();
    }

    @ApiOperation("表单发布")
    @GetMapping("/publishForm")
    public ResultBean publishForm(@RequestParam Long formId,@ApiParam("1为发布，2为取消发布")@RequestParam Integer status)  {

        CoreCustomForm byId = coreCustomFormService.getById(formId);
        byId.setStatus(status);
        coreCustomFormService.publishForm(byId);
        return ResultBean.buildSuccess();
    }


}
