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

  /**
   * 页面的初始数据
   */
  data: {
    shopBean: null,
    baseImageUrl: app.urls.baseImgUrl,
    shopId: '',
    show: false,
    qrCode: '',
    baseImg: app.urls.baseImgUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId
    })
    this.getShopDetail(options.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({})
      })
  },
  //职位管理
  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,
    })
  },
  /**
   * 获取添加员工二维码
   */
  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({})
        if (code == 200) {
          _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,
      })
    }
  },
  onShow() {
    this.getShopDetail(this.data.shopId)
  }
})