const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
var util = require('../../utils/md5.js')
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
//import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
Page({
  data: {
    defaultType1: true,
    passwordType1: true,
    xyAgree: false
  },
  change(e) {
    console.log(e)
    if (e.detail.value.length != 0) {
      this.setData({
        xyAgree: true
      })
    } else {
      this.setData({
        xyAgree: false
      })
    }
    console.log(this.data.xyAgree)
  },
  eyeStatus1() {
    if (this.data.defaultType1) {
      this.setData({
        passwordType1: false,
        defaultType1: false,
      })
    } else {
      this.setData({
        passwordType1: true,
        defaultType1: true,
      })
    }
  },
  formSubmit(e) {
    console.log(e.detail, "daying");
    var password = e.detail.value.pwd;
    if (e.detail.value.name) {
      if (e.detail.value.pwd) {
        if (!this.data.xyAgree) {
          wx.showToast({
            title: '仔细阅读协议并勾选',
            icon: 'none',
            duration: 3000,
          })
          return
        }
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.publicUrl + '/applet/bing',
          method: "GET",
          data: {
            name: e.detail.value.name,
            pwd: util.hexMD5(password),
            appletsId: wx.getStorageSync('openId')
          },
          success: (res) => {
            console.log(res, "777777");
            if (res.data.code == 0) {
              Notify({
                type: 'success',
                message: '登录成功'
              });
              // wx.clearStorageSync();
              wx.setStorageSync('user', 'user');
              wx.switchTab({
                url: '/pages/index/index',
              })
              // tools.finishInfo().then((res) => {
              //   console.log(res.data.data.perfect, '我完善信息了吗 1代表完后 0代表未完成')
              //   console.log(res.data.data.comparison, '我人脸识别过了吗 1代表完后 0代表未完成')
              //   if (res.data.data.perfect == '0') {
              //     wx.redirectTo({
              //       url: '/pages/finishInfo/finishInfo',
              //     })
              //   }
              //   if (res.data.data.perfect == '1' && res.data.data.comparison == '0') {
              //     wx.redirectTo({
              //       url: '/pages/faceRecognition/faceRecognition',
              //     })
              //   }
              // })
            } else {
              Notify({
                type: 'danger',
                message: res.data.message
              });
            }
          },
          complete: (res) => {
            wx.hideLoading()
          }
        })
      } else {
        wx.showToast({
          title: "密码不能为空",
          icon: 'none',
          duration: 2000,
        })
      }
    } else {
      wx.showToast({
        title: "账号不能为空",
        icon: 'none',
        duration: 2000,
      })
    }
  },
  goXy() {
    wx.navigateTo({
      url: '/pages/xy/xy',
    })
  },
  goHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onShow() {
    wx.login({
      success: res => {
        // 获取到用户的 code 之后：res.code
        console.log("用户的code:" + res.code);
        // const appid = "wx1c015df104db7030"
        // const secret = "d1956c1b5d3601657c98b0dc80a006f7"
        // let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
        wx.request({
          // 自行补上自己的 APPID 和 SECRET
          url: app.globalData.publicUrl + '/wx/getAppletOpenId',
          method: "GET",
          data: {
            cid: 'applet',
            code: res.code
          },
          success: res => {
            // 获取到用户的 openid
            console.log("用户的openid:" + res.data.data.openid);
            wx.setStorageSync('openId', res.data.data.openid)
            wx.request({
              url: app.globalData.publicUrl + '/applet/isExist',
              method: "GET",
              data: {
                appletsId: wx.getStorageSync('openId')
              },
              success: () => {}
            })
          }
        });
      }
    });
  },
})