import {url} from '../request'
Page({
  data: {
    
  },
  onLoad(options) {
    const commoditiesImage = options.commoditiesImage;
    const commoditiesName = options.commoditiesName;
    const commoditiesPrice =options.commoditiesPrice
    const meijiashiname = options.meijiashiname;
    const yuyuetime = options.yuyuetime;
    const meijiashiid = options.meijiashiid;
    const selectedTime = options.selectedTime; // 例如 "9:00"
    const dianpuid = options.dianpuid;
    const businessName = options.businessName;
    const shopid = options.shopid
  
    // 获取当前日期
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // 补充两位
    const day = currentDate.getDate().toString().padStart(2, '0'); // 补充两位
  
    // 处理 selectedTime，确保格式正确，例如 "9:00" 转为 "09:00"
    const timeParts = selectedTime.split(':');
    const hours = timeParts[0].padStart(2, '0'); // 补充小时
    const minutes = timeParts[1].padStart(2, '0'); // 补充分钟
  
    // 拼接日期和时间
    const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}`;
  
    // 设置格式化后的 selectedTime
    this.setData({
      commoditiesImage: commoditiesImage,
      commoditiesName: commoditiesName,
      commoditiesPrice: commoditiesPrice,
      meijiashiname: meijiashiname,
      yuyuetime: yuyuetime,
      meijiashiid: meijiashiid,
      selectedTime: formattedDateTime, // 存储格式化后的时间
      dianpuid: dianpuid,
      businessName: businessName,
      shopid:shopid
    });
  },
  name(e){
    this.setData({
      name: e.detail.value,
    });
    console.log(e.detail.value);
  },
  phone(e){
    this.setData({
      phone: e.detail.value,
    });
    console.log(e.detail.value);
  },
  // zhifu(){
  //   my.getStorage({
  //     key: 'userInfo',
  //     success: (res) => {
  //       const userInfo = res.data;
  //       this.setData({
  //         id: userInfo.id, // 获取 id
  //       });
  //   // 发送请求移除商品
  //   if (userInfo && userInfo.cookie) {
  //   my.request({
  //     url: url + '/api/appointments/add',
  //     method: 'POST',
  //     data: { 
  //       appointmentTime: this.data.selectedTime,
  //       businessId: this.data.dianpuid,
  //       businessName: this.data.businessName,
  //       id: "",
  //       manicuristId: this.data.meijiashiid,
  //       manicuristName: this.data.meijiashiname,
  //       notes: "",
  //       payMethod: 0,
  //       phone: this.data.phone,
  //       serviceMode: 0,
  //       userId: userInfo.id,
  //       userName: this.data.name,
  //     }, 
  //     headers: { 
  //       'content-type': 'application/json', 
  //       'Cookie': userInfo.cookie 
  //   },
  //     dataType: 'json',
  //     success: (res) => {
  //       console.log(res);
  //       if (res.data.code === 0) {
  //         my.alert({content:'预约成功'})
  //         my.navigateBack();
  //         console.log('chenggong');
  //       } else {
  //         my.alert({ content: '失败，请稍后重试' });
  //         console.log(this.data.name,userInfo.id,this.data.phone,this.data.meijiashiname,this.data.meijiashiid,this.data.businessName,this.data.dianpuid,this.data.selectedTime,);
  //       }
  //     },
  //       });
  //      }
  //     },
  //   });
  //   this.add()
  // },
  add(){
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        const userInfo = res.data;
        this.setData({
          id: userInfo.id, // 获取 id
        
        });
    // 发送请求移除商品
    if (userInfo && userInfo.cookie) {
    my.request({
      url: url + '/api/orders/add',
      method: 'POST',
      data: { 
        appointmentTime: this.data.selectedTime,
        businessName: this.data.businessName,
        manicuristId: this.data.meijiashiid,
        manicuristName:  this.data.meijiashiname,
        id: "",
        businessId: this.data.dianpuid,
        notes:this.data.note,
        orderItemsAddRequest: [
        {
          attributeNames: this.data.commoditiesName,
          commoditiesId: this.data.shopid,
          quantity: 1
        }
        ],
        payMethod: 0,
        serviceMode: 0,
        phone: this.data.phone,
        totalPrice: this.data.commoditiesPrice,
        userId: this.data.id,
        userName: this.data.name
      }, 
      headers: { 
        'content-type': 'application/json', 
        'Cookie': userInfo.cookie 
    },
      dataType: 'json',
      success: (res) => {
        console.log(this.data.name,this.data.commoditiesPrice,this.data.phone,this.data.shopid,this.data.commoditiesName,this.data.note,this.data.dianpuid,this.data.id,);
        console.log(res,'成功');
        if (res.data.code === 0) {
          my.showToast({
            content: '待商家确认，请稍后',
            duration: 2000 // 提示持续时间
          });
          my.navigateBack();
          console.log('chenggong');
        } else {
          my.alert({ content: '失败，请稍后重试' });
          console.log(this.data.name,this.data.commoditiesPrice,this.data.phone,this.data.shopid,this.data.commoditiesName,this.data.note,this.data.dianpuid,'else');
        }
      },
      fail:(res)=>{
        console.log(this.data.name,this.data.commoditiesPrice,this.data.phone,this.data.shopid,this.data.commoditiesName,this.data.note,this.data.dianpuid,'失败');
      }
        });
       }
      },
    });
  },
  note(e){
    this.setData({
      note: e.detail.value,
    });
    console.log(e.detail.value);
  }
});
