package com.palmnest.webapp.controller.backstage;

import com.palmnest.application.AppConstants;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.RenovationManageAuditParam;
import com.palmnest.model.User;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.estateserve.release.RenovationManageService;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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


/**
 * 装修管理PC
 */
@Slf4j
@Controller
@RequestMapping(value = "/admin/renovation/*")
public class RenovationManageController extends BaseFormController {


	@Autowired
	private RenovationManageService renovationManageService;


	/**
	 * 装修信息配置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "jumpVirModel")
	public ModelAndView jumpVirModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model                model            = new ExtendedModelMap();
		List<TbOrganization> organizationList = getUserRelationOrgList(request);
		model.addAttribute("orgs", organizationList);
		return new ModelAndView("admin/renovation/enterSetting", model.asMap());
	}

	/**
	 * 装修信息配置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "jumpRenovationApplyList")
	public ModelAndView jumpRenovationApplyList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model                model            = new ExtendedModelMap();
		List<TbOrganization> organizationList = getUserRelationOrgList(request);
		model.addAttribute("orgs", organizationList);
		return new ModelAndView("admin/renovation/renovationApply", model.asMap());
	}


	/**
	 * @param request
	 * @return 物品放行列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public WebPageResultDto list(HttpServletRequest request) {
		log.info("RenovationManageController list======");
		try {
			int                 pageSize = Integer.parseInt(request.getParameter("pageSize"));
			int                 pageNo   = Integer.parseInt(request.getParameter("pageNo"));
			Map<String, Object> params   = new HashMap<String, Object>();
			//String openId = request.getParameter("openId");


			String virName = request.getParameter("virName");//企业名称
			if (StringUtils.isNotEmpty(virName)) {
				virName = virName.trim();
				params.put("virName", virName);
			}
			String applyName = request.getParameter("applyName");//申请人
			if (StringUtils.isNotEmpty(applyName)) {
				applyName = applyName.trim();
				params.put("applyName", applyName);
			}
			String status = request.getParameter("status");//状态 1-待审核;2-通过;3拒绝;
			if (StringUtils.isNotBlank(status)) {
				params.put("status", status);
			}
			String applyTime = request.getParameter("applyTime");//申请时间
			if (StringUtils.isNotEmpty(applyTime)) {
				applyTime = applyTime.trim();
				params.put("applyTime", applyTime);
			}

			params.put("startTime", request.getParameter("startTime"));
			params.put("endTime", request.getParameter("endTime"));


			User   user  = (User) request.getSession().getAttribute("UserCache");//----------联调时需放开注释
			String orgId = request.getParameter("orgId");
			if (StringUtils.isBlank(orgId)) {
				orgId = getUserManageOrgIds(user);
			}
			params.put("orgId", orgId);


			Page             page = renovationManageService.findPageBy(params, pageSize, pageNo);
			WebPageResultDto dto  = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", page.getItems());
			dto.setCount(page.getTotalCount());
			return dto;
		} catch (Exception e) {
			e.printStackTrace();
			return new WebPageResultDto(AppConstants.REPAIR_BILL_LIST_ERROR, "查询列表失败", LogExceptionStackUtil.LogExceptionStack(e));
		}
	}

	/**
	 * @return 查找电子放行条详情
	 */
	@RequestMapping(value = "/getDetail")
	@ResponseBody
	public AppResultDto getDetail(@RequestParam("id") Long id) {

		log.info("RenovationManageController getDetail======");

		return AppResultUtil.createSuccesDto(renovationManageService.findById(id));
	}

	/**
	 * 提交物品放行申请
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/audit")
	@ResponseBody
	public AppResultDto audit(@ModelAttribute("RenovationManageAuditParam") RenovationManageAuditParam dto, HttpServletRequest request) {

		log.info("RenovationManageController audit======");

		dto.setBillFrom(CodeConstants.PC);
		AppResultDto rdto = renovationManageService.audit(dto, request);

		return rdto;
	}

}
