// pages/handleLogin/index.js
import { userValidate } from "../../service/user";
import { alertInfo, alertSuccess, loadingFunc, wxLogin } from "../../utils/index";
import request from "../../utils/request"
import Dialog from '@vant/weapp/dialog/dialog';
const { OK } = request;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    name: "",
    parkName: "",
    park: "",
    tenementName: "",
    tenement: "",
    canBack: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onChangeName(e) {
    this.setData({
      name: e.detail
    })
  },
  onChangePhone(e) {
    this.setData({
      phone: e.detail
    })
  },
  onChoosePark(e) {
    const { data } = e.detail;
    this.setData({
      park: data.id,
      parkName: data.name
    })
  },
  onChooseTenement(e) {
    const { data } = e.detail;
    this.setData({
      tenement: data.id,
      tenementName: data.name
    })
  },
  callPhone(e) {
    const { phone } = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  handleSubmit() {
    const { park, tenement, name, phone } = this.data;
    if (!park) {
      alertInfo("请选择园区")
      return
    }
    if (!tenement) {
      alertInfo("请选择商户")
      return
    }
    if (!phone) {
      alertInfo("请输入预留手机号")
      return
    }
    if (!name) {
      alertInfo("请输入你的昵称")
      return
    }
    loadingFunc(async () => {
      const wxLoginCode = await wxLogin()
      const { code, message, data } = await userValidate({ park, tenement, phone, name, code: wxLoginCode })
      if (code !== OK) {
        alertInfo(message)
        return
      }
      const { exist, user } = data;
      if (!exist) {
        alertInfo("绑定失败，请检查信息")
        return
      }
      alertSuccess("绑定成功")
      const { token, ...currentUser } = user;
      wx.setStorageSync('user', currentUser)
      wx.setStorageSync('token', token)
      wx.switchTab({
        url: '/pages/home/index',
      })
    })
  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onTenementFocus(e) {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "公司",
      type: 'tenement'
    })
  },
  showTooltip() {
    Dialog.alert({
      title: '提示',
      message: '该手机号为开户时预留的手机号，一般为接收电费欠费短信的管理员手机号。',
    }).then(() => {
      // on close
    });
  },
  scan() {
    wx.scanCode({
      scanType: "qrCode",
      success: ({ path }) => {
        wx.navigateTo({
          url: '/' + path,
        })
      },
      fail: err => {
        console.log('scan code err', err)
        alertInfo("扫码失败，请稍后重试")
      }
    })
  },
  onConfirm(e) {
    const { type, data = {} } = e.detail;
    switch(type) {
      case "park":
        this.setData({
          park: data.id,
          parkName: data.name,
        })
        break;
      case "tenement":
        this.setData({
          tenement: data.id,
          tenementName: data.name,
        })
    }
    this.onCancel();
  },
  onCancel() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
})