/**
 * 绑定邀请码信息
 */
import gameToast from "../Network/gameToast"
cc.Class({
    extends: cc.Component,

    properties: {
        neighborValue: {
            default: null,
            type: cc.Label
        },
        neighborConsumValue: {
            default: null,
            type: cc.Label
        },
        neighborDirectValue: {
            default: null,
            type: cc.Label
        },
        farmerValue: {
            default: null,
            type: cc.Label
        },
        farmerConsumValue: {
            default: null,
            type: cc.Label
        },

        myInviteValue: {
            default: null,
            type: cc.Label
        },
        parentInviteValue: {
            default: null,
            type: cc.Label
        },
        inputInviteValue: {
            default: '',
            visible: false
        },

        ToastParent: {
            default: null,
            type: cc.Node,
            serializable: true
        },
        //输入节点
        inputContainer:{
            default:null,
            type:cc.Node,
            serializable:true
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    onOpenInviteInfo() {
        if (!GlobalD.dapp) {
            console.error("InviteInfo start,dapp未初始化！");
            return;
        }
        this.node.active = true;
        this.myInviteValue.string = GlobalD.Dapp.UserInfo.invite_code;
        if (GlobalD.Dapp.UserInfo.parent) {
            this.parentInviteValue.string = GlobalD.Dapp.UserInfo.parent.invite_code;
            //如果存在父节点，隐藏输入框
            this.inputContainer.active = false;
        }

        // {
        //     "children_count": 1, // 直推下级数量
        //     "team_count": 1, // 伞下成员数量（包含直推数量）
        //     "children_village_chief_count": 1, // 直推村长数量（镇长身份才需要展示）
        //     "children_cnt_amount": 0, // 直推下级租赁土地消费CNT业绩数量
        //     "team_cnt_amount": 0 // 伞下团队租赁土地消费CNT数量（含直推数量）
        // }
        // 获取镇长/村长团队信息（村长或镇长身份才需要）
        // if (0 === GlobalD.Dapp.UserInfo.agent_level) {
        //     console.log("村民不需要获取团队信息");
        //     return;
        // }
        GlobalD.dapp.teamData().then((data) => {
            console.log("teamData：" + JSON.stringify(data));
            if (data.err === null) {
                // 返回成功，见下面消息体
                this.neighborValue.string = data.res.children_count;
                this.neighborConsumValue.string = data.res.children_cnt_amount;
                this.neighborDirectValue.string = data.res.children_village_chief_count;

                this.farmerValue.string = data.res.team_count;
                this.farmerConsumValue.string = data.res.team_cnt_amount;
            } else {
                //TODO 服务错误
                console.log(data.err)
                GlobalD.GameData.showToast(this.ToastParent, data.err, 3, () => {
                    console.log("finish toast!");
                });
            }
        })
    },

    inputInvite(value, e) {
        console.log(value);
        this.inputInviteValue = value;
    },

    onBindInviteInfo() {
        console.log('绑定的inputInviteValue：' + this.inputInviteValue);
        if (!GlobalD.dapp) {
            console.error("onBindInviteInfo warn,dapp未初始化！");
            return;
        }
        let _self = this;
        GlobalD.dapp.bindParent(_self.inputInviteValue).then((data) => {
            if (data.err === null) {
                // 返回成功，见下面消息体
                console.log(data.res)
                //绑定成功时候，隐藏输入框，设置父节点
                GlobalD.Dapp.UserInfo.parent = data.res;
                _self.parentInviteValue.string = GlobalD.Dapp.UserInfo.parent.invite_code;
                //如果存在父节点，隐藏输入框
                _self.inputContainer.active = false;

                GlobalD.GameData.showToast(_self.ToastParent, "绑定成功！", 3, () => {
                    console.log("finish toast!");
                });
            } else {
                //TODO 服务错误
                console.log(data.err)
                GlobalD.GameData.showToast(_self.ToastParent, "绑定失败！", 3, () => {
                    console.log("finish toast!");
                });
            }
        })
    },

    onClose() {
        this.node.active = false;
    },

    webCopyString() {
        console.log('复制');

        var input = GlobalD.Dapp.UserInfo.invite_code + '';
        const el = document.createElement('textarea');
        el.value = input;
        el.setAttribute('readonly', '');
        el.style.contain = 'strict';
        el.style.position = 'absolute';
        el.style.left = '-9999px';
        el.style.fontSize = '12pt'; // Prevent zooming on iOS

        const selection = getSelection();
        var originalRange = false;
        if (selection.rangeCount > 0) {
            originalRange = selection.getRangeAt(0);
        }
        document.body.appendChild(el);
        el.select();
        el.selectionStart = 0;
        el.selectionEnd = input.length;

        var success = false;
        try {
            success = document.execCommand('copy');
        } catch (err) { }

        document.body.removeChild(el);

        if (originalRange) {
            selection.removeAllRanges();
            selection.addRange(originalRange);
        }

        return success;
    },
    // update (dt) {},
});
