<?php
    // Get this user's associated officials associations
    $oaus = Auth::user()->getLinkedLoas();

    // Over-ride association for OSAA staff (allows to pick a LOA to view)
    $info['view_loa_id'] = null;                        
    $info['list_loas'] = array();
    if (Auth::user()->isOsaaUser())
    {
        $info['list_loas'] = officialsassociation::where('is_retired', '=', 0)
                                                 ->orderBy(DB::raw('FIELD (sport, \'Football\', \'Volleyball\', \'Soccer\', \'Basketball\', \'Wrestling\', \'Baseball\', \'Softball\')', 'desc'))
                                                 ->orderBy('name')
                                                 ->get();        

        if (Input::has('view-association'))
        {
            $info['view_loa_id'] = intval(Input::get('view-association'));
            $temp_loa = officialsassociation::whereIn('id', array($info['view_loa_id']))->first();
            $oaus = array($temp_loa->id => array($temp_loa->name, array('Cmsh.' => 'Commissioner (OVERRIDE)')));
        }
    }
    
    // Get a list of all officials associations linked with this user
    $oas = array_keys($oaus);

    // Setup counters
    $edit_loa_ejections = array();
    $view_loa_ejections = array();
    $unsubmitted_ejections = 0;
    $forms = array();

    $edit_loa_records = array();
    $view_loa_records = array();
    $records = array();
    $unverified = 0;
    $total_reimbursement = 0.00;
    
    $assigned_teams = 0;    
    $total_teams = 0;
    $all_school_objects = array();
    
    $list_of_activities = array();  

    // Ensure there are LOAs to work from
    if (count($oas) > 0)
    {
        // Get a list of sports this user is linked to LOAs with
        $loa_sports = officialsassociation::whereIn('id', $oas)
                                          ->lists('sport');



        /*
         * Ejection Reports
         */

        // Get a list of associations this user can access ejection reports: edit/view        
        foreach ($oaus as $loa_id => $item)
        {
            foreach ($item[1] as $role_slug => $role_name)
            {
                // Commissioners can edit
                if (Helpers::strEqual($role_slug, 'Cmsh.'))
                {
                    $edit_loa_ejections[] = $loa_id;
                }

                // Other board members can view
                if (Helpers::strEqual($role_slug, array('Cmsh.', 'Assoc. Pres.', 'Assoc. Board', 'Assoc. Treas.')))
                {
                    $view_loa_ejections[] = $loa_id;
                }
            }
        }

        // Get any ejection reports
        $forms = ejection::whereIn('officials_association', $oas)
                         ->whereNotIn('ejection_status', array('WTDN'))
                         ->where('school_year', '=', Session::get('year'))                                         
                         ->get();
        
        foreach ($forms as $form)
        {
            if (Helpers::strEqual($form->ejection_status, array('WORK', 'CMSH')))
            {
                $unsubmitted_ejections++;
            }
        }

        $forms->sort(function($a, $b)
        {
            $data_a = json_decode($a->data);
            $data_b = json_decode($b->data);

            if (is_null($data_a) or is_null($data_b) or
                !is_object($data_a) or !is_object($data_b) or
                !property_exists($data_a, 'contest') or !property_exists($data_b, 'contest') or
                !property_exists($data_a->contest, 'date') or !property_exists($data_b->contest, 'date'))
            {
                return 0;
            }

            $a_date = strtotime($data_a->contest->date);
            $b_date = strtotime($data_b->contest->date);

            if ($a_date > $b_date)
            {
                return -1;
            }
            elseif ($a_date < $b_date)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        });


        /*
         * Reimbursement Reports
         */

        // Get a list of associations this user can access reimbursement reports: edit/view        
        foreach ($oaus as $loa_id => $item)
        {
            foreach ($item[1] as $role_slug => $role_name)
            {
                // Commissioners and Treasurers can edit
                if (Helpers::strEqual($role_slug, array('Cmsh.', 'Assoc. Treas.')))
                {
                    $edit_loa_records[] = $loa_id;
                }

                // Other board members can view
                if (Helpers::strEqual($role_slug, array('Cmsh.', 'Assoc. Pres.', 'Assoc. Board', 'Assoc. Treas.')))
                {
                    $view_loa_records[] = $loa_id;
                }
            }
        }

        // Get any officials state championship reimbursement records
        $records = officialschampionship::where('school_year', '=', Session::get('year'))
                                        ->whereIn('officials_association', $oas)
                                        ->get();                        

        if (count($records) > 0)
        {
            foreach ($records as $record)
            {
                if (is_null($record->cmsh_verified_at))
                {
                    $unverified++;
                }

                $record->calculate();

                $record->matchup = matchup::findOrFail($record->matchup);
                $record->officials_fee = officialsfee::findOrFail($record->officials_fee);
                $record->round = round::findOrFail($record->matchup->round);
                $record->round_type = roundtype::where('slug', '=', $record->round->round_type)->firstOrFail();
                $record->bracket = bracket::findOrFail($record->round->bracket);
                $record->contest = contest::findOrFail($record->matchup->contest);
                $record->event = event::findOrFail($record->contest->event);

                $total_reimbursement += $record->total_amount;

                $list_of_activities[$record->contest->activity] = 1;
            }

            $records = $records->sort(function($a, $b)
            {
                $a1 = strtotime($a->event->start_at);
                $b1 = strtotime($b->event->start_at);

                if ($a1 < $b1)
                {
                    return -1;
                }
                elseif ($a1 > $b1)
                {
                    return 1;
                }
                
                $a2 = $a->contest->id;
                $b2 = $b->contest->id;

                if ($a2 < $b2)
                {
                    return -1;
                }
                elseif ($a2 > $b2)
                {
                    return 1;
                }

                $a3 = $a->officials_fee->id;
                $b3 = $b->officials_fee->id;

                if ($a3 < $b3)
                {
                    return -1;
                }
                elseif ($a3 > $b3)
                {
                    return 1;
                }
                
                return 0;
            });
        }


        // Get a list of assigned schools                
        foreach ($oas as $oa_id)
        {
            $oa = officialsassociation::findOrFail($oa_id);
            $school_objects = $oa->getAssignedSchoolObjects();

            // Add the list of school objects if any exist
            if ($school_objects !== false and !is_null($school_objects) and count($school_objects) > 0)
            {
                $all_school_objects = array_merge($all_school_objects, $school_objects);
            }            
        }

        // Count the number of assigned teams
        foreach ($all_school_objects as $school_object)
        {
            $assigned_teams += count($school_object->activity_programs);

            foreach ($school_object->activity_programs as $ap)
            {
                $total_teams += team::where('activity_program', '=', $ap->id)->count();
            }
        }

        // Sort the list of school objects by school name
        uasort($all_school_objects, function($a, $b)
        {
            $a1 = $a->school->short_name;
            $b1 = $b->school->short_name;

            $cmp = strcasecmp($a1, $b1);

            if ($a1 < $b1)
            {
                return -1;
            }
            elseif ($a1 > $b1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        });    
    }
?>

<?php if ($unverified > 0): ?>
    <div class="ui-corner-bottom" style="border: 1px solid #e1c146; position: absolute; width: 250px; font-size: 9pt; line-height: 1.15em; background-color: #ffefb0; height: 2.25em; text-align: center; top: 0; left: 430px; padding: 3px 0 2px 0;">
        You have unsubmitted playoff<br />reimbursement records.
    </div>
<?php endif; ?>

<a href="<?php echo url('/forms/ejection'); ?>" class="new_ejection_button" style="float: right; font-size: 9pt;">New Ejection Report</a>

<h2>OSAA Commissioner Dashboard</h2>                

<?php if (Auth::user()->isOsaaUser()): ?>
    <div style="float: right; position: absolute; top: 60px; left: 225px;">
        <form method="GET" action="<?php echo url('/account#commissioner'); ?>">
            <select name="view-association" id="view-association">
                <?php if (is_null($info['view_loa_id'])): ?><option value=""></option><?php endif; ?>
                <?php
                    $current_sport = null;
                ?>
                <?php foreach ($info['list_loas'] as $loa): ?>                                
                    <?php
                        if (is_null($current_sport))
                        {
                            $current_sport = $loa->sport;
                            echo '<optgroup label="' . $current_sport . '">';
                        }                        

                        if (!Helpers::strEqual($current_sport, $loa->sport))
                        {
                            $current_sport = $loa->sport;
                            echo '</optgroup><optgroup label="' . $current_sport . '">';                        
                        }
                    ?>
                    <option
                        value="<?php echo $loa->id; ?>"
                        <?php if ($loa->id == $info['view_loa_id']): ?>
                            selected="selected"
                        <?php endif; ?>
                    >
                        <?php echo $loa->name; ?>
                    </option>
                <?php endforeach; ?>
                </optgroup>
            </select>
        </form>
    </div>
<?php endif; ?>

<h3 style="color: #990000;">Officials Association</h3>
<div style="font-size: 9pt;">
    <?php if (count($oaus) < 1): ?>
        Your account is not associated to any local officials association.<br />
    <?php else: ?>
        Your account is currently linked to the following associations:<br />
        <ul>
            <?php foreach ($oaus as $item): ?>
                <li><b><?php echo $item[0]; ?></b> - <?php echo Helpers::arrayToList($item[1]); ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>

<div class="vertical_tabs my_school">
    <ul style="padding-left: 5px; font-size: 9pt; /* height: 502px;*/ height: 800px; list-style-type: none !important;">
        <li><a href="#cmsh_association" data-sub-tab-index="0">Association</a></li>
        <li><a href="#cmsh_ejections" data-sub-tab-index="1">Ejection Reports</a></li>
        <li><a href="#cmsh_reimbursements" data-sub-tab-index="2">Reimbursements</a></li>
        <li><a href="#cmsh_schools" data-sub-tab-index="3">Schools</a></li>        
    </ul>

    <!-- Association Details -->
    <div id="cmsh_association">

        <h2>My Local Officials Association Details</h2>

        <?php if (count($oas) > 0): ?>

            <?php foreach ($oas as $oa_id): ?>
                <?php
                    // Get the LOA
                    $oa = officialsassociation::findOrFail($oa_id);
                    

                    // Get the rules clinic file name
                    $rules_clinic_file_name = Helpers::getCurrentYear() . '-' . substr((intval(Helpers::getCurrentYear()) + 1), 2, 2) . ' ' . $oa->sport . ' Rules Clinic Materials.zip';

                    if (file_exists(public_path() . '/docs/officials/' . $rules_clinic_file_name))
                    {
                        echo '<!-- OK [ ' . public_path() . '/docs/officials/' . $rules_clinic_file_name . ' ] -->';
                        echo '<div style="float: right; position: absolute; top: 0.5em; right: 1em;"><a href="' . asset('/docs/officials/' . $rules_clinic_file_name) . '">Rules Clinic Packet</a></div>';
                    }
                    else
                    {
                        echo '<!-- Rules Clinic Files Does Not Exist [ ' . public_path() . '/docs/officials/' . $rules_clinic_file_name . ' ] -->';
                    }

                    // Get the LOA's mailing address
                    $address = null;
                    if (!is_null($oa->mailing_address))
                    {
                        $address = address::findOrFail($oa->mailing_address);
                    }

                    // Get all LOA user accounts
                    $oa_users = officialsassociationsuser::where('officials_association', '=', $oa->id)
                                                         ->where('is_retired', '=', 0)
                                                         ->get();                    
                    if (count($oa_users) > 0)
                    {
                        foreach ($oa_users as $oa_user)
                        {
                            $oa_user->user = user::findOrFail($oa_user->user);
                            $oa_user->role = role::where('slug', '=', $oa_user->role)->firstOrFail();                              
                        }
                    }

                    // Get the LOA's Commissioner
                    $oa_commissioner = $oa->getCommissioner();
                    if (!is_null($oa->oregon_region))
                    {
                        $oa->oregon_region = oregonregion::where('slug', '=', $oa->oregon_region)->firstOrFail();
                    }

                    // Decode the LOA's Executive Board
                    $oa_board = array();
                    if (!is_null($oa->board))
                    {
                        $oa_board = json_decode($oa->board);
                        if (!is_array($oa_board))
                        {
                            $oa_board = array();
                        }
                    }
                ?>

                <div class="label small gray">Full Name</div><br />
                <?php echo $oa->name; ?>

                <br /><br />

                <div class="columns">
                    <div class="half">                
                        <div class="label small gray">Short Name</div><br />
                        <?php echo $oa->short_name; ?>
                    </div>
                    <div class="fourth">
                        <div class="label small gray">Abbreviation</div><br />
                        <?php if (!Helpers::strIsEmpty($oa->abbreviation)): ?>
                            <?php echo $oa->abbreviation; ?>
                        <?php else: ?>
                            <i class="gray">- -</i>
                        <?php endif; ?>                
                    </div>
                    <div class="sixth">
                        <div class="label small gray">OSAA ID #</div><br />
                        <?php echo $oa->id; ?>
                    </div>
                </div>
                
                <br class="clear" /><br />
                
                <div class="label small gray">Website</div><br />
                <?php if (Helpers::strIsEmpty($oa->website)): ?>
                    <i class="gray">- -</i> <?php /*  - <a href="#" data-action="add_loa_website">Add URL</a>  */ ?>
                <?php else: ?>
                    <a href="<?php echo $oa->website; ?>" target="_blank"><?php echo $oa->website; ?></a>
                <?php endif; ?>
                
                <br /><br />

                <div class="columns">
                    <div class="half">
                        <div class="label small gray">Region</div><br />
                        <?php if (!is_null($oa->oregon_region)): ?>
                            <?php echo $oa->oregon_region->slug; ?> - <?php echo $oa->oregon_region->name; ?>
                        <?php else: ?>
                            <i class="gray">- -</i>
                        <?php endif; ?>
                    </div>
                    <div class="third">
                        <div class="label small gray">Sport</div><br />
                        <?php echo $oa->sport; ?>
                    </div>
                </div>

                <br class="clear" /><br />

                <div class="columns">
                    <div class="half">
                        <div class="label small gray">Mailing Address</div><br />
                        <?php if (is_null($address)): ?>
                            <i class="gray">- -</i> <?php /*  - <a href="#" data-action="add_loa_address">Add Address</a>  */ ?>
                        <?php else: ?>
                            <?php echo $address->line_1; ?><br />
                            <?php if (!Helpers::strIsEmpty($address->line_2)): ?>
                                <?php echo $address->line_2; ?><br />
                            <?php endif; ?>
                            <?php echo $address->city; ?>, <?php echo $address->state; ?> <?php echo $address->zip; ?>
                        <?php endif; ?>
                    </div>

                    <div class="half">
                        <div class="label small gray">AP/AR Address</div><br />
                        <?php if (is_null($address) or false): ?>
                            <i class="gray">- -</i> <?php /*  - <a href="#" data-action="add_loa_address">Add Address</a>  */ ?>
                        <?php elseif (true): ?>
                            <i class="">Same as mailing address</i>
                        <?php else: ?>
                            <?php echo $address->line_1; ?><br />
                            <?php if (!Helpers::strIsEmpty($address->line_2)): ?>
                                <?php echo $address->line_2; ?><br />
                            <?php endif; ?>
                            <?php echo $address->city; ?>, <?php echo $address->state; ?> <?php echo $address->zip; ?>
                        <?php endif; ?>
                    </div>
                </div>

                <br class="clear" /><br />

                <h3>Commissioner of Officials</h3>
                <div class="columns">
                    <div class="third">
                        <div class="label gray small">Name</div><br />
                        <?php if(!is_null($oa_commissioner)): ?>
                            <?php echo $oa_commissioner->name; ?>
                        <?php else: ?>
                            <i class="gray">- -</i>
                        <?php endif; ?>
                    </div>

                    <div class="third" style="width: 45%;">
                        <div class="label gray small">Email</div><br />
                        <?php if(!is_null($oa_commissioner)): ?>
                            <?php echo $oa_commissioner->email; ?>
                        <?php else: ?>
                            <i class="gray">- -</i>
                        <?php endif; ?>
                    </div>

                    <div class="sixth">
                        <div class="label gray small">Phone</div><br />
                        <?php if(!is_null($oa_commissioner) and $oa_commissioner->phone): ?>
                            <?php echo Helpers::displayCombinedPhone($oa_commissioner->phone->number); ?>
                        <?php else: ?>
                            <i class="gray">- -</i>
                        <?php endif; ?>
                    </div>

                </div>


                <br class="clear" /><br />

                <?php /*  <h3>Local Executive Board</h3>

                <table class="bia_table" style="font-size: 9pt;">
                    <thead>
                        <tr>
                            <th>Title</th>                            
                            <th>Name</th>
                            <th>E-Mail</th>                            
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (count($oa_board) > 0): ?>
                            <?php foreach ($oa_board as $index => $board_member): ?>                                
                                <tr <?php if ($index++ % 2 == 1): ?> class="odd" <?php endif; ?>>
                                    <td><?php echo $board_member->title; ?></td>                                                                                                    
                                    <td><?php echo $board_member->name; ?></td>
                                    <td><?php echo $board_member->email; ?></td>                                    
                                    <td style="text-align: right;">
                                        <a href="#"
                                           class="remove_loa_board_member"                                            
                                           data-loa-id="<?php echo $oa->id; ?>"                                            
                                           data-href-return="<?php echo url('/account#commissioner'); ?>"
                                           data-member-index="<?php echo $index; ?>"
                                           data-member-name="<?php echo $board_member->name; ?>"
                                           data-member-title="<?php echo $board_member->title; ?>"><img src="<?php echo asset('images/icons/missing_16px.png'); ?>" alt="" title="" data-tooltip="Remove This Board Member" style="vertical-align: middle; margin-right: 8px;" /></a>
                                        <a href="#"
                                           class="order_loa_board_member"                                            
                                           data-loa-id="<?php echo $oa->id; ?>"                                            
                                           data-member-index="<?php echo $index; ?>"
                                           data-member-name="<?php echo $board_member->name; ?>"
                                           data-member-title="<?php echo $board_member->title; ?>"><img src="<?php echo asset('images/icons/vertical_grabber_dark_16px.png'); ?>" alt="" title="" data-tooltip="Drag to Change Order" style="vertical-align: middle;" /></a>
                                    </td>                                                            
                                </tr>                                
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="4">There are no Executive Board members to display.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>

                <br class="clear" />  */ ?>

                <h3>Dashboard Access</h3>
                <p class="small">
                    The following list shows each OSAA Website user account that has access to your association's OSAA Commissioner Dashboard.  To grant access to others, share your association's
                    <?php if (Helpers::strEqual($oa->getUserAccess(), array('FULL', 'MANAGE'))): ?>
                        <a href="#" data-action="show_loa_access_code" data-access-code="<?php echo $oa->access_code; ?>" data-loa-id="<?php echo $oa->id; ?>" data-loa-name="<?php echo $oa->short_name; ?>" style="font-weight: bold;">Access Code</a>.
                    <?php else: ?>
                        <b>Access Code</b> (only viewable to users with association editing privileges).
                    <?php endif; ?>
                    Remove user access by clicking the delete icon, <img src="<?php echo asset('/images/icons/missing_16px.png'); ?>" alt="" title="" style="vertical-align: middle; height: 1em;" />, in that user's row.
                </p>

                <table class="bia_table" style="font-size: 9pt;">
                    <thead>
                        <tr>
                            <th>User Name</th>
                            <th>E-Mail / Login</th>
                            <th>Role</th>                                                
                            <th>Access</th>
                            <th></th>                                            
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (count($oa_users) > 0): ?>
                            <?php foreach ($oa_users as $index => $oa_user): ?>                                
                                <tr <?php if ($index++ % 2 == 1): ?> class="odd" <?php endif; ?>>
                                    <td><?php echo $oa_user->user->last_name; ?>, <?php echo $oa_user->user->first_name; ?></td>
                                    <td><?php echo $oa_user->user->email; ?></td>
                                    <td>
                                        <?php echo $oa_user->role->name; ?>
                                    </td>                                                            
                                    <td>
                                        <?php if (Helpers::strEqual($oa_user->role->slug, array('Cmsh.'))): ?>
                                            FULL
                                        <?php elseif (Helpers::strEqual($oa_user->role->slug, array('Assoc. Treas.'))): ?>
                                            FINANCE
                                        <?php elseif (Helpers::strEqual($oa_user->role->slug, array('Assoc. Pres.'))): ?>
                                            MANAGE
                                        <?php else: ?>
                                            NONE
                                        <?php endif; ?>
                                    </td>
                                    <td style="text-align: right;">
                                        <?php /*  Commissioners cannot be removed  */ ?>
                                        <?php if (Helpers::strEqual($oa_user->role->slug, 'Cmsh.')): ?>
                                            <a href="#" class="disabled"><img src="<?php echo asset('images/icons/missing_gray_16px.png'); ?>" alt="" title="" data-tooltip="You Cannot Remove the Commissioner" style="vertical-align: middle; margin-right: 2px;" /></a>
                                        <?php else: ?>                                            
                                            <?php /*  Users can remove themselves; for others, user must have FULL or MANAGE permissions  */ ?>
                                            <?php if (Auth::user()->id == $oa_user->user->id or Helpers::strEqual($oa->getUserAccess(), array('FULL', 'MANAGE'))): ?>
                                                <a href="#"
                                                   class="remove_loa_user"
                                                   data-record-id="<?php echo $oa_user->id; ?>"
                                                   data-loa-id="<?php echo $oa->id; ?>"
                                                   data-loa-name="<?php echo $oa->name; ?>"
                                                   data-user-id="<?php echo $oa_user->user->id; ?>"
                                                   data-href-return="<?php echo url('/account#commissioner'); ?>"
                                                   data-user-name="<?php echo $oa_user->user->first_name; ?> <?php echo $oa_user->user->last_name; ?>"
                                                   data-role-name="<?php echo $oa_user->role->name; ?>"><img src="<?php echo asset('images/icons/missing_16px.png'); ?>" alt="" title="" data-tooltip="Remove This User" /></a>
                                            <?php else: ?>
                                                <a href="#" class="disabled"><img src="<?php echo asset('images/icons/missing_gray_16px.png'); ?>" alt="" title="" data-tooltip="You Cannot Remove this User" style="vertical-align: middle; margin-right: 2px;" /></a>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </td>                                                            
                                </tr>                                
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="5">There are no association users to display.  This should not have happened.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table> 

                <div class="small" style="float: right; margin-top: 0.5em;"><a href="#" data-action="action-legend">User Access Legend</a></div>

            <?php endforeach; ?>

        <?php else: ?>

            Your account is not linked to any local officials association.

        <?php endif; ?>
    </div>


    <!-- Ejection Reports -->    
    <div id="cmsh_ejections">
        <h2>Ejection Reports</h2>
        <h3 style="color: #990000;">
            <?php echo count($forms); ?>
            <?php if (count($forms) == 1): ?>
                Report
            <?php else: ?>
                Reports
            <?php endif; ?>
            <?php if ($unsubmitted_ejections > 0): ?> w/ <?php echo $unsubmitted_ejections; ?>-Not Submitted<?php endif; ?></h3>

         <div style="font-size: 9pt; margin-top: 1em;">
            <div class="columns">
                <div class="half" style="width: 60%;">
                    <span class="ui-icon ui-icon-info float_left" style="vertical-align: middle;"></span><b>Instructions</b><br />
                    The table below shows any ejection reports linked to your officials association.  Click the report ID number to view/edit an ejection report.
                </div>

                <div class="third" style="width: 35%;">                    
                    <br />
                    &raquo; <a href="<?php echo asset('/docs/commissioners/Ejection Information for Commissioners.pdf'); ?>" target="_blank">Ejection Information for Commissioners</a>
                    <br />
                    &raquo; <a href="<?php echo asset('/docs/commissioners/Online Ejection Report Instructions.pdf'); ?>" target="_blank">Online Ejection Report Instructions</a>
                </div>
            </div>
        </div>

        <br class="clear" /><br />

        <?php if (count($forms) > 0): ?>

            <div style="max-height: 500px; overflow-x: hidden; overflow-y: auto;">
            <table class="bia_table">

                <thead>
                    <tr>
                        <th>ID #</th>
                        <th>Act.</th>
                        <th>Status</th>                                
                        <th>School</th>
                        <th>Contest Date</th>
                        <th>Updated At</th>  
                        <th>Outcome</th>                              
                    </tr>
                </thead>

                <tbody>

                    <?php foreach ($forms as $form): ?>

                        <?php
                            $object = $form->getObject();
                        ?>
                        
                        <tr <?php if (Helpers::strEqual($form->ejection_status, array('WORK', 'CMSH'))): ?> class="unsubmitted" <?php endif; ?> data-color-rows="1">
                            <td>
                                <?php /*  Edit Link  */ ?>
                                <?php if (in_array($form->officials_association, $edit_loa_ejections)): ?>                                
                                    <a href="<?php echo url('/forms/ejection/' . $form->id); ?>" target="_blank" style="font-weight: bold;"><?php echo $form->id; ?></a>
                                <?php /*  View Link  */ ?>
                                <?php elseif (in_array($form->officials_association, $view_loa_ejections)): ?>                                
                                    <a href="<?php echo url('/forms/ejection/' . $form->id . '?view=' . $form->access_code); ?>" target="_blank" style="font-weight: bold;"><?php echo $form->id; ?></a>
                                <?php endif; ?>
                            </td>
                            <td><?php echo !is_null($form->activity) ? $form->activity : '- '; ?></td>
                            <td><?php echo $form->ejection_status; ?></td>                                    
                            <td><?php echo (!is_null($object->host_school)) ? $object->host_school->short_name : '- -'; ?></td>
                            <td><?php echo (!is_null($object->data->contest->date)) ? date('D n/j/y', strtotime($object->data->contest->date)) : '- -'; ?></td>                                    
                            <td><?php echo date('D n/j/y g:ia', strtotime($form->updated_at)); ?></td>                                    

                            <td>
                                <?php
                                    if (Helpers::strEqual($form->ejection_status, array('WORK')))
                                    {
                                        $outcome = 'Not Submitted';
                                    }
                                    elseif (Helpers::strEqual($form->ejection_status, array('CMSH')))
                                    {
                                        $outcome = 'Not Submitted';
                                    }
                                    elseif (Helpers::strEqual($form->ejection_status, array('SUBT')))
                                    {
                                        $outcome = 'Under School Review';
                                    }
                                    elseif (Helpers::strEqual($form->ejection_status, array('RECD')))
                                    {
                                        $outcome = 'Accepted';
                                    }
                                    elseif (Helpers::strEqual($form->ejection_status, array('APPL')))
                                    {
                                        $outcome = 'Appealed - Pending';
                                    }
                                    elseif (Helpers::strEqual($form->ejection_status, array('DONE')))
                                    {
                                        $ejection_data = json_decode($form->data);
                                        
                                        if (is_null($ejection_data->receipt->is_granted))
                                        {
                                            $outcome = 'Accepted';
                                        }
                                        elseif ($ejection_data->receipt->is_granted)
                                        {
                                            $outcome = 'Appeal - Granted';
                                        }
                                        else
                                        {
                                            $outcome = 'Appeal - Denied';
                                        }
                                    }
                                    else
                                    {
                                        $outcome = 'Unknown';
                                    }
                                ?>
                                <?php echo $outcome; ?>
                            </td>
                        </tr>

                    <?php endforeach; ?>

                </tbody>

            </table>
            </div>                                       

        <?php else: ?>

            You have no ejection reports in the database for <?php echo Session::get('year'); ?>-<?php echo substr(intval(Session::get('year')) + 1, 2, 2); ?>.

        <?php endif; ?>
       
    </div>

    
    <!-- Playoff Reimbursements -->
    <div id="cmsh_reimbursements">
        <h2>OSAA Playoff Reimbursements</h2>
        <h3 style="position: relative;">
            <?php echo count($records); ?> Records <?php if ($unverified > 0): ?> w/ <?php echo $unverified; ?>-Not Submitted<?php endif; ?>
            <div style="float: right; position: absolute; right: 0; top: 0;">Total: $<?php echo number_format($total_reimbursement, 2); ?></div>
        </h3>

        <div style="font-size: 9pt; margin-top: 1em;">
            <div class="columns">
                <div class="half" style="width: 60%;">
                    <span class="ui-icon ui-icon-info float_left" style="vertical-align: middle;"></span><b>Instructions</b><br />
                    The table below shows OSAA Playoff reimbursement entries for your officials association.  You can edit and resubmit an entry as many times as necessary until the submission deadline.
                </div>

                <div class="third" style="width: 35%;">
                    <br />                    
                    &raquo; <a href="<?php echo asset('/docs/commissioners/Online Playoff Reimbursement Instructions.pdf'); ?>" target="_blank">Online Playoff Reimbursement Instructions</a>
                    <br />
                    <?php if (count($oaus) < 1): ?>
                        <span class="gray">Summary Report Unavailable</span>
                    <?php else: ?>
                        Summary Reports:                        
                        <?php
                            $assoc_count = 0;
                        ?>
                        <?php foreach ($oaus as $oa_id => $item): ?>
                            <a href="<?php echo url('/officials/associations/' . $oa_id . '/reimbursement-report'); ?>" target="_blank"><?php echo $item[0]; ?></a>
                            
                            <?php if ($assoc_count++ < (count($oaus) - 1)): ?>
                                |
                            <?php endif; ?>
                        <?php endforeach; ?> 
                    <?php endif; ?>
                </div>
            </div>
            <br class="clear" />
        </div>

        <?php if (count($oas) > 0): ?>
            <div style="font-size: 8pt; margin-top: 1em; line-height: 1.15em; margin-bottom: 1em;">

                <?php foreach ($loa_sports as $sport): ?>


                    <?php if (Helpers::strEqual($sport, 'Football')): ?>

                        The OSAA will reimburse football mileage at the rate of 75&#162; per mile up to 60 miles round trip and 50&#162; per mile for 61 or more miles round trip.  A minimum of $5 per official is allowed.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Each reimbursement record includes a $1.50 assigning fee.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is December 8.</b>

                    <?php elseif (Helpers::strEqual($sport, 'Volleyball')): ?>

                        The OSAA will reimburse volleyball mileage at the rate of 50&#162; per mile with a minimum of $5 per official.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Line judge assignments for the same contest and officials association are included with that assignment's primary reimbursement record which lists the higher officials game fee rate.  Round trip miles for the initial arrival at the final tournament site and for the return home are included in one assignment record, nominally a semi-final contest assignment.  Similarly, meal claims for the tournament at the final-site will be input by OSAA staff into one assignment to reimburse meal expenses, if any, for the entire tournament at the final-site.  Each reimbursement record includes a $1.50 assigning fee, except for assignments at the final-site.  In which case one, single assigning fee of $1.50 will be added to one assignment during the tournament.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is November 10.</b>

                    <?php elseif (Helpers::strEqual($sport, 'Soccer')): ?>

                        The OSAA will reimburse soccer mileage at the rate of 50&#162; per mile with a minimum of $5 per official.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Fourth official assignments for the same contest and officials association are included with that assignment's primary reimbursement record which lists the higher officials game fee rate.  Each reimbursement record includes a $1.50 assigning fee.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is November 17.</b>

                    <?php elseif (Helpers::strEqual($sport, 'Basketball')): ?>

                        The OSAA will reimburse basketball mileage at the rate of 50&#162; per mile with a minimum of $5 per official.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Round trip miles for the initial arrival at the final tournament site and for the return home are included in one assignment record, nominally the last assignment for finals.  Similarly, meal claims for the tournament at the final-site will be input by OSAA staff into one assignment to reimburse meal expenses, if any, for the entire tournament at the final-site.  Each reimbursement record includes a $1.50 assigning fee, except for assignments at the final-site.  In which case one, single assigning fee of $1.50 will be added to one assignment during the tournament.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is March 15</b>.

                    <?php elseif (Helpers::strEqual($sport, 'Wrestling')): ?>

                        <?php /*  The OSAA will reimburse mileage at the rate of 50&#162; per mile with a minimum of $5 per official.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Each reimbursement record includes a $1.50 assigning fee.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is February 23.</b>  */ ?>

                        The OSAA will reimburse wrestling tournament officials through local associations per defined fee schedule rates.

                    <?php elseif (Helpers::strEqual($sport, 'Baseball')): ?>

                        The OSAA will reimburse baseball mileage at the rate of 50&#162; per mile with a minimum of $5 per official.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Each reimbursement record includes a $1.50 assigning fee.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is June 8.</b>

                    <?php elseif (Helpers::strEqual($sport, 'Softball')): ?>

                        The OSAA will reimburse softball mileage at the rate of 50&#162; per mile with a minimum of $5 per official.  The OSAA assumes the minimum mileage for each assignment unless otherwise entered by the commissioner.  Each reimbursement record includes a $1.50 assigning fee.  Where meal claims are allowed, receipts are not required and the rates are as follows: breakfast - $8, lunch - $10, dinner - $15.  When the OSAA determines that lodging is necessary, complimentary rooms will be provided based on two officials per room.  If an official wishes not to share a room, there will be no reimbursement for lodging expenses.  In these cases, the official is responsible for their own reservations and are not eligible for the OSAA discounted rates.  <b>The deadline for submitting expenses is June 8.</b>

                    <?php endif; ?>
                <?php endforeach; ?>                
                
            </div>
        <?php endif; ?>
        

        <?php if (count($records) == 0): ?>
            <br />

            You have no OSAA Playoff reimbursement entries in the database for <?php echo Session::get('year'); ?>-<?php echo substr(intval(Session::get('year')) + 1, 2, 2); ?>.
        <?php else: ?>
            

            <div style="/*max-height: 500px;*/ max-height: 715px; overflow-x: hidden; overflow-y: auto;">

            <table class="bia_table" style="font-size: 8.5pt;">
                <thead>
                    <tr>
                        <th>Submitted?</th>
                        <th>ACT</th>
                        <th>Round</th>
                        <th>Date</th>                                
                        <th>Contest</th>
                        <th>Rate</th>
                        <th>Miles</th>                                
                        <th>Total</th>
                        <th>Action</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($records as $record): ?>
                        <tr data-record-id="<?php echo $record->id; ?>" data-matchup-id="<?php echo $record->matchup->id; ?>" data-color-rows="1">
                            <td>
                                <?php if (is_null($record->cmsh_verified_at)): ?>
                                    <img src="<?php echo asset('/images/icons/assignment_posted_16px.png'); ?>" alt="" title="" style="vertical-align: middle; width: 1em; height: 1em;" /> Not Submitted
                                <?php else: ?>
                                    <img src="<?php echo asset('/images/icons/assignment_confirmed_16px.png'); ?>" alt="" title="" style="vertical-align: middle; width: 1em; height: 1em;" /> <?php echo date('n/j/y g:ia', strtotime($record->cmsh_verified_at)); ?>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php echo $record->bracket->activity; ?>
                            </td>
                            <td>
                                <?php if (Helpers::strEqual($record->bracket->activity, array('VBL', 'BBX', 'GBX')) and Helpers::strEqual($record->round->round_type, array('QF', 'SF', 'F', 'S', 'SC', 'C'))): ?>
                                    Final-Site
                                <?php else: ?>
                                    <?php echo $record->round_type->name; ?>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php echo date('D n/j', strtotime($record->event->start_at)); ?>
                            </td>                                    
                            <td>
                                <a href="<?php echo url('/activities/' . strtolower($record->bracket->activity) . '/brackets?div=' . $record->bracket->division); ?>" target="_blank"><?php echo $record->contest->away_team_name; ?> @ <?php echo $record->contest->home_team_name; ?></a>
                            </td>   
                            <td>
                                <?php echo $record->number; ?> &#215; <?php echo $record->officials_fee->position; ?>
                            </td>
                            <td style="text-align: center;">
                                <?php echo $record->miles; ?>
                            </td>                                    
                            <td>
                                $<?php echo number_format($record->total_amount, 2); ?>
                            </td>
                            <td>
                                <?php if (is_null($record->closed_at)): ?>
                                    
                                    <?php if (in_array($record->officials_association, $edit_loa_records)): ?>
                                        <a href="#" data-dialog="officials_championship_record">Edit/Submit</a>
                                    <?php else: ?>
                                        VIEW ONLY
                                    <?php endif; ?>
                                <?php else: ?>
                                    CLOSED
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            </div>                

        <?php endif; ?>
    </div>


    <!-- Schools -->
    <div id="cmsh_schools">
        
        <div style="position: absolute; float: right; right: 0.5em; top: 0.5em; font-size: 10pt;">
            <?php foreach ($oas as $oa): ?>
                &raquo; <a href="<?php echo url('/officials/associations/' . $oa . '/download-contacts'); ?>" target="_blank">Download Contacts</a><br />
            <?php endforeach; ?>
        </div>

        <h2>Assigned Schools</h2>
        <h3 style="color: #990000;">
            <?php echo count($all_school_objects); ?> Assigned 
            <?php if (count($all_school_objects) == 1): ?>
                School,
            <?php else: ?>
                Schools,
            <?php endif; ?>
            <?php echo $assigned_teams; ?>  
            <?php if ($assigned_teams == 1): ?>
                Activity Programs, Total of
            <?php else: ?>
                Activity Programs, Total of 
            <?php endif; ?>
            <?php echo $total_teams; ?>  
            <?php if ($total_teams == 1): ?>
                Team
            <?php else: ?>
                Teams
            <?php endif; ?>
        </h3>

        <?php if (count($all_school_objects) == 0): ?>
            <br />

            You have no assigned schools in the OSAA database.
        <?php else: ?>
            

            <div style="/*max-height: 500px;*/ max-height: 715px; overflow-x: hidden; overflow-y: auto;">

            <table class="bia_table" style="font-size: 8.5pt;">
                <thead>
                    <tr>
                        <th>School</th>                        
                        <th>Information</th>                        
                    </tr>
                </thead>

                <tbody>
                    <?php $index = 0; ?>
                    <?php foreach ($all_school_objects as $school_object): ?>
                        <?php $index++ ?>
                        <tr data-school-id="<?php echo $school_object->school->id; ?>" <?php echo ($index % 2 == 0) ? 'class="odd"' : null; ?>>
                            <td style="font-weight: bold; padding-left: 2px;">
                                <a href="<?php echo url('/schools/' . $school_object->school->id); ?>" target="_blank"><?php echo $school_object->school->short_name; ?></a>
                            </td>                            
                            <td>
                                <table style="width: 580px; float: right;">
                                    <tr style="height: 1.5em; line-height: 1.5em;">
                                        <td style="width: 50px;">AD</td>
                                        <td style="width: 145px;">
                                            <?php if (!is_null($school_object->ad)): ?>
                                                <?php echo $school_object->ad->getDisplayName(); ?>
                                            <?php else: ?>
                                                - -
                                            <?php endif; ?>
                                        </td>
                                        <td style="width: 250px;">
                                            <?php if (!is_null($school_object->ad) and !Helpers::strIsEmpty($school_object->ad->email)): ?>
                                                <?php echo $school_object->ad->getDisplayEmail(); ?>
                                            <?php else: ?>
                                                - -
                                            <?php endif; ?>
                                        </td>
                                        <td style="width: 135px;">
                                            <?php if (!is_null($school_object->ad) and !Helpers::strIsEmpty($school_object->ad->work_phone)): ?>
                                                <?php echo $school_object->ad->getDisplayPhone('WORK'); ?>
                                            <?php else: ?>
                                                - -
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <tr style="height: 1.5em; line-height: 1.5em;">
                                        <td>Sec.</td>
                                        <td>
                                            <?php if (!is_null($school_object->ad_sec)): ?>
                                                <?php echo $school_object->ad_sec->getDisplayName(); ?>
                                            <?php else: ?>
                                                - -
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if (!is_null($school_object->ad_sec) and !Helpers::strIsEmpty($school_object->ad_sec->email)): ?>
                                                <?php echo $school_object->ad_sec->getDisplayEmail(); ?>
                                            <?php else: ?>
                                                - -
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if (!is_null($school_object->ad_sec) and !Helpers::strIsEmpty($school_object->ad_sec->work_phone)): ?>
                                                <?php echo $school_object->ad_sec->getDisplayPhone('WORK'); ?>
                                            <?php else: ?>
                                                - -
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>                            
                        </tr>
                        <?php foreach ($school_object->activity_programs as $ap): ?>
                            <tr <?php echo ($index % 2 == 0) ? 'class="odd2"' : null; ?>>
                                <td style="text-align: right; border-top: 1px solid #ffefb0;">
                                    <a href="<?php echo url('/teams/' . $ap->id); ?>" target="_blank"><?php echo Helpers::getActivityName($ap->activity); ?></a>
                                </td>
                                <td style="border-top: 1px solid #ffefb0;">
                                    <table style="width: 580px; float: right;">
                                        <tr style="height: 1.5em; line-height: 1.5em;">
                                            <td style="width: 75px; text-align: right; padding-right: 10px;"><?php echo $ap->division; ?></td>                                            
                                            <td style="width: 225px;">
                                                <?php if (Helpers::strIsEmpty($ap->league)): ?>
                                                    Independent
                                                <?php else: ?>
                                                    <?php
                                                        $league = league::where('slug', '=', $ap->league)->firstOrFail();
                                                    ?>
                                                    <?php echo $league->name; ?>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <?php echo $ap->getTeams(true); ?>
                                            </td>
                                        </tr>                                    
                                    </table>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
            </div>                

        <?php endif; ?>

    </div>   


</div>