// pages/mine/shop/mineshop.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mineShopList: [],
    instance: null,
    currentPage: 1,
    baseImageUrl: app.urls.baseImgUrl,
    isOpenCell: false,
    refresherTriggered: false
  },
  onOpen(e) {
    this.setData({
      isOpenCell: true
    })
  },
  onClose(e) {
    const {
      position,
      instance
    } = e.detail
    switch (position) {
      case 'right':
      case 'cell':
        this.setData({
          isOpenCell: false
        })
        instance.close()
        break
    }

  },
  //显示店铺详情
  showShopDetail(e) {
    if (!this.data.isOpenCell) {
      wx.navigateTo({
        url: './mineshopdetail?shopId=' + e.currentTarget.dataset.item.shopId,
      })
    }
  },
  //删除店铺
  doDelItem(e) {
    let _self = this
    wx.showModal({
      title: '提示',
      content: '确定要删除该店铺吗?',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中...',
          })
          app.http.delete(app.urls.doDelShop.format({
              ids: e.currentTarget.dataset.item.shopId
            }), {
              header: {
                token: app.globalData.token
              }
            })
            .then(res => {
              wx.hideLoading({})
              wx.showToast({
                title: '删除成功',
              })
              _self.refreshView()
            })
            .catch(err => {
              wx.hideLoading({})
            })
        }
      }
    })
  },
  //编辑店铺
  doEditItem(e) {
    wx.navigateTo({
      url: './shopedit?shopId=' + e.currentTarget.dataset.item.shopId,
    })
  },
  //添加店铺
  toAddShop() {
    wx.navigateTo({
      url: './addshop',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getMineShopList() {
    wx.showLoading({
      title: '加载中...',
    })
    let _self = this
    app.http.get(app.urls.getMineShopList, {
        header: {
          token: app.globalData.token
        },
        data: {
          page: _self.data.currentPage
        }
      })
      .then(res => {
        wx.hideLoading({})
        _self.setData({
          refresherTriggered: false
        })
        _self.setData({
          mineShopList: res.data.rows
        })
        _self.doOpenOneCell()
      })
      .catch(err => {
        wx.hideLoading({})
        _self.setData({
          refresherTriggered: false
        })
      })
  },
  //自动打开第一个条目
  doOpenOneCell() {
    var cell = this.selectComponent('#swipe-item-0')
    if (cell && app.globalData.isFirst) {
      cell.open()
      app.globalData.isFirst = false
    }
  },
  //刷新页面
  refreshView() {
    this.setData({
      currentPage: 1,
      refresherTriggered: true
    })
    this.getMineShopList()
  },
  onShow() {
    this.refreshView()
  },
  //认证
  doAuth(e) {
    if (e.currentTarget.dataset.item.isCertification == 0) {
      //去认证
      wx.navigateTo({
        url: './shopauthentication?shopId=' + e.currentTarget.dataset.item.shopId,
      })
    }
  }
})