﻿templates["AllSurveyItemBySurvey"] = [
        '<table summary="List survey item" class="table">',
            '<colgroup span="4">',
            '<col width="auto" />',
            '<col width="auto" />',
            '<col width="auto" />',
            '<col width="auto" />',
		'</colgroup>',
			'<tr class="heading">',
				'<th scope="col"><nobr>Tên ca sĩ</nobr></th>',
				'<th scope="col"><nobr>Số phiếu</nobr></th>',
    		'</tr>',
            '<tpl for=".">',
                '<tr>',
                    '<td valign="top">{ItemName}</td>',
                    '<td valign="top" class="txtc">{NumberOfVote}</td>',
                '</tr>',
            '</tpl>',
         '</table>'
         ];
templates["Top3Singer"] = [
'<table width="100%" height="100%" cellspacing="0" cellpadding="0" border="0" valgin="top" style="padding: 10px 0px 10px 8px; background-color: rgb(238, 238, 221);">',
    '<colgroup>',
    '<col width = "60%" />',
    '<col width = "40%" />',
    '</colgroup>',
    '<tr valign="top">',
			'<td id="tdSurveyHomePageTitle" colspan="2" style ="text-align:center">',
			'</td>',
	'</tr>',
	'<tr valign="top">',
			'<td style="text-align:center">',
			    '<b>Tên ca sĩ</b>',
			'</td>',
			'<td style="text-align:center">',
			    '<b>Số phiếu</b>',
			'</td>',
	'</tr>',
	'<tpl for=".">',
	    '<tr valign="top">',
	       '<td style="text-align:center">{ItemName}</td>',
	       '<td style="text-align:center">{NumberOfVote}</td>',
	    '</tr>',
	'</tpl>',
    '<tr>',
	    '<td colspan = "3" style ="text-align:center">',
	    '<hr style="width: 100%; margin-bottom: 3px; border: dotted 0.5 gray;"/>',
	    '<input type="button" id="btnShowPopupSurvey" class="button" value="Bầu chọn" onclick="ShowPopupSurvey();"/></td>',
    '</tr>',

'</table>'
];
templates["SurveyForm"] = [
'<table width="100%" height="100%" cellspacing="0" cellpadding="0" border="0" valgin="top" style="padding: 10px 0px 10px 8px; background-color: rgb(238, 238, 221);">',
    '<colgroup>',
    '<col width = "10%"/>',
    '<col width = "90%" />',
    '</colgroup>',
    '<tr valign="top">',
			'<td id="tdSurveyTitle" colspan="2" style ="text-align:center">',
			'</td>',
	'</tr>',
	'<tpl for=".">',
	    '<tr valign="top">',
	        '<td><input type="checkbox" id="cb{Stt}" value={Id} onclick="SelectId(\'{Id}\');"/></td>',
	        '<td>{ItemName}</td>',
	    '</tr>',
	'</tpl>',
	    '<tr>',
		    '<td colspan = "2" style ="text-align:center">',
		    '<hr style="width: 100%; margin-bottom: 3px; border: dotted 0.5 gray;"/>',
		    '<input type="button" class="button" value="Chọn" onclick="VoteForSurvey();"/>   <input type="button" class="button" value="Xem kết quả" onclick="ViewSurveyResult();"/></td>',
	    '</tr>',

'</table>'
];

templates["SurveyResult"] = [
            '<table style="margin: 5px;">',
                    '<tr>',
                        '<td id="tdTitleResult">',
                        '</td>',
                    '</tr>',
            '</table>',
            '<table border="1" align="center" cellpadding="3" cellspacing="1" width="100%">',
                '<tpl for=".">',
                    '<tr height="22">',
                        '<td bgcolor="#ffffff" class="txtc">{ItemName}</td>',
                        '<td bgcolor="#ffffff" width="300px">',
                            '<table align="left" width="300px" border="0" cellpadding="0" cellspacing="0" height="20">',
                                '<tbody>',
                                    '<tr>',
                                        '<td style="background-color: {Color}; width: {RoundedPercent}px; display: {IsDisplay};">',
                                            '&nbsp;</td>',
                                        '<td class="txtc">',
                                            '&nbsp;{VotePercent}%</td>',
                                    '</tr>',
                                '</tbody>',
                            '</table>',
                        '</td>',
                        '<td class="txtc" align="right" bgcolor="#ffffff">',
                            '{NumberOfVote}&nbsp;phiếu</td>',
                    '</tr>',
                '</tpl>',
                '<tr height="25">',
                    '<td id="tdTotal" colspan="3" class="txtc" align="right" bgcolor="#ffffff">',
                        '</td>',
                '</tr>',
            '</table>'
         ];
