import {url} from '../request'

Page({
  data: {
    commoditiesImage: '',
    commoditiesPrice: '',
    commoditiesName: '',
    businessId: '',
    ids: '',
    time: [
      { title: '9:00' },
      { title: '10:00' },
      { title: '11:00' },
      { title: '12:00' },
      { title: '13:00' },
      { title: '14:00' },
      { title: '15:00' },
      { title: '16:00' },
      { title: '17:00' },
    ],
    selectedTime: '',
    meijiashi:[],
  },
  
  onLoad(options) {
    const commoditiesImage = options.commoditiesImage;
    const commoditiesName = options.commoditiesName;
    const commoditiesPrice = options.commoditiesPrice;
    const ids = options.ids;
    const businessId = options.businessId;
    const dianpuid = options.dianpuid
    const businessName = options.businessName

    this.setData({
      commoditiesImage: commoditiesImage,
      commoditiesName: commoditiesName,
      commoditiesPrice: commoditiesPrice,
      ids: ids,
      businessId: businessId,
      dianpuid:dianpuid,
      businessName:businessName
    });
   console.log(ids,'shangpinid');
  },
  
  // 切换标签
  onChange(current) {
    this.setData({
      current,
    });
  },
  onSwipeChange(e) {
    this.setData({
        current: e.detail.current,
    });
},
  onShow(){
this.meijiahsi()
  },
  jiaru() {
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
          const userInfo = res.data;
          if (userInfo && userInfo.cookie) {
            my.request({
              url: url + '/api/cart/add',
              method: 'POST',
              data: {
                businessId: this.data.businessId,
                commoditiesId: this.data.ids,
                quantity: 1,
                selectedOptions: "",
              },
              headers: {
                'content-type': 'application/json',
                'Cookie': userInfo.cookie, // 通过头部传递 cookie
              },
              dataType: 'json',
              success: (res) => {
                if(res.data.code===0){
                  console.log(res);
                  my.alert({ content: '成功添加到购物车' });
                }
                else if(res.data.code===40100){
                  my.alert({
                    content: '登录信息已过期，请重新登录'
                  });
                  my.navigateTo({
                    url:'/pages/denglu/denglu'
                  })
              }
              },
              fail: (error) => {
                console.error('请求失败: ', JSON.stringify(error));
                my.alert({ content: '请求失败，请稍后重试' });
              },
            });
          } else {
              my.alert({
                  content: '您未登录，请先登录。',
                  success: () => {
                      my.navigateTo({
                          url: '/pages/denglu/denglu',
                      });
                  },
              });
          }
      },
  });
  },
  selectTime(e) {
    const selectedTime = e.currentTarget.dataset.time;  // 获取点击的时间
    console.log(selectedTime);
    this.setData({
      selectedTime: selectedTime,  // 更新选中的时间
    });
    console.log('选中的时间:', this.data.selectedTime);  // 打印选中的时间
  },
    // 美甲师
  meijiahsi(){
    my.request({
      url: url + '/api/manicurist/userQueryAll',
      method: 'GET',
      data: {
        businessId: this.data.dianpuid
      },
      headers: {
        'content-type': 'application/json',
      },
      dataType: 'json',
      success: (res) => {
        console.log('Request succeeded:meijiashi', res);
        if (res.data && res.data.data) {
          const modifiedData = res.data.data.map(item => {
            return {
              ...item, // 保留原来的属性
              selected: false // 添加 selected 属性
            };
          });
  
          // 更新 data 中的 meijiashi 数据
          this.setData({
            meijiashi: modifiedData
          });
  
          console.log(this.data.meijiashi, '更新后数据');
        } else {
          console.log('shibaile')
        }
      },
      fail: (error) => {
        console.error('Request failed', error);
      }
    });
  },
  onManicuristClick(event) {
    const index = event.currentTarget.dataset.index;
    let meijiashi = this.data.meijiashi;
    // 取消所有美甲师的选中状态
    meijiashi.forEach((item, i) => {
      item.selected = false;
    });
  
    // 设置当前点击的美甲师为选中状态
    meijiashi[index].selected = true;
  
    // 更新 data 中的 meijiashi
    this.setData({
      meijiashi: meijiashi
    });
    this.setData({
      meijiashiname:meijiashi[index].manicuristName,
      meijiashiid:meijiashi[index].id
    })
  
    console.log('当前选中的美甲师:', this.data.meijiashiname,this.data.meijiashiid);
  },
  zhifu(){
    const commoditiesImage = this.data.commoditiesImage;
    const commoditiesName = this.data.commoditiesName;
    const commoditiesPrice = this.data.commoditiesPrice;
    const meijiashiname = this.data.meijiashiname;
    const yuyuetime = this.data.selectedTime
    const meijiashiid = this.data.meijiashiid
    const selectedTime = this.data.selectedTime
    const dianpuid = this.data.dianpuid
    const businessName = this.data.businessName
    const shopid = this.data.ids
    console.log(dianpuid,'dianpuid');
    if (!selectedTime || !meijiashiname) {
      my.showToast({
          content: "请选择预约时间,并选择美甲师",
          type: "none",
          duration: 5000
      });
      return;
  }
    my.navigateTo({
      url:`/pages/yuyuezhifusm/yuyuezhifusm?commoditiesImage=${commoditiesImage}&&commoditiesName=${commoditiesName}&&commoditiesPrice=${commoditiesPrice}&&meijiashiname=${meijiashiname}&&yuyuetime=${yuyuetime}&&meijiashiid=${meijiashiid}&&selectedTime=${selectedTime}&&dianpuid=${dianpuid}&&businessName=${businessName}&&shopid=${shopid}`
    })
  }
});
