// 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 Login extends cc.Component {

    @property(cc.EditBox)
    Iphone: cc.EditBox = null;

    @property(cc.EditBox)
    Code: cc.EditBox = null;



    protected onLoad(): void {

    }



    CountDown = 0
    SendCode() {
        if (!this.checkIphone()) {
            Tips.Instance.show('手机号码不正确')
            return
        }

        if (this.CountDown != 0) {
            return
        }

        HTTPS.Instance.post(NetPost.SendIphoneCode, {
            mobile: this.Iphone.string
        }).then(res => {

            if (res.code == 200) {
                Tips.Instance.show(res.msg)

                let Label = this.node.getChildByName("SendCode").getChildByName("Label").getComponent(cc.Label)
                this.CountDown = 60
                Label.string = `(${this.CountDown}s)`

                this.schedule(() => {
                    Label.string = `(${--this.CountDown}s)`
                    if (this.CountDown == 0) {
                        this.unscheduleAllCallbacks()
                        Label.string = '验证码'
                    }
                }, 1, 59, 1)
            } else {
                Tips.Instance.show(res.msg)
            }
        })
    }


    login() {

 
        if (!this.checkIphone()) {
            Tips.Instance.show('手机号码不正确')
            return
        }



        HTTPS.Instance.post(NetPost.IphoneLogin, {
            mobile: this.Iphone.string,
            code: this.Code.string
        }).then(res => {
            if (res.code != 200) {
                Tips.Instance.show(res.msg)
                return
            }

            this.node.active = false

            HTTPS.Instance.user_info = res.data.user_info
            HTTPS.Instance.token = res.data.token


            cc.director.loadScene('scene')

        })


    }
    // update (dt) {}


    checkIphone() {
        function isValidPhoneNumber(phoneNumber: string): boolean {
            // 正则表达式，适用于中国手机号码
            const phonePattern = /^1[3-9]\d{9}$/;
            return phonePattern.test(phoneNumber);
        }
        return isValidPhoneNumber(this.Iphone.string)
    }

    //记录广告观看
    Setrecord() {
        HTTPS.Instance.post(NetPost.record, {
            ad_type: 'sign_in',//广告类型(默认reward)
            duration: 'sign_in',//观看时长，单位秒(默认30)
        }).then(res => {
            console.error(res);
            if (res.code == 200) {

            }
        })
    }

    openTask() {
        let Task = this.node.getChildByName("Task")
        Task.active = true
    }


    openwithdraw() {
        let Task = this.node.getChildByName("withdraw")
        Task.active = true
    }
}
