<table id="opv" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td width="100%">
            <table class="bordered" width="100%">
                <tr>
                    <th class="row-header" colspan="4">OPERATIVE FLIGHT PLAN</th>
                    <th>SELECT AWG SPEED: <?php echo $dataFormMassBalance['ac_model']["speed"]; ?></th>
                </tr>
                <tr>
                    <th colspan="2">DEPARTURE BASE AND ENROUTE DATA</th>
                    <th>PRUA</th>
                    <th>FREQ. RADIO AIDS</th>
                    <th>INS. W.P. NAME OR ICAO CODE</th>
                </tr>
                <?php foreach($legs as $index => $leg): ?>
                <tr>
                    <th rowspan="2"><?php echo ($index==0)?"DEPT":($index)."&deg; LEG"; ?></th>
                    <td><?php echo $leg->city; ?></td>
                    <td><?php echo ($index==0)?"Degrees":($leg->degNM->trk)."&deg;"; ?></td>
                    <td rowspan="2"><?php echo isset($leg->freq)?$leg->freq:""; ?></td>
                    <td rowspan="2"><?php echo isset($leg->icao)?$leg->icao:""; ?></td>
                </tr>
                <tr>
                    <td class="gps_cell"><b>N</b> <?php echo ($leg->latdeg)."&deg;"; ?> <?php echo ($leg->latmin)."'"; ?> <?php echo ($leg->latsec)."''"; ?> <b>E</b> <?php echo ($leg->londeg)."&deg;"; ?> <?php echo ($leg->lonmin)."'"; ?> <?php echo ($leg->lonsec)."''"; ?> </td>
                    <td><?php echo ($index==0)?"NM":($leg->degNM->dist); ?></td>
                </tr>
                <?php endforeach; ?>
            </table>
        </td>
    </tr>
    <tr>
        <td width="100%">
            <table class="bordered" width="100%">
                <tbody>
                <tr>
                    <th class="centered">INFO WAYPOINTS AND ENROUTE NOTES</th>
                </tr>
                <tr>
                    <td><?php echo $dataFormMassBalance['ac_model']["info_waypoints"]; ?></td>
                </tr>
                </tbody>
            </table>
        </td>
    </tr>
    <tr>
        <td width="100%">
            <table class="bordered">
                <tbody>
                    <tr>
                        <th rowspan="2" class="centered">AVERAGE CONS kg/min</th>
                        <th colspan="6" class="centered">ESTIMATE ENDURANCE (MINUTES OF FLIGHT)</th>
                        <th colspan="2" class="centered">FLIGHT TIME min</th>
                    </tr>
                    <tr>
                        <th class="centered" colspan="2">INITIAL FUEL LOAD KG</th>
                        <th class="centered" colspan="2">ENDURANCE h min.</th>
                        <th class="centered">Res VFR</th>
                        <th class="centered">Res VFRN</th>
                        <th class="centered">MAX</th>
                        <th class="centered">REST</th>
                    </tr>
                    <tr>
                        <td id="avg_cons_end"><?php echo round($dataFormMassBalance['ac_model']["avg_cons"],3); ?></td>
                        <td colspan="2" id="init-fuel"><?php echo $totals->d->kg; ?></td>
                        <td id="endurance-hours"><?php echo $totals_legs->endhh; ?></td>
                        <td id="endurance-mins"><?php echo $totals_legs->endmm; ?></td>
                        <td id="res-vfr"><?php echo round($totals_legs->resvfr); ?></td>
                        <td id="res-vfrn"><?php echo round($totals_legs->resvfrn); ?></td>
                        <td id="qty-max" class="<?php if($totals_legs->qtymax<=21): ?> danger <?php elseif($totals_legs->qtymax<30): ?> warning <?php elseif($totals_legs->qtymax>=30): ?> success <?php endif; ?>"><?php echo round($totals_legs->qtymax); ?></td>
                        <td id="qty-rest" class="<?php if($totals_legs->qtyrest<=21): ?> danger <?php elseif($totals_legs->qtyrest<30): ?> warning <?php elseif($totals_legs->qtyrest>=30): ?> success <?php endif; ?>"><?php echo round($totals_legs->qtyrest); ?></td>
                    </tr>
                    <tr>
                        <td>LEG</td>
                        <td>(min)</td>
                        <td>(Kg)</td>
                        <td>(Kg)</td>
                        <td>(Kg)</td>
                        <td>(Kg)</td>
                        <td>(min)</td>
                        <td id="time-max" class="<?php if(isset($totals_legs->timemax)): ?> <?php if($totals_legs->timemax<=21): ?> danger <?php elseif($totals_legs->timemax<30): ?> warning <?php elseif($totals_legs->timemax>=30): ?> success <?php endif; ?> <?php endif; ?>"><?php if(isset($totals_legs->timemax)): ?> <?php echo round($totals_legs->timemax); ?> <?php endif; ?></td>
                        <td id="time-rest" class="<?php if(isset($totals_legs->timerest)): ?> <?php if($totals_legs->timerest<=21): ?> danger <?php elseif($totals_legs->timerest<30): ?> warning <?php elseif($totals_legs->timerest>=30): ?> success <?php endif; ?> <?php endif; ?>"><?php if(isset($totals_legs->timerest)): ?> <?php echo round($totals_legs->timerest); ?> <?php endif; ?></td>
                    </tr>
                    <tr>
                        <td>N°</td>
                        <td>E.E.T.</td>
                        <td>Estimated necessary fuel</td>
                        <td>Estimated residual fuel</td>
                        <td>Real residual fuel</td>
                        <td>Real fuel consumption</td>
                        <td>E.T.E.</td>
                        <td>Start up time</td>
                        <td>Stop time</td>
                    </tr>
                    <?php foreach($legs as $index => $leg): ?>
                    <?php if($index>0): ?>
                    <tr>
                        <td><?php echo $index; ?></td>
                        <td><?php echo $leg->eet; ?></td>
                        <td><?php echo round($leg->enf); ?></td>
                        <td><?php echo round($leg->erf); ?></td>
                        <td width="14%"><?php echo ($leg->rrf==0)?"":$leg->rrf; ?></td>
                        <td><?php echo ($leg->rfc==0)?"":$leg->rfc; ?></td>
                        <td><?php echo ($leg->ete==0)?"":floor($leg->ete/60).":".floor($leg->ete%60); ?></td>
                        <td width="14%"><?php echo ($totals_legs->tot_ete==0)?"":$leg->start; ?></td>
                        <td width="14%"><?php echo ($totals_legs->tot_ete==0)?"":$leg->stop; ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <th>TOT</th>
                        <th id="tot-eet"><?php echo $totals_legs->tot_eet; ?></th>
                        <th id="tot-enf"><?php echo round($totals_legs->tot_enf); ?></th>
                        <th id="tot-erf"><?php echo round($totals_legs->tot_erf); ?></th>
                        <th id="tot-rrf"><?php echo ($totals_legs->tot_rrf==0)?"":$totals_legs->tot_rrf; ?></th>
                        <th id="tot-rfc"><?php echo ($totals_legs->tot_rfc==0)?"":$totals_legs->tot_rfc; ?></th>
                        <th id="tot-ete"><?php echo ($totals_legs->tot_ete==0)?"":floor($totals_legs->tot_ete/60).":".floor($totals_legs->tot_ete%60); ?></th>
                        <th colspan="2">TOTAL FLIGHT TIME</th>
                    </tr>
                </tfoot>
            </table>
        </td>
    </tr>
    <tr>
        <td width="100%">
            <table class="bordered" width="100%">
                <tbody>
                    <tr>
                        <th width="20%">MASS KG</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <?php if($index==1): ?>
                        <th>DEPT</th>
                        <?php else: ?>
                        <th><?php echo $index-1; ?>&deg; LEG</th>
                        <?php endif; ?>

                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td width="14%" class="warning">Warnings</td>
                    </tr>
                    <tr>
                        <th>ZERO FUEL LOAD</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <td><?php echo $leg->fuel; ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td class="danger">Exceeded limits</td>
                    </tr>
                    <tr>
                        <th>RESERVE+CONT. F</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <td><?php echo $leg->reserve; ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td class="success">Limits ok</td>
                    </tr>
                    <tr>
                        <?php if(count($legs)>1): ?>
                        <th colspan="2">RE-FUEL</th>
                        <?php else: ?>
                        <th>RE-FUEL</th>
                        <?php endif; ?>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>1): ?>
                        <td><?php echo $leg->refuel; ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td>FUEL</td>
                    </tr>
                    <?php if($dataFormMassBalance['ac_model']["flight_type"]=="EMS"): ?>
                    <tr>
                        <th>LOAD (patient)</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <td><?php echo $leg->lpatient; ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td>Fill in flight</td>
                    </tr>
                    <?php endif; ?>
                    <tr>
                        <th>RESIDUAL PAY LOAD</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <td><?php echo round($leg->residual); ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td>Residual P.L. x <?php echo $dataFormMassBalance['ac_model']["mass_kg"]; ?> Kg</td>
                    </tr>
                    <tr>
                        <th>TAKE OFF MASS</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <td class="<?php if($leg->residual<0): ?> danger <?php elseif($leg->residual<25): ?> warning <?php elseif($leg->residual>0): ?> success <?php endif; ?>"><?php echo round($leg->takeoffmass); ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                        <td rowspan="2">TO/LDG Mass OK (<?php echo $dataFormMassBalance['ac_model']["mass_kg"]; ?> Kg) check FM for Cat. A TO/LDGS</td>
                    </tr>
                    <tr>
                        <th>LANDING MASS</th>
                        <?php foreach($legs as $index => $leg): ?>
                        <?php if($index>0): ?>
                        <td class="<?php if($leg->landingmass>$dataFormMassBalance['ac_model']['mass_kg']): ?> danger <?php elseif($leg->landingmass>=$dataFormMassBalance['ac_model']['mass_kg']-25): ?> warning <?php else: ?> success <?php endif; ?>"><?php echo round($leg->landingmass); ?></td>
                        <?php endif; ?>
                        <?php endforeach; ?>
                    </tr>
                </tbody>
            </table>
        </td>
    </tr>
</table>
