<?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"> Nombre :</span> <span> <?php echo $competition->nombre; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> Tipo de competición :</span>
                <span> <?php echo $competition->tcDescripcion; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> Grupo de edad :</span>
                <span> <?php echo $competition->geDescripcion; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> Fecha :</span> <span> <?php echo $competition->fecha; ?></span></li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> Autonomía :</span> <span> <?php echo $competition->autonomia; ?></span>
            </li>
        </div>
        <div class="col s12">
            <li><span style="font-weight: bold"> Provincia :</span> <span> <?php echo $competition->provincia; ?></span>
            </li>
        </div>

        <li>
            <div class="divider"></div>
        </li>
        <div class="col s12">
            <div class="collection">
                <a href="<?php echo base_url() ?>referee_inscription/<?php echo $competition->idCompeticion ?>"
                   class="collection-item ">Referee inscription</a>
                <a href="" class="collection-item active">Competitor Inscription</a>
                <a href="#!" class="collection-item"> Combats</a>

            </div>
        </div>
        <div class="col s12">

        </div>
        <span style="font-weight: bold"> Elija el sexo del competidor</span>
        <div class="switch">
            <form class="row"
                  action="<?php echo base_url() ?>competitorInscription/competitor_inscription/<?php echo $competition->idCompeticion ?>"
                  method="post">
                <p>
                    <input name="sex" type="radio" id="test1" value="F"
                           onchange="this.form.submit()" <?php if (isset($_POST['sex']) && $_POST['sex'] == 'F') echo ' checked="checked"'; ?>/>
                    <label for="test1">Female</label>
                </p>

                <input name="sex" type="radio" id="test2" value="M"
                       onchange="this.form.submit()"<?php if (isset($_POST['sex']) && $_POST['sex'] == 'M') echo ' checked="checked"'; ?>/>
                <label for="test2">Male</label>
                </p>

            </form>

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

<div class="row">

    <div id="modal-competitor" class="modal">
        <div class="modal-content">
            <form method="post" action="<?php echo base_url() ?>competitorInscription/enrollCompetitor">
                <input type="hidden" name="idCompetition" value="<?php echo $competition->idCompeticion ?>">
                <input type="hidden" name="peso" value="" id="idPeso">
                <input type="hidden" name="competition_date" value="<?php echo $competition->fecha ?>">
                <div class="row">


                    <div class="col s4">
                        <span style="font-weight: bold"><?php echo "Add competitor " ?></span>
                        <div class="input-field col s12">

                            <input disabled value="I am not editable" id="idselectweight" type="text" class="validate">
                            <label for="disabled">weight</label>
                        </div>
                    </div>

                </div>
                <div class="row">

                    <div class="input-field col s4 offset-s2">
                        <input class="validate" type="text" name="dni" id="dni" data-length="9" oninput="verify()"/>
                        <label for="dni"> Dni</label>
                    </div>
                    <div class="input-field col s4">
                        <input class="validate" type="text" name="name" id="name" required/>
                        <label for="nombre">Nombre</label>
                    </div>
                </div>
                <div class="row">
                    <div class="input-field col s4 offset-s2">
                        <input class="validate" type="text" name="surname"
                               required/>
                        <label for="directorTecnico">Primer apellido</label>
                    </div>
                    <div class="input-field col s4">
                        <input class="validate" type="text" name="surname2"/>
                        <label for="directorTecnico">Segundo apellido</label>
                    </div>
                </div>
                <div class="row" >

                    <div class="input-field col s4">
                        <select name="country">
                            <option value="" disabled selected>Pais</option>
                            <option value="ESP">España</option>
                            <option value="FRA">Alemania</option>

                        </select>
                        <label>Pais</label>
                    </div>

                    <div class="input-field col s4">
                        <input class="validate" type="text" name="community"/>
                        <label for="directorTecnico">Autonomía</label>
                    </div>
                    <div class="input-field col s4">
                        <input class="validate" type="text" name="province"/>
                        <label for="directorTecnico">Provincia</label>
                    </div>
                    <div class="col s6 offset-s3">
                        <select name="team">
                            <option value="" disabled selected>Team</option>

                            <?php foreach ($teams as $team) { ?>

                                <option name="<?php echo $team->nombre ?>"><?php echo $team->nombre ?></option>

                            <?php } ?>
                        </select>
                        <label>Team</label>
                    </div>

                    <div class="input-field col s12">
                        <input class="validate" type="text" name="email" id="email"/>
                        <label for="email">Email</label>
                    </div>

                </div>

        </div>

        <div class="modal-footer">
            <button class="btn btn-large waves-effect indigo">Crear</button>
        </div>
        </form>

    </div>

</div>
</div>

