package com.community.huiju.controller;

import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.service.BillServiceI;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author FXF
 * @date 2018-10-25
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "缴费相关的API", description = "缴费相关的API")
public class BillController extends BaseController {
	
	@Autowired
	private BillServiceI billServiceI;
	
	@ApiOperation(value = "获取本人的收费账单信息(已缴费或未缴费)", notes = "获取本人的收费账单信息(已缴费或未缴费)")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "payType", value = "0是未缴费1是已缴费"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
	})
	@RequestMapping(value = "/bills/current_user/{payType}",method = RequestMethod.GET)
	public ResponseBean getBillInvoice(@PathVariable Integer payType,
									   @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									   @RequestParam(value ="pageSize",defaultValue = "5") Integer pageSize, HttpSession session){
		UserElement userElement = getUserElement(session);
		ResponseBean responseBean = new ResponseBean();

		if (!check(userElement.getCommunityId())) {
			responseBean.addError("小区不存在");
			return responseBean;
		}

		Map<String, Object> map = billServiceI.getBillsList(userElement, payType, pageNum, pageSize);
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	@ApiOperation(value = "获取账单的详细信息", notes = "获取账单的详细信息")
	@ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "String", name = "communityId", value = "小区Id"),
						@ApiImplicitParam(paramType = "path", dataType = "Integer", name = "billInvoiceId", value = "账单id")})
	@RequestMapping(value = "/bill/{communityId}/{billInvoiceId}",method = RequestMethod.GET)
	public ResponseBean getBillInvoiceDetail(@PathVariable Integer communityId,@PathVariable Integer billInvoiceId){
		ResponseBean responseBean = new ResponseBean();

		if (!check(communityId)) {
			responseBean.addError("小区不存在");
			return responseBean;
		}

		Map<String,Object> billInvoiceMap = billServiceI.getBillInvoiceDetail(communityId,billInvoiceId);
		responseBean.addSuccess(billInvoiceMap);
		return responseBean;
	}

	@ApiOperation(value = "获取历史缴费", notes = "获取历史缴费")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")
	})
	@RequestMapping(value = "/bill/pay/history", method = RequestMethod.GET)
	public ResponseBean payHistory(HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = billServiceI.payHistory(userElement);
		return responseBean;
	}

	@ApiOperation(value = "获取订单详情", notes = "获取订单详情")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataTypeClass = String.class, name = "orderNumber", value = "订单号")
	})
	@RequestMapping(value = "/bill/order/info/{orderNumber}", method = RequestMethod.GET)
	public ResponseBean getOrderInfo(HttpSession session, @PathVariable("orderNumber") String orderNumber) {
		ResponseBean responseBean = new ResponseBean();
		responseBean = billServiceI.getOrderInfo(orderNumber);
		return responseBean;
	}
}
