package com.keyware.shandan.dynacmicform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.dynacmicform.core.basic.entity.FormInfoPo;
import com.keyware.shandan.dynacmicform.core.basic.services.DynamicFormService;
import com.keyware.shandan.dynacmicform.core.basic.services.FormInfoService;
import com.keyware.shandan.common.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 动态表单前端控制器
 */
@RestController
@RequestMapping("/dynamic-form")
public class DynamicFormController {

    @Autowired
    private FormInfoService formInfoService;

    @Autowired
    private DynamicFormService dynamicFormService;

    /**
     * 视图 - 表单列表视图
     *
     * @return 视图
     */
    @GetMapping("/view/form/list")
    public ModelAndView listView() {
        return new ModelAndView("dynamicForm/formList");
    }

    /**
     * 视图 - 表单编辑视图
     *
     * @return 视图
     */
    @GetMapping("/view/form/edit")
    public ModelAndView editView() {
        return new ModelAndView("dynamicForm/formEdit");
    }

    /**
     * 视图 - 表单预览视图
     *
     * @return 视图
     */
    @GetMapping("/view/form/preview")
    public ModelAndView previewView(ModelAndView mav, String formId) {
        mav.setViewName("dynamicForm/preview");
        return mav.addObject("formInfo", formInfoService.getById(formId));
    }

    /**
     * 视图 - 表单设计视图
     *
     * @return 视图
     */
    @GetMapping("/view/form/design")
    public ModelAndView formDesignView() {
        return new ModelAndView("dynamicForm/designForm");
    }

    /**
     * 视图 - 数据查询列表设计视图
     *
     * @return 视图
     */
    @GetMapping("/view/query/design")
    public ModelAndView queryDesignView(ModelAndView mav, String formId) {
        mav.setViewName("dynamicForm/querySetting");
        return mav.addObject("formInfo", formInfoService.getById(formId));
    }

    @GetMapping("/api/form/get")
    public Result<FormInfoPo> getFormInfo(String id) {
        if (StringUtils.hasText(id)) {
            return Result.of(formInfoService.getById(id));
        }
        return Result.of(null);
    }

    /**
     * 分页列表
     *
     * @param page 分页参数
     * @return 分页数据
     */
    @GetMapping("/api/form/list")
    public Result<Page<FormInfoPo>> list(Page<FormInfoPo> page, String searchText) {
        QueryWrapper<FormInfoPo> param = new QueryWrapper<>();
        if (StringUtils.hasText(searchText)) {
            param.like("FORM_IDENTITY", searchText).or().like("FORM_NAME", searchText);
        }
        return Result.of(formInfoService.page(page, param));
    }

    /**
     * 保存表单信息
     *
     * @param formInfo 表单信息
     * @return 结果
     */
    @PostMapping("/api/form/save")
    public Result<Object> saveFormInfo(FormInfoPo formInfo) throws Exception {
        return Result.of(dynamicFormService.saveForm(formInfo));
    }

    /**
     * 保存查询列表配置
     * @param formId 表单ID
     * @param queryFieldConfig 配置数据
     * @return -
     * @throws Exception -
     */
    @PostMapping("/api/form/query/save")
    public Result<Object> saveFormQueryConfig(String formId, String queryFieldConfig) throws Exception {
        if (StringUtils.isEmpty(formId) || StringUtils.isEmpty(queryFieldConfig)) {
            return Result.of(null, false, "参数错误");
        }

        FormInfoPo formInfo = formInfoService.getById(formId);
        formInfo.setQueryConfig(queryFieldConfig);
        return Result.of(dynamicFormService.saveForm(formInfo));
    }

    /**
     * 禁用表单
     *
     * @param id
     * @return 结果集
     */
    @PostMapping("/api/form/disabled")
    public Result<Object> disabledForm(int id) {
        return Result.of(dynamicFormService.disabledForm(id, true));
    }

    /**
     * 启用表单
     *
     * @param id
     * @return 结果集
     */
    @PostMapping("/api/form/enabled")
    public Result<Object> enabledForm(int id) {
        return Result.of(dynamicFormService.disabledForm(id, false));
    }

    /**
     * 删除表单信息
     *
     * @param id 表单ID
     * @return 结果
     */
    @PostMapping("/api/form/delete")
    public Result<Object> deleteFormInfo(int id) throws Exception {
        return Result.of(dynamicFormService.delete(id));
    }
}