</div>
<div class="row">


    <div class="col s12">
        <div class="row">
            <?php $size = sizeof($weights);
            $count = 0;
            $colors = array('green lighten-4', 'light-blue lighten-4', 'orange lighten-4',
                'blue-grey lighten-4', 'pink lighten-4', 'purple lighten-4', 'indigo lighten-4', 'cyan lighten-4');
            ?>

            <?php foreach ($weights as $weight) { ?>
                <div class="col s4">


                    <ul class="collection with-header "
                    >
                        <li class="collection-header center tooltipped "
                            data-tooltip="Click para añadir competidor en la categoria de <?php echo $count == $size - 1 ? '+' : '-' ?> <?php echo $weight->descripcion ?> KG"
                            id="<?php echo $weight->idPeso ?>"
                            onmouseover=" document.body.style.cursor = 'pointer'"
                            onmouseleave="document.body.style.cursor = 'auto'"
                            onclick="openModalAddCompetitor(this.id,<?php echo $count == $size - 1 ? '+' : '-' ?> <?php echo $weight->descripcion ?>)">
                            <h4><?php echo $count == $size - 1 ? '+' : '-' ?> <?php echo $weight->descripcion ?> KG</h4>
                        </li>
                        <?php foreach ($competitors_p[$weight->idPeso] as $item) { ?>

                            <form id="<?php echo $item->dni ?>" ondragstart="drag(event)" draggable="true" method="post"
                                  action="<?php echo base_url() ?>competitorInscription/deleteCompetitor">

                                <li class="row collection-item <?php echo $colors[$count] ?>">
                                    <div class="col s3 "><span
                                            style="font-weight: bold"><?php echo $item->nombre ?></span></div>
                                    <div class="col s2 "><?php echo $item->apellido1 ?></div>
                                    <div class="col s4 "><span
                                            style="font-weight: bold"><?php echo $item->nombreEquipo ?></span></div>
                                    <input type="hidden" name="dni" value="<?php echo $item->dni; ?>">

                                    <input type="hidden" name="fecha" value="<?php echo $competition->fecha; ?>">
                                    <input type="hidden" name="oldweight" value="<?php echo $weight->idPeso ?>">
                                    <input type="hidden" name="idCompetition"
                                           value="<?php echo $competition->idCompeticion; ?>">
                                    <div class="col s3">
                                        <button onclick="return confirm('Quiere eliminar la participacion del competidor : <?php echo $item->dni; ?>')"
                                                class="btn red lighten-3" id="<?php echo $item->dni ?>"><i
                                                class="material-icons">cancel</i></button>
                                    </div>

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

                        </li>
                    </ul>

                </div>
                <?php $count++ ?>
            <?php } ?>

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

</div>


<!-- Modal Structure -->
<div id="exits_competitor_modal" class="modal modal-fixed-footer">
    <form action="<?php echo base_url() ?>competitorInscription/definitive_registration" method="post">
        <input type="hidden" name="idCompetition" value="<?php echo $competition->idCompeticion ?>">
        <input type="hidden" name="competition_date" value="<?php echo $competition->fecha ?>">
        <input type="hidden" name="teamName"
               value="<?php echo $this->session->has_userdata('teamName') ? $this->session->userdata('teamName') : null ?>">
        <input type="hidden" name="idWeight"
               value="<?php echo $this->session->has_userdata('inscription') ? $this->session->userdata('inscription') : null ?>">
        <input type="hidden" name="dni"
               value="<?php echo $this->session->has_userdata('user_inscription') ? $this->session->userdata('user_inscription')->dni : null ?>">
        <div class="modal-content">
            <h4>Inscripción a la competición </h4>
            <p>¿ Quiere inscribir a este competidor?</p>
            <?php if ($this->session->has_userdata('user_inscription')) { ?>

                <li style="font-weight: bold">DNI
                    : <?php echo $this->session->userdata('user_inscription')->dni; ?></li> <br>
                <li style="font-weight: bold">Nombre
                    : <?php echo $this->session->userdata('user_inscription')->nombre; ?></li> <br>
                <li style="font-weight: bold">Primer apellido
                    : <?php echo $this->session->userdata('user_inscription')->apellido1; ?></li> <br>
                <li style="font-weight: bold">Segundo apellido
                    : <?php echo $this->session->userdata('user_inscription')->apellido2; ?></li> <br>
                <li style="font-weight: bold">Autonomia
                    : <?php echo $this->session->userdata('user_inscription')->autonomia; ?></li> <br>
                <li style="font-weight: bold">
                    Provincia: <?php echo $this->session->userdata('user_inscription')->provincia; ?></li> <br>
                <li style="font-weight: bold">Email
                    : <?php echo $this->session->userdata('user_inscription')->email; ?></li> <br>
            <?php } ?>
        </div>
        <div class="modal-footer">
            <button class="modal-action modal-close waves-effect waves-green btn-flat teal lighten-2">Inscribir</button>

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

</body>

<script>

    function openModalAddCompetitor(id, value) {
        $(document).ready(function () {
            // the "href" attribute of the modal trigger must specify the modal ID that wants to be triggered
            $('.modal').modal();
            $('#idPeso').val(id);
            $('#idselectweight').val(value);
            $('#modal-competitor').modal('open');
        });

    }
</script>
<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('dni competitor :' + document.getElementById(data).elements.item(0).value);
        console.log('Fecha competicion :' + document.getElementById(data).elements.item(1).value);
        console.log('Peso antiguo :' + document.getElementById(data).elements.item(2).value);
        data['dni'] = document.getElementById(data).elements.item(0).value;
        data['idCompetition'] = document.getElementById(data).elements.item(1).value;

        console.log('Id peso actualizacion -> ' + ev.target.id);
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()?>competitorInscription/updateParticipation",
            data: {
                dni: document.getElementById(data).elements.item(0).value,
                date: document.getElementById(data).elements.item(1).value,
                oldWeight: document.getElementById(data).elements.item(2).value,
                idWeight: ev.target.id,
                competitionId:<?php echo $competition->idCompeticion?>},
            success: function (result) {
                location.reload();
                console.log(result);


            },
            error: function (result) {
                alert('ERROR -> mirar consola');
                console.log(result);
            }
        })
    }
</script>

<script>
    function verify() {

        console.log("<?php echo $dnis[0] -> dni?>");

        <?php foreach ($dnis as $dni){?>
        if($('#dni').val() == "<?php echo $dni -> dni?>")
        //$('#prueba').hide()
        <?php }?>
            }
</script>