import lib from 'lib'
import extSdk from 'extSdk'
import API from 'API'
const Api = new API();

export default {
    data() {
        return {
            showList: false,
            zoneList: ['电信南方区','联通北方区'],
            zoneIndex: 0,
            pn: "",
            checkApi: '//wotapp.duowan.com/index.php?r=api/searchPlayer&from=bbsapp',
            selectList:""
        }
    },

    mounted() {
        lib.setTitle('坦克世界查询战绩')
    },

    methods: {
        selectZone(index) {
            this.zoneIndex = index
            this.showList = false;
        },

        query(pn,zone) {
            let href =  `http://wotapp.duowan.com/index.php?r=m/home&pn=${pn}&zone=${zone}&wotapp_device=AiFjYBS6TUOXAckdTJcbbNiv678Pdr1EflOcpbn61ey8&from=bbsapp`
            location.href = href
            
        },
        
        submit(pn,zone) {
            var self = this
            
            let searchHistory = {
                pn,
                zone
            }
            self.$store.commit("setSearchHistory", searchHistory);
            self.query(pn, zone)
            


            
        },

        delSearchHistory() {
            this.$store.commit("delSearchHistory");
        }

    },

    

    computed: {
        searchHistory() {
            return this.$store.state.searchHistory
        }
    },

    watch: {
        pn(cur, old) {
            let self = this
            let selectList = []
            let reg = new RegExp(cur,'i')
            Api.jsonp(self.checkApi, {pn: cur}).done(function(ret){
                if(ret.data) {
                    selectList = ret.data
                    selectList.forEach(element => {
                        element.match = element.pn.match(reg)
                        element.test = element.pn.replace(reg,'')
                    });
                    self.selectList = selectList
                }
                else {
                    self.selectList = ''
                }
            })
        }
    }
}