package com.sifang.modules.flowpath.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.flowable.service.ProcessFlowableService;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.flowpath.entity.ExamineInfo;
import com.sifang.modules.flowpath.service.BondLimitApplyService;
import com.sifang.modules.flowpath.service.DistributionGoodsApplyService;
import com.sifang.modules.flowpath.service.ExamineInfoService;
import com.sifang.modules.flowpath.vo.BondLimitApplyVo;
import com.sifang.modules.flowpath.vo.UserApplyVo;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.apache.commons.lang3.StringUtils;
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 org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/** 保证金额度申请
 * @version
 */
@Controller
@RequestMapping(value = "${adminPath}/apply/bondLimitApply")
public class BondLimitApplyController extends BaseExceptionController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BondLimitApplyService bondLimitApplyService;

    @Autowired
    private DistributionGoodsApplyService applyService;

    @Autowired
    private ExamineInfoService examineInfoService;

    @Autowired
    private ProcessFlowableService flowableService;

    //页面
    @RequestMapping(value = "bondLimitApplyListPage")
    public String bondAdjustmentListPage(Model model) {
        User user = UserUtils.getUser();
        model.addAttribute("userId",user.getId());
        model.addAttribute("formId", FormUtils.BOND_LIMIT_APPLY);
        return "modules/flowpath/bondLimitApplyListPage";
    }

    //列表数据
    @ResponseBody
    @RequestMapping(value = "listData")
    public String listData(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        bondLimitApplyService.applyList(page,map);
        return page.renderJson();
    }

    //新增页面
    @RequestMapping(value = "addBondLimitApplyPage")
    public String addBondLimitApplyPage(Model model){
        List<Record> list=bondLimitApplyService.getTrader();
        model.addAttribute("traders",list);
        model.addAttribute("imgsCount", 0);
        return "modules/flowpath/addBondLimitApplyPage";
    }

    //新建/修改申请
    @ResponseBody
    @RequestMapping(value = "saveBondLimitApply")
    public Result saveBondLimitApply(@RequestBody BondLimitApplyVo vo){
        try{
            bondLimitApplyService.saveBondLimitApply(vo);
            return Result.ok();
        }catch (RuntimeException r){
            r.printStackTrace();
            return Result.fail(r.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(">>>>>>>>>>>saveBondAdjustmentApply e:{}",e);
        }
        return Result.fail("保存失败");
    }

    //编辑页面
    @RequestMapping(value = "editBondAdjustmentApplyPage")
    public String editBondAdjustmentApplyPage(HttpServletRequest request,Model model){
        String id=request.getParameter("id");
        Record apply=bondLimitApplyService.getDetailById(id);
        model.addAttribute("apply",apply);
        List<Record> list=applyService.getTrader();
        model.addAttribute("traders",list);
        List<ExamineInfo> infos=examineInfoService.getByApplyId(id,FormUtils.BOND_LIMIT_APPLY);
        model.addAttribute("infos",infos);
        int imgsCount = 0;
        if (org.apache.commons.lang3.StringUtils.isNotBlank(apply.getStr("voucher"))) {
            String[] imgArr = apply.getStr("voucher").split(",");
            imgsCount = imgArr.length;
            List<String> imglist = Arrays.asList(imgArr);
            model.addAttribute("imglist", imglist);
        }
        model.addAttribute("imgsCount", imgsCount);
        return "modules/flowpath/addBondLimitApplyPage";
    }

    //查看详情
    @RequestMapping(value = "bondLimitApplyDetail")
    public String bondLimitApplyDetail(HttpServletRequest request,Model model){
        String id=request.getParameter("id");
        String from=request.getParameter("from"); //from = audit 那么就是审批操作  否则就是查看
        model.addAttribute("from",from);
        Record apply=bondLimitApplyService.getDetailById(id);
        model.addAttribute("apply",apply);
        List<ExamineInfo> infos=examineInfoService.getByApplyId(id,FormUtils.BOND_LIMIT_APPLY);
        model.addAttribute("infos",infos);
        if (StringUtils.isNotBlank(apply.getStr("voucher"))) {
            String[] imgArr = apply.getStr("voucher").split(",");
            List<String> imglist = Arrays.asList(imgArr);
            model.addAttribute("imglist", imglist);
        }
        User user=UserUtils.getUser();
        model.addAttribute("user",user);
        if(!"audit".equals(from)){
            flowableService.readApplication(apply.getStr("process_instance_id"),FormUtils.BOND_LIMIT_APPLY,id,user);
        }
        return "modules/flowpath/bondLimitApplyDetail";
    }

    //删除申请
    @ResponseBody
    @RequestMapping(value = "deleteBondLimitApply")
    public Result deleteBondLimitApply(HttpServletRequest request){
        String id=request.getParameter("id");
        try{
            bondLimitApplyService.deleteBondLimitApply(id);
            return Result.ok();
        }catch (RuntimeException r){
            r.printStackTrace();
            return Result.fail(r.getMessage());
        }catch (Exception e){
            logger.error(">>>>>>>>>>>>>>deleteBondLimitApply e:{}",e);
            e.printStackTrace();
            return Result.fail("删除失败！");
        }
    }

    //审核通过
    @ResponseBody
    @RequestMapping(value = "examineBondLimitApplyApprove")
    public Result examineBondLimitApplyApprove(UserApplyVo userApplyVo){
        bondLimitApplyService.examineBondLimitApplyApprove(userApplyVo.getId(),userApplyVo.getContent());
        return Result.ok();
    }

    //审核驳回
    @ResponseBody
    @RequestMapping(value = "rejectBondLimitApplyTask")
    public Result rejectBondLimitApplyTask(@Valid UserApplyVo userApplyVo){
        try{
            bondLimitApplyService.rejectBondLimitApplyApprove(userApplyVo.getId(),userApplyVo.getContent());
            return Result.ok();
        }catch (Exception e){
            logger.error(">>>>>>>>>>>>>>rejectBondLimitApplyTask e:{}",e);
            return Result.fail("审核失败！");
        }
    }


}
