﻿
var ticket_id = 0;
var dept_idTransfeer = 0;
var emp_idTransfeer = 0;

function transfeerTicket(ticketId) {
    var pageName = DrawTransfeerTicketForm();
    SelectTicketData(ticketId);
    openTransfeerTicketModal('Transfer Ticket', pageName, '1');
}

function openTransfeerTicketModal(Title, contentvalue, index) {
    //    setCookie('index', index, 1);
    modal_var = $.modal({
        content: contentvalue,
        title: Title,
        maxWidth: 16000,
        buttons: {
            'Close': function (win) { win.closeModal(); }
        }
    });
}

function SelectTicketData(ticketId) {
    ticket_id = ticketId;
    WebService.getTicketForTransfeer(ticket_id, ViewTicket, errorViewTicket);
}

function ViewTicket(result) {
    if (result != null) {
        dept_idTransfeer = result.DeptId;
        emp_idTransfeer = result.empid;
        fillLists();
    }
    else {
        window.location = "Default.aspx";
    }
}

function errorViewTicket() {
    openModalMessageAlert("Error", "Can not get Departments!", '');
}

function fillLists() {
    try {
        WebService.getOrderedDepartments( onfillDeptDropListSuccess, onfillDeptDropListFailed);
    }
    catch (e) {
        alert(e);
    }
}

function onfillDeptDropListSuccess(deptList) {
    if ((deptList != null) & (deptList.length > 0)) {
        for (var i = 0; i < deptList.length; i++) {
            var Text = deptList[i].name;
            var Id = deptList[i].id.toString();
            var opt = document.createElement("option"); // Create an Option object
            // Add an Option object to Drop Down/List Box                                                                           
            opt.text = Text;
            opt.value = Id;
            if (Id == dept_idTransfeer) {
                opt.setAttribute("selected", "selected");
            }
            document.getElementById("Slct_Dept").options.add(opt); // Assign text and value to Option object   
        }
    //    fillEmplist();
    }
}

function onfillDeptDropListFailed() {
    openModalMessageAlert("Error", "Can not get Departments!", '');
}

function fillEmplist() {
    WebService.getEmployeesByDept(dept_idTransfeer, onfillEmpDropListSuccess, onfillEmpDropListFailed);
}

function onfillEmpDropListSuccess(empList) {
    if ((empList != null) & (empList.length > 0)) {
        for (var i = 0; i < empList.length; i++) {
            var Text = empList[i].Name.toString()
            var Id = empList[i].Id.toString()
            var opt = document.createElement("option"); // Create an Option object
            // Add an Option object to Drop Down/List Box                                                                           
            opt.text = Text;
            opt.value = Id;
            if (Id == emp_idTransfeer) {
                opt.setAttribute("selected", "selected");
            }
            document.getElementById("Slct_emp").options.add(opt); // Assign text and value to Option object   
        }
    }

}

function onfillEmpDropListFailed() {
    openModalMessageAlert("Error", "Can not get Employees!", '');
}

function trans_dept_change() {
    $("#Slct_emp").empty().append('<option value="0">select Employee</option>');
    var dep_selected = $("#Slct_Dept option:selected").val();
    WebService.getEmployeesByDept(dep_selected, onfillEmpDropListSuccess, onfillEmpDropListFailed);
}

function ticketUpdate() {
    var dep_selected = $("#Slct_Dept option:selected").val();
    var emp_selected = $("#Slct_emp option:selected").val();
    WebService.transfeerTicket(ticket_id, dep_selected, emp_selected, onUpdatedTicketSuccess, onUpdatedTicketFailed);

}

function onUpdatedTicketSuccess(result) {
    if (result == 1) {
        openModalMessageAlert("Alert", "Ticket Transfered Successfully!", 'ViewTickets.aspx');
    }
    else {
        openModalMessageAlert("Alert", "Ticket Update Failed!", '');
    }
}

function onUpdatedTicketFailed() {
    openModalMessageAlert("Error", "Can not update ticket!", '');
}


function DrawTransfeerTicketForm() {
    var HtmlTags = '<div class="block-controls" style="width: 97.4%;">';
    try {
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + ' <div style="text-align: left; float: left; width: 300px; height: 50px; vertical-align: middle; font-family: Arial, Helvetica, sans-serif; font-size: large; color: #0575CA;font-weight: bold;">';
        HtmlTags = HtmlTags + '<img src="images/icons/users_2states.png" alt="" /></div>';
        HtmlTags = HtmlTags + '<ul class="controls-buttons">';
        HtmlTags = HtmlTags + '<li><a href="#" onclick="ticketUpdate()">';
        HtmlTags = HtmlTags + '<img src="images/icons/edit.png" title="UpDate" width="32" height="32" border="none" />';
        HtmlTags = HtmlTags + '</a></li>';
        HtmlTags = HtmlTags + '</ul>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="block-content" style="height: 100px;">'; 
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '  Department :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '<select id="Slct_Dept" style="width: 170px;" class="txtbox_style" onchange="trans_dept_change();">';
        HtmlTags = HtmlTags + '   </select>';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="clearAdimProfil">';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '<div class="floatAdminProfile">';
        HtmlTags = HtmlTags + '    Employee :</div>';
        HtmlTags = HtmlTags + '<div class="withOfTextAdminProfile">';
        HtmlTags = HtmlTags + '    <select id="Slct_emp" style="width: 170px;" class="txtbox_style">';
        HtmlTags = HtmlTags + '    <option value="0">select Employee</option>';
        HtmlTags = HtmlTags + '    </select></div>';
        HtmlTags = HtmlTags + '</div>';
        HtmlTags = HtmlTags + '</div>';
    }
    catch (e) {
    }
    return HtmlTags;
}
