package com.sifang.modules.sunshine.fittingManager.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.service.RecevierAddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.fittingManager.dao.CrmSiteFittingDao;
import com.sifang.modules.sunshine.fittingManager.entity.CrmSiteFitting;
import com.sifang.modules.sunshine.fittingManager.service.CrmSiteFittingService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.CrmUtils;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/site/fitting")
public class CrmSiteFittingController extends BaseController {
    @Autowired
    CrmSiteFittingService crmSiteFittingService;
    @Autowired
    CrmSiteFittingDao crmSiteFittingDao;
    @Autowired
    RecevierAddressService addressService;
    @Autowired
    AddressService provinService;

    @RequestMapping(value = "siteFitting")
    public String getPlantformFittingList(HttpServletRequest request , Model model){
        User user = UserUtils.getUser();
        if(User.DEL_FLAG_DELETE.equals(user.getUserType()) || User.USER_TYPE_WXG.equals(user.getUserType())){
            return "modules/yangguang/fittingManager/fitting/platFormsitefitting";

        }
        return "modules/yangguang/fittingManager/fitting/sitefitting";
    }

    @ResponseBody
    @RequestMapping("getSiteFittingData")
    public Object finishedFitApplyData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        crmSiteFittingService.getSiteFittingPage(page,map);
        return page.renderJson();
    }

    /**
     * 申请备件，可以批量操作
     */
    @RequestMapping("getbatchWarehousing")
    public String getbatchWarehousing(HttpServletRequest request,Model model){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        String ids = request.getParameter("ids");
        if(StringUtil.isNotBlank(ids)){
            List<Record> list = crmSiteFittingService.getSiteFitting(ids);
            model.addAttribute("list",list);
        }
            //获取服务商收货地址
        Record rd = addressService.getAddress(siteId);
        model.addAttribute("address",rd);
        List<Record> pros = provinService.getProvinces();
        model.addAttribute("pros",pros);
        List<Record> citys = AddressUtils.getCityByProName(rd != null?rd.getStr("province"):pros.get(0).getStr("ProvinceName"));
        List<Record> areas = AddressUtils.getAreaByCityName(rd != null?rd.getStr("city"):citys.get(0).getStr("CityName"));
        model.addAttribute("citys",citys);
        model.addAttribute("areas",areas);
        return "modules/yangguang/fittingManager/fitting/siteApply";
    }

    // 保存备件申请
    @ResponseBody
    @RequestMapping(value = "fittingInstockSave")
    public Result<Void> fittingInstockSave(HttpServletRequest request) {
        return crmSiteFittingService.fittingInstockSave(request);
    }

    /**
     * 备件详情页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("toFittingDetail")
    public String toFittingDetailNo(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        Record re = crmSiteFittingService.getApplyDetail(id);
        String licenseImg = re.getStr("img");
        List<String> imgs = new ArrayList<>();
        if (StringUtil.isNotBlank(licenseImg)) {
            String[] imgArr = licenseImg.split(",");
            for (String img : imgArr) {
                imgs.add(img);
            }
        }
        model.addAttribute("apply", re);
        model.addAttribute("imgs", imgs);
        return "modules/yangguang/fittingManager/fitting/fittingDetail";
    }

    @ResponseBody
    @RequestMapping("saveLocation")
    public Result saveLocation(HttpServletRequest request){
        String id = request.getParameter("id");
        String location = request.getParameter("location");
        CrmSiteFitting fitting = crmSiteFittingDao.get(id);
        fitting.setLocation(location);
        crmSiteFittingDao.save(fitting);
        return Result.ok();
    }

    @RequestMapping(value = "platformFitting")
    public String platformFitting(HttpServletRequest request , Model model){
        return "modules/yangguang/fittingManager/fitting/platformFittingApplyList";
    }

    @ResponseBody
    @RequestMapping("getPlatformFittingData")
    public Object getPlatformFittingData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        crmSiteFittingService.getPlatformFittingPage(page,map);
        return page.renderJson();
    }

    /**
     * 申请备件，可以批量操作
     */
    @RequestMapping("paltformFittingApply")
    public String paltformFittingApply(HttpServletRequest request,Model model){
        String ids = request.getParameter("ids");
        if(StringUtil.isNotBlank(ids)){
            List<Record> list = crmSiteFittingService.getPlatformFitting(ids);
            model.addAttribute("list",list);
        }
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        //获取服务商收货地址
        Record rd = addressService.getAddress(siteId);
        model.addAttribute("address",rd);
        List<Record> pros = provinService.getProvinces();
        model.addAttribute("pros",pros);
        List<Record> citys = AddressUtils.getCityByProName(rd != null?rd.getStr("province"):pros.get(0).getStr("ProvinceName"));
        List<Record> areas = AddressUtils.getAreaByCityName(rd != null?rd.getStr("city"):citys.get(0).getStr("CityName"));
        model.addAttribute("citys",citys);
        model.addAttribute("areas",areas);
        return "modules/yangguang/fittingManager/fitting/platformApply";
    }

    // 保存备件申请
    @ResponseBody
    @RequestMapping(value = "fittingApplySave")
    public Result<Void> fittingApplySave(HttpServletRequest request) {
        return crmSiteFittingService.fittingApplySave(request);
    }

    /**
     * 备件详情页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("toPlatformFittingDetail")
    public String toPlatformFittingDetail(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        Record re = crmSiteFittingService.getPlatformApplyDetail(id);
        String licenseImg = re.getStr("img");
        List<String> imgs = new ArrayList<>();
        if (StringUtil.isNotBlank(licenseImg)) {
            String[] imgArr = licenseImg.split(",");
            for (String img : imgArr) {
                imgs.add(img);
            }
        }
        model.addAttribute("apply", re);
        model.addAttribute("imgs", imgs);
        return "modules/yangguang/fittingManager/fitting/platformFittingDetail";
    }

}
