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

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

import com.alibaba.fastjson.JSON;
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.entity.TraderApply;
import com.sifang.modules.flowpath.service.ExamineInfoService;
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.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialBrandService;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialSettingService;
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.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sifang.modules.flowpath.entity.SupplySupplierApply;
import com.sifang.modules.flowpath.service.SupplySupplierApplyService;

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

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

	@Autowired
	private SupplySupplierApplyService supplySupplierApplyService;

	@Autowired
	private ExamineInfoService examineInfoService;

	@Autowired
	AddressService addressService;
	@Autowired
	private CrmSupplyMaterialSettingService materialSettingService;

	@Autowired
	private CrmSupplyMaterialBrandService brandService;

	@Autowired
	private ProcessFlowableService flowableService;

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@ModelAttribute
	public SupplySupplierApply get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return supplySupplierApplyService.get(id);
		}else{
			return new SupplySupplierApply();
		}
	}

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

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

	@RequestMapping(value = "addSupplierApply")
	public String addSupplierApply(String id, String supplierId, String type, Model model) {
		List<Record> province = addressService.getProvinces();//获取所有的省
		String[] licenseImgArray = {};
		if (org.apache.commons.lang3.StringUtils.isNotBlank(id)) {
			SupplySupplierApply apply = supplySupplierApplyService.get(id);

			model.addAttribute("apply", apply);

			List<Record> city = AddressUtils.getCityByProName(apply.getProvince());
			List<Record> area = AddressUtils.getAreaByCityName(apply.getCity());
			model.addAttribute("city", city);
			model.addAttribute("area", area);
			licenseImgArray = apply.getLicenseImg().split(",");
			model.addAttribute("task", "1");
		}else if(StringUtil.isNotBlank(supplierId)){
			SupplySupplierApply apply = supplySupplierApplyService.findBySupplierId(supplierId);

			model.addAttribute("apply", apply);

			List<Record> city = AddressUtils.getCityByProName(apply.getProvince());
			List<Record> area = AddressUtils.getAreaByCityName(apply.getCity());
			model.addAttribute("city", city);
			model.addAttribute("area", area);
			licenseImgArray = apply.getLicenseImg().split(",");
			model.addAttribute("task", "2");
		}
		model.addAttribute("materielTypeList", materialSettingService.listMaterielType());
		model.addAttribute("province", province);
		model.addAttribute("licenseImg", licenseImgArray);
		model.addAttribute("licenseImgCnt", licenseImgArray.length);
		model.addAttribute("type", type);
		List<Record> brands= brandService.getAllMaterialBrand();
		model.addAttribute("brands",brands);
		return "modules/flowpath/addSupplierApply";
	}

	@RequestMapping(value = "supplierApplyDetail")
	public String supplierApplyDetail(String id,String from,Model model,HttpServletRequest request){
		//String from=request.getParameter("from"); //from = audit 那么就是审批操作  否则就是查看
		List<Record> province = addressService.getProvinces();//获取所有的省
		model.addAttribute("from",from);
		SupplySupplierApply supplier = null;
		if (org.apache.commons.lang3.StringUtils.isNotBlank(id)) {
			supplier = supplySupplierApplyService.get(id);
			model.addAttribute("apply", supplier);
			List<Record> city = AddressUtils.getCityByProName(supplier.getProvince());
			List<Record> area = AddressUtils.getAreaByCityName(supplier.getCity());
			model.addAttribute("city", city);
			model.addAttribute("area", area);

		}
		model.addAttribute("province", province);
		List<ExamineInfo> infos=examineInfoService.getByApplyId(id,FormUtils.SUPPLIER_ADMISSION_APPLICATION);
		model.addAttribute("infos",infos);
		model.addAttribute("formId", FormUtils.SUPPLIER_ADMISSION_APPLICATION);
		model.addAttribute("materielTypeList", materialSettingService.listMaterielType());
		List<Record> brands= brandService.getAllMaterialBrand();
		model.addAttribute("brands",brands);
		if(!"audit".equals(from)){
			User user = UserUtils.getUser();
			flowableService.readApplication(supplier.getProcessInstanceId(),FormUtils.SUPPLIER_ADMISSION_APPLICATION,id,user);
		}
		return "modules/flowpath/supplierApplyDetail";


	}

	@ResponseBody
	@RequestMapping(value = "save")
	public Result save(@RequestBody SupplySupplierApply supplySupplierApply) {
		logger.info("into supplySupplierApply/save, req={}", JSON.toJSONString(supplySupplierApply));
		supplySupplierApplyService.save(supplySupplierApply);

		return Result.ok();
	}

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

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

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


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

}
