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

Page({
  onShow() {
    wx.setNavigationBarTitle({
      title: '填写预约信息'
    })
  },
  data: {
    UserInfo: app.globalData.UserInfo, // 用户信息
    SubmitOff: true, // 提交判重开关
    FormData: { // 提交表单数据
      EstateId: '', // 楼盘id
      AppointmentDate: '', // 预约时间戳
      Name: '', // 姓名
      PhoneNum: '', // 手机号
      Remark: '' // 备注
    },
    AppointmentDate: '', // 预约时间
    EstateName: '', // 楼盘名称
    EstateList: [{ // 楼盘列表数据
      value: '示例楼盘A',
      id: '1'
    }, {
      value: '示例楼盘B',
      id: '2'
    }, {
      value: '示例楼盘C',
      id: '3'
    }],
    TriggerSelectEstate: false, // 显隐楼盘选择器
    MinDate: new Date().getTime(),
    TriggerSelectAppointmentDate: false // 显隐预约时间选择器
  },
  MakeAnAppointment() { // 预约
    if (this.data.SubmitOff) {
      this.setData({
        SubmitOff: false
      })
    }
  },
  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
    })
  },
  SelectEstateConfirm(e) { // 确认选择楼盘
    this.setData({
      FormData: {
        ...this.data.FormData,
        EstateId: e.detail.value.id
      },
      EstateName: e.detail.value.value,
      TriggerSelectEstate: false
    })
  },
  SelectEstateCancel() { // 取消选择楼盘
    this.setData({
      TriggerSelectEstate: false
    })
  },
  SelectEstate() { // 打开楼盘选择器
    this.setData({
      TriggerSelectEstate: true
    })
  }
})