package com.jeesharp.business.member.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jeesharp.business.modules.finance.api.MemberInvoiceApplyApi;
import com.jeesharp.business.modules.finance.invoice.dto.MemberInvoiceApplyCreateDTO;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceApplyResultVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/invoiceApply")
@Tag(name = "移动端-发票申请")
@AllArgsConstructor
public class InvoiceApplyMobileController {

    private final MemberInvoiceApplyApi memberInvoiceApplyApi;

    @Operation(summary = "创建发票申请")
    @PostMapping("/create")
    @SaCheckLogin
    public AjaxResult<MemberInvoiceApplyResultVO> create(@RequestBody @Valid MemberInvoiceApplyCreateDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberInvoiceApplyApi.create(memberId, dto));
    }

    @Operation(summary = "查看发票申请结果")
    @GetMapping("/result/{id}")
    @SaCheckLogin
    public AjaxResult<MemberInvoiceApplyResultVO> result(@PathVariable("id") String id) {
        return AjaxResult.okData(memberInvoiceApplyApi.getResult(id));
    }

    @Operation(summary = "按订单查看最新申请")
    @GetMapping("/order/{orderId}")
    @SaCheckLogin
    public AjaxResult<MemberInvoiceApplyResultVO> latestByOrder(@PathVariable("orderId") String orderId) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberInvoiceApplyApi.getLatestByOrder(memberId, orderId));
    }

    @Operation(summary = "我的发票列表")
    @GetMapping("/list")
    @SaCheckLogin
    public AjaxResult<List<MemberInvoiceApplyResultVO>> list(@RequestParam(value = "status", required = false) String status) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberInvoiceApplyApi.listByMember(memberId, status));
    }
}
