const app = getApp();
Page({

  data: {
    //身份证手持页
    // 上传的案例图片集合
    scsfzImages: [],
    // 案例图片数目是否达到了最大数目
    scsfzImagesNum: false,   //加号图标 显示
    scsfzimg: true,      //上传图片 隐藏
    scsfzinitial: true,   //初始图片 隐藏

    // 身份证头像页
    sfztxyImages: [],
    sfztxyImagesNum: false, //加号图标 显示
    sfztxyimg: true,              //上传图片隐藏
    // sfztxyinitial: true,   //初始图片 隐藏

    // 身份证反面页
    sfzfmImages: [],
    sfzfmImagesNum: false,  //加号图标 显示
    sfzfmimg: true,      //上传图片 隐藏
    // sfzfminitial: true,   //初始图片 隐藏

    // 设置上传案例图片的最大数目
    maxImages: 1,


    loadingHidden: false, //加载中 

    name: '',   //姓名
    number: '',  //证件号码
    hold: '',  //手持身份证
    positive: '',  //身份证正面
    opposite: '',  //身份证反面

    initial: [],  //头部信息


    SubmissionHidden: true, //提交隐藏显示


    isShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    //  获取信息
    let initial_url = 'my/authentication/ididentitychange';
    let initial_data = {
    };
    app.postRequest(initial_url, initial_data, function (res) {
      if (res.code == 201) {
        console.log(res.data.hold);
        that.setData({
          initial: res.data,   // 初始数据
          initialcode: res,    //获取未上传状态

          name: res.data.name,                //姓名
          number: res.data.number,         //证件号码

        })
      }

      if (res.data.status == 2){   //如果审核通过  隐藏掉提交按钮
            that.setData({
              SubmissionHidden: false
            })
      } else if (res.code != 201) {
        that.setData({
          SubmissionHidden: false
        })
      }else{
          that.setData({
            SubmissionHidden: true
          })
      }



    })


  },


  //获取姓名
  ObtainName: function (e) {
    var name = e.detail.value;
    console.log(name);
    this.setData({
      name: name
    });
  },

  //获取身份证
  Obtainsfz: function (e) {
    var number = e.detail.value;
    console.log(number);
    this.setData({
      number: number
    });
  },


  // 上传手持身份证
  scsfzclick: 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;
                     }

                    wx.showToast({

                      title: '上传中...',

                      icon: 'loading'

                    });




                          //  头部信息
                          let dt_url = 'my/images/tempimages';
                          let dt_filePath = res.tempFilePaths[0];

                          app.Multigraph(dt_url, dt_filePath,function(res){
                  
                            if (res.code==201){
                                //  console.log(res.data.img_name);


                                _this.setData({
                                  scsfzImages: dt_filePath,
                                  scsfzImagesNum: true,
                                  scsfzimg: false,
                                   hold:res.data.img_name,     //手持身份证
                                  });
                               
                                      wx.showToast({
                                        title: '上传成功',
                                        icon: 'none',  // 不显示图标
                                        duration: 2000
                                      })

                                 return;     
                            }

                                app.Tips(res.msg);
 
                          });
                      
                  }
          })
  },

  // 正面页
  sfztxyclick: 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;
                }


              wx.showToast({

                title: '上传中...',

                icon: 'loading'

              });

                //  头部信息
                let dt_url = 'my/images/tempimages';
                let dt_filePath = res.tempFilePaths[0];

                app.Multigraph(dt_url, dt_filePath, function (res) {

                      if (res.code == 201) {
                        //  console.log(res.data.img_name);
                            _this.setData({
                                  sfztxyImages: dt_filePath,
                                  sfztxyImagesNum: true,
                                  sfztxyimg: false,
                                  positive: res.data.img_name,
                            });

                              wx.showToast({
                                title: '上传成功',
                                icon: 'none',  // 不显示图标
                                duration: 2000
                              })

                             return;     
                      }

                      app.Tips(res.msg); 

                });



      }

    })
  },

  // 反面页
  sfzfmclick: function (e) {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // console.log(res.tempFilePaths[0]);
        // tempFilePath可以作为img标签的src属性显示图片


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

        if (tempFilesSize > 1024 * 1024 * 3) {

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

          return;
        }
        
        wx.showToast({

          title: '上传中...',

          icon: 'loading'

        });


        //  头部信息
        let dt_url = 'my/images/tempimages';
        let dt_filePath = res.tempFilePaths[0];

        app.Multigraph(dt_url, dt_filePath, function (res) {

          if (res.code == 201) {
            //  console.log(res.data.img_name);
                _this.setData({
                  sfzfmImages:dt_filePath,
                  sfzfmImagesNum: true,
                  sfzfmimg: false,
                  opposite:res.data.img_name,
                });
          
                  wx.showToast({     
                    title: '上传成功',
                    icon: 'none',  // 不显示图标
                    duration: 2000
                  })

                      return;    
              }


               app.Tips(res.msg);

        });



      }

    })
  },


  //最后提交认证
Submission: function (e) {
  var _this =this;

  console.log(this.data.name)
  console.log(this.data.number)
  console.log(this.data.hold)
  console.log(this.data.positive)
  console.log(this.data.opposite)


  if (this.data.name == '') {
    var _name = '请填写您的真实姓名'
    Tips(_name)
  } else if (this.data.number == '') {
    var _number = '请填写本人身份证号码'
    Tips(_number)
  } else if (this.data.hold == '') {
    var _hold = '请上传手持身份证'
    Tips(_hold)
  } else if (this.data.positive == '') {
    var _positive = '请上传身份证头像页'
    Tips(_positive)
  } else if (this.data.opposite == '') {
    var _opposite = '请上传身份证反面页'
    Tips(_opposite)
  } else {

        let income_url = 'my/authentication/ididentity';
        let income_data = {
          name: this.data.name,                //姓名
          number: this.data.number,         //证件号码
          hold: this.data.hold,                    //手持身份证
          positive: this.data.positive,          //身份证正面
          opposite: this.data.opposite,       //身份证反面
        };
        app.postRequest(income_url, income_data, function (res) {
          console.log(res)
          if (res.code == 201) {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000
            })
            _this.onLoad();
            return
          }

          // console.log(res.data.name)
          // console.log(res.data.number)
          if (res.data.name){
            _this.setData({
              text1: res.data.name,
              isShow: true,
            });

          }

          if (res.data.number) {
            _this.setData({
              text2: res.data.number,
              isShow: true,
            });

          }

          setTimeout(function () {
            _this.setData({
              text1: '',
              text2: '',
              isShow: false,
            });
          }, 2000)
      

        })

  }




},




})

// 提示框
function Tips(text) {

  wx.showToast({
    title: text,
    icon: 'none',  // 不显示图标
    duration: 2500
  })

}