const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    picture: '',
    showQrcode: false,
    showCode: false,
    checkRadio: 1
  },
  onLoad: function (options) {
    console.log(options);
    if (options.flag == 1) {
      this.setData({
        checkRadio: 1
      })
    }
    if (!util.isObjEmpty(options) && options.type == 'share' && util.isObjEmpty(app.globalData.userInfo)) {
      wx.navigateTo({
        url: '/pages/home/login'
      });
    }
  },
  onReady: function () {

  },
  onShow: function () {

  },
  // 单选框
  radioChange(e) {
    console.log(e)
    let value = e.detail.value;
    this.setData({ checkRadio: value })
  },
  // 选择图片
  selectPic() {
    let that = this;
    if (util.isObjEmpty(app.globalData.userInfo)) {
      // wx.showModal({
      //   content: '您还未登录，请先前往登录。',
      //   confirmText:'立即前往',
      //   showCancel:false,
      //   success (res) {
      //     if (res.confirm) {
      //       wx.navigateTo({
      //         url: '/pages/home/login'
      //       });
      //     }
      //   }
      // })
      // 静默登录
      let that = this
      util.silentLogin().then(resr => {
        wx.chooseImage({
          sizeType: ['original', 'compressed'],
          count: 1,
          sourceType: ['album', 'camera'],
          success(res) {
            wx.showLoading({ title: '上传中…' });
            that.uploadAli(res.tempFilePaths[0]);
          }
        })
      })
      return false;
    }
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      count: 1,
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({ title: '上传中…' });
        that.uploadAli(res.tempFilePaths[0]);
      }
    })
  },
  // 图片上传
  uploadAli(img) {
    var that = this;
    wx.uploadFile({
      url: util.config.apiServer + 'user/upload_img_app.do',
      filePath: img,
      name: 'avatarFile',
      formData: {
        "rid": app.globalData.userInfo['rid']
      },
      success: function (res) {
        let datas = JSON.parse(res.data);
        if (res.statusCode == 200 && datas.code == 0) {
          that.setData({ picture: datas.data.image })
        }
        wx.hideLoading();
      }
    })
  },
  // 删除图片
  deletPic() {
    this.setData({ picture: '' })
  },
  // 联系客服 
  connect() {
    this.setData({ showQrcode: true })
  },
  // 提交
  submit() {
    util.requestMsg('9N_BQYOCniXrjVJ52O9AvbNHs2T078uIcR1iAJI7ql0').then(e => {
      this.sub()
    }).catch(res => {
      this.sub()
    })
  },
  sub() {
    let that = this;
    if (!util.isEmpty(this.data.picture)) {
      util.ajax({
        func: 'v2/user/apply/save',
        data: {
          content: this.data.picture,
          type: this.data.checkRadio
        },
        method: "POST",
      }, function (res) {
        if (res.code == 0) {
          // that.setData({showCode:true})
          wx.showModal({
            content: '上传成功！请耐心等待工作人员审核。',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                that.setData({ picture: '' })
              }
            }
          })
        } else {
          util.showTips(res.reason);
        }
      })
    } else {
      util.showTips('请上传图片！');
    }
  },
  closeTip(e) {
    let data = e.detail;
    switch (data.type) {
      case "contact":
        this.setData({ showQrcode: data.status })
        break;
      case "subSuccess":
        this.setData({ showCode: data.status })
        break;
    }
  },
  // 用户点击右上角分享
  onShareAppMessage: function () {
    return {
      title: '上传发圈截图，领取更多优惠福利。',
      path: '/pages/turntable/hairpin/index?type=share',
      imageUrl: 'https://img.bbztx.com/image_test/upload/thumbs/20220905/logo/1662359120902091698.png'
      // http://img.bbztx.com/image_test/upload/thumbs/20211028/logo/1635393155723077256.jpg
      // http://img.bbztx.com/image_test/upload/thumbs/20210326/logo/1616725535699049709.jpg
    }
  },
})