<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<main>

</main>
<body>

<a data-activates="slide-out"
   class="button-collapse btn-floating waves-effect waves-light indigo lighten-1 "><i
            class="material-icons">more_horiz</i></a>
<div class="row">
    <ul id="slide-out" class="side-nav">
        <div class="col s12">
            <li><span style="font-weight: bold">  <?php echo $text_lateral_nav['nav_lateral_competition_name'];?> :</span> <span> <?php echo $competition->nombre; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> <?php echo $text_lateral_nav['nav_lateral_competition_type_of_competition'];?> :</span>
                <span> <?php echo $competition->tcDescripcion; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> <?php echo $text_lateral_nav['nav_lateral_competition_age_group'];?>:</span>
                <span> <?php echo $competition->geDescripcion; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold">  <?php echo $text_lateral_nav['nav_lateral_competition_date'];?> :</span> <span> <?php echo $competition->fecha; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> <?php echo $text_lateral_nav['nav_lateral_competition_community'];?> :</span> <span> <?php echo $competition->autonomia; ?></span>
            </li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> <?php echo $text_lateral_nav['nav_lateral_competition_province'];?> :</span> <span> <?php echo $competition->provincia; ?></span>
            </li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> <?php echo $text_lateral_nav['nav_lateral_number_of_mats'];?> :</span> <span> <?php echo sizeof($mats) ?></span>
            </li>
        </div>
        <li>
            <div class="divider"></div>
        </li>
        <div class="col s12">
            <div class="collection ">
                <a href="" class="collection-item active indigo"><?php echo $text_lateral_nav['nav_lateral_competition_referee_inscription'];?></a>
                <a href="<?php echo base_url() ?>competitor_inscription/<?php echo $competition->idCompeticion ?>" class="collection-item indigo-text"><?php echo $text_lateral_nav['nav_lateral_competition_competitor_inscription'];?><i class="material-icons right">visibility</i></a>
                <a href="<?php echo base_url() ?>viewcompetition/<?php echo $competition->idCompeticion ?>" class="collection-item indigo-text"> <?php echo $text_lateral_nav['nav_lateral_competition_combats'];?><i class="material-icons right">visibility</i></a>

            </div>
        </div>
    </ul>
