<!-- wrapper  -->
<div class="container-fluid dashboard-content ">
    <!-- pageheader  -->
    <div class="row">
        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
            <div class="page-header">
                <h2 class="pageheader-title">{{ session.name }}</h2>
                <p class="pageheader-text">{{ session.description }} - this is session {{ session.id }}</p>
                <div class="page-breadcrumb">
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="#" class="breadcrumb-link">Sessions</a></li>
                            <li class="breadcrumb-item active" aria-current="page">{{ session.resource.name }}</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
    <!-- end pageheader  -->
    <div class="row">
        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 mb-5">
            <!-- tabs -->
            <div class="tab-regular">
                <ul class="nav nav-tabs nav-fill" id="myTab7" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" id="session-tab-summary" data-toggle="tab" href="#session-content-summary" role="tab" aria-controls="live" aria-selected="true">Live</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="profile-tab-justify" data-toggle="tab" href="#profile-justify" role="tab" aria-controls="profile" aria-selected="false">Summary</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="session-tab-costs" data-toggle="tab" href="#session-content-costs" role="tab" aria-controls="costs" aria-selected="false">Costs</a>
                    </li>
                </ul>
                <div class="tab-content" id="myTabContent7">

                    <!-- tab "Live" -->
                    <div class="tab-pane fade show active" id="session-content-summary" role="tabpanel" aria-labelledby="session-tab-summary">
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <!-- title row -->
                            <div class="row">
                                <p id="itemId">Results for the last processed item</p>
                            </div>
                            <!-- thumbnails row -->
                            <div class="row">
                                <div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-3">
                                    <div class="card card-figure">
                                        <!-- .card-figure -->
                                        <figure class="figure">
                                            <img class="img-fluid" id="img_micrograph" src="{{ url_for('static', filename='images/card-img.jpg') }}" alt="Card image cap">
                                            <!-- .figure-caption -->
                                            <figcaption class="figure-caption">
                                                <h6 class="figure-title"> Micrograph </h6>
                                                <p class="text-muted mb-0"> Give some text description </p>
                                            </figcaption>
                                            <!-- /.figure-caption -->
                                        </figure>
                                        <!-- /.card-figure -->
                                        <!-- /.card -->
                                    </div>
                                </div>
                                <div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-3">
                                    <div class="card card-figure">
                                        <!-- .card-figure -->
                                        <figure class="figure">
                                            <img class="img-fluid" id="img_shifts" src="{{ url_for('static', filename='images/card-img.jpg') }}" alt="Card image cap">
                                            <!-- .figure-caption -->
                                            <figcaption class="figure-caption">
                                                <h6 class="figure-title"> Drift plot </h6>
                                                <p class="text-muted mb-0"> Give some text description </p>
                                            </figcaption>
                                            <!-- /.figure-caption -->
                                        </figure>
                                        <!-- /.card-figure -->
                                        <!-- /.card -->
                                    </div>
                                </div>
                                <div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-3">
                                    <div class="card card-figure">
                                        <!-- .card-figure -->
                                        <figure class="figure">
                                            <img class="img-fluid" id="img_psd" src="{{ url_for('static', filename='images/card-img.jpg') }}" alt="Card image cap">
                                            <!-- .figure-caption -->
                                            <figcaption class="figure-caption">
                                                <h6 class="figure-title"> PSD plot </h6>
                                                <p class="text-muted mb-0"> Give some text description </p>
                                            </figcaption>
                                            <!-- /.figure-caption -->
                                        </figure>
                                        <!-- /.card-figure -->
                                        <!-- /.card -->
                                    </div>
                                </div>
                                <div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col-3">
                                    <div class="card card-figure">
                                        <!-- .card-figure -->
                                        <figure class="figure">
                                            <img class="img-fluid" src="{{ url_for('static', filename='images/card-img.jpg') }}" alt="Card image cap">
                                            <!-- .figure-caption -->
                                            <figcaption class="figure-caption">
                                                <h6 class="figure-title"> CTF fit plot </h6>
                                                <p class="text-muted mb-0"> Give some text description </p>
                                            </figcaption>
                                            <!-- /.figure-caption -->
                                        </figure>
                                        <!-- /.card-figure -->
                                        <!-- /.card -->
                                    </div>
                                </div>
                            </div>
                            <!-- end thumbnails row -->
                            <!-- charts row -->
                            <div class="row">
                                <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                                    <div class="card">
                                        <h5 class="card-header">Defocus</h5>
                                        <div class="card-body">
                                            <div id="c3chart_zoom"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- end charts row -->
                        </div>
                    </div>
                    <!-- end tab "Live" -->

                    <!-- tab "Summary" -->
                    <div class="tab-pane fade" id="profile-justify" role="tabpanel" aria-labelledby="profile-tab-justify">
                        <!-- parameters row -->
                        <div class="row">
                            <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12">
                                <div class="card">
                                    <h5 class="card-header">Microscope parameters</h5>
                                    <div class="card-body">
                                        <table class="table table-striped">
                                            <tbody>
                                            <tr>
                                                <td>Voltage</td>
                                                <td>{{ session.acquisition['voltage'] }} keV</td>
                                            </tr>
                                            <tr>
                                                <td>Cs</td>
                                                <td>{{ session.acquisition['cs']  }} mm</td>
                                            </tr>
                                            <tr>
                                                <td>VPP</td>
                                                {% if session.acquisition['phasePlate']  %}
                                                    <td>Yes</td>
                                                {% else %}
                                                    <td>No</td>
                                                {% endif %}
                                            </tr>
                                            <tr>
                                                <td>Detector</td>
                                                <td>{{ session.acquisition['detector'] }}</td>
                                            </tr>
                                            <tr>
                                                <td>Mode</td>
                                                <td>{{ session.acquisition['detectorMode'] }}</td>
                                            </tr>
                                            <tr>
                                                <td>Pixel size</td>
                                                <td>{{ session.acquisition['pixelSize'] }} A</td>
                                            </tr>
                                            <tr>
                                                <td>Dose per frame</td>
                                                <td>{{ session.acquisition['dosePerFrame'] }} e/A2</td>
                                            </tr>
                                            <tr>
                                                <td>Total dose</td>
                                                <td>{{ session.acquisition['totalDose']}} e/A2</td>
                                            </tr>
                                            <tr>
                                                <td>Exp. time</td>
                                                <td>{{ session.acquisition['exposureTime'] }} s</td>
                                            </tr>
                                            <tr>
                                                <td>Num. of frames</td>
                                                <td>{{ session.acquisition['numOfFrames'] }}</td>
                                            </tr>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12">
                                <div class="card">
                                    <h5 class="card-header">Session parameters</h5>
                                    <div class="card-body">
                                        <table class="table table-striped">
                                            <tbody>
                                            <tr>
                                                <td>User</td>
                                                <td>{{ session.operator.name }}</td>
                                            </tr>
                                            <tr>
                                                <td>Started</td>
                                                <td>{{ session.start|pretty_datetime }}</td>
                                            </tr>
                                            <tr>
                                                <td>Particle size</td>
                                                <td>{{ session.ptclSizeMin }}-{{ session.stats['ptclSizeMax'] }} A</td>
                                            </tr>
                                            <tr>
                                                <td>Num. of particles</td>
                                                <td>{{ session.stats['numOfPtcls'] }}</td>
                                            </tr>
                                            <tr>
                                                <td>2D classes</td>
                                                <td>{{ session.stats['numOfCls2D'] }}</td>
                                            </tr>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- end parameters row -->
                        <!-- histograms row -->
                        <div class="row">
                            <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
                                <div class="card">
                                    <h5 class="card-header">Average defocus</h5>
                                    <div class="card-body">
                                        <canvas id="chartjs_bar1"></canvas>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
                                <div class="card">
                                    <h5 class="card-header">Average CTF fit resolution</h5>
                                    <div class="card-body">
                                        <canvas id="chartjs_bar2"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- end histograms row -->

                        <!-- table row -->
                        <div class="row">
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="mb-0">Data Tables - Print, Excel, CSV, PDF Buttons</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table id="example" class="table table-striped table-bordered" style="width:100%">
                                                <thead>
                                                <tr>
                                                    <th>Id</th>
                                                    <th>Filename</th>
                                                    <th>Defocus</th>
                                                    <th>Resolution</th>
                                                    <th>CTF fit</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                {% for mic in micrographs %}
                                                <tr>
                                                    <td>{{ mic.id }}</td>
                                                    <td>{{ mic.location|basename }}</td>
                                                    <td>{{ mic.ctfDefocus }}</td>
                                                    <td>{{ mic.ctfResolution }}</td>
                                                    <td>{{ mic.ctfFit }}</td>
                                                </tr>
                                                {% endfor %}

                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- end table row -->

                    </div>
                    <!-- end tab "Summary" -->

                    <!-- tab "Costs" -->
                    <div class="tab-pane fade" id="session-content-costs" role="tabpanel" aria-labelledby="session-tab-costs">

                        <!-- table row -->
                        <div class="row">
                            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                                <div class="card">
                                    <div class="card-header">
                                        <div class="row" style="align-items: center">
                                            <h5 class="mb-0 float-left col-3" style="">Session Costs</h5>
                                            <div class="col-9" style="text-align: right;">
                                                <a href="#" class="btn btn-primary"><i class="fas fa-plus-circle"></i> Add Cost</a>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table class="table">
                                                <thead class="bg-light">
                                                    <tr class="border-0">
                                                        <th class="border-0">Date</th>
                                                        <th class="border-0">Comment</th>
                                                        <th class="border-0">Amount</th>
                                                        <th class="border-0">Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                {% for c in session.costs %}
                                                    <tr>
                                                        <td> {{ c.date|pretty_datetime }}</td>
                                                        <td> {{ c.comment }}</td>
                                                        <td> {{ c.amount }}</td>
                                                        <td>
                                                            <button class="btn btn-sm btn-outline-light" data-toggle="modal" data-target="#deleteModal">
                                                                <i class="far fa-trash-alt"></i>
                                                            </button>
                                                        </td>
                                                    </tr>
                                                {% endfor %}
                                                </tbody>
                                            </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- end table row -->

                    </div>
                    <!-- end tab "Costs" -->
                </div>
            </div>
            <!-- end tabs -->
        </div>
    </div>
