var request = getRequest(),
    teamCode = request.teamCode,
    patient = request.patient,
    state,
    diseases,
    startDate = '',
    endDate = '',
    totalCount = 0;

$(function(){
    if(!teamCode){
        getPatientInfo();
    }else{
        getFilter();
        getList();
    }
    bindEvents();
})

function getPatientInfo(){
    var params = {patient: patient};
    prescriptionListAPI.getPatientInfo({data: params}).then(function(res){
        if(res.status == 200){
            teamCode = res.data.jtAdminTeam;
            getFilter();
            getList();
        }else{
            toastr.warning(res.msg);
        }
    })
}

function getFilter(){
    var params = {
        teamCode: teamCode,
        patient: patient
    };
    prescriptionListAPI.getFilter({data: params}).then(function(res){
        if(res.status == 200){
            fillDropDown(res);
        }else{
            toastr.warning(res.msg);
        }
    });
}

function getList(isRefresh){
    var params = {
        startDate: startDate,
        endDate: endDate,
        state: state,
        diseases: diseases,
        teamCode: teamCode,
        patient: patient
    };
    prescriptionListAPI.getPrescriptionListCount({data: params}).then(function(res){
        if(res.status == 200){
            totalCount = res.data.total;
            if(isRefresh){
                $("#prescriptTable").bootstrapTable('refresh');
            }
            var options = $.extend(prescriptionListAPI.getPrescriptionListTableAjaxObj, {
                queryParams: queryParams,
                queryParamsType: "limit",
                pagination: true,
                paginationLoop: true,
                sidePagination: 'server',
                pageNumber: 1,
                pageSize: 10,
                responseHandler: function (res) {
                    return {
                        rows: res.data,
                        total: totalCount
                    }
                },
                columns: [{
                    field: '',
                    title:'',
                    formatter: function(val, row, index){
                        var html = template('info_tmp', row);
                        return html;
                    }
                },{
                    field: 'createTime',
                    title: '申请时间',
                    class: 'c-999 pr20',
                    align: 'right',
                    formatter: function(val, row, index){
                        return '<span class="pr10">'+val+'</span>';
                    }
                }],
                onClickRow: function(row, $el){
                    document.location.href = "prescription-detail.html?code="+row.code+'&id='+teamCode;
                }
            })
            $("#prescriptTable").bootstrapTable(options);
        }else{
            toastr.warning(res.msg);
        }
    });
}

function queryParams(params) {
    return {
        page: params.offset/params.limit + 1,
        size: params.limit,
        startDate: startDate,
        endDate: endDate,
        state: state,
        diseases: diseases,
        teamCode: teamCode,
        patient: patient
    };
}

function fillDropDown(res){
    var statusHtml = template('status_tmp', {list: res.data.states});
    $("#status").append(statusHtml);

    var diseaseHtml = template('disease_tmp', {list: res.data.diseases});
    $("#disease").append(diseaseHtml);
}

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

    $("#disease").on('change', function(){
        var $this = $(this);
        diseases = $this.val();
        getList(true);
    });

    $("#time").on('change', function(){
        var $this = $(this);
        var seDate = getStartEndDate($this.val());
        startDate = seDate.startDate;
        endDate = seDate.endDate;
        getList(true);
    });

    template.helper('getStatusName', function(status){
        if(parseInt(status)< -1){
            return "续方取消";
        }
        return getStatusName(status);
    })
}

