
import { _decorator, Component, EditBox, game, instantiate, Label, Node, Prefab } from 'cc';
import { UtilsPanel } from '../../scene/script/UtilsPanel';
import { Generate } from '../../scene/script/components/Generate';
import { Config, nav_info } from '../../scene/script/NavInfo';
import { WebRequest } from '../../scene/script/WebRequest';
const { ccclass, property } = _decorator;

type ItemType = {
    ItemId: number
    Count: number
}
export type ShopData = {
    AninationType: number
    Bonus: number
    Extra: Array<ItemType>
    IsBotton: number
    IsHot: number
    PayType: number
    Price: number
    ProductDesc: string
    ProductId: string
    ProductName: string
    ShopType: number
    ShowPrice: number
    Sort: number
    Status: number
    UserType: number
}

@ccclass('Pay')
export class Pay extends Component {

    private node_descid: Node = null;
	private node_buy: Node = null;
	private gene_items: Generate = null;
	private lb_money: Label = null;
	private edit_id: EditBox = null;
    
    private data: ShopData[] = null;
    private select_data:ShopData = null;

	protected onLoad(){
		UtilsPanel.getAllNeedCom(this, this.node, false);
        UtilsPanel.addBtnEvent(this.node_buy, this.onBuy, this);
        // @ts-ignore
        game.on("select_item", this.onSelect, this);
        let url = nav_info.is_test ? Config.url_product_test : Config.url_product;
        new WebRequest().getData(url, "", (succ:boolean,content:string)=>{
            if(succ){
                this.data = JSON.parse(JSON.parse(content).Data);
            }
            this.gene_items.initData(this.data);
            // console.log("fwef:",this.data);
        }, false);
	}
    private onEditChange(){
        this.node_descid.active = this.edit_id.string.length == 0;
    }
    private onSelect(index:number){
        this.select_data = this.data[index];
        this.lb_money.string = this.select_data.Price + "";
    }
    private onBuy(){
        let user_id = this.edit_id.string;
        if(user_id.length == 0){
            nav_info.scene.showTip("please input user id");
            return;
        }
        if(!this.select_data){
            nav_info.scene.showTip("please select product");
            return;
        }
        let data = this.select_data;
        let url = nav_info.is_test ? Config.url_pay_test : Config.url_pay;
        let final_url = `${url}?UserID=${user_id}&Price=${data.Price}&ProductID=${data.ProductId}&PartnerID=110&Currency=SAR&Country=SA&T=${new Date().getTime()}`;
        window.open(final_url);
    }
    
}
