import {addEvent} from '../../base/Utils';
import {Device} from '../../base/Device';
import NetWork from '../../base/NetWork';
/**
 * @author yeyuchao
 * @description loading pages
 */
let RESOURCE = [
  // "https://dm.static.elab-plus.com/图-1.jpg.1539678713888",
  // "https://dm.static.elab-plus.com/1.jpg.1540437352665",
  // "https://dm.static.elab-plus.com/图-3.jpg.1539678694474"
];
export default {
  data() {
    return {
      resource:[],
      numFilesTotal:0,
      numFilesLoaded:0,
      onCompleted:false,
      progress:0,
      progressTxt:0,
      bottomWidth:0,
      device:null,
      pageListData:null,
      deviceMobile:false,
    };
  },
  mounted() {
  },
  created() {
    this.device = new Device();
    if(this.device.phone){
      this.deviceMobile = true;
    }
    this.getPageList();
  },
  methods:{
    getPageList(){
      const that = this;
      NetWork.fetchGet('listBanner').then((date)=>{
        RESOURCE = date.data.result.list;
        window.banelResList = date.data.result.list;
        this.startLoad();
      }).catch((err)=>{
      })
    },
    startLoad(){
      const that = this;
      for(let item of RESOURCE){
        that.resource.push(item.image);
      }
      that.numFilesTotal = that.resource.length;
      that.numFilesLoaded = 0;
      that.load();
    },
    load(){
      if(this.resource.length>0){
        const res = this.resource.pop();
        const img = new Image();
        addEvent(img, 'load', () => {
          window.loadedResource.push(res);
          this._onLoaded();
        });
        img.src = res;
      }
    },
    _onLoaded(){
      this.numFilesLoaded++;
      let progress = (this.numFilesLoaded/this.numFilesTotal).toFixed(2)*100;
      let bottomWidth = (this.numFilesLoaded/this.numFilesTotal).toFixed(2)*560;
      if(this.deviceMobile){
        bottomWidth = (this.numFilesLoaded/this.numFilesTotal).toFixed(2)*310;
      }
      this.progressTxt = parseInt(progress);
      this.bottomWidth = parseInt(bottomWidth);
      if(this.numFilesTotal === this.numFilesLoaded){
        window.loadComplete = true;
        this._onLoadCompleted();
      }else {
        this.load();
      }
    },
    _onLoadCompleted(){
      const that = this;
      setTimeout(function () {
        that.$router.push('/index');
      },800)
    }
  }
};
