(function (c) {
    config.init();
    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 selectedDate = window.Fx.getUrlParam('date') || (new Date()).pattern('yyyy-MM-dd');//选择的时间
    var a = {
        hospitalId: Fx.getUrlParam("hospitalId"),
        deptId: Fx.getUrlParam("deptId"),
        startDate: startDate,
        endDate: endDate
    };

    //顶部月份
    var month = (new Date()).pattern('M');
    var e_month = '';
    switch (month){
        case '1':e_month = 'Jan';break;
        case '2':e_month = 'Feb';break;
        case '3':e_month = 'Web';break;
        case '4':e_month = 'Apr';break;
        case '5':e_month = 'May';break;
        case '6':e_month = 'Jun';break;
        case '7':e_month = 'Jly';break;
        case '8':e_month = 'Aug';break;
        case '9':e_month = 'Sept';break;
        case '10':e_month = 'Oct';break;
        case '11':e_month = 'Nov';break;
        case '12':e_month = 'Dec';break;
        default : e_month = 0;
    }
    c('#month').html('');
    c('#month').append('<span>'+month+'</span>' +
        '<span>月</span>' +
        '<div>'+e_month+'</div>');

    //请求顶部日期
    c.ajax({
        type: "get",
        url: window.apiUrl+"getEnableTime.php?day=8&callback=jsonp",
        dataType: "jsonp",
        data: c.extend(window.hzAPI.getToken(), a),
        jsonp: "callback",
        async: false,
        crossDomain: true,
        complete: window.hzAPI.dialog(false,window.hzAPI.options.timeout,'数据请求中，请稍后'),
        success:function (data) {
            // console.log(data);
            c('.headdate .right').html('');
            var ulStr = '';

            //生成顶部日期
            for(var i in data){
                for(var j in data[i]){
                    if( data[i][j] && j == selectedDate ){
                        ulStr += '<li name="'+j+'" class="active has-appointment">' +
                            '<div class="week">'+new Date(j).pattern('E')+'</div>' +
                            '<div class="day">'+j.split('-')[2]+'</div>' +
                            '</li>';
                    }else if( data[i][j]){
                        ulStr += '<li name="'+j+'" class="has-appointment">' +
                            '<div class="week">'+new Date(j).pattern('E')+'</div>' +
                            '<div class="day">'+j.split('-')[2]+'</div>' +
                            '</li>';
                    }else if( !data[i][j] && j == selectedDate){
                        ulStr += '<li name="'+j+'" class="active">' +
                            '<div class="week">'+new Date(j).pattern('E')+'</div>' +
                            '<div class="day">'+j.split('-')[2]+'</div>' +
                            '</li>';
                    }else {
                        ulStr += '<li name="'+j+'">' +
                            '<div class="week">'+new Date(j).pattern('E')+'</div>' +
                            '<div class="day">'+j.split('-')[2]+'</div>' +
                            '</li>';
                    }
                }
            }
            c('.headdate .right').html(ulStr);

            //绑定顶部事件
            c('.headdate .right li').click(function (e) {
                window.location.href = window.location.href.split('&date')[0]+'&date='+c(e.currentTarget).attr('name');
            });
        },
        error: function (error) {
            window.hzAPI.dialog(false, window.hzAPI.options.timeout, "网络链接失败");
        },
        timeout: 9000
    });

    var b = {
        hospitalId: Fx.getUrlParam("hospitalId"),
        deptId: Fx.getUrlParam("deptId"),
        startDate: startDate,
        endDate: startDate,
        callback: 'callback'
    };

    if(window.Fx.getUrlParam('date')){
        b = {
            hospitalId: Fx.getUrlParam("hospitalId"),
            deptId: Fx.getUrlParam("deptId"),
            startDate: Fx.getUrlParam("date"),
            endDate: Fx.getUrlParam("date"),
            callback: 'callback'
        }
    }

    //请求医生
    c.ajax({
        type: "get",
        url: window.apiUrl+"getRegInfo.php",
        dataType: "jsonp",
        data: c.extend(window.hzAPI.getToken(), b),
        jsonp: "callback",
        async: false,
        crossDomain: true,
        complete: window.hzAPI.dialog(false,window.hzAPI.options.timeout,'数据请求中，请稍后'),
        success:function (data) {
            //todo 返回了false，不允许频繁请求接口
            //初始化内容
            c('#morning').html('');
            c('#afternoon').html('');
            if(data!=false && data.Schedules){
                var doctorList = data.Schedules.Schedule;
                for(var i in doctorList){
                    var docinfo = doctorList[i];
                    var docStr = '',btnStr = '';

                    if(docinfo.AvailableLeftNum > 0){
                        btnStr = '<button id="'+docinfo.DoctorCode+'" class="btn-green">可预约</button>';
                    }else{
                        btnStr = '<button type="button" disabled>已满</button>';
                    }

                    docStr = '<li class="doctor">' +
                        '<div class="avatar" style="overflow: hidden;">' +
                        '<img src="' + docinfo.ImageUrl + '" alt="">' +
                        '</div>' +
                        '<div class="body">' +
                        '<div class="docname">' + docinfo.DoctorName + '</div>' +
                        '<div class="doctitle">' + docinfo.DoctorTitle + '</div>' +
                        '</div>' +
                        '<div class="right">' + btnStr
                    '</div>' +
                    '</li>';
                    //下午
                    if(doctorList[i].SessionCode == 'X'){
                        c('#afternoon').append(docStr);
                    } else if (doctorList[i].SessionCode == 'S') {
                        c('#morning').append(docStr);
                    }
                }
            }
            //添加提示文案
            if(c('#afternoon li').length<1){
                c('#afternoon').append('<li style="height: '+c('#afternoon').parent().height()+'px;line-height: '+c('#afternoon').parent().height()+'px;text-align: center;">暂无排班记录</li>');
            }
            //添加提示文案
            if(c('#morning li').length<1){
                c('#morning').append('<li style="height: '+c('#morning').parent().height()+'px;line-height: '+c('#morning').parent().height()+'px;text-align: center;">暂无排版记录</li>');
            }
            c('button').click(function (e) {
                window.location.href = window.location.origin + window.location.pathname.replace('appointmentDoctorList','doctorWho') + '?hospitalId=' +
                    window.Fx.getUrlParam('hospitalId') + '&deptId=' + window.Fx.getUrlParam('deptId') +
                    '&doctorId=' + e.currentTarget.id +'&date=' + selectedDate;
            });
        },
        error: function (error) {
            window.hzAPI.dialog(false, window.hzAPI.options.timeout, "网络链接失败");
        },
        timeout: 9000
    });
    // hzAPI.getData(window.apiUrl+"?c=api&a=getRegInfo", a, ".right");

})(Zepto);
