<?php
$bigSettingsMapper = new BigContact_Models_SettingsMapper();
if (!$bigSettings = $bigSettingsMapper->fetchAll())
    $bigSettings = new BigContact_Models_Settings();
else
    $bigSettings = $bigSettings[0];

$messages = array();
$message_type = '';

if (!empty($_POST)) {
    $bigSettings->setEmail_to(trim(strip_tags($_POST['email-to'])))
            ->setAppointTo(trim(strip_tags($_POST['email-appoint'])))
            ->setEmailSubject(trim(strip_tags($_POST['general-subject'])))
            ->setEmailMessage(trim(strip_tags($_POST['general-message'])))
            ->setAppointSubject(trim(strip_tags($_POST['appoint-subject'])))
            ->setAppointMessage(trim(strip_tags($_POST['appoint-message'])))
            ->setResponse(trim(strip_tags($_POST['response'])))
            ->setReplyEmail(trim(strip_tags($_POST['response-reply'])))
            ->setReplySubject(trim(strip_tags($_POST['response-subject'])))
            ->setReplyMessage(trim(strip_tags($_POST['response-message'])))
            ->setGapiKey(trim(strip_tags($_POST['gapiKey'])))
            ->setJQueryUiPath(trim(strip_tags($_POST['jQuery-ui-style'])));
    if (!$bigSettingsMapper->save($bigSettings)) {
        $messages[] = 'No changes made';
        $message_type = 'update';
    } else {
        $messages[] = 'Options updated';
        $message_type = 'update';
    }
}
$jQuery_styles = Functions_GetJQueryUiStyles();
?>

<div class="wrap">
    <?php $title = __('Big Contact Settings') ?>
    <div id="icon-options-general" class="icon32"><br></div>
    <h2><?php echo $title . ' '; ?></h2>
    <?php echo Functions_GetMessages($messages, $message_type) ?>
    <form method="post" action="?page=big-contact-settings">
        <h3>General Email Settings:</h3>
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><label for="email-to">General E-mail To</label><br /><span class="description">Who should receive the e-mails? Enter each address on a new line</span></th>
                <td><textarea name="email-to" id="email-to" class="regular-text" cols="55" rows="3"><?php _e($bigSettings->getEmailTo()) ?></textarea></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="email-appoint">Appointment E-mail To</label><br /><span class="description">Who should receive the Appointment e-mails? Enter each address on a new line</span></th>
                <td><textarea name="email-appoint" id="email-appoint" class="regular-text" cols="55" rows="3"><?php _e($bigSettings->getAppointTo()) ?></textarea></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="general-subject">General Email Subject</label></th>
                <td><input type="text" name="general-subject" id="general-subject" class="regular-text" value="<?php _e($bigSettings->getEmailSubject()) ?>"/></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="appoint-subject">Appointment Email Subject</label></th>
                <td><input type="text" name="appoint-subject" id="appoint-subject" class="regular-text" value="<?php _e($bigSettings->getAppointSubject()) ?>"/></td>
            </tr>
        </table>
        <h3>Appointment Response Settings:</h3>
        <table class="form-table">
            <tr valign="top">
                <th scope="row">Send Reply E-mail</th>
                <td><label for="response"><input type="checkbox" name="response" id="response" <?php if ($bigSettings->getResponse() == 'on') echo 'checked' ?> /> Send confirmation email on appointment request.</label></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="response-reply">Reply E-mail</label><br /><span class="description">example: no-reply@company.com</span></th>
                <td><input type="text" name="response-reply" id="response-reply" class="regular-text" value="<?php _e($bigSettings->getReplyEmail()) ?>"/></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="response-subject">Subject</label></th>
                <td><input type="text" name="response-subject" id="response-subject" class="regular-text" value="<?php _e($bigSettings->getReplySubject()) ?>"/></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="response-message">Message Template</label>
                    <br /><span class="description">[name] = clients name<br />
                        [email] = clients email<br /> [phone] = clients phone<br />
                        [extra] = clients extra<br /> [message] = clients message<br />
                        [date] => appointment date</span></th>
                <td><textarea name="response-message" id="response-message" class="regular-text" cols="55" rows="9"><?php _e($bigSettings->getReplyMessage()) ?></textarea></td>
            </tr>
        </table>
        <h3>UI Settings:</h3>
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><label for="jQuery-ui-style">Select Calendar Style</label></th>
                <td><select name="jQuery-ui-style" id="jQuery-ui-style" class="regular-text">
                        <?php foreach ($jQuery_styles as $jQuery_style): ?>
                            <option value="<?php echo $jQuery_style->getPath() ?>"
                            <?php if ($bigSettings->getJQueryUiPath() == $jQuery_style->getPath()): ?>
                                        selected="selected"
                                    <?php endif; ?>>
                                        <?php echo $jQuery_style->getName() ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
        </table>
        <h3>Google Maps Api Settings:</h3>
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><label for="gapiKey">Enter your Google Maps API key</label></th>
                <td><input type="text" name="gapiKey" id="gapiKey" class="regular-text" value="<?php _e($bigSettings->getGapiKey()) ?>"/><br/>
                    <span class="description">To create your API key:<br /><br />

                        1. Visit the APIs Console at <a href="https://code.google.com/apis/console">https://code.google.com/apis/console</a> and log in with your Google Account.<br />
                        2. Click the Services link from the left-hand menu.<br />
                        3. Activate the Google Maps API v3 service.<br />
                        4. Click the API Access link from the left-hand menu. Your API key is available from the API Access page, in the Simple API Access section. Maps API applications use the Key for browser apps.</span></td>
            </tr>
        </table>
        <p class="submit"><input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes"  /></p></form>

</div>