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

  /**
   * 页面的初始数据
   */
  data: {
    mineShopList: [],
    instance: null,
    currentPage: 1,
    baseImageUrl: app.urls.baseImgUrl,
    isOpenCell: false,
    refresherTriggered: false,
    shopBean: null,
    shopId: '',
    top: app.globalData.CustomBar + 10, //状态栏+标题栏+页面margin-top
  },
  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: _self.data.shopId
            }), {
              header: {
                token: app.globalData.token
              }
            })
            .then(res => {
              wx.hideLoading({})
              wx.showToast({
                title: '删除成功',
                success() {
                  wx.navigateBack({})
                }
              })
            })
            .catch(err => {
              wx.hideLoading({})
            })
        }
      }
    })
  },
  //编辑店铺
  doEditItem(e) {
    wx.navigateTo({
      url: './shopedit?shopId=' + this.data.shopId,
    })
  },
  //添加店铺
  toAddShop() {
    wx.navigateTo({
      url: './addshop',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId
    })
    this.getShopDetail(options.shopId)
  },
  onShow() {
    this.getShopDetail(this.data.shopId)
  },
  getShopDetail(id) {
    let _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.http.get(app.urls.getShopDetail.format({
        shopId: id
      }), {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        _self.setData({
          shopBean: res.data
        })
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  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()
  },
  //认证
  doAuth(e) {
    if (e.currentTarget.dataset.item.isCertification == 0) {
      //去认证
      wx.navigateTo({
        url: './shopauthentication?shopId=' + e.currentTarget.dataset.item.shopId,
      })
    }
  },
  //显示宣传视频
  showMovie(e) {
    var path = e.currentTarget.dataset.path
    if (path) {
      var tempList = [{
        url: this.data.baseImageUrl + path,
        type: 'video'
      }]
      wx.previewMedia({
        sources: tempList,
        current: 0,
        showmenu: false
      })
    }
  },
  //显示当前模板
  showTemplete(e) {
    var path = e.currentTarget.dataset.path
    if (path) {
      var tempList = [{
        url: this.data.baseImageUrl + path,
        type: 'image'
      }]
      wx.previewMedia({
        sources: tempList,
      })
    }
  },
  //职位管理
  positionManage() {
    wx.navigateTo({
      url: '../manage/shopposition?shopId=' + this.data.shopBean.shopId,
    })
  },
  // 职员管理
  staffManage() {
    wx.navigateTo({
      url: '../manage/shopstaff?shopId=' + this.data.shopBean.shopId,
    })
  },
  // 会员管理
  memberManage() {
    wx.navigateTo({
      url: '../manage/shopmember?shopId=' + this.data.shopBean.shopId,
    })
  },
  // 商品类目管理
  catalogManage() {
    wx.navigateTo({
      url: '../product/catalogmanage/catalogmanage?shopId=' + this.data.shopBean.shopId,
    })
  },
  //商品管理
  productManage() {
    wx.navigateTo({
      url: '../product/productmanage/productlist?shopId=' + this.data.shopBean.shopId,
    })
  },
  // 优惠券管理 
  couponManage() {
    wx.navigateTo({
      url: '/pages/mine/product/coupon/couponlist?shopId=' + this.data.shopBean.shopId,
    })
  },
  /**
   * 获取添加员工二维码
   */
  addStaff() {
    let _self = this
    wx.showLoading({
      title: '获取中...',
    })
    app.http.get(app.urls.getShopCode.format({
        shopId: _self.data.shopId,
        type: '1'
      }), {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        _self.setData({
          qrCode: app.urls.baseImgUrl + res.data.data,
          show: true
        })
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  /**
   * 获取添加会员二维码
   */
  addMember() {
    let _self = this
    wx.showLoading({
      title: '获取中...',
    })
    app.http.get(app.urls.getShopCode.format({
        shopId: _self.data.shopId,
        type: '1'
      }), {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        _self.setData({
          qrCode: app.urls.baseImgUrl + res.data.data,
          show: true
        })
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  scanCode() {
    let _self = this
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        _self.doAddMember(res.result)
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  doAddMember(res) {
    console.log(res)
    var result = JSON.parse(res)
    console.log(result)
    if (result.type == '2') {
      //会员添加
      wx.showLoading({
        title: '会员注册中...',
      })
    } else if (result.type == '1') {
      //员工添加
      wx.showLoading({
        title: '员工注册中...',
      })
    }
    var url = app.urls.url + result.url
    app.http.post(url, {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        if (result.type == 2) {
          wx.showToast({
            title: '会员注册成功',
            icon: 'success'
          })
        } else if (result.type == 1) {
          wx.showToast({
            title: '员工注册成功,请等待审核',
            icon: 'none'
          })
        }
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  showCertificationInfo(e) {
    let _self = this
    if (_self.data.shopBean.auditStatus == 0) {
      if (_self.data.shopBean.shopCertificationId == '') {
        //未提交审核
        wx.navigateTo({
          url: './shopauthentication?shopId=' + _self.data.shopId,
        })
      } else {
        //审核中
        wx.navigateTo({
          url: './shopauthenticationdetail?shopId=' + _self.data.shopId + '&certificationId=' + _self.data.shopBean.shopCertificationId,
        })
      }
    } else if (_self.data.shopBean.auditStatus == 1) {
      //审核通过
      wx.navigateTo({
        url: './shopauthenticationdetail?shopId=' + _self.data.shopId + '&certificationId=' + _self.data.shopBean.shopCertificationId,
      })
    } else {
      //审核不通过
      wx.navigateTo({
        url: './shopauthenticationedit?shopId=' + _self.data.shopId + '&certificationId=' + _self.data.shopBean.shopCertificationId,
      })
    }
  },
})