var request = getRequest();

var doctor = APIService.userAgent.uid,
    reply = request.reply,
    status = request.status,
    patientName = '',
    startDate = '',
    endDate = '',
    totalCount = 0;

$(function(){
    getListData();
    bindEvents();
});

function getListData(isRefresh){
    var params = {
        doctor: doctor,
        reply: reply,
        status: status,
        type: 8,
        patientName: patientName,
        startTime: startDate,
        endTime: endDate
    }
    consultingAPI.getListCount({data: params}).then(function(res){
        if(res.status == 200){
            totalCount = res.total;
            if(isRefresh){
                $("#listTable").bootstrapTable('refresh');
            }
            var options = $.extend(consultingAPI.getListTableAjaxObj, {
                queryParams: queryParams,
                queryParamsType: "limit",
                pagination: true,
                paginationLoop: true,
                sidePagination: 'server',
                pageNumber: 1,
                pageSize: 10,
                responseHandler: function (res) {
                    if(res.status == 200){
                        return {
                            rows: res.data,
                            total: totalCount
                        }
                    }else{
                        return {
                            rows: [],
                            total: totalCount
                        }
                    }
                    
                },
                columns:[{
                    field: 'patientName',
                    title: '居民姓名'
                },{
                    field: 'create_time',
                    title: '咨询时间'
                },{
                    field: 'status',
                    title: '咨询状态',
                    formatter: function(val, row, index){
                        var text = "";
                        if(val == 0){
                            if(reply == 0){
                                text = "待回复";
                            }else{
                                text = "进行中";
                            }
                        }else{
                            text = "已结束";
                        }
                        return text;
                    }
                },{
                    field: '',
                    title: '操作',
                    formatter: function(val, row, index){
                        return '<a class="c-12b7f5" href="prescription-main.html?sessionId='+row.session_id+'&patiCode='+row.patient+'">查看</a>';
                    }
                }]
            });
        
            $("#listTable").bootstrapTable(options);
        }
    })
}

function queryParams(param){
    return {
        page: param.offset/param.limit + 1,
        pagesize: param.limit,
        doctor: doctor,
        reply: reply,
        status: status,
        type: 8,
        patientName: patientName,
        startTime: startDate,
        endTime: endDate
    }
}

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;
            getListData(true);
        }
    });

    $("#searchName").on('keyup', function(){
        if (e.which === 13) {
            getListData(true);
        }
    })

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