//获取团队信息
var request = getRequest(),
    teamCode = request.id,
    isLeader = isTeamLeader();
//记录页面控件的值
var startDate = '', 
    endDate = '',
    state = '0,2,3,4,10', // 状态,初始为：审核中
    dispensaryType = '', //配送方式
    page = 1,
    size = 10,
    nameKey, //搜索的姓名
    totalCount = 0,
    selectDoctor; //选中的医生
$(function(){
    //非团队长隐藏操作按钮
    if(!isLeader){
        $("#arrange").hide();
    }
    //填充下拉框数据
    managementApis.getFilterInfo({
    	data:{
	    	teamCode: teamCode
	    }
    }).then(function(res){
        fillDropDown(res);
        $('#presctiptionStatus').val(state);
        //获得续方订单列表
        getPrescriptionList();
    });
    bindEvents();
});

//获得续方订单列表
function getPrescriptionList( refresh){
    var params = {
        teamCode: teamCode,
        startDate: startDate,
        endDate: endDate,
        state: state,
        diseases: dispensaryType,
        nameKeyword: nameKey
    };
    //先请求获得所有的数量
    managementApis.getListCount({data:params}).then(function(res){
        if(res.status == 200){
            totalCount = res.data.total;
            if(refresh){
                $("#prescriptTable").bootstrapTable('refresh');
            }
            var options = $.extend(managementApis.getListTableAjaxObj, {
                queryParams: queryParams,
                queryParamsType: "limit",
                pagination: true,
                paginationLoop: true,
                sidePagination: 'server',
                pageNumber: 1,
                pageSize: 10,
                responseHandler: function (res) {
                    var data = _.map(res.data, function(o){
                        var result = "";
                        for(var i=0; i<o.prescriptionDt.length; i++){
                            if(i>0){
                                result += ','+o.prescriptionDt[i].name;
                            }else{
                                result += o.prescriptionDt[i].name;
                            }
                        }
                        o.result = result;
                        o.statusName = getStatusName(o.status);
                        o.drugs = getDrugs(o.prescriptionInfo);
                        o.daysRemaining = getDaysRemaining(o)
                        o.action = '<a class="c-12b7f5" href="prescription-detail.html?code='+o.code+'">查看</a>';
                        return o;
                    });
                    
                    return {
                        rows: data,
                        total: totalCount
                    }
                }
            })
            $("#prescriptTable").bootstrapTable(options);
        }
    });
    
}

function queryParams(params) {
    console.log(params);
    //当表格数据变化的时候，则取消全选按钮，然后之前的选中的信息将取消选中
    page = params.offset/params.limit + 1;
    selectItemNum = 0;
    $("#selectAll").prop("checked", false);
    $("#selectedCount").text(0);
    $("#arrange").addClass("disabled");
    
    return {
        teamCode: teamCode,
        page: params.offset/params.limit + 1,
        size: params.limit,
        startDate: startDate,
        endDate: endDate,
        state: state,
        diseases: dispensaryType,
        nameKeyword: nameKey
    };
}

function fillDropDown(res){
    if(res.status == 200){
        var stateHtml = template('state_tmpl', {list: res.data.states});
        $("#presctiptionStatus").append(stateHtml);

        var expressHtml = template('express_tmpl', {list: res.data.diseases});
        $("#express").append(expressHtml);
    }else{

    }
}

//绑定事件
function bindEvents(){
    $(".n-tab").on('click', function(){
        if($(this).hasClass("active")){
            return false;
        }else{
            $(this).addClass("active");
            $(this).siblings().removeClass("active");
            var seDate = getStartEndDate($(this).attr("data-val"));
            startDate = seDate.startDate;
            endDate = seDate.endDate;
            getPrescriptionList(true);
        }
    });

    $("#presctiptionStatus").on('change', function(){
        var $this = $(this);
        state = $this.val();
        getPrescriptionList(true);
    });

    $("#express").on('change', function(){
        dispensaryType = $(this).val();
        getPrescriptionList(true);
    });
    

    $("#searchBtn").on('click', function(){
        var $input = $("#searchName"),
            text = $.trim($input.val());
        nameKey = text;
        getPrescriptionList(true);
    });
}
