//index.js
//获取应用实例
const app = getApp()

const page = require('../../utils/page.js');
import fetch from '../../utils/http'
const $api = require('../../config/api.js').$api;

Page({
  onShow() {
    wx.setNavigationBarTitle({
      title: '填写预约信息'
    })
    this.setData({
      BuildingId: page.getCurrentPageOptions().id,
      BuildingName: page.getCurrentPageOptions().name
    })
  },
  data: {
    UserInfo: app.globalData.UserInfo, // 用户信息
    SubmitOff: true, // 提交判重开关
    BuildingId: '',
    BuildingName: '',
    FormData: { // 提交表单数据
      AppointmentDate: '', // 预约时间戳
      Name: '', // 姓名
      PhoneNum: '', // 手机号
      Remark: '' // 备注
    },
    AppointmentDate: '', // 预约时间
    MinDate: new Date().getTime(),
    TriggerSelectAppointmentDate: false // 显隐预约时间选择器
  },
  MakeAnAppointment() { // 预约
    if (this.data.SubmitOff) {
      this.setData({
        SubmitOff: false
      })
      if (this.data.FormData.AppointmentDate == '') {
        wx.showToast({
          title: '请选择预约时间',
          icon: 'none'
        })
        return
      }
      if (this.data.FormData.Name == '') {
        wx.showToast({
          title: '请填写您的姓名',
          icon: 'none'
        })
        return
      }
      if (this.data.FormData.PhoneNum == '') {
        wx.showToast({
          title: '请输入您的手机号',
          icon: 'none'
        })
        return
      }
      fetch({
        url: $api.appointment.save.url.replace(':openid', app.globalData.UserInfo.openid),
        method: $api.appointment.save.method,
        data: JSON.stringify({
          buildingId: this.data.BuildingId,
          appointmentDate: this.data.FormData.AppointmentDate,
          customerName: this.data.FormData.Name,
          phone: this.data.FormData.PhoneNum,
          remark: this.data.FormData.Remark
        })
      }).then((data) => {
        wx.showToast({
          title: '预约成功!',
          success: () => {
            wx.navigateTo({
              url: '/pages/EstateDetail/index?id=' + this.data.BuildingId
            })
          }
        })
      })
    }
  },
  FormInput(e) { // 表单输入数据绑定
    this.setData({
      FormData: {
        ...this.data.FormData,
        [e.target.dataset.name]: e.detail.value
      }
    })
  },
  DateFormat(timetamp) { // 日期格式化
    let aDate = new Date(timetamp)
    return `${aDate.getFullYear()}-${(aDate.getMonth() + 1).toString().padStart(2, 0)}-${aDate.getDate().toString().padStart(2, 0)} ${aDate.getHours().toString().padStart(2, 0)}:${aDate.getMinutes().toString().padStart(2, 0)}`
  },
  SelectAppointmentDateCancel() { // 取消选择预约时间
    this.setData({
      TriggerSelectAppointmentDate: false
    })
  },
  SelectAppointmentDateConfirm(e) { // 确认选择预约时间
    this.setData({
      FormData: {
        ...this.data.FormData,
        AppointmentDate: e.detail
      },
      AppointmentDate: this.DateFormat(e.detail),
      TriggerSelectAppointmentDate: false
    })
  },
  SelectAppointmentDate() { // 打开预约时间选择器
    this.setData({
      MinDate: new Date().getTime(),
      TriggerSelectAppointmentDate: true
    })
  }
})