package com.iamberry.rent.handlers.admin;

import com.iamberry.rent.face.rebate.OrderRebateService;
import com.iamberry.wechat.core.entity.rent.RebateRecord;
import com.iamberry.wechat.tools.PayUtil;
import com.iamberry.wechat.tools.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 租赁订单返利 控制器
 * @author 献
 * @company Goochine-深圳古辰创智科技有限公司
 * @website www.goochine.com
 * @tel 18271840547
 * @date 2017/4/27
 */
@Controller
@RequestMapping("/admin/rebate")
public class AdminRebateHandler {

    @Autowired
    private OrderRebateService orderRebateService;

    @RequestMapping("/_list")
    public ModelAndView listUI(@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "pageNO", required = false, defaultValue = "1") Integer pageNO,
                               @RequestParam(value = "pageTotal", required = false) Integer pageTotal,
                               HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("admin/reserve/rebate/list");

        Map<String, Object> result = orderRebateService.listPayRebate(pageSize, pageNO, pageTotal == null);
        pageTotal = pageTotal == null ? ((Integer) result.get("pageTotal")) : pageTotal;
        // 为页面添加数据
        mv.addObject("list", result.get("list"));
        mv.addObject("pageSize", pageTotal);
        StringBuilder url = new StringBuilder(request.getRequestURI().replaceAll(request.getContextPath(), ""));
        url.append("?pageTotal=").append(pageTotal).append("&pageSize=").append(pageSize + "&pageNO=");
        mv.addObject("url", url);
        mv.addObject("pageNO", pageNO);
        return mv;
    }

    @RequestMapping("/transfer_pay")
    @ResponseBody
    public ResponseJson transferPay(@RequestParam("rebateId") Integer rebateId) {
        return orderRebateService.transPay(rebateId);
    }
}
