<?php $__env->startSection('page_title'); ?>
    OSAA - Officials Associations
<?php $__env->stopSection(); ?>

<?php $__env->startSection('page_sub_title'); ?>    
    Local Officials Associations &amp; Commissioners
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
    @parent

    <style type="text/css">
    
        .associations_list {
            margin-bottom: 2em;

        }

        .association_row {
            line-height: 1.5em;
        }


    </style>
	
<?php $__env->stopSection(); ?>

<?php $__env->startSection('jquery_init'); ?>
	
    $('.associations_list .association_row:odd').css('background-color', '#f2f2f2');

    <?php if (Auth::check() and Auth::user()->isOsaaUser()): ?>
        $('.assign_commissioner')
            .click(function(event)
            {
                event.preventDefault();

                var link = $(this);
                var parent = link.parents('.association_row');
                var association_id = parent.attr('data-id');
                var association_name = $('.association_name', parent).text();

                 $('<div></div>')
                    .appendTo('body')
                    .dialog(
                    {                       
                        title : 'Assign Commissioner',
                        height : 300,
                        width : 600,
                        draggable : false,
                        resizable : false,
                        modal : true,
                        buttons: [
                            {
                                text : "Go",
                                'class' : "float_left",
                                icons : { secondary : 'ui-icon-triangle-1-e'},                             
                                click : function()
                                        {                                                       
                                            var dialog_object = $(this);

                                            // Get the data from the form fields
                                            if ($('input[name="first_name"]', dialog_object).size() > 0)
                                            {
                                                var data = { email : $('input[name="email"]', dialog_object).val(),
                                                             first_name : $('input[name="first_name"]', dialog_object).val(),
                                                             last_name : $('input[name="last_name"]', dialog_object).val(),
                                                             work_phone : $('input[name="work_phone"]', dialog_object).val(),
                                                             mobile_phone : $('input[name="mobile_phone"]', dialog_object).val(),
                                                             create_new_user : "true" };
                                            }
                                            else
                                            {
                                                var data = { email : $('input[name="email"]', dialog_object).val() };
                                            }

                                            // Debug
                                            console.log(data);

                                            // Show the loading animation while the e-mail address is looked up
                                            dialog_object.html("<div class='ajax_loader' style='width:16px; height:11px; margin:4em auto; background-image: url(http://www.osaa.org/images/icons/ajax_loader2.gif); background-repeat:no-repeat;'></div>");

                                            // Submit the change
                                            var jqxhr = $.ajax(
                                                {
                                                    type : 'POST',
                                                    url : 'http://www.osaa.org/officials/associations/' + association_id + '/assign-commissioner',
                                                    data : data,
                                                    dataType : 'html'
                                                })
                                                .done(function (return_data)
                                                {                               
                                                    // User was not found
                                                    if (return_data == 'No Such User')
                                                    {                                                    
                                                        var html = '<div style="font-size: 12pt; margin: 1em 0; color: #1C43A6; font-weight: bold;">' + association_name + '</div>';

                                                        html += '<div style="font-size: 11pt; margin: 1em 0;"><span class="ui-icon ui-icon-alert" style="display: inline-block; vertical-align: middle; background-image: url(<?php echo asset('/images/icons/ui-icons_cd0a0a_256x240.png'); ?>);"></span> That e-mail address was not found.  Please provide additional information to create a new user.  Phone numbers are optional.</div>';
                                                                                    
                                                        html += '<div style="float: left;"><b>E-Mail: </b>';
                                                        html += '<input type="text" name="email" style="width: 300px;" value="' + data.email + '"/>';
                                                        html += '</div>';

                                                        html += '<br class="clear" /><br />';

                                                        html += '<div style="float: left;"><b>First Name: </b>';
                                                        html += '<input type="text" name="first_name" style="width: 150px; margin-right: 30px;" data-autofocus="true" />';
                                                        html += '</div>';

                                                        html += '<div style="float: left;"><b>Last Name: </b>';
                                                        html += '<input type="text" name="last_name" style="width: 150px;" />';
                                                        html += '</div>';

                                                        html += '<br class="clear" /><br />';

                                                        html += '<div style="float: left;"><b>Work Phone: </b>';
                                                        html += '<input type="text" name="work_phone" style="width: 125px; margin-right: 30px;" />';
                                                        html += '</div>';

                                                        html += '<div style="float: left;"><b>Mobile Phone: </b>';
                                                        html += '<input type="text" name="mobile_phone" style="width: 125px;" />';
                                                        html += '</div>';

                                                        html += '<br class="clear" /><br />';

                                                        html += '<div style="font-size: 11pt; margin: 1em 0;">This will create a new user and assign him/her as the commissioner.  The user\'s default password will be "Cmsh2014" and must be changed at first login.<br /><br />Click <b>Go</b> when ready.</div>'; 
                                                        
                                                        // Expand the dialog box, change the text, and focus where it should
                                                        dialog_object
                                                            .animate({'height' : 325}, 400, 'swing', function()
                                                            {
                                                                dialog_object.hide()                                                        
                                                                             .html(html)
                                                                             .fadeIn('slow', function()
                                                                             {
                                                                                 $('[data-autofocus="true"]', dialog_object).focus();
                                                                             });
                                                            });
                                                    }
                                                    
                                                    // User was found
                                                    else
                                                    {
                                                        // Reload the page
                                                        location.reload();     
                                                    }                                                
                                                })
                                                .fail(function (jqXHR, status, error)
                                                {                                                   
                                                    var response = jqXHR.responseText;
                                                    var errorData = $.parseJSON(response);
                                                    console.log(errorData);
                                                    alert ("There was an error.\n\n" + errorData.error.message + "\n\nPlease try again.");
                                                    location.reload();
                                                });              
                                        }
                            },                         
                            {
                                text : "Cancel",
                                'class' : "float_right",                            
                                icons : { primary : 'ui-icon-cancel'},
                                click : function()
                                        {
                                            // Close the dialog box
                                            $(this).dialog("close");                                         
                                        }
                            }
                        ],                   
                        open : function ()
                            {  
                                var dialog_object = $(this);                            

                                var html = '<div style="font-size: 12pt; margin: 1em 0; color: #1C43A6; font-weight: bold;">' + association_name + '</div>';

                                html += '<div style="font-size: 11pt; margin: 1em 0;"><span class="ui-icon ui-icon-info" style="display: inline-block; vertical-align: middle;"></span> Assign a commissioner to this association by e-mail address.  If the e-mail address is not found, you will be prompted to create a new user for the commissioner.  Click <b>Go</b> when ready.</div>';
                                                            
                                html += '<div style="float: left;"><b>E-Mail: </b>';
                                html += '<input type="text" name="email" style="width: 300px;" data-autofocus="true" />';                            
                                html += '</div>';

                                html += '<br class="clear" /><br />';

                                dialog_object.html(html);

                                $('.ui-dialog-buttonpane').css('padding', '0 0.5em').css('font-size', '9pt');
                                $('.ui-dialog-buttonpane .ui-dialog-buttonset').css('width', '100%');   

                                $('[data-autofocus="true"]').focus();                            
                            },
                        close : function ()
                            {
                                var dialog_object = $(this);

                                $(dialog_object).remove();
                            }                
                    });
            });
    <?php endif; ?>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('page_functions'); ?> 		

    	