var SurveyItem = function()
{
    this.Id = null;
    this.ItemName = null;
    this.NumberOfVote = null;
}

var SurveyItemController = function(host, ns, tns)
{
    if (typeof host == "string"){host = getControlById(host);}    
    inherit(new AbstractController(host, ns, tns), this);
    this.provider = new SurveyItemProvider();
    this.state = "";
    this.pageIndex = 1;    
//    System.Environment.HistoryKeeper.register(ns, this.state, "onStateChange", this);
//    var currentState = System.Environment.HistoryKeeper.getBookmarkedState(ns);
}

SurveyItemController.prototype.onStateChange = function()
{

}

var SurveyItemProvider = function()
{
    this.GetAllItemSurveyURL = "./surveyitem/GetAllItemSurvey.html";
    this.GetAllItemSurveySortedIdURL = "./surveyitem/GetAllItemSurveySortedId.html";
    this.GetTop3Items = "./surveyitem/GetTop3Items.html";
    this.GetAllSinger = "./surveyitem/GetAllSinger.html";
    this.GetSurveyResult = "./surveyitem/GetSurveyResult.html";
    this.Vote = "./surveyitem/Vote.html";
    this.GetSurveyInfoURL = "./surveyitem/GetSurveyInfo.html";
}

//Get one
SurveyItemController.prototype.GetItemBySurveyForEdit = function(surveyId, callBack)
{
    var fnc = function(_this, context)
    {
        var response = context.responseText;
        if(response != null || response != "null")
        {
            var list = JSON.decode(response);
            callBack(list);
        }
        else
            alert("Fail");
        
     }
    
    this.provider.GetBySurveySortedId(surveyId, fnc, this);
}

SurveyItemController.prototype.GetItemBySurvey = function(surveyId)
{
    var fnc = function(_this, context)
    {
        var response = context.responseText;
        if(response != null || response != "null")
        {
            var tpl = new TemplateHelper().getTemplate("AllSurveyItemBySurvey");
            var list = JSON.decode(response);
            for(var i = 0 ; i < list.length; i++)
                list[i].ItemName = htmlEncode(list[i].ItemName);
            tpl.overwrite('div_all_item_survey', list);
        }
        else
            alert("Fail");
        
     }
    
    this.provider.GetBySurvey(surveyId, fnc, this);
}

SurveyItemProvider.prototype.GetBySurvey = function(surveyId, callBack, scope)
{
    var params = {};
    params.SurveyId = surveyId;
    
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.GetAllItemSurveyURL, params, this.onSuccess, this.onFailure, context);
}

SurveyItemProvider.prototype.GetBySurveySortedId = function(surveyId, callBack, scope)
{
    var params = {};
    params.SurveyId = surveyId;
    
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.GetAllItemSurveySortedIdURL, params, this.onSuccess, this.onFailure, context);
}


//Get top 3
SurveyItemController.prototype.GetTop3 = function(callBack)
{
     var fnc = function(_this, context)
     {
        var response = context.responseText;
        if(response != null && response != "null")
        {
            var tpl = new TemplateHelper().getTemplate("Top3Singer");
            var list = JSON.decode(response);
            var i=0;
            while (list[i] != null)
            {
                list[i].Stt = i+1;
                list[i].ItemName = htmlEncode(list[i].ItemName);
                
                i++;
            }
            tpl.overwrite('div_survey', list);
            if (document.getElementById("popup_survey") && document.getElementById("popup_survey").style.display == "block")
            {
                document.getElementById("btnShowPopupSurvey").disabled = true;
            }
            callBack(false);
        }
        else
        {
            document.getElementById("div_survey_homepage").style.display = "none";
        }
     }
    this.provider.GetTop3(fnc, this);
}

SurveyItemProvider.prototype.GetTop3 = function(callBack, scope)
{
    var params = {};
    
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.GetTop3Items, params, this.onSuccess, this.onFailure, context);
}

