var C_SCHEDULE_FILTER                    = "300";
var C_DELETE_SCHEDULE                    = "301";
var C_UPDATE_SCHEDULE_VIEW               = "302";

var schedule_obj                         = null;

function getTaskScheduleAutoSearch(){
    var data = { "type": "C_SEARCH" } ;
    var url = "request.pzx?c=" + taskschedule_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onTaskScheduleAutoSearch,schedule_obj) ;
}

function onTaskScheduleAutoSearch(obj,resp) { 
    if (resp.status == C_OK) {
        if (resp.data != null || resp.data.length != 0 ) {
            var filteredBuilding = resp.data;

            $('#filterJobNumberSchedule').autocomplete({
                source: filteredBuilding,
                minLength: 1,
                autoFocus: true
            });
        }
    }
}

function filterTaskSchedule(){
    var fromDate = $('#fromDateScheduleFilter').val();
    var toDate = $('#toDateScheduleFilter').val();

    if(fromDate.trim() == "" || toDate.trim()=="") {
        $('#errorShow').html("* Please input both 'From Date' and 'To Date'.");
        return false;
    }

    var data = { "type"      : C_SCHEDULE_FILTER,
                 "fromDate"  : fromDate, 
                 "toDate"    : toDate,
                 "jobnumber" : $("#filterJobNumberSchedule").val(),
                 "customer"  : $("#cobCustomerFilter").val(),
                 "status"    : $("#cobStatusFilter").val()
                };
    var url  = "index.pzx?c=" + taskschedule_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onTaskScheduleResponse,null) ;
}

function showEditTaskSchedule(id, obj) {
    schedule_obj = obj ;
    showProgress("loading Task Schedule" ) ;
    var data  = { "type": C_UPDATE_SCHEDULE_VIEW, "id" : id} ;
    var url   = "index.pzx?c=" + taskschedule_url + "&t=" + C_UPDATE_SCHEDULE_VIEW  + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onTaskScheduleResponse,null) ;
}

function updateTaskSchedule() {
    saveTaskSchedule(C_UPDATE) ; 
}

function saveTaskSchedule(type){
    var data = {        "type"              : type,
                        "id"                : $('#txtTaskScheduleID').val(),
                        "result"            : $('#cmbTaskScheduleResult').val(),
                        "remarks"           : $('#txtTaskScheduleRemarks').val(),
                        "startdate"         : $('#txtPlanActualStartDate').val(),
                        "starttime"         : $('#txtPlanActualStartTime').val(),
                        "enddate"           : $('#txtPlanActualEndDate').val(),
                        "endtime"           : $('#txtPlanActualEndTime').val()

                    };
    var url  = "index.pzx?c=" + taskschedule_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onTaskScheduleResponse,schedule_obj) ;
}

function popupDeleteTaskSchedule(id, obj){
    var strBtn = "<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Cancel</button>" +
                     "<button onclick=\"return deleteTaskSchedule(" + id + ", this);\" class=\"btn btn-danger\" > Delete </button>";
    $('#modalDeleteTaskScheduleConfirmationButton').html(strBtn);
    $('#modalDeleteTaskScheduleConfirmationContent').html("Are you sure you want to delete this Task Schedule?");
    $('#modalDeleteTaskScheduleConfirmationTitle').html("Delete TaskSchedule");
    $("#deleteTaskScheduleConfirmationView").modal("show");
}

function deleteTaskSchedule(id, obj) {
    $("#deleteTaskScheduleConfirmationView").modal("hide");
    var data = { 
            "id"            : id,
            "type"          : C_DELETE_SCHEDULE
            };
    var url = "index.pzx?c=" + taskschedule_url + "&d=" + new Date().getTime() ;
    callServer(url,"json",data,onTaskScheduleResponse,obj) ;
}

function onTaskScheduleResponse(obj,resp){
	$('#activityLoadingModal').modal('hide');
    if (resp.status != C_OK ) {
        showMessage('error', resp.mesg, "Warning");
        return false;
    }

    if(resp.type == C_SCHEDULE_FILTER) {
        $('#task-schedule-table').html('');
        $('#task-schedule-table').append(resp.data);
    } else if (resp.type == C_UPDATE_SCHEDULE_VIEW) {
        $("#addTaskScheduleView").modal("show");
        $('#modalAddTaskScheduleHeaderViewContent').show().html(resp.data.all);

    
        $('#txtTaskScheduleID').val(resp.data.headerData.id) ;
        /*$('#txtTaskSchedulePlanID').val(resp.data.headerData.plan_id) ;*/
        $('#txtTaskSchedulePlanType').val(resp.data.headerData.plan_type) ;
        $('#txtPlanStartTime').val(resp.data.headerData.start_plan) ;
        $('#txtPlanEndTime').val(resp.data.headerData.end_plan) ;
        $('#txtTaskSchedulePlanTolerance').val(resp.data.headerData.plan_tolerance) ;
        $('#txtTaskSchedulePlanDesc').val(resp.data.headerData.plan_desc) ;
        $('#txtTaskScheduleRemarks').val(resp.data.headerData.remarks) ;
        $('#txtPlanActualStartDate').val(resp.data.headerData.startdate) ;
        $('#txtPlanActualStartTime').val(resp.data.headerData.starttime) ;
        $('#txtPlanActualEndDate').val(resp.data.headerData.enddate) ;
        $('#txtPlanActualEndTime').val(resp.data.headerData.endtime) ;

        var functionName = "updateTaskSchedule()";
        var  strBtn = "<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Cancel</button>" +
                        "<button id=\"btnTaskScheduleAdd\" class=\"btn btn-primary\" onclick=\"return "+functionName+";\">Save</button>";
        $('#txtTaskScheduleRemarks').prop('disabled', false);
        $('#cmbTaskScheduleResult').prop('disabled', false);

        if (resp.data.headerData.result!=0) {
            $('#cmbTaskScheduleResult').val(resp.data.headerData.result) ;
            $('#txtTaskScheduleRemarks').prop('disabled', true);
            $('#cmbTaskScheduleResult').prop('disabled', true);
            strBtn = "";
        }
        $('#modalAddTaskScheduleHeaderViewButtonContent').show().html(strBtn);
    } else if (resp.type == C_UPDATE) {
        var status;
        $("#addTaskScheduleView").modal("hide");
        showMessage('success', resp.mesg, "Success");
        filterTaskSchedule();
    } else if (resp.type == C_DELETE_SCHEDULE) {
        showMessage('success', resp.mesg, "Success");
        $('#task-schedule-rows-'+resp.data).remove() ;
    }
}
