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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSite;
import com.sifang.modules.sunshine.SystemSetting.service.SiteInformationService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.setting.dao.MemberDao;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.util.LayuiPage;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务商信息---平台端维护  controller
 */
@Controller
@RequestMapping(value = "${adminPath}/siteInformation")
public class SiteInformationController extends BaseController{
    @Autowired
    private SiteInformationService siteInformationService;
    @Autowired
    private UserDao userDao;

    @Autowired
    AddressService addressService;

    private Logger logger = LoggerFactory.getLogger(SiteInformationController.class);
    /**
     * 跳转服务商信息页面
     * @param request
     * @return
     */
    @RequestMapping(value = "siteDataList")
    public String siteDataList(HttpServletRequest request,Model model){
       /* List<Map<String,Object>> map = siteInformationService.getAllCustomerMap();
        model.addAttribute("map",map);*/
        return "modules/yangguang/systemSetting/siteManage/siteList";
    }

    /**
     * 获取服务商信息列表
     * @param request
     * @return
     */
    @Autowired
    MemberDao memberDao;
    @ResponseBody
    @RequestMapping(value = "site")
    public Object getSite(HttpServletRequest request){
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map= getParams(request);
        User user = UserUtils.getUser();
        Record record=memberDao.getMemberByUserid(user.getId());
        if(record!=null){
            map.put("is_many",record.getStr("is_many"));
            map.put("provices",record.getStr("provices"));
            map.put("city",record.getStr("city"));
        }
        siteInformationService.getSite(page,map);
        return page.renderJson();
    }


    /**
     * 跳转绑定crm客户页面
     * @param request
     * @return
     */
    @RequestMapping(value = "bindingCustomer")
    public String bindingCustomer(HttpServletRequest request, Model model){
        //服务商id
        String id = request.getParameter("id");
        model.addAttribute("siteId",id);
        //获取CRM客户信息
        List<Map<String,Object>> map = siteInformationService.getCustomerMap();
        model.addAttribute("map",map);

        return "modules/yangguang/systemSetting/bindingCustomer";
    }

    /**
     * 绑定CRM客户
    */
    @ResponseBody
    @RequestMapping("siteBinDingCustomer")
    public Object siteBinDingCustomer(HttpServletRequest request){

        String siteId = request.getParameter("siteId");
        String cusromerId = request.getParameter("cusromerId");
        String customerName = request.getParameter("customerName");
        if(StringUtil.isBlank(siteId)){
            return Result.fail("202","数据有误没请刷新页面再试");
        }
        if(StringUtil.isBlank(cusromerId)){
            return Result.fail("202","请选择客户信息");
        }
        if(StringUtil.isBlank(customerName)){
            return Result.fail("202","请选择客户信息");
        }
        try {

            return siteInformationService.siteBinDingCustomer(siteId,cusromerId,customerName);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("202","绑定失败"+e);
        }

    }

    /**
     * 解除绑定
    */
    @ResponseBody
    @RequestMapping("Unbundling")
    public Object Unbundling(HttpServletRequest request){
        try {
            String siteId = request.getParameter("siteId");
            siteInformationService.Unbundling(siteId);

        }catch (Exception e){
            logger.error("解绑失败 错误  -->>>>>>>>>>>>>>>>> Exception : {}  ",e);
            e.printStackTrace();
            return Result.fail("解绑失败");
        }
        return Result.ok();
    }




    /**
     * 跳转添加服务商页面
     * @param request
     * @return
     */
   @RequestMapping(value = "addSite")
    public String addSite(HttpServletRequest request, Model model){
       String id = request.getParameter("id");
       List<Record> province = addressService.getProvinces();//获取所有的省
       int imgsCount=0;
       if (StringUtils.isNotBlank(id)) {
           CrmSite site = siteInformationService.getById(id);
           if (StringUtils.isNotBlank(site.getLicenseImg())) {
               String[] imgArr = site.getLicenseImg().split(",");
                imgsCount = imgArr.length;
               List<String> imglist = new ArrayList<>();
               for (String img:imgArr) {
                   imglist.add(img);
               }
               model.addAttribute("imglist", imglist);
           }
           User user = userDao.get(site.getUserId());
           model.addAttribute("site", site);
           model.addAttribute("user", user);
           List<Record> city = AddressUtils.getCityByProName(site.getProvince());
           List<Record> area = AddressUtils.getAreaByCityName(site.getCity());
           model.addAttribute("city", city);
           model.addAttribute("area", area);
       }
       model.addAttribute("imgsCount", imgsCount);
       model.addAttribute("province", province);
       return "modules/yangguang/systemSetting/siteManage/addSite";
   }


    /**
     * 跳转添加地址页面
     * @param request
     * @return
     */
    @RequestMapping(value = "addRess")
    public String addRess(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        Record siteAddress = siteInformationService.getCurrentSiteAddress(id);
        model.addAttribute("siteAddress",siteAddress);
        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province",province);
        if(siteAddress != null){
            if(StringUtils.isNotBlank(siteAddress.getStr("province"))){
                List<Record> city = AddressUtils.getCityByProName(siteAddress.getStr("province"));
                List<Record> area = AddressUtils.getAreaByCityName(siteAddress.getStr("city"));
                model.addAttribute("city",city);
                model.addAttribute("area",area);
            }

        }
        return "modules/yangguang/systemSetting/siteAddress";
    }

    /**
     * 添加地址保存
     * @param request
     * @return
     */
   @RequestMapping(value = "addRessSave")
   @ResponseBody
    public Object addRessSave(HttpServletRequest request){
       Map<String,String> map = getParams(request);
        return siteInformationService.addRessSave(map);
   }

    /**
     * 删除服务商
     * @param request
     * @return
     */
    @RequestMapping(value = "deleteSite")
    @ResponseBody
    public Object deleteSite(HttpServletRequest request){
        String ids = request.getParameter("ids");
        return siteInformationService.deleteSite(ids);
    }

    /**
     * 添加保存服务商
     * @param request
     * @return
     */
    @RequestMapping(value = "siteSave")
    @ResponseBody
    public Object siteSave(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        return siteInformationService.saveSite(map);
    }


    /**
     * 跳转修改地址页面
     * @param request
     * @return
     */
    @RequestMapping(value = "editAddRess")
    public String editAddRess(HttpServletRequest request, Model model){
        String id = request.getParameter("id");
        Record siteEditorAddress = siteInformationService.getCurrentSiteEditorAddress(id);
        model.addAttribute("siteEditorAddress",siteEditorAddress);
        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province",province);
        if(StringUtils.isNotBlank(siteEditorAddress.getStr("province"))){
            List<Record> city = AddressUtils.getCityByProName(siteEditorAddress.getStr("province"));
            model.addAttribute("city",city);
            List<Record> area = AddressUtils.getAreaByCityName(siteEditorAddress.getStr("city"));
            model.addAttribute("area",area);
        }
        return "modules/yangguang/systemSetting/siteEditorAddress";
    }

    /**
     * 修改地址后保存
     * @param request
     * @return
     */
    @RequestMapping(value = "editorAddressSave")
    @ResponseBody
    public Object editorAddressSave(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        return siteInformationService.editorAddressSave(map);
    }


}
