// packageA/mycomponent/goodsComponent/plugin/timeAppointment/timeAppointment.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goods_plugin:{
      type:Object,
      value:{}
    },
    goodsInfo:{
      type:Object,
      value:{}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    personnel:[],
    radio:0,
    personnel_obj:{},

    dateArr: [],
    day_index:0,
    time_index:null,
    day_obj:{},
    time_obj:{},

    service_text:"服务",
    reserve_obj_text:"预约人员"

  },
  lifetimes:{
    attached(){
      // let dateArr = this.GetDateStr(14);
      // this.setData({dateArr});
      wx.getStorage({
        key: "yz_basic_info",
        success: (res)=> {
          this.setData({
            service_text:res.data.lang.reserve_simple.service||'服务',
            reserve_obj_text:res.data.lang.reserve_simple.reserve_obj||'预约人员',
          });
        },
      });

      if(this.data.goods_plugin && this.data.goods_plugin.reserve_simple_goods && this.data.goods_plugin.reserve_simple_goods.objs){
        let personnel = this.data.goods_plugin.reserve_simple_goods.objs;
        let changeData={personnel};
        if(personnel.length>0){
          changeData.radio = personnel[0].id;
          changeData.personnel_obj = personnel[0];
        }
        this.setData(changeData);
        if(personnel.length>0){
          this.getWorkTime(personnel[0].id);
        }
        
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getWeek(dateString) {
      let dateArray = dateString.split("-");
      let date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
      return "周" + "日一二三四五六".charAt(date.getDay());
    },
    onChangeRadio(event) {
      this.setData({
        radio: event.detail,
      });
      let obj = this.data.personnel.find((item)=>{
        return item.id==event.detail;
      });
      this.setData({personnel_obj:obj});
      this.getWorkTime(event.detail);
    },
    setDayIndex(evt){
      let {item,index} = evt.currentTarget.dataset;
      if(item.status==0) return;
      this.setData({
        day_index:index,
        day_obj:item,
        time_index:null
      });
    },
    setTimeIndex(evt){
      let {item,index} = evt.currentTarget.dataset;
      if(item.status==0) return;
      this.setData({
        time_index:index,
        time_obj:item
      });
    },
    gotoOrder(){
      if(this.data.time_index==null) return app.tips("请选择预约时间");
      let {service_id,id,goods_option_id,name} = this.data.personnel_obj;
      let begin_time = this.data.dateArr[this.data.day_index].work_date+" "+this.data.dateArr[this.data.day_index].periods[this.data.time_index].period_time_start_str;
      let reserve_order = {service_id,obj_id:id,begin_time,name};
      wx.navigateTo({
        url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&total=1&optionsId="+goods_option_id+"&goodsId=" + this.data.goodsInfo.id + "&reserve_order=" + JSON.stringify(reserve_order)
      });
    },
    gotoUserDe(evt){
      let id = evt.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/packageF/timeAppointment/AppointmentUserDe/AppointmentUserDe?id='+id,
      });
    },
    getWorkTime(id){
      let obj = this.data.personnel.find((item)=>{
        return item.id==id;
      });
      console.log(obj);
      if(!obj) return;
      let urlStr = app.getNetAddresss("plugin.reserve-simple.frontend.work-time.get-service-obj-periods");
      app._getNetWork({
        url: urlStr,
        data:{
          obj_id:obj.id,
          service_id:obj.service_id
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result != 1) return app.tips(res.msg);
          
          let list = res.data;
          list.forEach((item)=>{
            item.week = this.getWeek(item.work_date);
            item.day = item.work_date.substr(-2,2);
            item.month = item.work_date.substr(-5,5);
          });
          console.log(list);
          this.setData({
            dateArr:list,
            day_obj:list[0]
          });
        }
      });
    },
    GetDateStr(AddDayCount) {
      let dateArr = [];
      for (let i = 0; i < AddDayCount; i++) {
        var dd = new Date();
        dd.setDate(dd.getDate() + i); //获取AddDayCount天后的日期
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1; //获取当前月份的日期，不足10补0
        var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
        let weekStr = this.getWeek(y + "-" + m + "-" + d);
        let str = {
          day: d,
          week: weekStr
        };
        dateArr.push(str);
      }
      return dateArr;
    }
  }
});
