<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
    <template id="result" name="Survey Result">
        <t t-call="survey.layout">
            <div class="oe_structure" id="oe_structure_survey_result_1"/>
            <div class="container js_surveyresult">
                <t t-call="survey.back" />
                <div class="jumbotron mt32">
                    <h1><span t-field="survey.title" /> <span style="font-size:1.5em;" class="fa fa-bar-chart-o float-right " role="img" aria-label="Chart" title="Chart"/></h1>
                    <div t-field="survey.description" class="oe_no_empty" />
                </div>
                <div class="card d-print-none">
                    <div class="card-header"><span class="fa fa-filter"></span>  Filters <span t-if="filter_display_data" class="float-right text-primary clear_survey_filter"><i class="fa fa-times"></i> Clear All Filters</span></div>
                    <div class="card-body">
                        <span t-if="filter_finish == True">
                            <span class="badge badge-secondary only_left_radius filter-all">All surveys</span><span class="badge badge-primary only_right_radius filter-finished">Finished surveys</span>
                        </span>
                        <span t-if="filter_finish == False">
                            <span class="badge badge-primary only_left_radius filter-all">All surveys</span><span class="badge badge-secondary only_right_radius filter-finished">Finished surveys</span>
                        </span>
                        <span t-foreach="filter_display_data" t-as="filter_data">
                            <span class="badge badge-primary only_left_radius"><i class="fa fa-filter" role="img" aria-label="Filter" title="Filter"></i></span><span class="badge badge-primary no_radius" t-esc="filter_data['question_text']"></span><span class="badge badge-success only_right_radius" t-esc="' > '.join(filter_data['labels'])"></span>
                        </span>
                    </div>
                </div>
                <div t-foreach="survey_dict['page_ids']" t-as='page_ids'>
                    <t t-set="page" t-value="page_ids['page']"/>
                    <h1 class="mt16" t-field='page.title'></h1>
                    <div t-field="page.description" class="oe_no_empty" />
                    <hr/>
                    <div t-foreach="page_ids['question_ids']" t-as='question_ids' class="mt16">
                        <t t-set="input_summary" t-value="question_ids['input_summary']"/>
                        <t t-set="question" t-value="question_ids['question']"/>
                        <t t-set="graph_data" t-value="question_ids['graph_data']"/>
                        <t t-set="prepare_result" t-value="question_ids['prepare_result']"/>
                        <h4>
                            <b>Question </b>
                            <span t-field='question.question'></span>
                            <t t-if="question.type == 'matrix'">
                                <small><span class="badge badge-secondary">Matrix: <span t-field='question.matrix_subtype'/></span></small>
                            </t>
                            <t t-if="question.type in ['simple_choice', 'multiple_choice']">
                                <small><span t-field='question.type' class="badge badge-secondary"></span></small>
                            </t>
                            <span class="float-right">
                                <span class="badge badge-success"><span t-esc="input_summary['answered']"></span> Answered</span>
                                <span class="badge badge-danger"><span t-esc="input_summary['skipped']"></span> Skipped</span>
                            </span>
                        </h4>
                        <t t-if="input_summary['answered'] != 0">
                            <t t-if="question.description">
                                <div class="text-muted oe_no_empty" t-field="question.description" />
                            </t>
                            <t t-if="question.type in ['textbox', 'free_text', 'date']">
                                <t t-call="survey.result_text"></t>
                            </t>
                            <t t-if="question.type in ['simple_choice', 'multiple_choice']">
                                <t t-call="survey.result_choice"></t>
                            </t>
                            <t t-if="question.type == 'matrix'">
                                <t t-call="survey.result_matrix"></t>
                            </t>
                            <t t-if="question.type == 'numerical_box'">
                                <t t-call="survey.result_number"></t>
                            </t>
                        </t>
                        <t t-if="input_summary['answered'] == 0">
                            <h2 style="padding-top:30px;padding-bottom:30px;text-align:center;" class="text-muted">Sorry, No one answered this question.</h2>
                        </t>
                    </div>
                </div>
            </div>
            <div class="oe_structure" id="oe_structure_survey_result_2"/>
        </t>
    </template>

    <!-- Result for free_text,textbox and date -->
    <template id="result_text" name="Text Result">
        <table class="table table-hover table-sm" t-att-id="'table_question_%d' % question.id">
            <thead>
                <tr>
                    <th>#</th>
                    <th>User Responses</th>
                </tr>
            </thead>
            <tbody>
                <t t-set="text_result" t-value="prepare_result"/>
                <tr t-foreach="text_result" t-as="user_input">
                    <td><a t-att-href="'%s/%s' % (user_input.user_input_id.print_url, user_input.user_input_id.token)"><t t-esc="user_input_index + 1"></t></a></td>
                    <t t-if="question.type == 'free_text'">
                        <td>
                            <a t-att-href="'%s/%s' % (user_input.user_input_id.print_url, user_input.user_input_id.token)" t-field="user_input.value_free_text"></a><br/>
                        </td>
                    </t>
                    <t t-if="question.type == 'textbox'">
                        <td>
                            <a t-att-href="'%s/%s' % (user_input.user_input_id.print_url, user_input.user_input_id.token)" t-field="user_input.value_text"></a><br/>
                        </td>
                    </t>
                    <t t-if="question.type == 'date'">
                        <td>
                            <a t-att-href="'%s/%s' % (user_input.user_input_id.print_url, user_input.user_input_id.token)" t-field="user_input.value_date"></a><br/>
                        </td>
                    </t>
                </tr>
            </tbody>
        </table>
        <t t-call="survey.pagination" />
    </template>

    <!-- Result for comments -->
    <template id="result_comments" name="Text Result">
        <!-- a 'comments' variable must be set an must contain a list of browse records of user input lines -->
        <table class="table table-hover table-sm" t-att-id="'table_question_%d' % question.id">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Comment</th>
                </tr>
            </thead>
            <tbody>
                <tr t-foreach="comments" t-as="user_input">
                    <td><a t-att-href="'%s/%s' % (user_input.user_input_id.print_url, user_input.user_input_id.token)"><t t-esc="user_input_index + 1"></t></a></td>
                        <td>
                            <span t-field="user_input.value_text"></span><br/>
                        </td>
                </tr>
            </tbody>
        </table>
    </template>


    <!-- Result for simple_choice and multiple_choice -->
    <template id="result_choice" name="Choice Result">
        <div>
            <!-- Tabs -->
            <ul class="nav nav-tabs d-print-none" role="tablist">
                <li class="nav-item" t-if="question.type != 'simple_choice'">
                    <a t-att-href="'#graph_question_%d' % question.id" t-att-aria-controls="'graph_question_%d' % question.id" class="nav-link active" data-toggle="tab" role="tab">
                        <i class="fa fa-bar-chart-o"></i> Graph
                    </a>
                </li>
                <li class="nav-item" t-if="question.type == 'simple_choice'">
                    <a t-att-href="'#graph_question_%d' % question.id" t-att-aria-controls="'graph_question_%d' % question.id" class="nav-link active" data-toggle="tab" role="tab">
                        <i class="fa fa-bar-chart-o"></i> Pie Chart
                    </a>
                </li>
                <li class="nav-item">
                    <a t-att-href="'#data_question_%d' % question.id" t-att-aria-controls="'data_question_%d' % question.id" class="nav-link" data-toggle="tab" role="tab">
                        <i class="fa fa-list-alt"></i> Data
                    </a>
                </li>
            </ul>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active survey_graph" t-if="question.type != 'simple_choice'" t-att-id="'graph_question_%d' % question.id" t-att-data-question_id="question.id" data-graph_type="bar" t-att-graph-data="graph_data">
                    <!-- svg element for drawing bar chart -->
                    <svg style="height:20em"></svg>
                </div>
                <div role="tabpanel" class="tab-pane active survey_graph" t-if="question.type == 'simple_choice'" t-att-id="'graph_question_%d' % question.id" t-att-data-question_id="question.id" data-graph_type="pie" t-att-graph-data="graph_data">
                    <!-- svg element for drawing pie chart -->
                    <svg style="height:20em"></svg>
                </div>
                <div role="tabpanel" class="tab-pane" t-att-id="'data_question_%d' % question.id">
                    <table class="table table-hover table-sm">
                        <thead>
                            <tr>
                                <th>Answer Choices</th>
                                <th>User Responses</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr t-foreach="prepare_result['answers']" t-as="user_input">
                                <td>
                                    <p t-esc="user_input['text']"></p>
                                </td>
                                <td class="survey_answer">
                                    <span t-esc="round(user_input['count']*100.0/(input_summary['answered'] or 1),2)"></span> %
                                    <span t-esc="user_input['count']" class="badge badge-primary">Vote</span>
                                    <i class="fa fa-filter text-primary invisible survey_filter" t-att-data-question_id="question.id" t-att-data-answer_id="user_input['answer_id']" role="img" aria-label="Filter question" title="Filter question"/>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <!-- handle comments -->
            <div>
                <t t-set="comments" t-value="prepare_result['comments']" />
                <t t-if="comments">
                    <t t-call="survey.result_comments" />
                </t>
            </div>

        </div>
    </template>

    <!-- Result for matrix -->
    <template id="result_matrix" name="Matrix Result">
        <t t-set="matrix_result" t-value="prepare_result"/>
        <!-- Tabs -->
        <ul class="nav nav-tabs d-print-none" role="tablist">
            <li class="nav-item">
                <a t-att-href="'#graph_question_%d' % question.id" t-att-aria-controls="'graph_question_%d' % question.id" class="nav-link active" data-toggle="tab" role="tab">
                    <i class="fa fa-bar-chart"></i>
                    Graph
                </a>
            </li>
            <li class="nav-item">
                <a t-att-href="'#data_question_%d' % question.id" t-att-aria-controls="'data_question_%d' % question.id" class="nav-link" data-toggle="tab" role="tab">
                    <i class="fa fa-list-alt"></i>
                    Data
                </a>
            </li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane active with-3d-shadow with-transitions survey_graph" t-att-id="'graph_question_%d' % question.id" t-att-data-question_id= "question.id" data-graph_type= "multi_bar" t-att-graph-data="graph_data">
                <!-- svg element for drawing Multibar chart -->
                <svg style="height:20em"></svg>
            </div>
            <div role="tabpanel" class="tab-pane" t-att-id="'data_question_%d' % question.id">
                <table class="table table-hover table-sm text-right">
                    <thead>
                        <tr>
                            <th></th>
                            <th class="text-right" t-foreach="matrix_result['answers']" t-as="answer_id">
                                <span t-esc="matrix_result['answers'][answer_id]"></span>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr t-foreach="matrix_result['rows']" t-as="row_id">
                            <td>
                                <span t-esc="matrix_result['rows'][row_id]"></span>
                            </td>
                            <td class="survey_answer" t-foreach="matrix_result['answers']" t-as="answer_id">
                                <span t-esc="round(matrix_result['result'][(row_id,answer_id)]*100.0/(input_summary['answered'] or 1),2)"></span> %
                                <span class="badge badge-primary" t-esc="matrix_result['result'][(row_id,answer_id)]"></span><i class="fa fa-filter text-primary invisible survey_filter" t-att-data-question_id="question.id" t-att-data-row_id="row_id" t-att-data-answer_id="answer_id" role="img" aria-label="Survey filter" title="Survey filter"></i>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <!-- handle comments to matrix -->
            <div>
                <t t-set="comments" t-value="matrix_result['comments']" />
                <t t-if="comments">
                    <t t-call="survey.result_comments" />
                </t>
            </div>
        </div>
    </template>

    <!-- Result for Numeric Data -->
    <template id="result_number" name="Number Result">
        <t t-set="number_result" t-value="prepare_result"/>
        <t t-set="text_result" t-value="number_result['input_lines']" />
        <span class="float-right mt8">
            <span class="badge badge-secondary only_left_radius">Sum </span> <span class="badge badge-info only_right_radius" t-esc="number_result['sum']"></span>
            <span class="badge badge-secondary only_left_radius">Maximum </span> <span class="badge badge-success only_right_radius" t-esc="number_result['max']"></span>
            <span class="badge badge-secondary only_left_radius">Minimum </span> <span class="badge badge-danger only_right_radius" t-esc="number_result['min']"></span>
            <span class="badge badge-secondary only_left_radius">Average </span> <span class="badge badge-warning only_right_radius" t-esc="number_result['average']"></span>
        </span>
        <ul class="nav nav-tabs d-print-none" role="tablist">
            <li class="nav-item">
                <a t-att-href="'#most_common_%d' % question.id" t-att-aria-controls="'most_common_%d' % question.id" class="nav-link active" data-toggle="tab" role="tab">
                    <i class="fa fa-list-ol"></i>
                    Most Common <span t-esc="len(number_result['most_common'])"></span>
                </a>
            </li>
            <li class="nav-item">
                <a t-att-href="'#data_question_%d' % question.id" t-att-aria-controls="'data_question_%d' % question.id" class="nav-link" data-toggle="tab" role="tab">
                    <i class="fa fa-list-alt"></i>
                    All Data
                </a>
            </li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane active with-3d-shadow with-transitions" t-att-id="'most_common_%d' % question.id">
                <table class="table table-hover table-sm">
                     <thead>
                         <tr>
                             <th>User Responses</th>
                             <th>Occurence</th>
                         </tr>
                     </thead>
                     <tbody>
                         <tr t-foreach="number_result['most_common']" t-as="row">
                             <td>
                                 <span t-esc="row[0]"></span>
                             </td>
                             <td>
                                 <span t-esc="row[1]"></span>
                             </td>
                         </tr>
                     </tbody>
                </table>
            </div>
            <div role="tabpanel" class="tab-pane" t-att-id="'data_question_%d' % question.id">
                <table class="table table-hover table-sm" t-att-id="'table_question_%d' % question.id">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>User Responses</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="d-none" t-foreach="number_result['input_lines']" t-as="user_input">
                            <td><a t-att-href="'%s/%s' % (user_input.user_input_id.print_url, user_input.user_input_id.token)"><t t-esc="user_input_index + 1"></t></a></td>
                            <td><span t-field="user_input.value_number"></span><br/></td>
                        </tr>
                    </tbody>
                </table>
               <t t-call="survey.pagination"/>
            </div>
        </div>
    </template>
    <template id="no_result" name="Survey Result">
        <t t-call="survey.layout">
            <div class="container">
                <t t-call="survey.back" />
                <h2><span t-field='survey.title'></span></h2>
                <div class="jumbotron mt32">
                    <h2 class="text-muted">
                        <span>
                            <i style="font-size:1.8em" class="fa fa-users float-right" role="img" aria-label="No answer" title="No answer"></i>
                        </span>
                        Sorry, No one answered this survey yet
                    </h2>
                </div>
            </div>
        </t>
    </template>
    <!-- Pagination Element -->
    <template id="pagination" name="Survey Result">
        <t t-set="record_limit" t-value="10"/><!-- Change This record_limit to change number of record  per page-->
        <ul t-att-id="'pagination_%d' % question.id" class="pagination" t-att-data-question_id="question.id" t-att-data-record_limit="record_limit">
            <t t-if="len(text_result) > record_limit">
                <li t-foreach="page_range(len(text_result), record_limit)" t-as="num" class="page-item">
                    <a href="#" class="page-link" t-esc="num"></a>
                </li>
            </t>
        </ul>
    </template>
</data>
</odoo>
