// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import HTTPS, { NetGet, NetPost } from "./HTTPS";
import Tips from "./Tips";

const { ccclass, property } = cc._decorator;

@ccclass
export default class withdraw extends cc.Component {

    @property(cc.Node)
    RuleContent: cc.Node = null;


    @property(cc.Node)
    withdrawContent: cc.Node = null;


    @property(cc.Node)
    HistoryContent: cc.Node = null;
    @property(cc.Node)
    HistoryItem: cc.Node = null;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.RuleContent.active = false

        let Close = this.node.getChildByName("Close")

        vv.deleteBtnEvent(Close, this)
        vv.addBtnEvent(Close, () => {
            this.node.active = false
        }, this)
    }

    protected onEnable(): void {
        this.getRule()
    }
    getRule() {

        let withdraw = this.node.getChildByName("withdraw")
        let history = this.node.getChildByName("history")
        withdraw.active = false
        history.active = false

        let Rule = this.node.getChildByName("Rule")
        Rule.active = true




        HTTPS.Instance.get(NetGet.rule).then(res => {
            if (res.code != 200) {
                Tips.Instance.show(res.msg)
                return
            }


            this.RuleContent.children[0].getChildByName("title").getComponent(cc.Label).string = '最小提现金额'
            this.RuleContent.children[0].getChildByName("reward").getComponent(cc.Label).string = res.data.min_amount


            this.RuleContent.children[1].getChildByName("title").getComponent(cc.Label).string = '最大提现金额'
            this.RuleContent.children[1].getChildByName("reward").getComponent(cc.Label).string = res.data.max_amount


            this.RuleContent.children[2].getChildByName("title").getComponent(cc.Label).string = '手续费率'
            this.RuleContent.children[2].getChildByName("reward").getComponent(cc.Label).string = res.data.fee_rate + '%'



            this.RuleContent.children[3].getChildByName("title").getComponent(cc.Label).string = '每日最大提现次数'
            this.RuleContent.children[3].getChildByName("reward").getComponent(cc.Label).string = res.data.daily_times



            this.RuleContent.children[4].getChildByName("title").getComponent(cc.Label).string = '今日已提现次数'
            this.RuleContent.children[4].getChildByName("reward").getComponent(cc.Label).string = res.data.today_times



            this.RuleContent.children[5].getChildByName("title").getComponent(cc.Label).string = '剩余提现次数'
            this.RuleContent.children[5].getChildByName("reward").getComponent(cc.Label).string = res.data.remain_times


            this.RuleContent.children[6].getChildByName("title").getComponent(cc.Label).string = '用户余额'
            this.RuleContent.children[6].getChildByName("reward").getComponent(cc.Label).string = res.data.balance


            this.RuleContent.active = true
        })
    }
    getwithdraw() {

        let Rule = this.node.getChildByName("Rule")
        let history = this.node.getChildByName("history")
        Rule.active = false
        history.active = false

        let withdraw = this.node.getChildByName("withdraw")
        withdraw.active = true


    }
    btnwithdraw() {

        let total = this.node.getComponentsInChildren(cc.EditBox)

 
        if (!total[0]?.string) {
            Tips.Instance.show('提现金额不能未空')
            return
        }

        if (!total[1].string) {
            Tips.Instance.show('提现方式不能未空')
            return
        }

        if (!total[2].string) {
            Tips.Instance.show('提现账号不能未空')
            return
        }

        if (!total[3].string) {
            Tips.Instance.show('真实姓名不能未空')
            return
        }


        HTTPS.Instance.post(NetPost.withdraw, {
            amount: total[0].string,//提现金额
            withdraw_type: total[1].string,//提现方式(alipay/wxpay/bank)
            account: total[2].string,//提现账号
            real_name:total[3].string,// 真实姓名
        }).then(res => {
            if (res.code != 200) {
                Tips.Instance.show(res.msg)
                return
            }
            Tips.Instance.show(res.msg)
        })
    }

    getwHistory() {

        let Rule = this.node.getChildByName("Rule")
        let withdraw = this.node.getChildByName("withdraw")
        Rule.active = false
        withdraw.active = false


        let history = this.node.getChildByName("history")
        history.active = true



        HTTPS.Instance.get(NetGet.withdrawHistory).then(res => {
            if (res.code != 200) {
                Tips.Instance.show(res.msg)
                return
            }

            this.HistoryContent.children.forEach(e => {
                e.active = false
            })

            for (let index = 0; index < res.data.list.length; index++) {
                const Data = res.data.list[index];
                let Node = this.HistoryContent[index] as cc.Node;

                if (!Node) {
                    Node = cc.instantiate(this.HistoryItem) as cc.Node
                    Node.parent = this.HistoryContent
                }

                let title = Node.getChildByName("title").getComponent(cc.Label)
                let type = Node.getChildByName("type").getComponent(cc.Label)
                let reward = Node.getChildByName("reward").getComponent(cc.Label)

                title.string = Data.withdraw_type_text + Data.actual_amount.toString()
                type.string = Data.status_text
                reward.string = Data.createtime



                Node.active = true
            }

        })
    }
    // update (dt) {}
}
