(function (c) {
    config.init();
    var selectedDate = window.Fx.getUrlParam('date')||(new Date()).pattern('yyyy-MM-dd');
    var startDate = (new Date()).pattern('yyyy-MM-dd');
    var endDate = (new Date(new Date().getTime()+ 24 * 60 * 60 * 1000 * 8)).pattern('yyyy-MM-dd');
    var a = {
        hospitalId: Fx.getUrlParam("hospitalId"),
        deptId: Fx.getUrlParam("deptId"),
        doctorId: Fx.getUrlParam("doctorId")
    };
    var b = {
        hospitalId: Fx.getUrlParam("hospitalId"),
        deptId: Fx.getUrlParam("deptId"),
        doctorId: Fx.getUrlParam("doctorId"),
        startDate: startDate,
        endDate: endDate
        // regDate: window.Fx.getUrlParam('date')||(new Date()).pattern('yyyy-MM-dd')
    }
    //默认进行第一次请求号源拿数据
    var d = c.extend(window.hzAPI.getToken(),a);
    var timeFlag = '';
    var regFee = '';
    var treatFee = '';

    //界面显示日期 url参数
    //c('#date').html(selectedDate);

    //医生信息
    hzAPI.getData(window.apiUrl + "?c=Index&a=doctorInfo", a, "#docinfo");
    hzAPI.getData(window.apiUrl + "?c=Index&a=doctorInfo", a, "#list1 ul", 1, null, null, null);


    //获取医生预约
    var getDoctorBooking = function () {
        var deferred = c.Deferred();
        c.ajax({
            type: "get",
            url: window.apiUrl+"index.php?c=Other&a=getDoctorBookingScheduleSyn",
            dataType: "jsonp",
            data: c.extend(window.hzAPI.getToken(),a),
            jsonp: "callback",
            async: false,
            crossDomain: true,
            success:function (data) {
                deferred.resolve(data);
            },
            error: function (error) {
                window.hzAPI.dialog(false, window.hzAPI.options.timeout, "网络链接失败");
            },
            timeout: 9000
        });
        return deferred.promise();
    }

    //获取的是日期
    var getDate = function () {
        var deferred = c.Deferred();
        c.ajax({
            type: "get",
            url: window.apiUrl+"getRegInfo.php",
            dataType: "jsonp",
            data: c.extend(window.hzAPI.getToken(),b),
            jsonp: "callback",
            async: false,
            crossDomain: true,
            success:function (data) {
                deferred.resolve(data);
            },
            error: function (error) {
                window.hzAPI.dialog(false, window.hzAPI.options.timeout, "服务器繁忙，请刷新重试");
            },
            timeout: 9000
        });
        return deferred.promise();
    }

    c.when(getDate(),getDoctorBooking()).done(function (res1,res2) {
        // console.log(res1);
        if(!res1){
            window.hzAPI.dialog(false,window.hzAPI.options.timeout,'服务器繁忙，请刷新重试');
            return false;
        }

        var scheduleList = res1.Schedules.Schedule;

	if(scheduleList instanceof Array){
            console.log(scheduleList);
        }else{
            scheduleList = [scheduleList];
        }
	//添加金额到界面上显示
	c('#money').html('￥'+scheduleList[0].Fee/100);
        var dateList = res2.dataInfo.data;
        var scheduleStr = '';
        //找出选择日期的参数的值
        for(var i=0;i < scheduleList.length;i++){
            if(scheduleList[i].regDate == selectedDate){
                timeFlag = dateList[i].timeFlag;
                regFee = dateList[i].regFee;
                treatFee = dateList[i].treatFee;
            }
        }

        //左边
        for(var i=0;i < scheduleList.length;i++){
            if(i == 0){
                scheduleStr += '<li name="'+scheduleList[i].SessionName+'" class="active" value="'+ scheduleList[i].ServiceDate +'">'+
                    scheduleList[i].ServiceDate + '  '+ scheduleList[i].SessionName +'</li>';
                continue;
            }
            scheduleStr += '<li name="'+scheduleList[i].SessionName+'" value="'+ scheduleList[i].ServiceDate +'">'+ scheduleList[i].ServiceDate + '  '+ scheduleList[i].SessionName +'</li>';

        }
        c('.wraplist.left ul').append(scheduleStr);

        //委托事件
        c('.wraplist.left').on('click','li',function (e) {
            c(e.currentTarget).addClass('active');
            c(e.currentTarget).siblings().removeClass('active');
            appointment(timeFlag,regFee,treatFee,c(e.target).html().split(' ')[0],c(e.target).attr('name'));
        });


        d.regDate = scheduleList[0].ServiceDate; //请求的日期
        appointment(timeFlag,regFee,treatFee,scheduleList[0].ServiceDate,scheduleList[0].SessionName);
    });


    var appointment = function (timeFlag,regFee,treatFee,regDate,timeInterval) {
	d.regDate = regDate;
        //获取内容
        c.ajax({
            type: "get",
            url: window.apiUrl+"index.php?c=ReqHis&a=getTimeRegInfo",
            dataType: "jsonp",
            data: d,
            jsonp: "callback",
            async: false,
            crossDomain: true,
            success:function (data) {
                var scheduleStr = '';
                var scheduleDate = data.dataInfo.data;
                c('.wraplist.right ul').html('');
		for (var i = 0; i < scheduleDate.length; i++) {
			if(scheduleDate[i].regLeaveCount>0){
				if(timeInterval=='上午'&&parseInt(scheduleDate[i].endTime.split(":")[0])<=12){
                            scheduleStr += '<li class="line1">' +
                            '<a href="doctorBookingSure.html?scheduleCode=' + scheduleDate[i].scheduleCode + '&hospitalId=' + b.hospitalId + '&deptId=' + b.deptId + '&doctorId=' + b.doctorId +
                            '&regDate=' + regDate + '&timeFlag=' + timeFlag + '&regFee=' + regFee + '&treatFee=' + treatFee + '&startTime=' + scheduleDate[i].startTime + '&endTime=' + scheduleDate[i].endTime + '">' +
                            '    <div class="info_line1" style="text-align: center;color: #008a8a">' + scheduleDate[i].startTime + '~' + scheduleDate[i].endTime +
                            '(总' + scheduleDate[i].regTotalCount + '余' + scheduleDate[i].regLeaveCount + ')号' +
                            '<span>可预约</span>' +
                            '    </div>' +
                            '    <div class="clear"></div>' +
                            '</a>' +
                            '</li>';
                        	}
				if(timeInterval=='下午'&&parseInt(scheduleDate[i].endTime.split(":")[0])>12){
                            scheduleStr += '<li class="line1">' +
                            '<a href="doctorBookingSure.html?scheduleCode=' + scheduleDate[i].scheduleCode + '&hospitalId=' + b.hospitalId + '&deptId=' + b.deptId + '&doctorId=' + b.doctorId +
                            '&regDate=' + regDate + '&timeFlag=' + timeFlag + '&regFee=' + regFee + '&treatFee=' + treatFee + '&startTime=' + scheduleDate[i].startTime + '&endTime=' + scheduleDate[i].endTime + '">' +
                            '    <div class="info_line1" style="text-align: center;color: #008a8a">' + scheduleDate[i].startTime + '~' + scheduleDate[i].endTime +
                            '(总' + scheduleDate[i].regTotalCount + '余' + scheduleDate[i].regLeaveCount + ')号' +
                            '<span>可预约</span>' +
                            '    </div>' +
                            '    <div class="clear"></div>' +
                            '</a>' +
                            '</li>';
                       		 }	
			}else{
				if(timeInterval=='上午'&&parseInt(scheduleDate[i].endTime.split(":")[0])<=12){
                            scheduleStr += '<li class="line1">' +
                            '<a href="javascript:;">' +
                            '    <div class="info_line1" style="text-align: center;color: #008a8a">' + scheduleDate[i].startTime + '~' + scheduleDate[i].endTime +
                            '(总' + scheduleDate[i].regTotalCount + '余' + scheduleDate[i].regLeaveCount + ')号' +
                            '<span style="color: gray;">不可预约</span>' +
                            '    </div>' +
                            '    <div class="clear"></div>' +
                            '</a>' +
                            '</li>';
                        	}
				if(timeInterval=='下午'&&parseInt(scheduleDate[i].endTime.split(":")[0])>12){
                            scheduleStr += '<li class="line1">' +
                            '<a href="javascript:;">' +
                            '    <div class="info_line1" style="text-align: center;color: #008a8a">' + scheduleDate[i].startTime + '~' + scheduleDate[i].endTime +
                            '(总' + scheduleDate[i].regTotalCount + '余' + scheduleDate[i].regLeaveCount + ')号' +
                            '<span style="color: gray;">不可预约</span>' +
                            '    </div>' +
                            '    <div class="clear"></div>' +
                            '</a>' +
                            '</li>';
                        	}
			}	
		}
                c('.wraplist.right ul').append(scheduleStr);
            },
            error: function (error) {
                window.hzAPI.dialog(false, window.hzAPI.options.timeout, "服务器繁忙，请刷新重试");
            },
            timeout: 9000
        });
    }

    //旧接口
    // hzAPI.getData(window.apiUrl + "?c=Other&a=getDoctorBookingScheduleSyn", a, "#list1 ul", 1, null, null, null, "该周暂无出诊安排");




    // c('#subscribe').click(function (e) {
    //     c('.lists').addClass('active');
    // });
    //
    //
    // c('.wrap').click(function (e) {
    //     c('.lists').removeClass('active');
    //     // c('#list1 ul').html('');
    // });
    //
    //
    // c('.close').click(function (e) {
    //     c('.lists').removeClass('active');
    // })
    //添加关注
    // c('#favorite').click(function (e) {
    //     window.hzAPI.sendData(window.apiUrl+"?c=Index&a=favoriteDoctor",c.extend(a,{status: 1}),false, ["更新失败", "更新成功"], [0, c(this)]);
    // })
})(Zepto);