</div>
<div class="container">
    <div class="row">
        <span style="font-weight: bold"> <?php echo $referee_inscription_text['competition_referee_inscription_step1']; ?></span>
        <div class="col s12 xl12">



            <a href="#modal-referee" class=" modal-trigger btn-floating btn waves-effect waves-light indigo"><i
                        class="material-icons">add</i></a>
            <div id="modal-referee" class="modal">
                <div class="modal-content">


                    <?php if ($referees) {
                        ?>
                        <form action="<?php echo base_url() ?>competition/addRefereeToCompetition/<?php echo $idCompetition ?>"
                              method="post" onsubmit=" p()">
                            <table class="centered striped responsive-table">
                                <thead class="row collection-item">
                                <tr>
                                    <th> <?php echo $referee_inscription_text['competition_referee_inscription_name']; ?></th>
                                    <th><?php echo $referee_inscription_text['competition_referee_inscription_surname']; ?></th>
                                    <th> <?php echo $referee_inscription_text['competition_referee_inscription_category']; ?></th>
                                    <th> <?php echo $referee_inscription_text['competition_referee_inscription_team']; ?></th>
<!--                                    <th> --><?php //echo $referee_inscription_text['competition_referee_inscription_select']; ?><!--</th>-->

                                </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($referees as $referee) { ?>
                                    <tr>
                                        <td><?php echo $referee->nombre ?></td>
                                        <td><?php echo $referee->apellido1 ?></td>
                                        <td><?php echo $referee->descripcion ?></td>
                                        <td>
                                            <select name="idTeams[]">
                                                <option value="" disabled selected>Team</option>
                                                <?php foreach ($teams as $team) {
                                                    $referee_json -> dni = $referee -> dni;
                                                    $referee_json -> team = $team -> nombre;

                                                    ?>

                                                    <option value='<?php echo json_encode($referee_json)?>'><?php echo $team->nombre; ?></option>

                                                <?php } ?>
                                            </select>
                                        </td>

                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>


                            <div class="modal-footer">
                                <button class="btn btn-large waves-effect indigo"><?php echo $referee_inscription_text['competition_referee_inscription_enroll']; ?></button>
                            </div>
                        </form>
                    <?php } else { ?>
                        <span style="font-weight: bold"><?php echo $referee_inscription_text['competition_referee_inscription_no_more_referees']?></span>
                    <?php } ?>

                </div>

            </div>
        </div>

    </div>
</div>
<div class="container">
    <?php if($referees_not_associate) {?>
    <div class="row ">
        <div class="col s12">



            <span style="font-weight: bold"> <?php echo $referee_inscription_text['competition_referee_inscription_referees_enroll']; ?></span>
            <?php } ?>
            <ul class="collection with-header">


                <?php foreach ($referees_not_associate as $referee) { ?>
                    <form  id="<?php echo $referee->dni ?>"ondragstart="drag(event)" draggable="true"  method="post"
                          action="<?php echo base_url() ?>competition/deleteRefereeInscription/<?php echo $idCompetition ?>">

                        <li class="row collection-item   brown lighten-4">
                            <div class="col s2 "><?php echo $referee->nombre ?></div>
                            <div class="col s2"><?php echo $referee->apellido1 ?></div>
                            <div class="col s4"><?php echo $referee->descripcion ?></div>
                            <input type="hidden" name="dni" value="<?php echo $referee->dni; ?>">
                            <input type="hidden" name="idcompetition" value="<?php echo $idCompetition; ?>">
                            <div class="col s2">
                                <button onclick="return confirm('<?php echo $referee_inscription_text['competition_referee_inscription_confirm_delete']?> <?php echo $referee->nombre; ?>')"
                                        class="btn red lighten-3" id="<?php echo $referee->dni ?>"><i
                                            class="material-icons">cancel</i></button>
                            </div>


                        </li>
                    </form>
                <?php } ?>

            </ul>

        </div>
    </div>
</div>

<?php
$class = '';

$number_of_mats = sizeof($mats);

switch ($number_of_mats) {
    case 1:
        $class = 'col s12 xl12';
        break;
    case 2:
        $class = 'col s12 xl6';
        break;
    case 3:
        $class = 'col s12 xl6 ';
        break;
    case 4:
        $class = 'col s12 xl6';
        break;
    default:
        $class = 'col s12 xl6';
        break;


}
?>
<br>

<div class="container">
    <div class="row">
        <span style="font-weight: bold">  <?php echo $referee_inscription_text['competition_referee_inscription_step2']; ?></span>
        <div class="col s12">


            <?php foreach ($mats as $mat) { ?>

                <div class="<?php echo $class ?>" >

                    <ul class="collection with-header">

                        <li class="collection-header indigo lighten-5 center">
                            <h4> <?php echo $referee_inscription_text['competition_referee_inscription_mat']; ?> : <?php echo $mat->numero; ?></h4>
                            <form method="post" action="<?php echo base_url()?>competition/asignTimekeeperToMat/<?php echo $idCompetition; ?>">
                                <div class="input-field ">
                                    <select onchange = "this.form = submit();" name ="timekeeper">
                                        <option value="" disabled selected><?php echo $referee_inscription_text['competition_select_choose']?></option>
                                        <?php if($mat -> emailCronometrador != null){?>
                                        <option  value =""  disabled selected> <?php echo $mat -> emailCronometrador?> </option>
                                        <?php }?>
                                   
                                    
                                
                                        <?php foreach ($timekeepers as $timekeeper){?>
                                            <option value="<?php echo $timekeeper -> email?>" <?php echo $timekeeper -> email == $mat ->emailCronometrador ? 'selected' : null ?>><?php echo $timekeeper -> email?>  </option>
                                        <?php } ?>
                                        <option style="color:#97310e" value ="delete"> <?php echo $referee_inscription_text['competition_select_delete_timekeeper']?></option>


                                    </select>
                                    <label><?php echo $referee_inscription_text['competition_referee_inscription_select_timekeeper']?></label>
                                </div>
                                <input type="hidden" name="mat" value="<?php echo $mat ->idTatami?>"/>
                            </form>
                        </li>

                        <?php foreach ($mat_referees[$mat -> idTatami] as $referee) { ?>
                            <form  id="<?php echo $referee->dni ?>"ondragstart="drag(event)" draggable="true"  method="post"
                                   action="<?php echo base_url() ?>competition/deleteRefereeInscription/<?php echo $idCompetition ?>">

                                <li class="row collection-item brown lighten-4">
                                    <div class="col s3 "><?php echo $referee->nombre ?></div>
                                    <div class="col s4"><?php echo $referee->apellido1 ?></div>
                                    <div class="col s4"><?php echo $referee->descripcion ?></div>
                                    <input type="hidden" name="dni" value="<?php echo $referee->dni; ?>">
                                    <input type="hidden" name="idcompetition" value="<?php echo $idCompetition; ?>">
                                    <div class="col s1">
                                        <button onclick="return confirm('<?php echo $referee_inscription_text['competition_referee_inscription_confirm_delete']?> <?php echo $referee->nombre; ?>')"
                                                class=" red lighten-3" id="<?php echo $referee->dni ?>"><i
                                                    class="material-icons">cancel</i></button>
                                    </div>


                                </li>
                            </form>
                        <?php } ?>
                        <li id="<?php echo $mat ->idTatami?>" class="collection-item" ondrop="drop(event)" ondragover="allowDrop(event)"></li>

                    </ul>
                </div>
            <?php } ?>
        </div>
        <div class="col s4">

        </div>
    </div>
</div>
<!-- Modal Structure -->


</body>
<script>
    function allowDrop(ev) {
        ev.preventDefault();
    }

    function drag(ev) {

        ev.dataTransfer.setData("text", ev.target.id);
    }

    function drop(ev) {
        const COLOR_ERROR = 'red lighten-1';
        const COLOR_OK = 'indigo lighten-2';
        ev.preventDefault();
        var data = ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById(data));
        console.log('referee :'+document.getElementById(data).elements.item(0).value);
        console.log('idCompetition :'+document.getElementById(data).elements.item(1).value);
        data['dni'] = document.getElementById(data).elements.item(0).value;
        data['idCompetition'] = document.getElementById(data).elements.item(1).value;
        console.log(ev.target.id);
        $.ajax ({
            type: "POST",
            url : "<?php echo base_url()?>competition/addRefereeToMat",
            data: {dni:document.getElementById(data).elements.item(0).value,
                idCompetition:document.getElementById(data).elements.item(1).value,
                idMat:ev.target.id},
            success: function(result) {
                location.reload();


            },
            error: function (result) {

                console.log(result);
            }
        })
    }
</script>