// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
import { info } from "../../apis/phone";
import { bindPhoneList } from "../../apis/consumer";
import { userInfo } from "../../apis/login";

Page({
  data: {
    // phone: '13408085719',
    phone: '',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    // isBind: false
  },
  // 事件处理函数
  handleResult() {
    // console.log('handleResult');
    // info(this.data.phone).then(res=>{
    //   console.log(res);

    // })
    const { phone } = this.data
    if (!phone) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    }
    // if (phone.length !== 11) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '手机号格式错误'
    //   })
    //   return
    // }
    // let reg = '';
    // console.log((reg.test(phone))
    
    // console.log(/^1[3|4|5|7|8][0-9]{9}$/.test(phone));
    
    if (!/^1[3|4|5|7|8][0-9]{9}$/.test(phone)) {
      wx.showToast({
        icon: 'none',
        title: '手机号格式错误!'
      })
      return
    }
    wx.navigateTo({
      url: `/pages/searchResult/searchResult?phone=${phone}`,
    })
  },
  goPrivacy() {
    wx.navigateTo({
      url: '/pages/privacy/privacy'
    })
  },
  goQA() {
    wx.navigateTo({
      url: '/pages/QA/QA'
    })
  },
  goService() {
    wx.navigateTo({
      url: '/pages/service/service'
    })
  },
  handleContact() {
    let { phone } = this.data
    wx.chooseContact({
      success: t => {
        let temp = t.phoneNumber.replaceAll('-', '').replaceAll(' ', '')
        let reg = /^1[3|4|5|7|8][0-9]{9}$/;
        if (reg.test(temp)) {
          this.setData({
            phone:temp
          })
        }
        else {
          wx.showToast({
            icon: 'none',
            title: '手机号格式错误!'
          })
        }
      },
      fail: e => console.log(e),
      complete: () => { }
    })
  },
  checkMy() {
    if (wx.getStorageSync('accountList').length > 0) {
      wx.navigateTo({
        url: '/pages/my/my'
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
  },
  getBindPhoneList() {
    bindPhoneList().then(res => {
      wx.setStorageSync('accountList', res.d);
      let userInfo = wx.getStorageSync('userInfo');
      if (!userInfo) {
        wx.setStorageSync('userInfo', res.d[0])
      } else {
        wx.setStorageSync('userInfo', res.d.find((consumer: any) => consumer.id === userInfo.id))
      }
    })
  },
  onLoad() {
    // @ts-ignore
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  onShow() {
    this.getBindPhoneList()

  },
  getUserProfile() {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户数据档案均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户数据档案后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e: any) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户数据档案
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})

export { }
