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


import com.sifang.modules.sunshine.SystemSetting.entity.CrmSite;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSiteCashOut;
import com.sifang.modules.sunshine.SystemSetting.entity.SiteReceiptInformation;
import com.sifang.modules.sunshine.SystemSetting.service.SiteReceiptInformationService;
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.CrmSiteUtils;
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 javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/siteReceipt")
public class SiteReceiptInformationController extends BaseController{

    @Autowired
    private SiteReceiptInformationService receService;

    @ResponseBody
    @RequestMapping(value = "saveSiteReceipt")
    public Object saveSiteReceipt(HttpServletRequest request, HttpServletResponse response, SiteReceiptInformation siterece){
        User user = UserUtils.getUser();
        String siteId = CrmUtils.getSiteId(user);
        String code = "200";
           try {
               siterece.setSiteId(siteId);
               receService.saveSiteReceipt(siterece);
           }catch (Exception e){
               code = "205";
               e.printStackTrace();
               throw e;
           }

        return Result.fail(code,"site");
    }

    @RequestMapping(value = "getCrmSiteCashOutPage")
    public String getCrmSiteCashOutPage(HttpServletRequest request, Model model){
        model.addAttribute("sites", CrmSiteUtils.getSiteConcise());

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

    @ResponseBody
    @RequestMapping(value = "getCrmSiteCashOutList")
    public Object getCrmSiteCashOutList(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        User user = UserUtils.getUser();
        String siteId = map.get("siteId");
        if(User.USER_TYPE_FWS.equals(user.getUserType()) || User.USER_TYPE_FWSRY.equals(user.getUserType())){
            siteId = CrmUtils.getSiteId(user);
        }
        LayuiPage page = new LayuiPage(request);
        receService.getCashOutPage(page,map,siteId);
        return page.renderJson();
    }

    /**
     * 登记信息
    */
    @ResponseBody
    @RequestMapping(value = "registerCashOut")
    public Object registerCashOut(HttpServletRequest request){
        String id = request.getParameter("id");
        String remark = request.getParameter("remark");
        try {
            receService.registerCashOut(id,remark);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("205","登记失败");
        }
    }



    /**
 * 跳转到结账页面
 */
    @RequestMapping(value = "siteSettleWithdraw")
    public String siteSettleWithdraw(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        model.addAttribute("id", id);
        if(StringUtil.isNotBlank(id)){
            CrmSiteCashOut out = receService.getById(id);
            model.addAttribute("out",out);
        }
        return "modules/yangguang/systemSetting/siteSettleWithdraw";
    }

    /**
     * 结账请款
    */
    @ResponseBody
    @RequestMapping(value = "CashSettlement")
    public Object CashSettlement(HttpServletRequest request){
        String id = request.getParameter("id");
        String imgs = request.getParameter("imgs");
        try {
            return receService.CashSettlement(id,imgs);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("205","结账失败");
        }
    }

    /**
     * 服务商查看请款明细
    */
    @RequestMapping(value = "getsiteCashOutList")
    public String getsiteCashOutList(HttpServletRequest request,Model model){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        model.addAttribute("siteId",siteId);

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

    /**
     * 服务商申请请款弹框
    */
    @RequestMapping(value = "siteApplePage")
    public String siteApplePage(HttpServletRequest request,Model model){

        String siteId = request.getParameter("siteId");
        if(StringUtil.isBlank(siteId)){
            siteId = CrmUtils.getSiteId(UserUtils.getUser());
        }
        CrmSite site = receService.getCrmSiteById(siteId);

        model.addAttribute("site",site);
        model.addAttribute("siteId",siteId);

        SiteReceiptInformation rece = receService.getBySiteId(siteId);
        model.addAttribute("rece",rece);

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

    /**
     * 服务商申请请款  ***
     */
    @ResponseBody
    @RequestMapping(value = "saveSiteCashOut")
    public Object saveSiteCashOut(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        User user = UserUtils.getUser();
        map.put("applicantName",CrmUtils.getUserXM(user));
        map.put("applicantId",CrmUtils.getSiteId(user));
        String siteId = CrmUtils.getSiteId(user);
        map.put("siteId",siteId);
        Result result = receService.applicantCashOut(map);
        return result;

    }
    /**
     * 服务商申请请款或者转货款的时候上传凭证
     */
    @RequestMapping(value="toUplodeImage")
    public String toUplodeImage(HttpServletRequest request,Model model){
        String ids = request.getParameter("ids");
        String type=request.getParameter("type");
        String len=request.getParameter("len");
        model.addAttribute("ids", ids);
        model.addAttribute("type",type);
        model.addAttribute("len",len);
        return "modules/yangguang/systemSetting/upLodeImagePage";
    }
    /**
     * 驳回取消服务商申请
    */
    @ResponseBody
    @RequestMapping(value = "cancelRejection")
    public Object cancelRejection(HttpServletRequest request){

        String id = request.getParameter("id");
        String title = request.getParameter("title");
        if(StringUtil.isBlank(id)){
            return Result.fail("202","请求数据为空，请稍后再试");
        }
        if(StringUtil.isBlank(title)){
            return Result.fail("202","请输入取消驳回原因");
        }
        Result result =receService.cancelRejection(id,title);
        return result;
    }

}
