<?php
    require_once('common/header.php');

    if(!empty($_GET['table'])){
        $tableName = $_GET['table'];
    }else{
        $session->message('<strong>Please select a table.</strong>');
        redirect_to("dashboard.php?errorPage=list.php");
    }

    $table = new Table($tableName);
    $totalRows  = $table->countAllRows();//Total number of entries
    //Pagination
    $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
    $perPage = PER_PAGE;
    $maxPages = ceil($totalRows/$perPage);//maximum number of pages that could be returned
    $pagination = new Pagination($page,$perPage,$totalRows);

    $allRows = $table->listAllRows($pagination->offset(),$perPage);

    $childTableInformation = $table->getChildTableInformation();
    //If no data is found, redirect to add new data
    if(empty($allRows) && !isset($_GET['action']) ){
        redirect_to("generate.php?table=".$tableName);
    }elseif(empty($allRows) && isset($_GET['action']) && $_GET['action'] == 'search'){
        $session->message('<strong>No search results match your query.</strong>');
        redirect_to("list.php?table=".$tableName);
    }
    $allFields = $table->getTableFields();

    //$columnsCount = count($allFields);
    //bug fixed:
    $columnsCount = $table->findSql("SELECT COUNT(id) AS COUNT FROM system WHERE table_name = '".unhashTable($tableName)."' AND is_visible = 1");
    $columnsCount = $columnsCount[0]['COUNT'];

    //Delete IDs
    if(!empty($_POST['delete'])){
        $table->deleteRows($_POST['ids']);
        die;

    }

    $tableInfo = $table->getTableInfo();
?>
<?php
    /*
    IMAGE POP
    */
    include(ADMIN_PATH.DS."plugins".DS."imagepop.php");
?>

<script type="text/javascript" src="<?php echo ADMIN_PATH_HTML;?>/js/qtip/qtip.js"></script>
<link rel="stylesheet" href="<?php echo ADMIN_PATH_HTML;?>/js/qtip/qtip.css" type="text/css" />

<?php include(ADMIN_PATH.DS."common/listForm.php"); ?>

<!-- Advanced Search -->
<div class="advanced-search" style="display: none;">
    <form name="advancedSearch" method="post"  style="background: #001072; width: 400px; padding:10px;" action="<?php echo pageLink('list.php?table='.$tableName.'&action=search'); ?>">
        <fieldset>
            <legend><strong>Advanced Search</strong></legend>
            <?php 

                foreach($tableInfo as $field) :


                    if(in_array($field['field_type'], array ('auto_date','position','id','photo_upload','thumbnail','pdf_upload','mp3_upload'))){
                        continue;   
                    }
                ?>

                <div class="field">
                    <label class="input-control text"><strong><?php echo printTableName($field['field_name']); ?></strong>
                        <?php 
                            switch($field['field_type']){
                                case 'checkbox':
                                    $element = new FormElement($table->getRawTableName(), $field['field_name'] );
                                    $fieldInfo = $element->getFieldInfo();
                                    echo '<select '.$fieldInfo.'>';
                                    echo '<option value="">Any</option>';
                                    echo '<option value="1">Checked</option>';
                                    echo '<option value="0">Unchecked</option>';
                                    echo '</select>';
                                    break;
                                default:
                                    echo $table->getFormElement($field['field_name']);
                                    break;   
                            }

                        ?>
                    </label>
                </div>

                <?php endforeach; ?>
            <input type="submit" name="submit" value="Search" />
            <input type="reset" name="reset" value="Reset" />
        </fieldset>
    </form>
</div>
<!-- END Advanced Search --> 

<?php if($table->isCrudEnabled()) :  ?>
    <div class="modal hide"></div>
    <div class="hide shadow round bulkEditContainer" id="bulkEditContainer">
        <div class="closeBulkEditBtn close">x</div>
        <div class="title">
            <h3>Edit Multiple entries</h3>
            <div class="result"></div>
        </div>
        <form action="actions/bulkEdit.php" method="post" name="bulkEditForm" class="bulkEditForm">
            <input type="hidden" name="token" value="<?php echo CSRF::generateToken(); ?>" />
            <div class="highlight input-control select">
                <label for="cmsgen_rowsToEdit">Select the rows you want to edit:</label><br />
                <select name="cmsgen_rowsToEdit[]" multiple="multiple" id="cmsgen_rowsToEdit" class="fullWidth">
                    <?php
                        $displayField = $table->getDisplayField();
                        foreach($allRows as $row) : ?>
                        <option value="<?php echo $row['id']; ?>"><?php echo $row['id'] .' | '.$row[$displayField]; ?></option>
                        <?php endforeach; ?>
                </select>
            </div>


            <?php
                //Loop through the current table's fields and output the corresponding form
                foreach($table->returnFieldsNamesTypes() as $fieldName => $fieldTypeLength){
                    $fieldType = $table->getFieldType($fieldName);
                    if(in_array($fieldName,array('id','pos')) || in_array($fieldType,array('photo_upload','thumbnail','textarea','mp3_upload','auto_date'))){
                        continue;
                    }


                    echo'<div class="field"><label class="input-control text"><div class="label '. ($table->isRequired($fieldName) ? 'required' : '' ) .'">';

                    $tooltip = $table->getTooltip($fieldName);

                    echo printTableName($fieldName);
                    echo'</div>';
                    echo $table->getFormElement($fieldName); //Displays the corresponding form element to the following field
                    if(!empty($tooltip))
                        echo '<span title="'.$tooltip.'" class="tooltipMessage"></span>';

                    echo '</label></div>';
                    echo '<br />';

                }
            ?>
            <br />
            <button class="submit round drop_shadow saveBulkEdit" style="float:left">Save</button>
            <input class="submit round drop_shadow"  type="reset" value="Reset" style="float:left;margin-left:4px;">
            <button class="submit round drop_shadow closeBulkEditBtn"  value="Close" style="float:left;margin-left:4px; background: red;">Close</button>
            <input type="hidden" value="<?php echo $tableName;?>" name="table" />
        </form>
    </div>
    <?php endif; ?>

<?php require_once('common/footer.php'); ?>