<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<main>
    <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>

            <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 indigo-text"><?php echo $text_lateral_nav['nav_lateral_competition_referee_inscription'];?> <i class="material-icons right">visibility</i></a>
                    <a href="" class="collection-item active indigo"><?php echo $text_lateral_nav['nav_lateral_competition_competitor_inscription'];?> </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>
            <div class="col s12">

            </div>
            <span style="font-weight: bold"> <?php echo $text_lateral_nav['nav_lateral_competition_choose_sex'];?></span>
            <div class="switch">
                <form class="row"
                      action="<?php echo base_url() ?>competitorInscription/competitor_inscription/<?php echo $competition->idCompeticion ?>"
                      method="post">
                    <div class="col s12 xl6">
                        <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"><?php echo $text_lateral_nav['nav_lateral_competition_female'];?></label>
                    </div>
                    <div class="col s12 xl6">
                        <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"><?php echo $text_lateral_nav['nav_lateral_competition_male'];?></label>
                    </div>

                </form>

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

    <div class="row">

        <div id="modal-competitor" class="modal">
            <div class="modal-content">
                <div class="container">
                    <div class="row z-depth-4 indigo lighten-3">
                        <div class="col s12 xl8"><br><span
                                    style="font-weight: bold"><?php echo $text_competitor_inscription['competitor_inscription_modal_exists_competitor_enter_dni']; ?></span>
                        </div>
                        <div class="col s12 xl4 input-field">
                            <i class="material-icons prefix">search</i>
                            <input type="text" id="dni" oninput="validate()" data-length="9"  maxlength="9" minlength="9" >
                            <label for="dni">Dni</label>

                        </div>

                    </div>

                    <div class="row " id="exists_dni" style="display: none">

                        <form action="<?php echo base_url() ?>competitorInscription/inscription" 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="idWeight" value="" id="idPeso">
                            <input type="hidden" name="dni" value="" id="dniP">
                            <div class="row">
                                <li id="exists_nombre" value=""></li>
                                <li id="exists_apellido1" value=""></li>
                                <li id="exists_apellido2" value=""></li>
                                <li id="exists_comunidad" value=""></li>
                                <li id="exists_provincia" value=""></li>

                                <div class="col s12"><span
                                            style="font-weight: bold"> <?php echo $text_competitor_inscription['competitor_inscription_modal_exists_select_team']; ?> </span>
                                </div>
                                <div class="col s12 xl6 ">
                                    <select name="team" id="teamID">
                                        <option value="" disabled
                                                selected><?php echo $text_competitor_inscription['competitor_inscription_modal_exists_team']; ?></option>

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

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

                                        <?php } ?>
                                    </select>
                                    <label><?php echo $text_competitor_inscription['competitor_inscription_modal_exists_team']; ?></label>
                                </div>
                            </div>

                            <div class="col s2 offset-s2">
                                <button class="btn btn-large waves-effect indigo" onclick="return notEmptyFieldsValidate()">
                                    <?php echo $text_competitor_inscription['competitor_inscription_modal_exists_enroll']; ?>
                                </button>
                            </div>

                        </form>
                    </div>
                    <div class="row" id="not_exists_dni">
                        <form method="post" action="<?php echo base_url() ?>competitorInscription/enrollCompetitor">
                            <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="idWeight" value="" id="idPesoC">
                            <input type="hidden" name="dni" value="" id="dniCreateCompetitor">
                            <div class="row">


                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">create</i>

                                    <input class="validate" type="text" name="name" id="name" required  maxlength="30" data-length="30"/>
                                    <label for="nombre"><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_nombre']; ?>
                                        (*)</label>
                                </div>
                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">create</i>

                                    <input class="validate" type="text" name="surname" id="surname" required  maxlength="30" data-length="30"/>
                                    <label for="apellido1"><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_surname']; ?>
                                        (*)</label>
                                </div>


                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">create</i>
                                    <input class="validate" type="text" name="surname2" id="surname2" required  maxlength="30" data-length="30"/>
                                    <label for="apellido2"><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_surname2']; ?>(*)</label>
                                </div>
                            </div>
                            <div class="row">

                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">list</i>
                                    <select name="country">
                                        <option value="" disabled
                                                selected><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_country']; ?></option>
                                        <option value="ESP">España</option>
                                        <option value="ALE">Alemania</option>
                                        <option value="FRE">Francia</option>

                                    </select>
                                    <label><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_country']; ?></label>
                                </div>
                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">create</i>
                                    <input class="validate" type="text" name="community" id="community"  maxlength="30" data-length="30"/>
                                    <label for="autonomia"><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_community']; ?></label>
                                </div>
                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">create</i>
                                    <input class="validate" type="text" name="province" id="province"   maxlength="30" data-length="30"/>
                                    <label for="provincia"><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_province']; ?></label>
                                </div>

                            </div>

                            <div class="row">
                                <div class="input-field col s12 xl6">
                                    <i class="material-icons prefix">create</i>
                                    <input class="validate" type="text" name="email" id="email"  maxlength="30" data-length="30"/>
                                    <label for="email"><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_email']; ?></label>
                                </div>
                                <div class=" input-field col s12 xl6">
                                    <i class="material-icons prefix">list</i>
                                    <select name="team" id="teamIDcreate">
                                        <option value="" disabled
                                                selected><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_team']; ?></option>

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

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

                                        <?php } ?>
                                    </select>
                                    <label><?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_team']; ?>
                                        (*)</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col s12 xl1 offset-xl7">
                                    <button class="btn btn-large waves-effect indigo"
                                            onclick="return notEmptyFieldsValidate2()">
                                        <?php echo $text_competitor_inscription['competitor_inscription_modal_exists_enroll']; ?>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

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


        <div class="col s12">

            <?php $size = sizeof($weights);
            $count = 0;
            $row_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) { ?>
                <?php if ($row_count == 0 || $row_count == 3 || $row_count == 6) { ?> <div class="row"> <?php } ?>
                <div class="col s12 xl4 ">


                    <ul class="collection with-header ">
                        <li class="collection-header center tooltipped "
                            data-tooltip="<?php echo $text_competitor_inscription['competitor_inscription_tooltiped_add_competitor']; ?>  <?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 $weight->descripcion ?>)">
                            <h4> <?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 z-depth-3 <?php echo $colors[$count] ?>">
                                    <div class="col s3 "><span
                                                style="font-weight: bold"><?php echo $item->nombre ?></span></div>
                                    <div class="col s4 "><?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 s1">
                                        <button onclick="return confirm('<?php echo $text_competitor_inscription['competitor_inscription_confirm_delete']; ?>  <?php echo $item->dni; ?>')"
                                                class=" 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 $row_count++; ?>
                <?php if ($row_count == 0 || $row_count == 3 || $row_count == 6) { ?> </div> <?php } ?>

            <?php } ?>

        </div>

    </div>

