/**
 */
package com.sifang.modules.flowpath.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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.ExamineInfoService;
import com.sifang.modules.flowpath.vo.TraderApplyVo;
import com.sifang.modules.flowpath.vo.UserApplyVo;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.q7link.utils.Q7DataUtil;
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.BaseExceptionController;
import org.apache.commons.lang.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.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import com.sifang.modules.flowpath.entity.TraderApply;
import com.sifang.modules.flowpath.service.TraderApplyService;

import java.util.List;
import java.util.Map;

/**
 * 代理商审批信息Controller
 * @author lzp
 * @version 
 */
@Controller
@RequestMapping(value = "${adminPath}/flowpath/traderApply")
public class TraderApplyController extends BaseExceptionController {

	@Autowired
	private TraderApplyService traderApplyService;

	@Autowired
	AddressService addressService;

	@Autowired
	private ProcessFlowableService flowableService;

	@Autowired
	private ExamineInfoService examineInfoService;
	private Logger logger = LoggerFactory.getLogger(getClass());



	@ModelAttribute
	public TraderApply get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return traderApplyService.get(id);
		}else{
			return new TraderApply();
		}
	}
	

	@RequestMapping(value = "traderApplyList")
	public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		model.addAttribute("formId", FormUtils.AGENT_ADMISSION_APPLICATION);
		model.addAttribute("userId", user.getId());
		return "modules/flowpath/traderApplyList";
	}

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

	@RequestMapping(value = "addTrderApply")
	public String addTrderApply(String id, Model model) {
		List<Record> province = addressService.getProvinces();//获取所有的省

		if (org.apache.commons.lang3.StringUtils.isNotBlank(id)) {
			TraderApply trader = traderApplyService.get(id);
			List<Record> agent = traderApplyService.findAgentList(id);
			model.addAttribute("agents", agent);
			model.addAttribute("trader", trader);

			List<Record> city = AddressUtils.getCityByProName(trader.getProvince());
			List<Record> area = AddressUtils.getAreaByCityName(trader.getCity());
			model.addAttribute("city", city);
			model.addAttribute("area", area);

			List<Record> city2 = AddressUtils.getCityByProName(trader.getTakeDeliveryProvince());
			List<Record> area2 = AddressUtils.getAreaByCityName(trader.getTakeDeliveryCity());
			model.addAttribute("city2", city2);
			model.addAttribute("area2", area2);
		}

		model.addAttribute("province", province);
		List<Record> qqProjectCodes= Q7DataUtil.getAllQQProjectCode();
		model.addAttribute("qqProjectCodes",qqProjectCodes);
		return "modules/flowpath/addTraderApply";
	}

	@RequestMapping(value = "traderApplyDetail")
	public String traderApplyDetail(String id, Model model,HttpServletRequest request){
		String from=request.getParameter("from"); //from = audit 那么就是审批操作  否则就是查看
		model.addAttribute("from",from);
		TraderApply trader=null;
		if (org.apache.commons.lang3.StringUtils.isNotBlank(id)) {
			 trader = traderApplyService.get(id);
			model.addAttribute("trader", trader);
			List<Record> agent = traderApplyService.findAgentList(id);
			model.addAttribute("agents", agent);

		}
		List<ExamineInfo> infos=examineInfoService.getByApplyId(id,FormUtils.AGENT_ADMISSION_APPLICATION);
		model.addAttribute("infos",infos);
		if(!"audit".equals(from)){
			User user = UserUtils.getUser();
			flowableService.readApplication(trader.getProcessInstanceId(),FormUtils.AGENT_ADMISSION_APPLICATION,id,user);
		}
		return "modules/flowpath/traderApplyDetail";


	}

	@ResponseBody
	@RequestMapping(value = "save")
	public Result save(@RequestBody TraderApplyVo traderApplyVo, String from, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return errRet(bindingResult);
		}
		if("1".equals(from)){
			traderApplyService.add(traderApplyVo);
		}else{
			traderApplyService.save(traderApplyVo,from);
		}


		return Result.ok();
	}
	
	@ResponseBody
	@RequestMapping(value = "delete")
	public Result delete(String id, RedirectAttributes redirectAttributes) {
		traderApplyService.delete(id);

		return Result.ok();
	}



	@ResponseBody
	@RequestMapping(value = "examineTraderApply")
	public Result examineTraderApply(String id ,String taskId,String content){

		traderApplyService.examineTraderApply(id, taskId, content);
		return Result.ok();
	}


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


}