</div>
<!-- end wrapper  -->
<!-- javascript  -->
<script>
    $(document).ready(function() {
    var table = $('#example').DataTable( {
        lengthChange: false,
        buttons: [ 'copy', 'excel', 'pdf', 'colvis' ]
    } );

    table.buttons().container()
        .appendTo( '#example_wrapper .col-md-6:eq(0)' );
} );
</script>
<script>
    function request_micrograph_images(sessionId, micId) {
        var requestMicThumb = $.ajax({
            url: "{{ url_for('get_mic_thumb') }}",
            type: "POST",
            data: {micId : micId, sessionId: sessionId},
            dataType: "json"
           // contentType: "img/png"
        });

        requestMicThumb.done(function(data) {
            //alert("length: " + $("#img_micrograph").length);
            $("#img_micrograph").attr('src', 'data:image/png;base64,' + data['micThumbData']);
            $("#img_psd").attr('src', 'data:image/png;base64,' + data['psdData']);
            $("#img_shifts").attr('src', 'data:image/png;base64,' + data['shiftPlotData']);
        });

        requestMicThumb.fail(function(jqXHR, textStatus) {
          alert( "Request failed: " + textStatus );
        });
    }

(function(window, document, $, undefined) {
    "use strict";
    $(function() {

        if ($('#c3chart_zoom').length) {
            var chart = c3.generate({
                bindto: "#c3chart_zoom",
                data: {
                    columns: [ {{ sample|tojson }} ],
                    colors: {sample: '#5969ff'},
                    onclick: function (d) {
                        console.log("onclick on item#", d.index);
                        request_micrograph_images({{ session.id }}, d.index);
                        document.getElementById("itemId").innerHTML = "Results for session #{{session.id}}, item #" + d.index;
                    },
                },
                zoom: {enabled: true},
                axis: {
                    y: {show: true},
                    x: {show: true}
                }
            });
        }

        if ($('#chartjs_bar1').length) {
            var ctx = document.getElementById("chartjs_bar1").getContext('2d');
            var myChart = new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: ["M", "T", "W", "R", "F", "S", "S"],
                    datasets: [{
                        label: {{ bar1['label']|tojson }},
                        data: {{ bar1['data']|tojson }},
                        backgroundColor: "rgba(89, 105, 255,0.5)",
                        borderColor: "rgba(89, 105, 255,0.7)",
                        borderWidth: 2
                    }]
                },
                options: {
                    legend: {
                        display: true,
                        position: 'bottom',
                        labels: {
                            fontColor: '#71748d',
                            fontFamily: 'Circular Std Book',
                            fontSize: 14,
                        }
                    },
                    scales: {
                        xAxes: [{
                            ticks: {
                                fontSize: 14,
                                fontFamily: 'Circular Std Book',
                                fontColor: '#71748d',
                            }
                        }],
                        yAxes: [{
                            ticks: {
                                fontSize: 14,
                                fontFamily: 'Circular Std Book',
                                fontColor: '#71748d',
                            }
                        }]
                    }
                }
            });
        }

        if ($('#chartjs_bar2').length) {
            var ctx = document.getElementById("chartjs_bar2").getContext('2d');
            var myChart = new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: ["M", "T", "W", "R", "F", "S", "S"],
                    datasets: [{
                        label: 'Cashew',
                        data: [30, 29, 5, 5, 20, 3, 10],
                        backgroundColor: "rgba(255, 64, 123,0.5)",
                        borderColor: "rgba(255, 64, 123,0.7)",
                        borderWidth: 2
                    }]
                },
                options: {
                    legend: {
                        display: true,
                        position: 'bottom',
                        labels: {
                            fontColor: '#71748d',
                            fontFamily: 'Circular Std Book',
                            fontSize: 14,
                        }
                    },
                    scales: {
                        xAxes: [{
                            ticks: {
                                fontSize: 14,
                                fontFamily: 'Circular Std Book',
                                fontColor: '#71748d',
                            }
                        }],
                        yAxes: [{
                            ticks: {
                                fontSize: 14,
                                fontFamily: 'Circular Std Book',
                                fontColor: '#71748d',
                            }
                        }]
                    }
                }
            });
        }

        request_micrograph_images({{ session.id }}, 1);

    });

})(window, document, window.jQuery);
</script>
