import {
  formatDay,
  timestamp
} from "../../../utils/util";
import HTTP from "../../../../../requestFn/Api"
Page({
  data: {
    detail: {},
    sex: '先生',
    date: '随时可看',
    start: '',
  },
  onLoad(options) {
    const that = this;
    const eventChannel = that.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      that.setData({
        detail:{...data.detail}
      })
    })
    const time = new Date();
    const start = formatDay(time);
    that.setData({
      start
    })
  },
  switchChange(e) {
    const {
      value
    } = e.detail;
    if (value) {
      this.setData({
        sex: "先生"
      })
    } else {
      this.setData({
        sex: "女士"
      })
    }
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  async submit(e) {
    const {
      name,
      sex,
      phone,
      time
    } = e.detail.value;
    if (name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: "error",
      })
      return false
    }
    if (phone == "") {
      wx.showToast({
        title: '请输入手机号',
        icon: "error",
      })
      return false
    }
    if (!/^1[345789]\d{9}$/.test(phone)) {
      wx.showToast({
        title: "手机号码有误",
        duration: 2000,
        icon: "error",
      });
      return false;
    }
    let appointment_time
    if (time == '随时可看') {
      appointment_time = 0
    } else {
      appointment_time = timestamp(time)
    }
    const {
      garden_id,
      rent_id
    } = this.data.detail;
    const res = await HTTP.Appointment({
      method:"post",
      garden_id,
      rent_id,
      name,
      phone,
      gender: sex ? 1 : 2,
      appointment_time
    })
    if (res.code == 0) {
       wx.showToast({
         title: '提交成功',
         icon:'success',
         success(){
           setTimeout(()=>{
            wx.navigateBack()
           },1000)
         }
       })
    }
  }
})