// c:\Users\Holy-Knight-IX\Desktop\Working Space\4.TikTok-MiniPro\ttb\pages\appointDetail\appointDetail.js
const config = require('../../config/config.js')
const Http = require('../../utils/http.js')
const HttpBasics = require('../../utils/HttpBasics.js')
const util = require('../../utils/util')
var app = getApp()

Page({
  data: {
    id: "",
    detail: null,
    isShowBtns: true,
    isSetTime: false,
    isSetAddress: false,
    pickedDate: "请选择日期",
    pickedStartTime: "请选择",
    pickedEndTime: "请选择",
    startTime: "",
    endTime: ""
  },

  onLoad(options) {
    console.log(options, 'options');
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getDetail(options.id)
    }
  },

  getDetail(id) {

    const that = this
    const data = {
      id
    }
    HttpBasics.get({
        url: config.api.appointDetail,
        data
      })
      .then(res => {
        console.log(res, 'res');
        console.log(typeof (data.id));
        const appointStart = util.timestampToTime(res.data.startDate, 'YYYY-MM-DD hh:mm:ss')
        const appointEnd = util.timestampToTime(res.data.endDate, 'hh:mm:ss')

        if (res.data.startDate && res.data.endDate) {
          res.data.appointTime = appointStart + ' - ' + appointEnd
        } else {
          res.data.appointTime = "暂无";
        }
        that.setData({
          detail: res.data,
          pickedDate: util.timestampToTime(res.data.startDate, 'YYYY-MM-DD') || '请选择日期',
          pickedStartTime: util.timestampToTime(res.data.startDate, 'hh:mm') || '请选择',
          pickedEndTime: util.timestampToTime(res.data.endDate, 'hh:mm') || '请选择',
        })
      }).catch(err => {
        console.log(err, 'err');
      })
  },

  back() {
    tt.navigateBack();
  },

  chooseDate(e) {
    console.log(e.detail.value, 'e');
    this.setData({
      pickedDate: e.detail.value
    })
  },

  chooseStartTime(e) {
    console.log(e.detail.value, 'e');
    this.setData({
      pickedStartTime: e.detail.value
    })
  },

  chooseEndTime(e) {
    console.log(e.detail.value, 'e');
    this.setData({
      pickedEndTime: e.detail.value
    })
  },

  cancel() {
    const that = this
    tt.showModal({
      title: "提示",
      content: "是否与用户取得联系并取消确认？",
      showCancel: true,
      confirmText: "确认取消",
      confirmColor: "#52a0fd",
      cancelText: "返回",
      success(res) {
        if (res.confirm) {
          that.goChange(that.data.id, 0)
        }
        if (res.cancel) {
          return
        }
      }
    });
  },

  confirm() {
    const that = this
    tt.showModal({
      title: "提示",
      content: "是否与用户取得联系并完成预约？",
      showCancel: true,
      confirmText: "确认完成",
      confirmColor: "#52a0fd",
      cancelText: "关闭",
      success(res) {
        if (res.confirm) {
          that.goChange(that.data.id, 5)
        }
        if (res.cancel) {
          return
        }
      }
    });
  },

  setDate() {
    const isSetTime = !this.data.isSetTime
    this.setData({
      isSetTime,
      isSetAddress: false
    })
  },

  confirmDate() {
    const isSetTime = !this.data.isSetTime
    this.setData({
      isSetTime,
      isSetAddress: false
    })
    if (!isSetTime) {
      const tempData = this.data

      if (tempData.pickedDate == '请选择日期') {
        tt.showToast({
          title: '请请选择日期！',
          icon: 'fail'
        });
        return
      }

      if (tempData.pickedStartTime == '请选择') {
        tt.showToast({
          title: '请起始时间！',
          icon: 'fail'
        });
        return
      }

      if (tempData.pickedEndTime == '请选择') {
        tt.showToast({
          title: '请结束时间！',
          icon: 'fail'
        });
        return
      }
      const data = {
        id: this.data.detail.id,
        couponOrderId: this.data.detail.couponOrderId,
        startDate: tempData.pickedDate + " " + tempData.pickedStartTime + ":00",
        endDate: tempData.pickedDate + " " + tempData.pickedEndTime + ":00"
      }
      this.goSetDate(data)
      console.log(data, 'data');
    }
  },

  setAddress() {
    const isSetAddress = !this.data.isSetAddress
    this.setData({
      isSetAddress,
      isSetTime: false
    })
  },

  settingAddress(e) {
    const detail = this.data.detail
    detail.detailedAddress = e.detail.value
    this.setData({
      detail
    })
  },

  confirmAddress() {
    const isSetAddress = !this.data.isSetAddress
    this.setData({
      isSetAddress,
      isSetTime: false
    })
    const data = {
      id: this.data.detail.id,
      couponOrderId: this.data.detail.couponOrderId,
      detailedAddress: this.data.detail.detailedAddress,
    }
    this.goSetDate(data)
  },

  goChange(id, status) {
    const data = {
      id,
      status
    }
    HttpBasics.post({
        url: config.api.appointConfirm,
        data
      })
      .then(res => {
        console.log(res, 'res');
        tt.showToast({
          title: status == 5 ? '确认成功！' : '取消成功！',
          icon: "success",
          duration: 2000
        });
        setTimeout(() => {
          tt.navigateBack();
        }, 2000);
      }).catch(err => {
        console.log(err, 'err');
        tt.showToast({
          title: err.message,
          icon: "none"
        });
      })
  },

  goSetDate(data) {
    const that = this
    HttpBasics.post({
        url: config.api.appointSetDate,
        data
      })
      .then(res => {
        console.log(res, 'res');
        tt.showToast({
          title: '修改成功！',
          icon: "success",
          duration: 2000
        });
        that.getDetail(that.data.id)

      }).catch(err => {
        console.log(err, 'err');
        tt.showToast({
          title: err.message,
          icon: "none"
        });
      })
  }
})