var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var check = require('../../../utils/check.js');
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderId:'',
		serviceId:'',
		bookCount:0,
		orderStatusText:'订单服务中',
		activeTab:1,
    orderBook:{
			status:'',
		},
		jzOrderBookDetails:[],
		unusedSerList:[],
		usedSerList:[],
		serList:[],
		choosePopup:false,
		timeList:check.getTimeList(1),
		dateList:util.getDateList(new Date(new Date().getTime()+60*60*1000*24),1),
		timeIndex:0,//选中的时间段
    dateIndex:0,//选中的日期
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
      orderId: options.orderId
    });
    this.getServiceDetail();
	},

	getServiceDetail: function() {
    // wx.showLoading({
    //   title: '加载中',
    // });

    // setTimeout(function() {
    //   wx.hideLoading();
    // }, 2000);

    util.request(api.ServiceDetail, {
			order_id: this.data.orderId
      // order_id: '1342922'
    }).then(res=> {
			// wx.hideLoading();
      if (res.errno === 0) {
				let jzOrderBookDetails=res.data.jzOrderBookDetails;
				let 	unusedSerList=jzOrderBookDetails.filter(item=>{
					return item.status===0||item.status==1
				});
				let usedSerList=jzOrderBookDetails.filter(item=>{
					return item.status==3||item.status==4
				});
				let timeList=check.getTimeList(res.data.goods.serviceDuration);
         this.setData({
					 goods:res.data.goods,
					orderBook:res.data.orderBook,
					jzOrderBookDetails:jzOrderBookDetails,
					activeTab:1,
					unusedSerList:unusedSerList,
					usedSerList:usedSerList,
					serList:unusedSerList,
					timeList:timeList
				 });
      }
    

    });
  },
	onTabChange(e){
    
    this.setData({
			activeTab:e.detail.name,});

			if(this.data.activeTab==1){
				this.setData({
					serList:this.data.unusedSerList
				});
       
			}else{
				this.setData({
					serList:this.data.usedSerList
				})
			}
		},
		showAppoint(e){
			this.setData({
				choosePopup:true,
				serviceId:e.currentTarget.dataset.id
			 });
		},
		 onClose(){
     this.setData({
      choosePopup:false
     });
	},
	dateCheck(e){
    let index=e.currentTarget.dataset.index;
    this.setData({
      dateIndex:index
    });
  },
  timeCheck(e){
    let index=e.currentTarget.dataset.index;
    this.setData({
      timeIndex:index
    });
  },
		confirmTime(){
			let date=this.data.dateList[this.data.dateIndex].date;
			let week=this.data.dateList[this.data.dateIndex].week;
			let time=this.data.timeList[this.data.timeIndex].time;
			console.log(date,week,time);
      
    let params={
			detail_id:this.data.serviceId,
			date:date,
			startTime:time.split('-')[0],
			endTime:time.split('-')[1],
     
    };
     util.request(api.ServiceOrderBook, params, "GET")
    .then(res=> {
			console.log(res);
      if (res.errno == 0) {
				wx.showToast({
					title: '预约成功',
					icon: 'none',
					duration: 2000
				});
				this.setData({
					// oneTimeAppoint:date+' '+week+' '+time,
					choosePopup:false,
				 });
        this.getServiceDetail();
      } else {
        util.showErrorToast(res.errmsg);
      }

    });



		},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})