package com.palmnest.webapp.controller.backstage;


import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.model.Element;
import com.palmnest.model.PushModel;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.estateserve.PushModelManger;
import com.palmnest.webapp.controller.BaseFormController;
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.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/pushModel/*")
public class PushModelController extends BaseFormController {

	@Autowired
	private PushModelManger pushModelManager;
	@Autowired
	private AppUserService  appUserService;

	/**
	 * 保存修改模板
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "addAndUpdatePushModel")
	public void addAndUpdatePushModel(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json         = new JSONObject();
		int        modelType    = Integer.parseInt(request.getParameter("modelType"));
		int        modelWay     = Integer.parseInt(request.getParameter("modelWay"));
		String     modelContent = request.getParameter("modelContent");
		String     modelId      = request.getParameter("pushModleId");
		String     orgId        = request.getParameter("orgId");
		User       user         = (User) request.getSession().getAttribute("UserCache");
		PushModel  push         = new PushModel();
		if (StringUtils.isNotBlank(modelId)) {
			push = pushModelManager.get(Integer.parseInt(modelId));
			push.setPushContent(modelContent);
			push.setBillType(modelType);
			push.setPushWay(modelWay);
			push.setModifier(user.getId());
			push.setOrgId(orgId);
			push.setModifyTime(new Date());
			push.setIsEnabled(1);
			pushModelManager.updateModel(push);
			json.put("result", "success");
			json.put("data", push);
			json.put("message", "");
			StringUtil.charSet(json, request, response);
		} else {
			push.setCreatorId(user.getId());
			push.setCreateTime(new Date());
			push.setIsEnabled(1);
			push.setOrgId(orgId);
			push.setPushContent(modelContent);
			push.setBillType(modelType);
			push.setPushWay(modelWay);
			push = pushModelManager.save(push);
			json.put("result", "success");
			json.put("data", push);
			json.put("message", "");
			StringUtil.charSet(json, request, response);
		}
	}

	/**
	 * 模板配置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addPushModelMenu")
	public ModelAndView addPushModelMenu(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model        model = new ExtendedModelMap();
		User         user  = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos  = appUserService.getUserOrgs(user.getId());
		model.addAttribute("orgs", dtos);
		String orgId = "";
		if (dtos != null && dtos.size() == 1) {
			OrgDto dto = dtos.get(0);
			orgId = dto.getId().toString();
		}
		List<PushModel> pushModelList = pushModelManager.getPushModel(orgId);
		model.addAttribute("pushModelList", pushModelList);
		List<Element> elList = this.getElementGroupManager().getElementsByGroupName("title");
		if (elList != null && elList.size() > 0) {
			for (int i = 0; i < elList.size(); i++) {
				Element ele = elList.get(i);
				if ("0".equals(ele.getItemcode())) {
					model.addAttribute("title1", ele.getItemname());//出账账单标题
				} else if ("1".equals(ele.getItemcode())) {
					model.addAttribute("title2", ele.getItemname());//逾期账单标题
				}
			}
		}

		List<Element> eleList = this.getElementGroupManager().getElementsByGroupName("remark");
		if (eleList != null && eleList.size() > 0) {
			for (int i = 0; i < eleList.size(); i++) {
				Element ele = eleList.get(i);
				if ("0".equals(ele.getItemcode())) {
					model.addAttribute("remark1", ele.getItemname());//出账账单备注
				} else if ("1".equals(ele.getItemcode())) {
					model.addAttribute("remark2", ele.getItemname());//逾期账单备注
				}
			}
		}

		return new ModelAndView("admin/billPush/templateSetting", model.asMap());
	}
}
