<!-- FILEPATH: /home/chris/git/dogstats/web/pages/dogs/dog.html -->

<div class="container">
    <div class="row">
        <div class="col-3">
            <div class="card">
                <img src="<?= $dogdata['photo']?:'https://pictshare.net/1ch3e5.png' ?>/300x200/forcesize" class="card-img-top" alt="<?= escape($dogdata['name']); ?>'s profile Picture">
                <div class="card-body">
                    <h5 class="card-title">
                        <?= escape($dogdata['name']); ?>
                    </h5>
                    <p class="card-text">
                    <ul>
                        <li>Alter:
                            <?= date_diff(date_create($dogdata['birthday']), date_create('now'))->y ?>
                        </li>

                        <?php if($dogdata['breed']): ?>
                        <li>Rasse:
                            <?= escape($dogdata['breed']); ?>
                        </li>
                        <?php endif; ?>
                        <?php if($dogdata['kennel_name']): ?>
                        <li>Zuchtname:
                            <?= escape($dogdata['kennel_name']); ?>
                        </li>
                        <?php endif; ?>
                        <?php if($dogdata['size']): ?>
                        <li>Größe:
                            <?= escape($dogdata['size']); ?> cm
                        </li>
                        <?php endif; ?>
                        <?php if($dogdata['agility_size']): ?>
                        <li>Agility Größe:
                            <?= escape($dogdata['agility_size']); ?>
                        </li>
                        <?php endif; ?>
                    </ul>

                    <?php if($ismydog===true): ?>
                    <div class="d-flex justify-content-end">
                        <button type="button" class="btn btn-secondary" hx-get="/dogs/edit/<?= $dogid; ?>" hx-target="#main">
                            <i class="fas fa-edit"></i>
                        </button>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="col-9" id="sitemain">
            <!-- table of all runs of this dog-->
            <div class="card p-2">
                <h4>Alle Ergebnisse</h4>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Turnier</th>
                                <th>Lauf</th>
                                <th>Platz</th>
                                <th>Disqualifiziert</th>
                                <th>Verweigerungen</th>
                                <th>Fehler</th>
                                <th>Zeitfehler</th>
                                <th>Zeit</th>
                                <th>Gesamtfehler</th>
                                <th>m/Sek</th>
                                <th>Bewertung</th>
                                <th>Teilnehmer</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(count($results) > 0): ?>
                                <?php foreach($results as $result) : ?>
                                <?php  
                            $t = new Tournament();
                            $run = new Run();

                            $tname = $t->getField('name',$result['tournament']);
                            $tdate = $t->getField('date',$result['tournament']);
                            $rname = $run->getField('name',$result['run']);

                            $sdata['dates'][] = $tdate;

                            $sdata['speed'][] = $result['speed'];
                            $sdata['errors'][] = $result['errors'];
                            $sdata['refusals'][] = $result['refusals'];
                            $sdata['time'][] = $result['runtime'];
                            $sdata['points'][] = $result['points'];
                            $sdata['rating'][] = $result['rating'];

                            //'Geschwindigkeit', 'Fehler', 'Verweigerungen', 'Zeit', 'Punkte','Platz'

                            ?>
                                <tr>
                                    <td><a href="/tournaments/event/<?= $result['tournament']; ?>">
                                            <?= $tname; ?>
                                        </a></td>
                                    <td><a href="/runs/overview/<?= $result['run']; ?>">
                                            <?= $rname; ?>
                                        </a></td>
                                    <td>
                                        <?= escape($result['ranking']); ?>
                                    </td>
                                    <td>
                                        <?= $result['disqualified']?'Ja':'Nein'; ?>
                                    </td>
                                    <td>
                                        <?= escape($result['refusals']); ?>
                                    </td>
                                    <td>
                                        <?= escape($result['errors']); ?>
                                    </td>
                                    <td>
                                        <?= escape($result['timefaults']); ?>
                                    </td>
                                    <td>
                                        <?= escape($result['runtime']); ?>
                                    </td>
                                    <td>
                                        <?= escape($result['penalties']); ?>
                                    </td>
                                    <td>
                                        <?= escape($result['speed']); ?>
                                    </td>
                                    <td>
                                        <?= escape($result['rating']); ?>
                                    </td>
                                    <td><a href="/dogs/overview/<?= $result['dog'] ?>">
                                            <?= escape( escape($dogdata['name']) ); ?>
                                        </a></td>
                                </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                            <tr>
                                <td colspan="12">
                                    <div class="alert alert-info" role="alert">
                                        <i class="fas fa-info-circle"></i> <?= escape($dogdata['name']); ?> hat noch keine Ergebnisse
                                    </div>
                                </td>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <?php if(count($results) > 0): ?>
            <div class="col">
                <div id="graph" data-bs-theme="light" class="card bg-light text-black" style="min-height: 400px;"></div>

                <script type="text/javascript">
                    // Initialize the echarts instance based on the prepared dom
                    var myChart = echarts.init(document.getElementById('graph'));

                    // Specify the configuration items and data for the chart
                    var option = {
                        title: {
                            text: 'Stacked Line'
                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data: ['Geschwindigkeit', 'Fehler', 'Verweigerungen', 'Zeit', 'Geschwindigkeit','Punkte','Platz']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        toolbox: {
                            feature: {
                                saveAsImage: {}
                            }
                        },
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: <?= json_encode($sdata['dates']); ?>
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: 'Geschwindigkeit',
                                type: 'line',
                                stack: 'Total',
                                data: <?= json_encode($sdata['speed']); ?>
                            },
                            {
                                name: 'Fehler',
                                type: 'line',
                                stack: 'Total',
                                data: <?= json_encode($sdata['errors']); ?>
                            },
                            {
                                name: 'Verweigerungen',
                                type: 'line',
                                stack: 'Total',
                                data: <?= json_encode($sdata['refusals']); ?>
                            },
                            {
                                name: 'Zeit',
                                type: 'line',
                                stack: 'Total',
                                data: <?= json_encode($sdata['time']); ?>
                            },
                            {
                                name: 'Punkte',
                                type: 'line',
                                stack: 'Total',
                                data: <?= json_encode($sdata['points']); ?>
                            },
                            {
                                name: 'Platz',
                                type: 'line',
                                stack: 'Total',
                                data: <?= json_encode($sdata['ranking']); ?>
                            }
                        ]
                    };

                    // Display the chart using the configuration items and data just specified.
                    myChart.setOption(option);
                </script>
            </div>
            <?php endif; ?>
        </div>
    </div>