<?php
$factors = $this->optimization->getDisplayData();
$naFactorHelps = array(
    'ImgNameContain' => 'image',
    'ImgAltContain' => 'image',
    'RelatedTerms' => 'terms',
    'H1Contain' => 'heading',
    'H2Contain' => 'heading',
    'H3Contain' => 'heading',
    'DecorationBold' => 'decoration',
    'DecorationItalic' => 'decoration',
    'DecorationUnderlined' => 'decoration',
    'LinkToHome' => 'decoration',
    'LinkToPost' => 'decoration',
);
$richMessages = array(
    'Error gettng related terms: You are not subscribed to the Bing API service' =>
        'Error gettng related terms: You are not subscribed to the Bing API service.<br />
        <a href="http://www.georgekatsoudas.com/contact/knowledgebase.php?article=66" target="_blank">View the solution</a>.',
);
$extraContentModeOptions = Ocs_Application::getService('Optimization')
    ->getExtraContentModeOptions(TRUE);
?>
<div id="ocs-help-bkg" style="display: none;"></div>

<div id="ocs_metabox">
    <input type="hidden" id="ocs_metabox_save" name="ocs_metabox_save" value="1" />

    <ul class="ocs_metabox-message">
        <?php foreach ($this->errors as $error): ?>
            <li class="error">
                <?php if (isset($richMessages[$error])): ?>
                    <?php echo $richMessages[$error]; ?>
                <?php else: ?>
                    <?php echo $this->escape($error); ?>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
    <div class="ocs_metabox-form ocs_metabox-form-top">
        <div class="ocs_metabox-field" id="ocs_keyword-field">
            <label class="ocs_metabox-label" id="ocs_keyword-label" for="ocs_keyword">Keyword: </label>
            <input type="text" id="ocs_keyword" name="ocs_keyword" class="ocs_input" value="<?php echo $this->escape($this->form->getValue('keyword')); ?>" />
        </div>
    </div>
    <ul id="ocs_metabox-input-factors">
        <li>
            <label for="ocs_metabox-factor-all" id="ocs_metabox-factor-all-label">
                <input id="ocs_metabox-factor-all" name="ocs_metabox-factor-all"
                    type="checkbox"
                />
                <span>(Check/Uncheck All)</span>
            </label>
        </li>
        <?php foreach ($factors as $data): ?>
            <?php
                $attributes = array();

                if ($data['selected']) {
                    $attributes['checked'] = 'checked';
                }

                $forceSelected = FALSE;
                if (!$data['configured']) {
                    $status = 'none';
                    unset($attributes['checked']);
                    $attributes['disabled'] = 'disabled';
                } else if (is_null($data['value'])) {
                    $status = 'none';
                } else if (Ocs_Service_Optimization::STATUS_NA == $data['value']) {
                    $status = 'na';
                    //$attributes['disabled'] = 'disabled';
                } else if (Ocs_Service_Optimization::STATUS_NO == $data['value']) {
                    $status = $data['selected']? 'yes' : 'no';
                } else if (Ocs_Service_Optimization::STATUS_YES == $data['value']) {
                    $status = 'yes';
                    $attributes['disabled'] = 'disabled';
                    $attributes['checked'] = 'checked';
                    $forceSelected = TRUE;
                } else {
                    $status = 'none';
                }
            ?>
            <li class="ocs_metabox-factor ocs_metabox-factor_<?php echo $status; ?>">
                <label class="ocs_metabox-factors_bool"
                     for="ocs_metabox-factor-<?php echo $this->escape($data['name']); ?>"
                >
                    <?php if ($forceSelected): ?>
                        <input type="hidden" name="ocs_metabox-factor[]"
                            value="<?php echo $this->escape($data['name']); ?>"
                            id="ocs_metabox-factor-force-<?php echo $this->escape($data['name']); ?>"
                            class="ocs_metabox-factor-force"
                        />
                    <?php endif; ?>
                    <input type="checkbox"
                        name="ocs_metabox-factor[]"
                        id="ocs_metabox-factor-<?php echo $this->escape($data['name']); ?>"
                        value="<?php echo $this->escape($data['name']); ?>"
                        <?php foreach ($attributes as $name=>$val): ?>
                            <?php echo $name; ?>="<?php echo $val; ?>"
                        <?php endforeach; ?>
                    />
                    <span>
                        <?php echo $this->escape($data['label']); ?>
                        <?php if (!$data['configured']): ?>
                            (<a href="<?php echo admin_url('admin.php?page=' . Ocs_WpPlugin::SLUG . '-options'); ?>">configure</a>)
                        <?php endif; ?>
                    </span>
                </label>
                <?php if (Ocs_Service_Optimization::STATUS_NA == $data['value'] && $data['configured'] && isset($naFactorHelps[$data['name']])): ?>
                    (<a href="#" class="ocs-help-link" data-identifier="na-<?php echo $naFactorHelps[$data['name']]; ?>">?</a>)
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>

     <div class="ocs_metabox-form ocs_metabox-form-bottom">
        <div class="ocs_metabox-field" id="ocs_extra_content_mode-field">
            <label class="ocs_metabox-label" id="ocs_extra_content_mode-label" for="ocs_extra_content_mode">
                Extra Content Keywords:
                <span class="ocs-help-link-wrap">(<a href="#" class="ocs-help-link" data-identifier="extra_content_mode">help</a>)</span>
            </label>
            <fieldset id="ocs_extra_content_mode">
                <legend class="screen-reader-text"><span>Extra Content Keywords:</span></legend>

                <?php $value = $this->form->getValue('extra_content_mode'); ?>

                <?php $id = "ocs_extra_content_mode-option-default"; ?>
                <label id="<?php echo $id; ?>-label"
                    for="<?php echo $id; ?>"
                    class="ocs-radio"
                >
                    <input type="radio"
                        id="<?php echo $id; ?>"
                        name="ocs_extra_content_mode"
                        value=""
                        <?php if ('' == $value): ?>
                            checked="checked"
                        <?php endif; ?>
                    />
                    <span>
                        use general setting<br />
                        <em>(<?php echo $this->escape($extraContentModeOptions[Ocs_Application::getModel('Options')->getValue('extra_content_mode')]); ?>)</em>
                    </span>
                </label>

                <?php foreach ($extraContentModeOptions as $key=>$label): ?>
                    <?php $id = "ocs_extra_content_mode-option-{$key}"; ?>
                    <label id="<?php echo $id; ?>-label"
                        for="<?php echo $id; ?>"
                        class="ocs-radio"
                    >
                        <input type="radio"
                            id="<?php echo $id; ?>"
                            name="ocs_extra_content_mode"
                            value="<?php echo $key; ?>"
                            <?php if ($key == $value): ?>
                                checked="checked"
                            <?php endif; ?>
                        />
                        <span>
                            <?php echo $this->escape($label); ?>
                        </span>
                    </label>
                <?php endforeach; ?>
            </fieldset>
        </div>
    </div>

    <div class="ocs_metabox-button_update-container">
        <img class="ajax-loading" id="ocs_metabox-update_ajax_loader" src="<?php echo esc_url(admin_url('images/wpspin_light.gif')); ?>" alt="Loading..." width="16" height="16" />
        <button type="button" class="button-primary ocs_metabox-button_update">Optimize &amp; Update</button>
    </div>
    <br class="clear" />
    <div class="ocs_metabox-links">
        <a href="admin.php?page=<?php echo Ocs_WpPlugin::SLUG; ?>">Mass&nbsp;Optimization&nbsp;Screen</a>
        <br />
        <a href="#" class="ocs-help-link" data-identifier="main">Instructions</a>
        |
        <a href="<?php echo Ocs_WpPlugin::PLUGIN_BASE_URL; ?>updates.html" target="_blank">Updates</a>
        |
        <a href="<?php echo Ocs_WpPlugin::PLUGIN_BASE_URL; ?>affiliates.html" target="_blank">Affiliates</a>
        <br />
        <a href="<?php echo Ocs_WpPlugin::AUTHOR_BASE_URL; ?>all-our-plugins" target="_blank">All&nbsp;Our&nbsp;Plugins</a>
        |
        <a href="<?php echo Ocs_WpPlugin::PLUGIN_BASE_URL; ?>contact.html" target="_blank">Contact&nbsp;Us</a>
    </div>
</div>

<?php
echo $this->partial('_help/popup.phtml', array(
    'folder' => 'metabox',
    'identifier' => 'main',
    'title' => Ocs_WpPlugin::TITLE,
));

foreach (array_unique($naFactorHelps) as $helpId) {
    echo $this->partial('_help/popup.phtml', array(
        'folder' => 'metabox',
        'identifier' => 'na-' . $helpId,
        'title' => "Unable to satisfy this factor",
    ));
}

// Extra content options
echo $this->partial('_help/popup.phtml', array(
    'folder' => 'metabox',
    'identifier' => 'extra_content_mode',
    'title' => 'Extra Content Keywords:',
));