<div class="wrap">

    <h1><?php echo e($__page->get('title')); ?></h1>

    <?php

    if ( isset( $_GET['journal'] ) ) {

        $evenements = EvenementModel::findByJournal( $_GET['journal'] );
        $articles   = ArticleModel::findByJournal( $_GET['journal'] );

        $evenements = array_merge( $evenements, $articles );

        if( $evenements ){

            ?>
            <div class="block-evenements">
                <form action="" method="post">
                <input type="hidden" name="mode" value="download-evenements-zip" />
                <h2>Liste des événements &amp; articles</h2>
                <table width="100%" class="tablesorter evenements">
                <thead>
                    <tr>
                        <th class="checkbox" data-sorter="false" >
                            <input type="checkbox" name="select-all-evenements" checked/>
                        </th>
                        <th class="titre"><?php _e("Titre", "farandole"); ?></th>
                        <th class="type"><?php _e("Type", "farandole"); ?></th>
                        <th class="rubrique"><?php _e("Rubrique", "farandole"); ?></th>
                        <th class="alaune"><?php _e("Mettre à la une", "farandole"); ?></th>
                        <th class="photo"><?php _e("Photo", "farandole"); ?></th>
                        <th class="ville"><?php _e("Ville", "farandole"); ?></th>
                        <th class="caracteres"><?php _e("Caractères", "farandole"); ?></th>
                        <th class="caracteres"><?php _e("Publié sur le web", "farandole"); ?></th>
                        <th class="caracteres"><?php _e("Statut de publication", "farandole"); ?></th>
                        <th class="actions" data-sorter="false"></th>
                    </tr>
                </thead>
                <tbody>
                <?php

                foreach( $evenements as $index => $evenement ){

                    $onweb = get_field('onweb', $evenement->ID);

                    $rubriques = array();
                    $journalalaune = false;
                    $journalshowpicture = false;

                    $data = EvenementModel::getJournalDataBy( $evenement->ID, $_GET['journal'] );

                    if( $data ){

                        $journalalaune = $data['onjournal_alaune'];
                        $journalshowpicture = $data['onjournal_showpicture'];
                        $rubrique = get_term( $data['onjournal_rubriques'], "evenements-rubriques" );
                        $rubriques = array( $rubrique->name );

                    }

                    $lieux = get_field('lieu', $evenement->ID);
                    $ville = "";

                    if( $lieux ){

                        $ville = get_field('ville', $lieux[0]);
                        $ville = "<a href=\"" . get_edit_post_link( $ville ) . "\" title=\"Editer la ville : " . esc_attr( get_the_title( $ville ) ) . "\">" . get_the_title( $ville ) . "</a>";

                    }

                    ?><tr class="row-evenement">
                        <td>
                            <input type="checkbox" name="evenements[]" value="<?php echo $evenement->ID; ?>" checked/>
                        </td>
                        <td>
                            <a href="<?php echo get_edit_post_link( $evenement->ID ); ?>" target="_blank"><?php echo get_the_title( $evenement->ID ); ?></a>
                        </td>
                        <td>
                           <?php echo $evenement->post_type; ?>
                        </td>
                        <td><?php echo implode(", ", $rubriques); ?></td>
                        <td><?php echo ( ($journalalaune) ? 'Oui' : 'Non' ); ?></td>
                        <td><?php echo ( ($journalshowpicture) ? 'Oui' : 'Non' ); ?></td>
                        <td><?php echo $ville; ?></td>
                        <td>
                            <?php echo $evenement->journalContentCaracteres; ?>
                        </td>
                        <td><?php echo ( ($onweb) ? 'Oui' : 'Non' ); ?></td>
                        <td><?php  switch($evenement->post_status){
                                            case 'publish' :
                                            _e("Publié", "farandole");
                                            break;
                                            case 'draft' :
                                            _e("Brouillon", "farandole");
                                            break;
                                            case 'auto-draft' :
                                            _e("Brouillon", "farandole");
                                            break;
                                            case 'future':
                                            _e("Publication à venir", "farandole");
                                            break;
                                            case 'demander-modif-ev' :
                                            _e("Demande de modification en cours", "farandole");
                                            break;
                                            default :
                                            echo $evenement->post_status;
                                            break;
                        }?></td>
                        <td>
                            <a href="#ajax" data-evenement="<?php echo $evenement->ID; ?>" data-function="info"><i class="fa fa-info-circle fa-2x" aria-hidden="true"></i></a>
                        </td>
                    </tr><?php

                }

                ?>
                </tbody>
                </table>
                <input type="submit" name="publish" id="publish" class="button button-primary button-large" value="Exporter les évènements sélectionnés">
                </form>
            </div>
            <?php

        }

    }

    ?>

</div>
