// nova-werun/pages/my/my-profile/index.js
const Parse = getApp().Parse;
const company = getApp().globalData.company;
const uid = Parse.User.current()?.id
const real = require("../../../../utils/real")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    idcard: '',
    sex: '',
    mobile: '',
    fittingCosts: '', //身高
    center: '', //部门
    showStorePopup: false,
    shop: null,
    shopList: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // await this.getname()
    // await this.getdep()
    // // this.getDepartment()
    // this.getwritedep()
    // this.getUptoken()

    this.refersh()
  },

  async refersh() {
    this.getUserProfile()
  },

  /**打开选择位置 */
  openStore() {
    this.getShop()
    this.setData({
      showStorePopup: true
    })
  },
  /**关闭位置选择 */
  closeStore() {
    this.setData({
      showStorePopup: false
    })
  },
  /**获取可选位置 */
  async getShop() {
    let query = new Parse.Query('Department')
    query.equalTo('company', company)
    query.notEqualTo('isDeleted', true)
    query.equalTo('isEnabled', true)
    let d = await query.find()
    let shopList = d.map(item => item.toJSON())
    this.setData({
      shopList
    })
  },
  /**选择位置 */
  chooseShop(e) {
    let {
      shopList,
    } = this.data
    let {
      index
    } = e.currentTarget.dataset
    console.log(shopList[index])
    this.setData({
      shop: shopList[index],
      showStorePopup: false
    })
  },
  /**获取当前用户和身份 */
  async getUserProfile() {
    let profile
    let query = new Parse.Query('Profile')
    query.notEqualTo("isDeleted", true)
    query.equalTo('user', uid)
    query.include('center')
    profile = await query.first()
    if (!profile?.id) {
      let Pro = Parse.Object.extend('Profile')
      profile = new Pro()
    } else {
      this.setData({
        name: profile.get('name') || '',
        idcard: profile.get('idcard') || '',
        sex: profile.get('sex') || '',
        mobile: profile.get('mobile') || '',
        fittingCosts: profile.get('fittingCosts') || '',
        center: profile.get('center') || '',
        shop: profile.get('center')?.toJSON()
      })
    }
    this.setData({
      profile
    })
  },

  onChangeSex(event) {
    this.setData({
      sex: event.detail,
    });
  },

  /** 立即预约*/
  async enlists(e) {
    let {
      name,
      idcard,
      sex,
      mobile,
      fittingCosts,
      center,
    } = this.data
    console.log(name,
      idcard,
      sex,
      mobile,
      fittingCosts, )
    if (!name || !idcard || !sex || !fittingCosts || !mobile) {
      wx.showToast({
        title: '存在未填项',
        icon: 'none',
        duration: 5000
      })
      return
    }
    if (!real.IdentityCodeValid(idcard)) {
      wx.showToast({
        title: '身份证格式错误',
        icon: 'none',
      });
      return
    }
    if (!real.isPoneAvailable(mobile)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
      });
      return
    }
    let {
      shop
    } = this.data
    if (!shop?.objectId) {
      wx.showToast({
        title: '请选择位置',
        icon: 'none',
        duration: 5000
      })
      return
    }
    let {
      profile,
    } = this.data
    profile.set('name', name || '')
    profile.set('mobile', mobile || '')
    profile.set('sex', sex || '')
    profile.set('idcard', idcard || '')
    profile.set('fittingCosts', parseFloat(fittingCosts || '0') || 0)
    profile.set('company', {
      className: 'Company',
      __type: 'Pointer',
      objectId: company
    })
    profile.set('user', {
      className: '_User',
      __type: 'Pointer',
      objectId: uid
    })
    profile.set('center', {
      className: 'Department',
      __type: 'Pointer',
      objectId: shop?.objectId
    })
    // profile.set('isCheck', false)
    let proSave = await profile.save()
    wx.showToast({
      title: '已提交',
      icon: 'none'
    })
    wx.navigateBack({
      delta: 1
    })
  },









  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
})