// pages/jigouRegistered/jigouRegistered.js
import Notify from '../../dist/notify/notify';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1:'',
    value2: '',
    value3: '',
    value4: '',
    checked: false,
    show:0,
    fileList: [],
    fileList1: [],
    fileList2: [],
    fileList3: [],
    upload:0,
    type:1,
    uploadPic:0,
    cli:0,
    content:'',
    legalPersonImgage1:'',
    legalPersonImgage2: '',
    licensenImgage:'',
    otherImgage:'',
    latitude:'',
    longitude:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(res);
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude
        })
      }
    })
    this.setData({
      type: options.type
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var x=this;
    wx.request({
      url: app.url + 'ajax_get_organization_rule_text.php',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.status == 'OK') {
          x.setData({
            content: res.data.result
          })
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    // event.detail 为当前输入的值
    this.setData({
      value1:event.detail
    })
  },
  onChangerideo(event) {
    this.setData({
      checked: event.detail
    });
    if(this.data.checked==false){
      this.setData({
        uploadPic:0
      })
    }
    if(this.data.cli==1){
      if (this.data.checked == true) {
        this.setData({
          uploadPic: 1
        })
      }
    }
  },
  surejigou(){
    this.setData({
      show:1,
      checked:true
    })
  },
  sure(){
    this.setData({
      show:0,
      upload:1
    })
  },
  zhuce(e){
    if(this.data.checked==true){
      var config = wx.getStorageSync('config');
      var that = this;
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          app.checkUser(res.code, null);
          if (config.zhuce == "未注册！") {
            this.surejigou()
            wx.getUserInfo({
              success: function (res) {
                console.log(res.userInfo.nickName)
                wx: wx.request({
                  url: app.url + 'ajax_social_user_register1.php',
                  data: {
                    unionId: config.unionId,
                    openId: config.openId,
                    name: res.userInfo.nickName,
                    mobile: '13124619413',
                    legalPerson: 'jia',
                    organizationContact: '13124619413',
                    longitude: that.data.longitude,
                    latitude: that.data.latitude
                  },
                  header: { 'content-type': 'application/json' },
                  method: 'GET',
                  dataType: 'json',
                  success: function (res) {
                    console.log(res, '成功')

                  },
                  fail: function (res) {
                    console.log(res, '失败')
                  },
                  complete: function (res) { },
                })
              }
            })
          } else {
            if (e.detail.errMsg == 'getUserInfo:ok') {
              wx.setStorage({
                key: "userInfo",
                data: e.detail.userInfo
              })
              console.log(e)
              wx.redirectTo({
                url: '../ageChange/ageChange?type=' + this.data.type
              })
            } else {
              wx.showToast({
                title: '注册失败',
                icon: 'none',
                duration: 3000,
                mask: true,
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
          }
        }
      })
    }else{
      Notify('请选择机构认证')
    }
  },
  delete(event) {
    const { index, name } = event.detail;
    const fileList = this.data[`fileList`];
    fileList.splice(index, 1);
    this.setData({ [`fileList`]: fileList });
  },  
  afterRead(event) {
    const { file, name } = event.detail;
    const fileList = this.data[`fileList`];
    this.setData({ 
      [`fileList`]: fileList.concat(file),
      legalPersonImgage1:file[0].path
    });
  },
  delete1(event) {
    const { index, name } = event.detail;
    const fileList1 = this.data[`fileList1`];
    fileList1.splice(index, 1);
    this.setData({ [`fileList1`]: fileList1 });
  },
  afterRead1(event) {
    const { file, name } = event.detail;
    const fileList1 = this.data[`fileList1`];

    this.setData({
       [`fileList1`]: fileList1.concat(file),
      legalPersonImgage2:file[0].path
        });
  },
  delete2(event) {
    const { index, name } = event.detail;
    const fileList2 = this.data[`fileList2`];
    fileList2.splice(index, 1);
    this.setData({ [`fileList2`]: fileList2 });
  },
  afterRead2(event) {
    const { file, name } = event.detail;
    const fileList2 = this.data[`fileList2`];

    this.setData({
      [`fileList2`]: fileList2.concat(file),
      licensenImgage: file[0].path
        });
  },
  delete3(event) {
    const { index, name } = event.detail;
    const fileList3 = this.data[`fileList3`];
    fileList3.splice(index, 1);
    this.setData({ [`fileList3`]: fileList3 });
  },
  afterRead3(event) {
    const { file, name } = event.detail;
    const fileList3 = this.data[`fileList3`];

    this.setData({
       [`fileList3`]: fileList3.concat(file),
      otherImgage: file[0].path 
       });
  },
  upload(){
    this.setData({
      upload:0,
      uploadPic:1,
      cli:1
    })
    var that = this;
    wx:wx.request({
      url: app.url +'ajax_social_user_register2.php',
      data: {
        unionId: '123',
        openId: '123',
        legalPersonImgage1: that.data.legalPersonImgage1,
        legalPersonImgage2: that.data.legalPersonImgage2,
        licensenImgage: that.data.licensenImgage,
        otherImgage: that.data.otherImgage
      },
      header: { 'content-type': 'application/json'},
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        console.log(res,'上传成功')
      },
      fail: function(res) {
        console.log(res,'上传失败')
      },
      complete: function(res) {},
    })
  },
  beforeRead(event) {
    const { file, callback } = event.detail;
    if (file[0].path.indexOf('jpg') < 0) {
      wx.showToast({ title: '请选择jpg图片上传', icon: 'none' });
      callback(false);
      return;
    }
    callback(true);
  }
})