package com.sifang.modules.changes.controller;

import com.jfinal.plugin.activerecord.Record;

import com.sifang.modules.changes.entity.CustomerEntrySupplementDetail;
import com.sifang.modules.changes.service.CustomerEntrySupplementService;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.utils.EmployeRoleUtils;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectSetting;
import com.sifang.modules.sunshine.SystemSetting.service.FormSetService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectInfoSettingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 并网信息变更
 * 开发说明：
 * 1、进件状态 待提交并网资料，并网资料审批拒绝  可以并网信息变更
 * 2、进件状态是待提交并网资料 时不需要推送进件，并网资料拒绝的 变更要再次推送一下
 *
 */
@Controller
@RequestMapping("${adminPath}/onGridInfo/change")
public class CustomerEntrySupplementController extends BaseExceptionController {


    @Autowired
    private CustomerEntryService entryService;

    @Autowired
    ProjectInfoSettingService projectService;

    @Autowired
    private CustomerEntrySupplementService supplementService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private FormSetService formSetService;

    //页面
    @RequestMapping(value = "onGridChangeInfoChangeList")
    public String customerContractList(HttpServletRequest request, Model model) {
        User user = UserUtils.getUser();
        model.addAttribute("user", user);
        return "modules/yangguang/changes/supplementChange/onGridChangeInfo";
    }
    //变更记录
    @RequestMapping("changeRecord")
    public String changeRecord(){
        return "modules/yangguang/changes/supplementChange/onGridInfoChangeRecord";
    }

    //列表数据
    @ResponseBody
    @RequestMapping(value = "onGridChangeInfoData")
    public Object onGridChangeInfoData(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> params = getParams(request);
        User user = UserUtils.getUser();
        if (User.USER_TYPE_WXG.equals(user.getUserType())) { //平台员工
            EmployeRoleUtils.getUserDataAuthority(user.getId(),params);
        }
        supplementService.listData(page, params, user);
        return page.renderJson();
    }


    //新增变更申请页面
    @RequestMapping("/toAddPage")
    public String toAddPage(Model model) {
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("pros", pros);
        List<Record> procodes = addressService.getProviceCodeList();
        model.addAttribute("procodes", procodes);
        return "modules/yangguang/changes/supplementChange/addChangePage";
    }

    /**
     * 添加变更记录/修改变更记录
     * @param recordVo
     * @return
     */
    @ResponseBody
    @RequestMapping("addChangeRecord")
    public Result addChangeRecord(@RequestBody CustomerEntrySupplementDetail recordVo) {
        supplementService.addChangeRecord(recordVo);
        return Result.ok();
    }



    /**
     * 变更详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toComDetail/{id}")
    public String toComDetail(@PathVariable("id") String id, Model model) {
        Record record=supplementService.getChangeRecord(id);
        CustomerEntry customerEntry = entryService.getById(record.getStr("entry_id"));
        CrmProjectSetting projectSetting = projectService.getById(customerEntry.getProjectId());
        model.addAttribute("customerEntry", customerEntry);
        model.addAttribute("project", projectSetting);
        CustomerEntrySupplementDetail detailOld= supplementService.getChangeRecordDetail(record.getStr("id"),"0");
        CustomerEntrySupplementDetail detailNew= supplementService.getChangeRecordDetail(record.getStr("id"),"1");
        model.addAttribute("detailOld",detailOld);
        model.addAttribute("detailNew",detailNew);
        model.addAttribute("changeRecord",record);
        model.addAttribute("form","look");
        String provinceCodeOld=AddressUtils.findProCodeNameByCode(detailOld.getProvinceCode());
        model.addAttribute("provinceCodeOld",provinceCodeOld);
        String provinceCodeNew=AddressUtils.findProCodeNameByCode(detailNew.getProvinceCode());
        model.addAttribute("provinceCodeNew",provinceCodeNew);
        model.addAttribute("mergeInfoSet", formSetService.getListByCondition("10", customerEntry.getPartnerId()));
        return "modules/yangguang/changes/supplementChange/onGridInfoChangeDetail";
    }

    /**
     * 审核页面
     *
     * @return
     */
    @RequestMapping("audit/{id}")
    public String toAuditPage(@PathVariable("id") String id, Model model) {
        Record record=supplementService.getChangeRecord(id);
        CustomerEntry customerEntry = entryService.getById(record.getStr("entry_id"));
        CrmProjectSetting projectSetting = projectService.getById(customerEntry.getProjectId());
        model.addAttribute("customerEntry", customerEntry);
        model.addAttribute("project", projectSetting);
        CustomerEntrySupplementDetail detailOld= supplementService.getChangeRecordDetail(record.getStr("id"),"0");
        CustomerEntrySupplementDetail detailNew= supplementService.getChangeRecordDetail(record.getStr("id"),"1");
        model.addAttribute("detailOld",detailOld);
        model.addAttribute("detailNew",detailNew);
        model.addAttribute("changeRecord",record);
        model.addAttribute("form","audit");
        String provinceCodeOld=AddressUtils.findProCodeNameByCode(detailOld.getProvinceCode());
        model.addAttribute("provinceCodeOld",provinceCodeOld);
        String provinceCodeNew=AddressUtils.findProCodeNameByCode(detailNew.getProvinceCode());
        model.addAttribute("provinceCodeNew",provinceCodeNew);
        model.addAttribute("mergeInfoSet", formSetService.getListByCondition("10", customerEntry.getPartnerId()));
        return "modules/yangguang/changes/supplementChange/onGridInfoChangeDetail";
    }

    /**
     * 审核通过
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("temp")
    public Result temp(HttpServletRequest request) {
        String id=request.getParameter("id");
        String auditRemarks=request.getParameter("auditRemarks");
        supplementService.temp(id,auditRemarks);
        return Result.ok();
    }

    /**
     * 审核通过
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("pass")
    public Result pass(HttpServletRequest request) {
        String id=request.getParameter("id");
        String auditRemarks=request.getParameter("auditRemarks");
        supplementService.pass(id,auditRemarks);
        return Result.ok();
    }

    /**
     * 审核不通过
     * @return
     */
    @ResponseBody
    @RequestMapping("refuse")
    public Result refuse(HttpServletRequest request) {
        String id=request.getParameter("id");
        String auditRemarks=request.getParameter("auditRemarks");
        supplementService.refuse(id,auditRemarks);
        return Result.ok();
    }

    /**
     * 撤回
     * @return
     */
    @ResponseBody
    @RequestMapping("cancel/{id}")
    public Result cancel(@PathVariable("id") String id) {
        supplementService.back(id);
        return Result.ok();
    }

}
