import { getBannerList } from "../../service/system"
import { alertInfo } from "../../utils/index";
import request from '../../utils/request'

const { OK } = request;
const result = wx.getAccountInfoSync();
const { envVersion } = result.miniProgram;

// pages/home/components/home-swiper/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    park: String,
    type: Number,
  },
  observers: {
    'park,type': function(newPark, newType) {
      if ((!newPark && newType === 0) || (!newType && newType !== 0)) {
        return
      }
      this.init(newPark, newType)
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 6000,
    duration: 500,
    list: [],
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async init(park, type) {
      const { code, message, data = [] } = await getBannerList(park, type);
      if (code !== OK) {
        alertInfo(message);
        this.setData({ list: [], park, type })
        return 
      }
      this.setData({ list: data })
    },
    handleJump(e) {
      const { data = {} } = e.currentTarget.dataset;
      switch(data.jumpType) {
        case 1:
          wx.navigateToMiniProgram({
            appId: data.appid,
            path: data.wxPath,
            envVersion: envVersion
          })
          break;
        case 2:
          wx.navigateTo({
            url: data.value,
          })
          break;
        case 3:
          wx.navigateTo({
            url: '/pages/webPage/index?path=' + data.value,
          })
          break;
      }
    }
  }
})