<%@page import="java.util.Iterator"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="com.csc.library.utilities.CheckNull"%>
<%@page import="com.csc.library.utilities.MyLog"%>
<%@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_002_PREVIEW" class="com.csc.library.system.Task" scope="page"/>
<%
    APS_NEW_002_PREVIEW.setChannel(request,response);
    APS_NEW_002_PREVIEW.checkProfile();
    HelpEntry screen = (HelpEntry) APS_NEW_002_PREVIEW.process("HelpEntry","MQUESTIONHELP");
    screen.referLangOff();
    CheckNull chk = new CheckNull();
    String lang = screen.getUProfile().get("lang");
    String questionid = chk.chkNullString(request.getParameter("questionid"));
    MyLog.debug(screen, screen.getUProfile(),"^^ Nut Freedom ^^ ----> questionid ==== " + questionid);
    screen.getInquiry().setFilter("1=2");
    screen.process();
    int tmpLineObjective = 1;
    int tmpLineSubjective = 1;
    int tmpLineSortSentence = 1;
    int tmpLineQuestionnaire = 1;
    int tmpLineQuestionnaireMulti = 1;
    int tmpLineQuestionnaireSubjective = 1;
    int tmpLineMatch = 1;
    
    if(!questionid.equals("")) {
        String[] sp = questionid.split(",");
        for (short i = 0; i < sp.length; i++) {
            try {
                DbInquiry mq = new InitialInquiry(screen.getUProfile()).getDbInquiry("mquestion");
                mq.setColumn("*");
                mq.clearOldCondition();
                mq.setFilter("questionid='" + sp[i] + "'");
                mq.refresh();
                while (mq.next()) {
                    String headQuestion = mq.getString("question");
                    String msgHeadFirst = headQuestion.substring(0, headQuestion.indexOf(">") + 1);
                    String msgHeadLast = headQuestion.substring(headQuestion.indexOf(">") + 1);
                    out.print("<br><strong>" + msgHeadFirst + (i + 1) + ". " + msgHeadLast + "</strong><br>");
                    MyLog.debug(screen, screen.getUProfile(), "^^ Nut Freedom ^^ ----> question ==== " + mq.getString("question"));
                    try {
                        DbInquiry dbi = new InitialInquiry(screen.getUProfile()).getDbInquiry("mquestion_detail");
                        dbi.setColumn("*");
                        dbi.clearOldCondition();
                        dbi.setFilter("questionid='" + mq.getString("questionid") + "'");
                        dbi.refresh();
                        // ---------- for จับคู่ ----------
                        LinkedHashMap<Integer, String> fieldQuestionDetail = new LinkedHashMap<Integer, String>();
                        LinkedHashMap<Integer, String> fieldAnswerMatchDetail = new LinkedHashMap<Integer, String>();
                        int lineMatch = 1;

                        // ---------- for เรียงประโยค ----------
                        LinkedHashMap<Integer, String> fieldSortSentence = new LinkedHashMap<Integer, String>();
                        int lineSortSentence = 1;

                        String titleType = mq.getString("title_type");
                        String queryType = mq.getString("query_type");

                        while (dbi.next()) {
                            String questionDetail = dbi.getString("question_detail");
                            String answerMatchDetail = dbi.getString("answer_match_detail");

                            if (titleType.equals("0")) { // ปรนัย
                                String msgFirst = questionDetail.substring(0, questionDetail.indexOf(">") + 1);
                                String msgLast = questionDetail.substring(questionDetail.indexOf(">") + 1);
                                out.print(msgFirst + "<input type=\"radio\" name=\"objective" + tmpLineObjective + "\">&nbsp;" + msgLast);
                            } else if (titleType.equals("1")) { // อัตนัย
                                tmpLineSubjective++;
                                out.print("<textarea id=\"subjective" + tmpLineSubjective + "\" name=\"subjective" + tmpLineSubjective + "\" cols=\"140\" rows=\"5\"></textarea>");
                            } else if (titleType.equals("2")) { // จับคู่
                                fieldQuestionDetail.put(lineMatch, questionDetail);
                                fieldAnswerMatchDetail.put(lineMatch, answerMatchDetail);
                                lineMatch++;

                            } else if (titleType.equals("3")) { // เรียงประโยค
                                fieldSortSentence.put(lineSortSentence, dbi.getString("result_sort_sentence"));
                                lineSortSentence++;

                            } else if (titleType.equals("4")) { // แบบสอบถาม
                                if (queryType.equals("0")) { // เลือกคำตอบได้ข้อเดียว
                                    String msgFirst = questionDetail.substring(0, questionDetail.indexOf(">") + 1);
                                    String msgLast = questionDetail.substring(questionDetail.indexOf(">") + 1);
                                    out.print(msgFirst + "<input type=\"radio\" name=\"questionnaire_objective" + tmpLineQuestionnaire + "\">&nbsp;" + msgLast);
                                } else if (queryType.equals("1")) { // เลือกคำตอบได้หลายข้อ
                                    String msgFirst = questionDetail.substring(0, questionDetail.indexOf(">") + 1);
                                    String msgLast = questionDetail.substring(questionDetail.indexOf(">") + 1);
                                    out.print(msgFirst + "<input type=\"checkbox\" name=\"questionnaire_objective_multi" + tmpLineQuestionnaireMulti + "\">&nbsp;" + msgLast);
                                } else if (queryType.equals("2")) { // ความคิดเห็น
                                    out.print("<textarea id=\"questionnaire_subjective" + tmpLineQuestionnaireSubjective + "\" name=\"questionnaire_subjective" + tmpLineQuestionnaireSubjective + "\" cols=\"140\" rows=\"5\"></textarea>");
                                    tmpLineQuestionnaireSubjective++;
                                }
                            }
                        }

                        if (titleType.equals("0")) { // ปรนัย
                            tmpLineObjective++;
                        }

                        if (titleType.equals("2")) { // จับคู่
                            Iterator<Integer> it = fieldQuestionDetail.keySet().iterator();
                            out.print("<table class=\"table-preview\" style=\"width: 100%;border-style: none;\">");
                            String option = "";
                            int size = fieldAnswerMatchDetail.size();
                            for (short j = 1; j <= size; j++ ) {
                                option += "<option value=\"" + j + "\">" + j + "</option>";
                            }
                            String tmpRandom = "";
                            int line = 1;
                            while (it.hasNext()) {
                                int key = it.next();
                                String value = fieldQuestionDetail.get(key);
                                int random = (int)(Math.random() * size + 1);
                                boolean statusFound = true;
                                while (statusFound) {
                                    if (tmpRandom.indexOf("," + random + ",") == -1 || tmpRandom.equals("")) {
                                        statusFound = false;
                                        tmpRandom += "," + random + ",";
                                        String msgQuestionFirst = value.substring(0, value.indexOf('>') + 1);
                                        String msgQuestionLast = value.substring(value.indexOf('>') + 1);
                                        String answerDetail = fieldAnswerMatchDetail.get(random);
                                        String msgAnswerFirst = answerDetail.substring(0, answerDetail.indexOf('>') + 1);
                                        String msgAnswerLast = answerDetail.substring(answerDetail.indexOf('>') + 1);
                                        if (!value.equals("")) {
                                            tmpLineMatch++;
                                            StringBuilder data = new StringBuilder();
                                            data.append("<tr>");
                                                data.append("<td width=\"40%\">");
                                                    data.append(msgQuestionFirst + "<select id=\"match" + tmpLineMatch + "\" name=\"match" + tmpLineMatch + "\">" + option + "</select>&nbsp;" + msgQuestionLast);
                                                data.append("</td>");
                                                data.append("<td width=\"3%\">");
                                                    data.append(msgAnswerFirst + line + ".");
                                                data.append("</td>");
                                                data.append("<td>");
                                                    data.append(msgAnswerLast);
                                                data.append("</td>");
                                            data.append("</tr>");
                                            out.print(data.toString()); 
                                        } else {
                                            StringBuilder data = new StringBuilder();
                                            data.append("<tr>");
                                                data.append("<td width=\"40%\">");
                                                    data.append("&nbsp;");
                                                data.append("</td>");
                                                data.append("<td width=\"3%\">");
                                                    data.append(msgAnswerFirst + line + ".");
                                                data.append("</td>");
                                                data.append("<td>");
                                                    data.append(msgAnswerLast);
                                                data.append("</td>");
                                            data.append("</tr>");
                                            out.print(data.toString());
                                        }
                                        line++;
                                    } else {
                                        random = (int)(Math.random() * size + 1);
                                    }
                                }
                            }
                            out.print("</table>");
                        }

                        if (titleType.equals("3")) { // เรียงประโยค
                            out.print("<div id=\"result" + tmpLineSortSentence + "\" style=\"height: 25px;border-bottom: 1px solid #000000;\"></div><br>");
                            Iterator<Integer> it = fieldSortSentence.keySet().iterator();
                            int size = fieldSortSentence.size();
                            String tmpRandom = "";
                            String question = "";
                            while (it.hasNext()) {
                                int key = it.next();
                                String value = fieldSortSentence.get(key);
                                int random = (int)(Math.random() * size + 1);
                                boolean statusFound = true;
                                while (statusFound) {
                                    if (tmpRandom.indexOf("," + random + ",") == -1 || tmpRandom.equals("")) {
                                        statusFound = false;
                                        tmpRandom += "," + random + ",";
                                        question += "<span class=\"plus\">" + fieldSortSentence.get(random) + "</span>";
                                    } else {
                                        random = (int)(Math.random() * size + 1);
                                    }
                                }
                            }
                            out.print("<div id=\"question" + tmpLineSortSentence + "\">" + question + "</div>");
                            tmpLineSortSentence++;
                        }

                        if (titleType.equals("4")) { // แบบสอบถาม
                            if (queryType.equals("0")) { // เลือกคำตอบได้ข้อเดียว
                                tmpLineQuestionnaire++;
                            } else if (queryType.equals("1")) { // เลือกคำตอบได้หลายข้อ
                                tmpLineQuestionnaireMulti++;
                            }
                        }

                    } catch (Exception e) {
                      e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

%>