/*
 * @Author: mojunshou 1637302775@qq.com
 * @Date: 2025-03-21 10:17:49
 * @LastEditors: mojunshou 1637302775@qq.com
 * @LastEditTime: 2025-04-23 09:57:20
 * @Description: 提现记录
 */
import { _decorator, Label, Node } from "cc";
import { oops } from "db://oops-framework/core/Oops";
import { DeviceUtil } from "db://oops-framework/core/utils/DeviceUtil";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { CCComp } from "db://oops-framework/module/common/CCComp";
import { UIID } from "../common/config/GameUIConfig";
import { smc } from "../common/SingletonModuleComp";
import List from "../ui/List";
import { Format } from "../utils/Format";

const { ccclass, property } = _decorator;

/** 视图层对象 */
@ccclass('WithdrawalRecordViewComp')
@ecs.register('WithdrawalRecordView', false)
export class WithdrawalRecordViewComp extends CCComp {
    /** 视图层逻辑代码分离演示 */
    @property(List)
    recordList: List = null!;

    data: any[] = [];
    @property(Node)
    noRecord: Node = null!;

    @property(Node)
    content: Node = null!;

    start() {
        this.setButton();
        // const entity = this.ent as ecs.Entity;         // ecs.Entity 可转为当前模块的具体实体对象
        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
            const count = smc.game.GameModel.recordList.length;
            this.noRecord.active = count <= 0;
            this.content.active = count > 0;
            if (count > 0) {
                this.data = smc.game.GameModel.recordList;
                this.recordList.numItems = this.data.length;
            }
        } else {
            const data = {
                amount: 0.01,
                payMethod: 1,  //1支付宝 2微信
                createTime: "2025-04-09 18:09:09",
                successTime: "2025-04-09 18:09:09",
                status: oops.random.getRandomInt(0, 4)
            }
            for (let i = 0; i < 20; i++) {
                data.amount = i;
                this.data.push(data);
            }
            this.recordList.numItems = this.data.length;
        }
    }


    // 列表渲染器
    private onListRender(item: Node, idx: number) {
        // item.getComponentInChildren(Label)!.string = this.data[idx] + '';
        const data = this.data[idx];
        let str: string = ""
        switch (data.status) {
            case 1:
                str = "待审核"
                break;
            case 2:
                str = "提现成功"
                break
            case 3:
                str = "提现失败"
                break
            case 4:
                str = "已拒绝"
                break
            case 5:
                str = ""
                break
        }

        const stateLabel = item.getChildByName("lab_state")?.getComponent(Label);
        if (stateLabel) stateLabel.string = str;

        const timeLabel = item.getChildByName("lab_time")?.getComponent(Label);
        if (timeLabel) timeLabel.string = data.createTime;

        const moneyLabel = item.getChildByPath("moneyNode/lab_money")?.getComponent(Label);
        if (moneyLabel) moneyLabel.string = Format.formatRedPacketCoin(data.amount);
        const statusNode1 = item.getChildByName("record_state1");
        if (statusNode1) statusNode1.active = data.status == 2;
        const statusNode2 = item.getChildByName("record_state2");
        if (statusNode2) statusNode2.active = data.status != 2;
    }


    private btn_back() {
        oops.gui.remove(UIID.WithdrawRecord)
    }

    /** 视图对象通过 ecs.Entity.remove(WithdrawalRecordViewComp) 删除组件是触发组件处理自定义释放逻辑 */
    reset() {
        this.node.destroy();
    }

    private btn_play() {
        if (oops.gui.has(UIID.WithdrawRecord)) {
            oops.gui.remove(UIID.WithdrawRecord)
        }
        if (oops.gui.has(UIID.RedPacketWithdraw)) {
            oops.gui.remove(UIID.RedPacketWithdraw)
        }
        if (oops.gui.has(UIID.WechatWithdraw)) {
            oops.gui.remove(UIID.WechatWithdraw)
        }
    }


    //保留两位小数
    formatNumber(num: number) {
        return Math.round(num * 100) / 100;
    }

}