<?php
/*
 * 
 */

namespace TodoList;

use \TodoList\Model\Todo;
use \TodoList\Util\Utils;
use \TodoList\Validation\ValidationError;

//~ Template for add-edit.php
// variables:
//  $errors - validation errors
//  $todo - submitted TODO
//  $edit - true for EDIT, false for ADD

function error_field($title, array $errors) {
    foreach ($errors as $error) {
        /* @var $error ValidationError */
        if ($error->getSource() == $title) {
            return ' error-field';
        }
    }
    return '';
}

/* @var $todo Todo */
?>

<h1>
    <?php if ($edit): ?>
        <?php echo Utils::escape($todo->getTitle()); ?>
    <?php else: ?>
        <i class="material-icons new">insert_invitation</i>
        Add new TODO
    <?php endif; ?>
</h1>

<?php if (!empty($errors)): ?>
<ul class="errors">
    <?php foreach ($errors as $error): ?>
        <?php /* @var $error ValidationError */ ?>
        <li><?php echo $error->getMessage(); ?></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

<form action="#" method="post">
    <fieldset>
        <div class="field">
            <label>Title:</label>
            <input type="text" name="todo[title]" value="<?php echo Utils::escape($todo->getTitle()); ?>"
                   class="text<?php echo error_field('title', $errors); ?>"/>
        </div>
        <div class="field">
            <label>Due On:</label>
            <input type="text" name="todo[due_on_date]" value="<?php echo Utils::escape($todo->getDueOn()->format('Y-n-j')); ?>"
                   class="text datepicker<?php echo error_field('due_on', $errors); ?>" />
            &nbsp;
            <select name="todo[due_on_hour]">
            <?php for ($i = 0; $i < 24; ++$i): ?>
                <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
                <option value="<?php echo $hour; ?>"
                        <?php if ($todo->getDueOn()
                                && $hour == $todo->getDueOn()->format('H')): ?>
                            selected="selected"
                        <?php endif; ?>
                        ><?php echo $hour; ?></option>
            <?php endfor; ?>
            </select>
            :
            <select name="todo[due_on_minute]">
            <?php for ($i = 0; $i < 60; $i = $i + 15): ?>
                <?php $minute = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
                <option value="<?php echo $minute; ?>"
                        <?php if ($todo->getDueOn()
                                && $minute == $todo->getDueOn()->format('i')): ?>
                            selected="selected"
                        <?php endif; ?>
                        ><?php echo $minute; ?></option>
            <?php endfor; ?>
            </select>
        </div>
        <div class="field">
            <label>Priority:</label>
            <select name="todo[priority]">
            <?php foreach (Todo::allPriorities() as $priority): ?>
                <option value="<?php echo $priority; ?>"
                        <?php if ($priority == $todo->getPriority()): ?>
                            selected="selected"
                        <?php endif; ?>
                        ><?php echo $priority; ?></option>
            <?php endforeach; ?>
            </select>
        </div>
        <div class="field">
            <label>Description:</label>
            <textarea name="todo[description]" cols="1" rows="1"
                      class="<?php echo error_field('description', $errors); ?>"><?php echo Utils::escape($todo->getDescription()); ?></textarea>
        </div>
        <div class="field">
            <label>Comment:</label>
            <textarea name="todo[comment]" cols="1" rows="1"
                      class="<?php echo error_field('comment', $errors); ?>"><?php echo Utils::escape($todo->getComment()); ?></textarea>
        </div>
        <div class="wrapper">
            <input type="submit" name="cancel" value="CANCEL" class="submit" />
            <input type="submit" name="save" value="<?php echo $edit ? 'EDIT' : 'ADD'; ?>" class="submit" />
        </div>
    </fieldset>
</form>
