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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmOrderServiceMeasures;
import com.sifang.modules.sunshine.SystemSetting.service.CrmOrderServiceMeasuresService;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
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.sifang.modules.sys.common.persistence.Page;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统设置---平台端服务措施 controller
 */

@Controller
@RequestMapping(value = "${adminPath}/serviceMeasure")
public class SystemMeasureController extends BaseController{
  @Autowired
    CrmOrderServiceMeasuresService  measuresService;

//服务措施页面
    @RequestMapping(value="operationServiceMeasure")
    public String  toServiceMeasureIndex(HttpServletRequest request, Model model){
        return  "modules/yangguang/systemSetting/serviceMeasureIndex";
    }
//获取服务措施页面数据
    @ResponseBody
    @RequestMapping(value="getServiceData")
    public String getServiceData(HttpServletRequest request,Model model,Page< Record > page){
         measuresService.getServiceMeasuresList(page);
        List<Record> list = page.getList();
      for(Record or: list){
          List<Record> listChild=measuresService.getMeasuresList((String) or.get("service_type"));
            or.set("detail", listChild);
      }
        return new LayuiPage(list, page.getCount()).renderJson();
    }

    //跳转到添加页面
    @RequestMapping(value="addMeasure")
    public String addMeasure(HttpServletRequest re){
       return  "modules/yangguang/systemSetting/addMeasure";
    }

    //保存添加的信息
    @ResponseBody
    @RequestMapping(value="saveMeasure")
    public String saveMeasure(HttpServletRequest request){
        String jsonData = request.getParameter("jsonData");
        try {
            return  measuresService.saveMeasure(jsonData);

        } catch (Exception e) {
            e.printStackTrace();
            return "202";
        }
    }

//删除页面上的服务措施
    @ResponseBody
    @RequestMapping(value="deleMeasure")
    public String deleMeasure(HttpServletRequest request){
        String  service_measures=request.getParameter("service_measures");
        try{
             String code = measuresService.deleMeasure(service_measures);
             return code;
        }catch (Exception e){
            e.printStackTrace();
            return "202";//删除失败
        }
    }


    //移除添加页面上的服务措施
    @ResponseBody
    @RequestMapping(value="removeMeasure")
    public String removeMeasure(HttpServletRequest request){
        String  service_measures=request.getParameter("service_measures");
        try{
            String code = measuresService.removeMeasure(service_measures);
            return code;
        }catch (Exception e){
            e.printStackTrace();
            return "202";//删除失败
        }
    }
//跳转到修改页面
    @RequestMapping(value="editMeasure")
    public String  editMeasure(HttpServletRequest request,Model model){
        String  serviceType=request.getParameter("service_type");
      List<CrmOrderServiceMeasures> list=measuresService.getDetailMeasure(serviceType);
      model.addAttribute("serviceMeasure",list);
      model.addAttribute("serviceType",serviceType);
        return "modules/yangguang/systemSetting/editMeasure";
    }

//保存修改的内容
    @ResponseBody
    @RequestMapping(value="saveEditMeasure")
    public  String editMeasure(HttpServletRequest request){
        String jsonData = request.getParameter("jsonData");
        String   service_type= request.getParameter("service_type");
      String code= measuresService.saveEditMeasure(jsonData,service_type);
        return code;
    }
}