</main>
<body>








</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);
            $('#idPesoC').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) {

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

<script>
    function validate() {
        $('#exists_dni').hide();
        $('#not_exists_dni').show();
        $('#dniCreateCompetitor').val($('#dni').val());

        <?php foreach ($dnis as $dni){?>
        if (parseInt($('#dni').val()) == parseInt("<?php echo $dni->dni?>")) { // Asi compara solo los digitos
            $('#exists_dni').show();
            $('#not_exists_dni').hide();

            $('#exists_nombre').text("<?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_nombre'];?> : <?php echo $dni->nombre?>")
            $('#exists_apellido1').text("<?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_surname']; ?> : <?php echo $dni->apellido1?>")
            $('#exists_apellido2').text("<?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_surname2']; ?> : <?php echo $dni->apellido2?>")
            $('#exists_comunidad').text("<?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_community']; ?> : <?php echo $dni->autonomia?>")
            $('#exists_provincia').text("<?php echo $text_competitor_inscription['competitor_inscription_modal_not_exists_province']; ?>: <?php echo $dni->provincia?>")
            $('#dniP').val("<?php echo $dni->dni?>");

        }
        <?php }?>
    }

    function notEmptyFieldsValidate() {
        if ($('#teamID').val() == null) {
            Materialize.toast("<?php echo $text_competitor_inscription['competitor_inscription_toast_choose_team']; ?>", 5000)
            return false;
        }


    }
    function notEmptyFieldsValidate2() {
        if ($('#teamIDcreate').val() == null) {
            Materialize.toast("<?php echo $text_competitor_inscription['competitor_inscription_toast_choose_team']; ?>", 5000)
            return false;
        }
        if ($('#dni').val() == "") {
            Materialize.toast("<?php echo $text_competitor_inscription['competitor_inscription_toast_enter_dni']; ?>", 5000)
            return false;
        }

    }
</script>