package com.sifang.modules.completionrectification.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.dao.CompletionDao;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.completionrectification.service.CompletionService;
import com.sifang.modules.completionrectification.vo.CrmCompletionInfoVo;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderEmploye;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
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.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/completion")
public class CompletionController extends BaseExceptionController {

    @Autowired
    private CompletionService completionService;

    @Autowired
    AddressService addressService;

    @Autowired
    TraderService traderService;

    @Autowired
    CompletionDao completionDao;

    @RequestMapping(value = "addCompletion")
    public String addCompletion(Model model, HttpServletRequest request){
        String idStr = request.getParameter("idStr");
        String ids[] = idStr.split(",");
        List<Record> list = completionService.getPowerStationByIds(ids);
        model.addAttribute("list",list);
        model.addAttribute("entryId",idStr);
        return "modules/yangguang/completionRectification/completion";
    }

    /**
     * 竣工提交
     */
    @ResponseBody
    @RequestMapping("/saveCompletionData")
    public Result saveCompletionData(HttpServletRequest request, CrmCompletionInfoVo completionInfoVo){
        return completionService.saveCompletionData(completionInfoVo);
    }

    @RequestMapping(value = "completionOrder")
    public String completionOrder(Model model, HttpServletRequest request){
        List<Record> pros = addressService.getProvinces();
        //代理商列表
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        model.addAttribute("province", pros);
        return "modules/yangguang/completionRectification/completionOrder";
    }

    @ResponseBody
    @RequestMapping("/getCompletionOrderList")
    public Object getCompletionOrderList(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        completionService.getCompletionOrderList(page, map);
        return page.renderJson();
    }

    @RequestMapping(value = "completionDetail")
    public String completionDetail(Model model, HttpServletRequest request){
        String id = request.getParameter("id");
        String idStr = request.getParameter("entryId");
        String ids[] = idStr.split(",");
        List<Record> list = completionService.getPowerStationByIds(ids);
        model.addAttribute("list",list);
        model.addAttribute("entryId",idStr);
        //竣工信息
        Record compInfo = completionDao.getCrmCompletionInfoById(id);
        model.addAttribute("compInfo",compInfo);
        return "modules/yangguang/completionRectification/completionDetail";
    }

}