<?php $__env->stopSection(); ?>


<?php $__env->startSection('main_content'); ?>    
	
    <br />

    <?php foreach ($info['list'] as $sport => $loas): ?>

        <h1><?php echo $sport; ?></h1>

        <div class="associations_list">

            <?php foreach ($loas as $loa): ?>

                <div class="association_row" style="width: 1020px; float: left;" data-id="<?php echo $loa->id; ?>" >

                    <div style="width: 450px; font-weight: bold; float: left;" class="association_name">
                        <?php echo $loa->name; ?>
                    </div>

                    <div style="width: 150px; float: left;" class="association_commissioner_name">
                        <?php if (!is_null($loa->commissioner)): ?>
                            <?php echo $loa->commissioner->user->getDisplayFullName(); ?>                        
                        <?php else: ?>
                            <?php if (Auth::check() and Auth::user()->isOsaaUser()): ?>
                                <a href="#" class="assign_commissioner">Add Commissioner</a>
                            <?php else: ?>
                                - -
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>

                    <div style="width: 250px; float: left;" class="association_commissioner_email">
                        <?php if (!is_null($loa->commissioner)): ?>
                            <?php echo Helpers::obfuscateEmailLink($loa->commissioner->user->email); ?>
                        <?php else: ?>
                            - -
                        <?php endif; ?>
                    </div>

                    <div style="width: 100px; float: left;">
                        <?php echo ucfirst(strtolower($loa->oregon_region)); ?>
                    </div>

                </div>

            <?php endforeach; ?>

            <br class="clear" />

        </div>       

    <?php endforeach; ?>

<?php $__env->stopSection(); ?>