// c:\Users\Holy-Knight-IX\Desktop\Working Space\4.TikTok-MiniPro\ttb\pages\Appointment\appointment\appointment.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: {
    currentID: "",
    pickedTime: "",
    startTime: "开始时间",
    endTime: "结束时间",
    pageNum: 1,
    status: 0,
    list: [],
    Phone: '',
  },

  onLoad(options) {
    this.getList()
  },

  getList(status, startTime, endTime) {
    const that = this
    const data = {
      pageNum: this.data.pageNum,
      userPhone: this.data.Phone,
      pageSize: 200,
    }

    if (startTime && endTime) {
      data.serchStartDate = startTime
      data.serchEndDate = endTime
    }

    if (status || status === 0) {
      data.status = status
    }

    HttpBasics.get({
        url: config.api.appointmentList,
        data
      })
      .then(res => {
        console.log(res, 'res');
        res.data.list.forEach(item => {
          const appointStart = util.timestampToTime(item.startDate, 'YYYY-MM-DD hh:mm:ss')
          const appointEnd = util.timestampToTime(item.endDate, 'hh:mm:ss')
          if (item.startDate && item.endDate) {
            item.appointTime = appointStart + ' - ' + appointEnd
          } else {
            item.appointTime = "暂无";
          }

        })
        that.setData({
          list: res.data.list,
        })
      }).catch(err => {
        console.log(err, 'err');
      })
  },

  setNav(e) {
    const id = e.currentTarget.dataset.id
    this.setData({
      currentID: id == this.data.currentID ? '' : id
    })

    console.log(this.data.currentID, 'currentID');
  },

  setType(e) {
    const type = e.currentTarget.dataset.type

    this.setData({
      currentID: '',
    })

    if (type === 'X') {
      this.getList(false)
    } else if (type === 'Y') {
      this.getList(0)
    } else {
      this.getList(type * 1)
    }


  },

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

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

  search() {
    if (this.data.startTime == '开始时间' || this.data.endTime == '结束时间') {
      return
    }
    const startTime = this.data.startTime + " 00:00:00"
    const endTime = this.data.endTime + " 23:59:59"
    this.getList(false, startTime, endTime)
  },

  goDetail(e) {
    const id = e.currentTarget.dataset.id
    tt.navigateTo({
      url: `/pages/appointDetail/appointDetail?id=${id}`,
    });
  },
  // 核销
  goVerification(e) {
    let _this = this
    // console.log(id, shop);
    console.log(e);

    // const id = BigInt(e.currentTarget.dataset.id)
    const id = e.currentTarget.dataset.id
    console.log(typeof (id));
    console.log(id);
    const shop = e.currentTarget.dataset.shop
    const data = {
      id
    }
    tt.showModal({
      title: "提示",
      content: `当前正在核销【${shop}】的预约订单，请确认是否核销？`,
      showCancel: true,
      confirmText: "确认核销",
      confirmColor: "#52a0fd",
      cancelText: "返回",
      success(res) {
        if (res.confirm) {
          HttpBasics.post({
            url: config.api.verify,
            data
          }).then(res => {
            console.log(res, 'res');
            _this.getList()
            tt.showToast({
              image: './../../assets/images/success.png',
              title: '核销成功',
            })
          }).catch(err => {
            console.log(err, 'err');
            tt.showToast({
              image: './../../assets/images/fail.png',
              title: '核销失败',
            })
          })
        }
        if (res.cancel) {
          return
        }
      }
    });
  },
  // 解决数字丢失精度
  parseBigNumber(numberStr) {
    let result = '';
    let carry = 0;

    for (let i = numberStr.length - 1; i >= 0; i--) {
      const digit = Number(numberStr[i]);
      const sum = digit + carry;

      if (sum >= 10) {
        result = (sum - 10).toString() + result;
        carry = 1;
      } else {
        result = sum.toString() + result;
        carry = 0;
      }
    }

    if (carry === 1) {
      result = '1' + result;
    }

    return result;
  },
  handleInput(e) {
    // console.log(e.detail.value);
    this.data.Phone = e.detail.value
  },
  onShow() {
    this.getList()
  }
})