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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmOrderCustomerSettlementTemplate;
import com.sifang.modules.sunshine.SystemSetting.service.CrmOrderCustomerSettlementTemplateService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.order.utils.OrderUtils;
import com.sifang.modules.sys.common.persistence.Page;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/*
 * 业务层-结算项设置
 * 
 * */
@Controller
@RequestMapping(value = "${adminPath}/yangguang/customerSettlementSet")
public class CrmOrderCustomerSettlementTemplateController extends BaseController {

	@Autowired
	private CrmOrderCustomerSettlementTemplateService crmOrderCustomerSettlementTemplateService;


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

		return "modules/yangguang/systemSetting/customerSettlement/customerSettlementSetList";
	}

	/*
	 * 加载用户结算项列表数据
	 */
	@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.setPageSize(Integer.valueOf(mapRq.get("limit").toString()));
				page.setPageNo(Integer.valueOf(mapRq.get("page").toString()));
			} else {
				page.setPageSize(20);
				page.setPageNo(1);
			}
		}
		crmOrderCustomerSettlementTemplateService.loadSettlementSetPage(page, mapRq);
		List<Record> list = page.getList();
		return new LayuiPage(list, page.getCount()).renderJson();
	}

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

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

		//获取服务措施
		List<Record> list = OrderUtils.getOrderServiceMeasures("维修");
		model.addAttribute("list",list);
		return "modules/yangguang/systemSetting/customerSettlement/addCustomerSettlementSet";
	}

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

	// 修改页面结算项设置
	@RequestMapping(value = "editCustomerSettlementSetPage")
	public String editCustomerSettlementSetPage(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		CrmOrderCustomerSettlementTemplate sett = crmOrderCustomerSettlementTemplateService.getById(id);
		model.addAttribute("sett",sett);

		//根据服务类型获取服务措施
		List<Record> sers = OrderUtils.getOrderServiceMeasures("维修");
		model.addAttribute("sers",sers);

		return "modules/yangguang/systemSetting/customerSettlement/editCustomerSettlementSetPage";
	}

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


}
