package com.seer.teach.mp.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.mp.admin.controller.req.MpDealerApplyQueryReq;
import com.seer.teach.mp.admin.controller.resp.MpDealerApplyResp;
import com.seer.teach.mp.request.DealerApplyReq;
import com.seer.teach.mp.admin.service.AdminDealerApplicationsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@LogPrint
@RequiredArgsConstructor
@RestController
@Tag(name = "管理端 - 经销商")
@RequestMapping("/mp/dealer/apply")
public class AdminDealerApplicationsController {


    private final AdminDealerApplicationsService dealerApplyService;


    @Operation(summary = "经销商申请列表")
    @GetMapping("/page-list")
    @SaCheckPermission("mp:admin:dealer:apply:list")
    public ResultBean<PageListBean<MpDealerApplyResp>> pageList(MpDealerApplyQueryReq query){
        return ResultBean.success(dealerApplyService.pageList(query));
    }

    @Operation(summary = "详情")
    @GetMapping("/{id}")
    @SaCheckPermission("mp:admin:dealer:apply:get")
    public ResultBean<MpDealerApplyResp> getApplication(@PathVariable Integer id) {
        return ResultBean.success(dealerApplyService.getApplicationById(id));
    }

    @Operation(summary = "申请")
    @PostMapping
    @SaCheckPermission("mp:admin:dealer:apply:get")
    public ResultBean<Boolean> apply(@RequestBody DealerApplyReq req) {
        return ResultBean.success(dealerApplyService.apply(req));
    }

    @Operation(summary = "审核通过")
    @PutMapping("/{id}/approve")
    @SaCheckPermission("mp:admin:dealer:apply:approve")
    public ResultBean<Boolean> approveApplication(@PathVariable Integer id) {
        return ResultBean.success(dealerApplyService.approveApplication(id, true, "审核通过"));
    }

    @Operation(summary = "审核拒绝")
    @PutMapping("/{id}/reject")
    @SaCheckPermission("mp:admin:dealer:apply:reject")
    public ResultBean<Boolean> rejectApplication(@PathVariable Integer id, @RequestParam String reason) {
        return ResultBean.success(dealerApplyService.approveApplication(id, false, reason));
    }

    @Operation(summary = "删除申请")
    @DeleteMapping
    @SaCheckPermission("mp:admin:dealer:apply:delete")
    public ResultBean<Boolean> deleteApplication(@RequestBody List<Integer> ids) {
        return ResultBean.success(dealerApplyService.deleteById(ids));
    }
}