package com.bizzan.bitrade.controller.activity;

import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.Admin;
import com.bizzan.bitrade.service.ExperienceGiftWithdrawService;
import com.bizzan.bitrade.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

/**
 * 体验金提现审核接口
 */
@RestController
@RequestMapping("activity/experience-gift/withdraw")
public class ExperienceGiftWithdrawController extends BaseAdminController {

    @Autowired
    private ExperienceGiftWithdrawService withdrawService;

    /**
     * 提现申请分页列表
     */
    @PostMapping("page-query")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金提现审核列表查询")
    public MessageResult pageQuery(
            @RequestParam(required = false) Long memberId,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String applyStartTime,
            @RequestParam(required = false) String applyEndTime,
            @RequestParam(required = false) String reviewStartTime,
            @RequestParam(required = false) String reviewEndTime,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize) {

        Page<Map> page = withdrawService.pageQuery(
                memberId, status,
                applyStartTime, applyEndTime,
                reviewStartTime, reviewEndTime,
                pageNo, pageSize);
        return success(page);
    }

    /**
     * 用户体验金钱包详情（弹窗展示）
     */
    @GetMapping("member-info/{memberId}")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金用户详情查询")
    public MessageResult memberInfo(@PathVariable Long memberId) {
        Map<String, Object> info = withdrawService.getMemberInfo(memberId);
        return success(info);
    }

    /**
     * 单条审核（通过/驳回）
     *
     * @param id   提现申请ID
     * @param pass true=通过，false=驳回
     * @param remark 驳回备注（驳回时填写）
     */
    @PostMapping("audit/{id}")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金提现审核")
    public MessageResult audit(@PathVariable Long id,
                               @RequestParam boolean pass,
                               @RequestParam(required = false) String remark,
                               HttpServletRequest request) {
        Admin admin = getAdmin(request);
        if (admin == null) return error("请先登录");
        boolean ok = withdrawService.audit(id, pass, remark, admin.getId());
        if (!ok) return error("审核失败，该记录不存在或已审核");
        return success(pass ? "审核通过" : "已驳回");
    }

    /**
     * 批量审核（一键审核）
     *
     * @param ids    提现申请ID列表
     * @param pass   true=全部通过，false=全部驳回
     * @param remark 驳回备注
     */
    @PostMapping("batch-audit")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金提现批量审核")
    public MessageResult batchAudit(@RequestParam List<Long> ids,
                                    @RequestParam boolean pass,
                                    @RequestParam(required = false) String remark,
                                    HttpServletRequest request) {
        Admin admin = getAdmin(request);
        if (admin == null) return error("请先登录");
        if (ids == null || ids.isEmpty()) return error("请选择要审核的记录");
        int count = withdrawService.batchAudit(ids, pass, remark, admin.getId());
        return success("已审核 " + count + " 条");
    }
}
