/**
 *
 */
package com.sifang.modules.flowable.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.service.ProcessFlowableService;
import com.sifang.modules.flowable.utils.TaskVo;
import com.sifang.modules.flowable.vo.AddProcessVo;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sifang.modules.flowable.service.ProcessDefService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @version
 */
@Controller
@RequestMapping(value = "${adminPath}/set/process")
public class ProcessDefController extends BaseController {

    Logger logger = LoggerFactory.getLogger(ProcessDefController.class);

    @Autowired
    private ProcessDefService processDefService;

    @Autowired
    private ProcessFlowableService flowableService;

    /**
     * 流程设置页面
     */

    @RequestMapping(value = "settingPage")
    public String processSetPage(HttpServletRequest request) {
        return "modules/yangguang/systemSetting/flowable/processDef";
    }

    /**
     * 流程页面列表数据
     */

    @ResponseBody
    @RequestMapping(value = "processFormList")
    public Object processFormList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        processDefService.processFormList(page, map);
        return page.renderJson();
    }

    //新建流程页面
    @RequestMapping(value = "addProcessPage")
    public String addProcessPage(HttpServletRequest request, Model model){
        //表单下拉
        List<Record> list = processDefService.getForm();
        model.addAttribute("forms",list);
        //角色下拉
        List<Record> list2=processDefService.getAllRole();
        model.addAttribute("roles",list2);

        List<Record> list3=processDefService.getAllPlateEmp();
        model.addAttribute("emps",list3);
        return "modules/yangguang/systemSetting/flowable/addProcessPage";
    }

    /**
     * 编辑流程
     * @param processId  流程id
     * @return
     */
    @RequestMapping(value = "editProcessPage")
    public String editProcessPage(@NotBlank(message = "必传参数不能为空!") final String processId, Model model) {
        Map<String, Object> map = processDefService.getProcessDefDetail(processId);
        model.addAttribute("map",map);
        //角色下拉
        List<Record> list2=processDefService.getAllRole();
        model.addAttribute("roles",list2);
        List<Record> list3=processDefService.getAllPlateEmp();
        model.addAttribute("emps",list3);
        return "modules/yangguang/systemSetting/flowable/editProcessPage";
    }


    /**
     * 保存新建的流程
     */

    @ResponseBody
    @RequestMapping(value = "saveProcessDef")
    public Result saveProcessDef(@RequestBody AddProcessVo addProcessVo, HttpServletRequest request) {

        try {
            processDefService.saveProcessDef(addProcessVo);
        } catch (IllegalArgumentException i) {
            logger.error(">>>>>>>>>>saveProcessDef i:{}", i.getMessage());
            return Result.fail(i.getMessage());
        } catch (Exception e) {
            logger.error(">>>>>>>>>>saveProcessDef e:{}", e);
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 编辑流程
     */

    @ResponseBody
    @RequestMapping(value = "updateProcessDef")
    public Result updateProcessDef(@RequestBody AddProcessVo addProcessVo) {
        try {
            processDefService.updateProcessDef(addProcessVo);
        } catch (IllegalArgumentException i) {
            i.printStackTrace();
            logger.error(">>>>>>>>>>updateProcessDef i:{}", i.getMessage());
            return Result.fail(i.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>updateProcessDef e:{}", e);
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }


    /**
     * 根据表单的id  获取表单 对应的字段
     */

    @ResponseBody
    @RequestMapping(value = "getAllFields")
    public Result getAllFields(final String formId) {
        List<Record> list = processDefService.getAllFields(formId);
        return Result.success(list);
    }

    /**
     *  启用/禁用/删除 流程
     * type=1 禁用  type=0 启用  type=2 删除
     */

    @ResponseBody
    @RequestMapping(value = "changeProcessStatus")
    public Result changeProcessStatus(@NotBlank(message = "必传参数不能为空!") final String processId, @NotBlank String type) {
        if (StringUtil.isBlank(processId) || StringUtil.isBlank(type)) {
            return Result.fail("必传参数不能为空!");
        }
        int i = processDefService.changeProcessStatus(processId, type);
        return i > 0 ? Result.ok() : Result.fail("操作失败");
    }


    @ResponseBody
    @RequestMapping("getBpmnModel")
    public Result getBpmnModel(HttpServletRequest request) {
        String processInstanceId = request.getParameter("processInstanceId");
        String status = request.getParameter("status");
        List<TaskVo> list=flowableService.getBpmnModel(processInstanceId,status);
        return Result.success(list);
    }

}
