package com.zbkj.front.controller;

import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.*;
import com.zbkj.service.service.OrderService;
import com.zbkj.service.service.StoreOrderReturnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("StoreOrderFrontController")
@RequestMapping("api/front/return")
@Api(tags = "售后")
public class StoreOrderReturnController {

    @Autowired private OrderService orderService;
    @Autowired private StoreOrderReturnService storeOrderReturnService;

    /**
     * 获取申请订单退款信息
     * @param orderId 订单编号
     */
    @ApiOperation(value = "获取申请订单退款信息")
    @RequestMapping(value = "/getOrder/{orderId}", method = RequestMethod.GET)
    public CommonResult<ApplyRefundOrderInfoResponse> refundApplyOrder(@PathVariable String orderId) {
        return CommonResult.success(orderService.applyRefundOrderInfo(orderId));
    }

    /**
     * 根据参数计算订单价格
     */
    @ApiOperation(value = "获取退款计算")
    @RequestMapping(value = "/computedPrice", method = RequestMethod.POST)
    public CommonResult<RefundComputedResponse> refundComputedPrice(@Validated @RequestBody RefundComputedRequest request) {
        return CommonResult.success(orderService.refundComputedPrice(request));
    }


    /**
     * 订单退款申请
     * @param request OrderRefundApplyRequest 订单id
     */
    @ApiOperation(value = "订单退款申请")
    @RequestMapping(value = "/returnApply", method = RequestMethod.POST)
    public CommonResult<Boolean> returnApply(@RequestBody @Validated OrderRefundApplyRequest request) {
        if(orderService.refundApply(request)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 退款/售后-售后申请列表
     * @param pageRequest 分页
     * @return 订单列表
     */
    @ApiOperation(value = "退款/售后-售后申请列表")
    @RequestMapping(value = "/canApplyOrderList", method = RequestMethod.GET)
    public CommonResult<CommonPage<CanApplyOrderResponse>> canApplyOrderList(@ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(orderService.canApplyOrderList(pageRequest));
    }

    /**
     * 退款/售后-售后记录
     * @param pageRequest 分页
     * @return 订单列表
     */
    @ApiOperation(value = "退款/售后-售后记录")
    @RequestMapping(value = "/applyRecordsList", method = RequestMethod.GET)
    public CommonResult<CommonPage<ApplyRecordsResponse>> applyRecordsList(@ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(storeOrderReturnService.applyRecordsList(pageRequest));
    }

    /**
     * 售后记录详情
     * @param id
     * @return
     */
    @ApiOperation(value = "售后记录详情")
    @RequestMapping(value = "/getApplyRecord/{id}", method = RequestMethod.GET)
    public CommonResult<ApplyRecordsResponse> getApplyRecord(@PathVariable String id) {
        return CommonResult.success(storeOrderReturnService.getApplyRecord(id));
    }
}
