package com.sifang.modules.sunshine.SystemSetting.controller;

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

import com.sifang.modules.order.utils.OrderUtils;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.web.BaseController;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.service.CrmOrderSiteSettlementTemplateService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.util.LayuiPage;


/*
 * 业务层-服务商结算项设置 
 * 
 * */
@Controller
@RequestMapping(value = "${adminPath}/yangguang/siteSettlementSet")
public class CrmOrderSiteSettlementTemplateController extends BaseController {
	@Autowired
	private CrmOrderSiteSettlementTemplateService crmOrderSiteSettlementTemplateService;

	private Logger logger = LoggerFactory.getLogger(CrmOrderSiteSettlementTemplateController.class);

	// 服务商结算项列表页面
	@RequestMapping(value = "siteSettlementSetList")
	public String siteSettlementSetList(HttpServletRequest request, Model model) {



		return "modules/yangguang/systemSetting/siteSettlement/siteSettlementSetList";
	}

	/*
	 * 加载服务商列表数据
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "loadSettlementSetList")
	public Object loadSettlementSetList(HttpServletRequest request, HttpServletResponse response) {
		Page<Record> page = new Page<Record>(request, response);
		Map<String, String> mapRq = getParams(request);
		if (mapRq != null) {
			if (mapRq.get("limit") != null && StringUtils.isNotBlank(mapRq.get("limit").toString())) {
				page.setPageNo(Integer.valueOf(mapRq.get("page").toString()));
				page.setPageSize(Integer.valueOf(mapRq.get("limit").toString()));
			} else {
				page.setPageSize(20);
				page.setPageNo(1);
			}
		}
		crmOrderSiteSettlementTemplateService.loadSettlementSetPage(page, mapRq);
		List<Record> list = page.getList();

		return new LayuiPage(list, page.getCount()).renderJson();
	}

	// 删除服务商结算项-保存操作
	@ResponseBody
	@RequestMapping(value="saveDelSiteSettlementSet")
	public Result<Void> saveDelSiteSettlementSet(HttpServletRequest request) {
		Map<String, String> map = getParams(request);
		return crmOrderSiteSettlementTemplateService.saveDelSiteSettlementSet(map);
	}

	// 新增页面-服务商结算项设置
	@RequestMapping(value = "addSiteSettlementSetPage")
	public String addSiteSettlementSetPage(HttpServletRequest request, Model model) {

		String type = request.getParameter("type"); //维修，安装
		//获取服务措施
		List<Record> list = OrderUtils.getOrderServiceMeasures(type);
		logger.info("type : {} , listsize :{}  ",type ,list.size());
		model.addAttribute("list",list);
		model.addAttribute("serviceType",type);
		return "modules/yangguang/systemSetting/siteSettlement/addSiteSettlementSet";
	}

	// 新增服务商结算项-保存操作
	@ResponseBody
	@RequestMapping(value = "addSaveSiteSettlementSet")
	public Result<Void> addSaveSiteSettlementSet(HttpServletRequest request) {
		Map<String, String> map = getParams(request);
		return crmOrderSiteSettlementTemplateService.addSaveSiteSettlementSet(map);
	}

	// 修改页面-服务商结算项设置
	@RequestMapping(value = "editSiteSettlementSetPage")
	public String editSiteSettlementSetPage(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		// 根据品类、编号获取对应的结算项信息
		Record rd = crmOrderSiteSettlementTemplateService.getById(id);
		if(rd != null){
			String type = rd.getStr("service_type");

			List<Record> list = OrderUtils.getOrderServiceMeasures(type);
			model.addAttribute("list",list);
			model.addAttribute("serviceType",type);
		}
		model.addAttribute("rd", rd);
		return "modules/yangguang/systemSetting/siteSettlement/editSiteSettlementSetPage";
	}

	// 修改服务商结算项-保存操作
	@ResponseBody
	@RequestMapping(value = "editSaveSiteSettlementSet")
	public Result<Void> editSaveSiteSettlementSet(HttpServletRequest request) {
		Map<String, String> map = getParams(request);
		return crmOrderSiteSettlementTemplateService.editSaveSiteSettlementSet(map);
	}

}
