package com.sifang.modules.account.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.account.entity.CrmTraderAccount;
import com.sifang.modules.account.entity.CrmTraderSupportSettleDetails;
import com.sifang.modules.account.service.CrmTraderSupportSettleDetailsService;
import com.sifang.modules.account.service.TraderAccountService;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.flowpath.entity.ExamineInfo;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.supply.order.service.SupplyOrderAuditService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.StringUtil;
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.List;
import java.util.Map;

//支架账户
@Controller
@RequestMapping(value = "${adminPath}/supportAccount")
public class CrmTraderSupportSettleDetailsController extends BaseExceptionController {

    @Autowired
    private TraderService traderService;

    @Autowired
    private CrmTraderSupportSettleDetailsService supportSettleDetailsService;

    @Autowired
    private TraderAccountService accountService;

    @Autowired
    SupplyOrderAuditService supplyOrderService;

    /**
     * 支架账户--支架账户
     */
    @RequestMapping(value = "toSupportAccountDetail")
    public String toSupportAccountDetail(HttpServletRequest request, Model model){
        //代理商列表
        List<Record> traderList = traderService.getTopTrader2();
        model.addAttribute("traderList", traderList);
        User user = UserUtils.getUser();
        model.addAttribute("user", user);
        if (UserConstants.TYPE_TRADER.equals(user.getUserType())) {
            CrmTrader trader = traderService.getTraderBuserId(user.getId());
            CrmTraderAccount account = accountService.findByTraderId(trader.getId());
            model.addAttribute("arrearsAmount", "");
        }
        return "modules/yangguang/account/zj/supportAccountList";
    }

    //待结算账户  变动明细列表数据
    @ResponseBody
    @RequestMapping(value = "supportAccountListData")
    public String supportAccountListData(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        User user = UserUtils.getUser();
        if (UserConstants.TYPE_TRADER.equals(user.getUserType())) {
            CrmTrader trader = traderService.getTraderBuserId(user.getId());
            map.put("traderId",trader.getId());
        }else if(User.USER_TYPE_ADVISOR.equals(user.getUserType())){
            String traderId = CrmUtils.getUserRelId(user);
            CrmTrader trader = traderService.getById(traderId);
            map.put("traderId",trader.getId());
        }if(User.USER_TYPE_WXG.equals(user.getUserType())){
            //平台人员
            //获取当前人员所负责代理商
            Record rd = CrmUtils.getByUserId(user);
            String trader_id=rd.getStr("trader_id");
            if(StringUtil.isNotBlank(trader_id)){
                map.put("traderId",trader_id);
            }
        }
        LayuiPage page = new LayuiPage(request);
        supportSettleDetailsService.supportAccountListData(page,map);
        return page.renderJson();
    }

    // 详情 页面
    @RequestMapping(value = "/supportAccountDetailPage")
    public String supportAccountDetailPage(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        CrmTraderSupportSettleDetails details= supportSettleDetailsService.getById(id);
        model.addAttribute("details",details);
        model.addAttribute("orderType",details.getType());
        // 订单详情
        Record order=supplyOrderService.orderDetail(details.getRelateOrderId());
        model.addAttribute("order",order);
        //物料信息
        List<Record> mates=supplyOrderService.getOrderMaterials(details.getRelateOrderId());
        model.addAttribute("details",mates);
        return "modules/yangguang/account/zj/supportAccountDetailPage";
    }
}
