import { ModuleTypes } from './../types/sceneTypes';
import { NetManager } from './../../libs/tw/manager/NetManager';
import Panel from "../views/Panel";
import { GDispatcher } from '../../libs/tc/util/GDispatcher';
import CityItem from "./CityItem";
import PanelCtrl from "../ctrls/panelCtrl";
import Msg from '../alert/Msg';
import Loading from '../loading/Loading';


export default class StorePanel extends Panel{

    private mData:any;

    public cityList:eui.List;
    public cityScroller:eui.Scroller;
    public provinceBtn:eui.Button;
    public cityBtn:eui.Button;
    public provinceLab:eui.Label;
    public cityLab:eui.Label;
    
    public colseBtn:eui.Button;

    // private provinceLocalData:any;//省-保存本地数据
    // private provinceSelect:any;//省-当前选择
    // private cityLocalData:any ;//市-保存本地数据
    // private citySelect:any;//市-当前选择
    // private addressLocalData:any ;//详细地址-保存本地数据

    public storePanelBg:eui.Image;
    private storePanelTexture:egret.Texture;

    public clickBG:eui.Rect;
    public get skinKey() { return 'StorePanel' }
    public get groupName() { return 'store_panel' }

    constructor(data?) {
        super(data);
        this.mData = data;
    }

    start() {
        super.start();
        this.percentHeight = 100;

    }
    protected async preLoadRes() {
        //加载背景并设置背景图片
        this.storePanelTexture = await RES.getResAsync('storePanelBg_jpg');
        return new Promise(function (resolve, reject) {
            resolve();
        });
        
    }

    protected onSkinComplete(): void {
        super.onSkinComplete();
        this.storePanelBg.texture = this.storePanelTexture;
        this.once(egret.Event.ADDED_TO_STAGE, this.onLoad, this);
        if (this.stage) {
            this.onLoad();
        }
    }

    initEvents() {
        super.initEvents();
        this.provinceBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onProvinceBtn,this);
        this.cityBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onCityBtn,this);
        this.provinceLab.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onProvinceBtn,this);
        this.cityLab.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onCityBtn,this);
        this.colseBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickColse,this);
        GDispatcher.addEvent("onClickSelect", this.onClickSelect, this);

        this.clickBG.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickColse, this);
    }
    removeEvents() {
        super.removeEvents();
        this.provinceBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onProvinceBtn,this);
        this.cityBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onCityBtn,this);
        this.provinceLab.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onProvinceBtn,this);
        this.cityLab.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onCityBtn,this);
        this.colseBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickColse,this);
        GDispatcher.removeEvent("onClickSelect", this.onClickSelect, this);

        this.clickBG.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickColse, this);
    }

    private onLoad(){
        //this.pMask.alpha = 0;
        this.init();
    }
    private init(){
        this.cityBtn.visible = false;
        this.getProvinceList(); 
    }
    private getProvinceList(){
        Loading.instace.show()
        NetManager.ins.getProvinceList((success,result)=>{
            Loading.instace.hide();
            if(!result || !success){
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                return
            }
            if(result.data){
                this.setCityData(result.data);
            }  
        })
    }
    private getCityList(province:string){
        Loading.instace.show()
        NetManager.ins.getCityList((success,result)=>{
            Loading.instace.hide();
            if(!result || !success){
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                return
            }
            if(result.data){
                this.setCityData(result.data);
            }  
        },province)
    }
    private getShopList(province:string,city:string){
        Loading.instace.show()
        NetManager.ins.getShopList((success,result)=>{
            Loading.instace.hide();
            if(!result || !success){
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                return
            }
            if(result.data){
                this.setCityData(result.data);
            }  
        },province,city)
    }

    private onProvinceBtn():void{
        PanelCtrl.instance.btnDelay(this.provinceBtn);
        this.cityLab.text = '';
        this.cityBtn.visible = false;
        this.provinceBtn.visible = true;
        this.provinceLab.text = '';
        this.getProvinceList(); 
    }
    private onCityBtn():void{
        PanelCtrl.instance.btnDelay(this.onCityBtn);
        this.cityLab.text = '';
        this.cityBtn.visible = true;
        this.getCityList(this.provinceLab.text); 
    }
    //选择
    private onClickSelect(selectData){
        if(selectData.select == 'province'){
            this.provinceBtn.visible = false;
            this.provinceLab.text = selectData.province;
            this.cityBtn.visible = true;
            this.cityBtn.x = this.provinceLab.width + this.provinceLab.x + 20;
            this.cityLab.x = this.provinceLab.width + this.provinceLab.x + 20;
            this.getCityList(selectData.province); 
        }else if(selectData.select == 'city'){
            this.cityBtn.visible = false;
            this.cityLab.text = selectData.city;
            this.getShopList(this.provinceLab.text,selectData.city); 
        }else{
            console.log('选择门店',selectData)
        }
        console.log('选择',selectData);
    }
    //数据显示
    private setCityData(cityData):void{
        let arrData = null;
        this.cityList.itemRenderer = CityItem;
        arrData = new eui.ArrayCollection(cityData);
        this.cityList.dataProvider = arrData;
    }
    private onClickColse(){
        PanelCtrl.instance.btnDelay(this.colseBtn);
        this.hidePanel();
    }
}