//Get all singer
SurveyItemController.prototype.AllSinger = function(callBack)
{
     var fnc = function(_this, context)
     {
        var response = context.responseText;
        if(response != "null")
        {
            var tpl = new TemplateHelper().getTemplate("SurveyForm");
            var list = JSON.decode(response);
            var i=0;
            while (list[i] != null)
            {
                list[i].Stt = i;
                list[i].ItemName = htmlEncode(list[i].ItemName);
                i++;
            }
            tpl.overwrite('survey_form', list);
            callBack(true);
        }
        else
        {
            document.getElementById("div_survey_homepage").style.display = "none";
        }
     }
    this.provider.AllSinger(fnc, this);
}

SurveyItemProvider.prototype.AllSinger = function(callBack, scope)
{
    var params = {};
    
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.GetAllSinger, params, this.onSuccess, this.onFailure, context);
}

//Get result
SurveyItemController.prototype.GetResult = function(callBack)
{
     var fnc = function(_this, context)
     {
        var response = context.responseText;
        
        if(response != null || response != "null")
        {
            var tpl = new TemplateHelper().getTemplate("SurveyResult");
            var list = JSON.decode(response);
            
            var entity;
            var i = 0;
            var total = 0;
            var title;
            while (list[i] != null)
            {
                entity = list[i];
                entity.ItemName = htmlEncode(entity.ItemName);
                entity.Color = GetColor(i);
                entity.RoundedPercent = Math.round(entity.VotePercent) * 3;
                if (entity.RoundedPercent == 0)
                    entity.IsDisplay = "none";
                else entity.IsDisplay = "block";
                total += entity.NumberOfVote*1;
                title = entity.SurveyTitle;
                list[i] = entity;
                i++; 
            }
            
            tpl.overwrite('div_survey_content', list);
            document.getElementById("tdTotal").innerHTML = "Tổng cộng: " +total+" phiếu";
            document.getElementById("tdTitleResult").innerHTML = "<b>" +title+"</b>";
        }
        else
        {
        }
        callBack();
     }
    this.provider.GetResult(fnc, this);
}

SurveyItemProvider.prototype.GetResult = function(callBack, scope)
{
    var params = {};
    
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.GetSurveyResult, params, this.onSuccess, this.onFailure, context);
}

// Vote
SurveyItemController.prototype.Vote = function(surveyIds, callBack)
{
    var fnc = function(_this, context)
    {
        var response = context.responseText;
        if(response != null || response != "null")
        {
            
        }
        else
            alert("Fail");
        callBack();
     }
    
    this.provider.VoteSinger(surveyIds, fnc, this);
}

SurveyItemProvider.prototype.VoteSinger = function(surveyIds, callBack, scope)
{
    var params = {};
    params.SurveyIds = surveyIds;
    
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.Vote, params, this.onSuccess, this.onFailure, context);
}

//Get survey info
SurveyItemController.prototype.GetSurveyTitle = function(isPopUp)
{
    var fnc = function(_this, context)
    {
        var response = context.responseText;
        if(response != null && response != "null")
        {
            var entity = JSON.decode(response);
            if(isPopUp)
            {
                document.getElementById("tdSurveyTitle").innerHTML = "<b>" + htmlEncode(entity.Title)+ "</b>";
            }
            {
                document.getElementById("tdSurveyHomePageTitle").innerHTML = "<b>" + htmlEncode(entity.Title) + "</b>";
            }
        }
        else
            alert("Fail");
        
     }
    
    this.provider.GetSurveyInfo(fnc, this);
}

SurveyItemProvider.prototype.GetSurveyInfo = function(callBack, scope)
{
    var params = {};
   
    var context = {};
    context.callBack = callBack;
    context.scope = scope;

    System.Environment.RequestManager.sendRequest(this.GetSurveyInfoURL, params, this.onSuccess, this.onFailure, context);
}

SurveyItemProvider.prototype.onSuccess = function(response, opts)
{
    var context = opts.context;
	context.responseText = response.responseText;
    if (context.callBack)
    {
        context.callBack(context.scope, context);
    }
}

SurveyItemProvider.prototype.onFailure = function(response, opts)
{
    var context = opts.context;
    if (context.callBack)
    {
        context.callBack(context.scope, context);
    }
}