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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.utils.EmployeRoleUtils;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderContract;
import com.sifang.modules.sunshine.SystemSetting.form.AddTraderContractVo;
import com.sifang.modules.sunshine.SystemSetting.service.CrmTraderContractService;
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.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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

//代理商合同管理
@Controller
@RequestMapping(value = "${adminPath}/trader/contract")
public class CrmTraderContractController extends BaseExceptionController {

    @Autowired
    private CrmTraderContractService crmTraderContractService;

    @Autowired
    TraderService traderService;

    //合同管理页面
    @RequestMapping(value = "listPage")
    public String listPage(){
        return "modules/yangguang/systemSetting/trader/traderContractList";
    }

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


    //新增/编辑 页面
    @RequestMapping(value = "addContractPage")
    public String addContractPage(HttpServletRequest request,Model model){
        //代理商列表
        Map<String, String> map= getParams(request);
        List<Record> traderList = traderService.getTopTraders(map);
        model.addAttribute("traderList", traderList);
        String id=request.getParameter("id");
        if(StringUtil.isNotBlank(id)){
            CrmTraderContract traderContract= crmTraderContractService.getById(id);
            model.addAttribute("traderContract",traderContract);
            String status= crmTraderContractService.getLatestContractStatus(traderContract.getTraderId(),traderContract.getId());
            model.addAttribute("lastStatus",status);
        }
        return "modules/yangguang/systemSetting/trader/addContractPage";
    }

    // 暂存/提交合同
    @ResponseBody
    @RequestMapping(value = "saveContract")
    public Result saveContract(@RequestBody AddTraderContractVo addTraderContractVo){
        crmTraderContractService.saveContract(addTraderContractVo);
        return Result.ok();
    }

    //显示已选择代理商最近一份合同的状态
    @ResponseBody
    @RequestMapping(value = "getLatestContractStatus")
    public Result getLatestContractStatus(HttpServletRequest request){
        String traderId=request.getParameter("traderId");
        String status= crmTraderContractService.getLatestContractStatus(traderId,"");
        return Result.success(status);
    }

    //获取签约链接
    @ResponseBody
    @RequestMapping(value = "getContractSignUrl")
    public Result getContractSignUrl(String id){
        Map<String,String> result=crmTraderContractService.signUrl(id);
        return Result.success(result);
    }

    //合同作废
    @ResponseBody
    @RequestMapping(value = "cancelContract")
    public Result cancelContract(String id){
        crmTraderContractService.cancelContract(id);
        return Result.ok();
    }

    //获取合同下载的地址
    @ResponseBody
    @RequestMapping(value = "downLoadContract")
    public Result downLoadContract(String id){
        String downLoadUrl=crmTraderContractService.downLoadContract(id);
        Map<String,String> result=new HashMap<>();
        result.put("downLoadUrl",downLoadUrl);
        return Result.success(result);
    }

    //删除合同
    @ResponseBody
    @RequestMapping(value = "deleteContract")
    public Result deleteContract(String id){
        crmTraderContractService.deleteContract(id);
        return Result.ok();
    }

}
