<!DOCTYPE html>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.csc.library.session.InitialInquiry"%>
<%@page import="com.csc.library.session.DbInquiry"%>
<%@page import="com.csc.library.entry.HelpEntry"%>
<%@page contentType="text/html; charset=UTF-8"%>
<jsp:useBean id="APS_NEW_003_ADD" class="com.csc.library.system.Task" scope="page"/>
<%
    APS_NEW_003_ADD.setChannel(request,response);
    APS_NEW_003_ADD.checkProfile();
    HelpEntry screen = (HelpEntry) APS_NEW_003_ADD.process("HelpEntry","MSUB_GROUPHELP");
    screen.referLangOff();
    String lang = screen.getUProfile().get("lang");
    String titleType = request.getParameter("title_type");
    screen.getInquiry().setFilter("");
    screen.process();

%>
<html lang="en">
    <head>
        <meta charset="window-874">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title></title>

        <link href="../CSS/TISCO.css" rel="stylesheet" type="text/css">

        <!-- Bootstrap -->
        <link href="../JS/QueryBuilder/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet">

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="../JS/html5shiv.min.js"></script>
          <script src="../JS/respond.min.js"></script>
        <![endif]-->
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="../JS/jquery-1.11.3.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="../JS/bootstrap.min.js"></script>
        <script src='../JS/SCREEN.js'></script>
        <script src="../JS/SWAPLANG_FREEDOM.js"></script>
        <script>setLangSwapLang("<%=lang%>");</script>

        <style>
            img[height="20"] {
                padding: 0px;
            }

            .table > tbody > tr > td {
                padding: 4px;
                border: 1px solid #CBC5C5;
                vertical-align: middle;
            }
            
            .valign-center {
                vertical-align: middle;
            }
            p {
                margin: 0px;
            }
            
            .detailHeader {
                text-align: center;
                background-color: #5992CB;
                color: #FFFFFF;
            }
            
            td.header {
                color: #ffffff;
                font-weight: bold;
                background-color: #336699;
                text-align: right;
                height: 22px;
                border: none;
            }
            
            .btn-sm,
            .btn-group-sm > .btn {
                padding: 3px 5px;
                font-size: 12px;
                line-height: 1.5;
                border-radius: 3px;
            }
        </style>

    </head>
    <body>
        <div class="container">
            <div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
            <script>initToolTips();</script>
            <form role="form" id="cscform" name="cscform" method="post" action="APS_NEW_003_ADD.jsp">
                <!----------BEGIN CSC INITIAL ZONE ------------------------>
                <%=screen.InitialVariable()%>
                <!----------END CSC INITIAL ZONE -------------------------->
                <input type="hidden"name="__screen"  value="APS_NEW_003_ADD">
                <input type="hidden" name="__help" value="">
                <input type="hidden" name="__fixCon" value="">
                <input type="hidden" name="__cmd"  value="">
                <input type="hidden" name="__language" value='<%=lang%>'>
                <input type="hidden" name="__companyid" value="<%=screen.getUProfile().get("companyid")%>">
                <input type="hidden" name="__user" value="<%=screen.getUProfile().getUserId()%>">
                <input type="hidden" name="__referPage" value="">
                <input type="hidden" id="titleType" name="titleType" value="<%=titleType%>">

                <div class="container">
                    <table id="tb" class="table">
                        <tbody>
                            <tr>
                                <td colspan="4" class="header"><script>swLang('SELECT');swLang('SUB_GROUPID');</script></td>
                            </tr>
                            <tr>
                                <td class="detailHeader"><script>swLang('SELECT');</script></td>
                                <td class="detailHeader"><script>swLang('QUESTIONID');</script></td>
                                <td class="detailHeader"><script>swLang('DETAIL');</script></td>
                                <td class="detailHeader"><script>swLang('SCORE');</script></td>
                            </tr>
                            
                            <%
                                if(screen.getInquiry().recCount() > 0) {
                                    while(screen.nextRec()) {
                                    %>
                                    <tr>
                                        <td class="text-center"><p><input type="checkbox" name="select"></p></td>
                                        <td class="text-center"><p><%=screen.getString("sub_groupid")%></p></td>
                                        <td class="text-left">
                                            <strong><%=screen.getString("sub_group_name")%></strong>&nbsp;<br>
                                            <%
                                                try {
                                                    DbInquiry sgd = new InitialInquiry(screen.getUProfile()).getDbInquiry("msub_group_detail");
                                                    sgd.setColumn("*");
                                                    sgd.clearOldCondition();
                                                    sgd.setFilter("sub_groupid='" + screen.getString("sub_groupid") + "'");
                                                    DbInquiry mq = new InitialInquiry(screen.getUProfile()).getDbInquiry("mquestion");
                                                    mq.setColumn("question");
                                                    sgd.putChild(mq);
                                                    sgd.refresh();
                                                    out.print("<ol>");
                                                    while (sgd.next()) {
                                                        out.print("<li>" + sgd.getChild("mquestion").getString("question") + "</li>");
                                                    }
                                                    out.print("</ol>");
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            %>
                                        </td>
                                        <td class="text-center">
                                            <p><%=screen.getString("total_score")%></p>
                                        </td>
                                    </tr>
                                    <%	  
                                    }
                                }
                            %>
                            <tr>
                                <td colspan="4" class="text-center" style="height: 50px;">
                                    <button type="button" id="selectAll" class="btn btn-primary btn-sm">
                                        <span class="glyphicon glyphicon-asterisk"></span> Select All
                                    </button>
                                    <button type="button" id="deselectAll" class="btn btn-primary btn-sm">
                                        <span class="glyphicon glyphicon-ban-circle"></span> Deselect All
                                    </button>
                                    <button type="button" id="add" class="btn btn-primary btn-sm">
                                        <span class="glyphicon glyphicon-plus"></span> Add
                                    </button>
                                    <button type="button" id="close" class="btn btn-primary btn-sm">
                                        <span class="glyphicon glyphicon-remove"></span> Close
                                    </button>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" style="background-color: #336699;height: 12px;"></td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <!----------BEGIN CSC ENDING ZONE ------------------------>
                <%=screen.endJSP()%>
                <!----------END CSC ENDING ZONE -------------------------->
            </form>
        </div>
        <script>
            
            $(function() { // Start jquery
                $('#tb > tbody > tr').find('.showHtml').each(function() {
                    $(this).html($(this).text());
                });
                
                $(window.opener.$('.questionid')).each(function() {
                   var $questionid = $(this).text();
                   $('#tb > tbody > tr').find('td:eq(1)').each(function() {
                      if ($(this).text() === $questionid) {
                          $(this).closest('tr').remove();
                      } 
                   });
                });
                
                $('#selectAll').click(function() {
                   $(':checkbox').prop('checked',true); 
                });
                
                $('#deselectAll').click(function() {
                    $(':checkbox').prop('checked',false);
                });
                
                $('#add').click(function() {
                    if ($(':checkbox').is(':checked')) {
                        $("input[name='select']:checked").each(function() {
                            var $tr = $(this).closest('tr');
                            var $questionid = $tr.find('td:eq(1)').text();
                            var $question_detail = $tr.find('td:eq(2)').html();
                            var $score = $tr.find('td:eq(3)').text();
                            window.opener.$('#tb').append(
                                $('<tr/>').append(
                                    $('<td class="text-center"/>').append(
                                        $('<input/>', {
                                            type: 'checkbox',
                                            name: 'select'
                                        })
                                    ),
                                    $('<td class="text-center questionid"/>').append(
                                        $questionid +
                                        '<input type="hidden" id="questionid_' + $questionid + '" name="questionid_' + $questionid + '" value="' + $questionid + '">'
                                    ),
                                    $('<td class="text-left"/>').append(
                                        $question_detail +
                                        '<textarea style="display: none;" id="question_detail_' + $questionid + '" name="question_detail_' + $questionid + '">' + $question_detail + '</textarea>'
                                    ),
                                    $('<td class="text-center"/>').append(
                                        $score +
                                        '<input type="hidden" id="score_' + $questionid + '" name="score_' + $questionid + '" value="' + $score + '">'
                                    )
                                )
                            ); 
                        });
                        window.opener.setTotalScore();
                        window.close();
                    } else {
                       alert(getSwapLang('PLEASE_SELECT_QUESTIONID'));
                    }
                    
                });
                
                $('#close').click(function() {
                   window.close(); 
                }); 
                
            }); // End jquery
        </script>
    </body>
</html>