const app = getApp();
Page({


  data: {
    information: '',   //个人信息

    optioninfo:'',    //择偶信息

    houselunbo:'',  //轮播图

    // uploadImages: '', //轮播图放大

    adopticon:'',  //认证信息

    headurl: [],    //个人头像


    photo: '',      //头像
    // 上传的案例图片集合
    uploadImages: [],
    // 设置上传案例图片的最大数目
    maxImages: 1,
    // 案例图片数目是否达到了最大数目
    isMaxImagesNum: false,
    uploadimg: true,
    

  },




  onShow: function (options) {
    var that = this;

  //  个人信息
    let information_url = 'my/personaldata/personal';
    let information_data = {
    };
    app.postRequest(information_url, information_data, function (res) {
      if (res.code == 201) {
        // console.log(res.data);
        that.setData({
          information: res.data
        })
      }
    })


 
    //  择偶信息
    let optioninfo_url = 'my/personaloption/optioninfo';
    let optioninfo_data = {
    };
    app.postRequest(optioninfo_url, optioninfo_data, function (res) {
          // console.log(res.data);
          if (res.code == 201) {

            that.setData({
              optioninfo: res.data,
              optioninfocode:res,
            })
          }
    })


    //  轮播图片
    // let img_url = 'my/images/albumchange';
    // let img_data = {
    // };
    // app.postRequest(img_url, img_data, function (res) {
    //   if (res.code == 201) {
    //     // console.log(res.data);
    //     that.setData({
    //       houselunbo: res.data
    //     })

    //     //  对象转数组
    //     var itemList = res.data;
    //     //声明一个空的数组
    //     var items = [];
    //     //遍历数据
    //     for (let i in itemList) {
    //       //push到空数组里
    //       items.push(itemList[i].img);
    //     }
    //     // console.log(items);
    //     //传输数据到页面
    //     that.setData({
    //       uploadImages: items,
    //     })


    //   }

      
    // })



    //  认证信息
    let adopticon_url = 'my/authentication/adopticon';
    let adopticon_data = {
    };
    app.postRequest(adopticon_url, adopticon_data, function (res) {
      console.log(res.data);
      // console.log(res);
      if (res.code == 201) {

        that.setData({
          adopticon: res.data,
          adopticoncode: res,
        })
      }
    })



    //  个人头像
    let headurl_url = 'my/images/headurl';
    let headurl_data = {
    };
    app.postRequest(headurl_url, headurl_data, function (res) {
      if (res.code == 201) {
        // console.log(res.data);
        that.setData({
          headurl: res.data
        })
      }
 
    })

  

  },

  // enlarge:function(e){
  //   var that = this;
  
  //   wx.previewImage({
  //     urls: that.data.uploadImages,
  //     current: e.currentTarget.dataset.src
  //   })

  // }



  // 上传图片
  chooseImageTap: function (e) {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {


        let tempFilesSize = res.tempFiles[0].size;

        if (tempFilesSize > 1024 * 1024 * 3) {

          wx.showModal({
            title: '提示',
            content: '图片不得超过3MB',
            showCancel: false,
          })

          return;
        }

        var Headportrait = res.tempFilePaths[0];
        console.log(res.tempFilePaths[0]);


        wx.showModal({
          title: '形象照',
          content: '您确认上传此图片为形象照吗?',
          success(res) {
            if (res.confirm) {
                        // console.log('用户点击确定')
     
                        // tempFilePath可以作为img标签的src属性显示图片
                        _this.setData({
                          uploadImages: Headportrait,
                          isMaxImagesNum: true,
                          uploadimg: false,
                          photo: Headportrait,
                        });

                        //上传图片
                        wx.uploadFile({
                          url: app.globalData.url + 'my/images/headimg',
                          filePath: Headportrait,
                          name: 'photo',
                          formData: {
                            key: wx.getStorageSync('access_key'),
                            token: wx.getStorageSync('access_token')
                          },
                          success: function (res) {

                            var data = JSON.parse(res.data);
                            console.log(data);
                            if (data.code == 201) {

                              var _qrcode = '上传成功'
                              getApp().Tips(_qrcode);


                              _this.onShow();

                              return;

                            }
                          
                            var _qrcode2 = data.msg;
                            getApp().Tips(_qrcode2);
                  

                          }, fail: function (d) {

                          }
                        })

           
            } else if (res.cancel) {

              console.log('用户点击取消')
            }

          }
        })







      }
    })


  },


  
})