<?php
if (!class_exists('nc_core')) {
    die;
}
?>
<link rel='stylesheet' type='text/css' media='screen' href="<?php echo $js_path . "css/jQuery.Tree.css" ?>">
<script src="<?php echo $js_path . "jquery-migrate-1.2.1.min.js" ?>"></script>
<script src="<?php echo $js_path . "jQuery.Tree.js" ?>"></script>

<div class="nc_admin_fieldset_head"><?= !empty($banner_id) ? NETCAT_MODULE_SERVICES_DIRECT_BANNER_EDIT : NETCAT_MODULE_SERVICES_DIRECT_BANNER_ADD; ?></div>
<?php
$form = $ui->form("?controller=$controller_name")->vertical();
$form->add()->input('hidden', 'action', 'save_banner');
$form->add()->input('hidden', 'data[CampaignID]', !empty($campaign_id) ? $campaign_id : 0);
$form->add()->input('hidden', 'data[BannerID]', !empty($banner_id) ? $banner_id : 0);
$form->add()->input('hidden', 'data[Geo]', !empty($banner['Geo']) ? $banner['Geo'] : "");

$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_REGIONS)->horizontal()->div()->attr('class', 'geo_str');
$form->add_row()->horizontal()->div(nc_services_admin_helpers::get_regions_tree_html($regions, !empty($banner['GeoArr']) ? $banner['GeoArr'] : "", true));

$form->add_row()->horizontal()
  ->div("<div rel='" . NETCAT_MODULE_SERVICES_DIRECT_BANNER_DEFAULT_TITLE . "' class='title'></div>"
    . "<div rel='" . NETCAT_MODULE_SERVICES_DIRECT_BANNER_DEFAULT_TEXT . "' class='text'></div>"
    . "<div rel='" . NETCAT_MODULE_SERVICES_DIRECT_BANNER_DEFAULT_HREF . "' class='href'></div>")->attr('class', 'banner_preview');

$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_TITLE)->horizontal()
  ->string('data[Title]', $banner['Title'])->style('width: 345px;')
;

$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_TEXT)->horizontal()
  ->textarea('data[Text]', $banner['Text'])->attr('class', 'no_cm textarea')->style('width: 300px; display: inline;');

$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_HREF_FORM)->horizontal()
  ->span(NETCAT_MODULE_SERVICES_DIRECT_BANNER_HREF_PREFIX . "&nbsp;")
  ->string('data[Href]', $banner['Href']);

$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_KEYWORDS)->horizontal()
  ->textarea('data[Phrases]', $banner['Phrases'])->attr('class', 'no_cm textarea');

$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_MINUS_WORDS)->horizontal()
  ->textarea('data[MinusKeywords]', $banner['MinusKeywords'])->attr('class', 'no_cm textarea');
$form->add_row(NETCAT_MODULE_SERVICES_DIRECT_BANNER_MINUS_WORDS_HINT)->horizontal();

?>
<div><?php echo $form; ?></div>
<script>
    (function() {
        $nc("#tree").Tree(setRegions);

        function getRegions(selector) {
            var regions = [];
            $nc(selector).each(function() {
                regions.push($nc(this).val());
            });
            return regions;
        }
        function getRegionsStr(selector) {
            var regions_str = [];
            $nc(selector).each(function() {
                regions_str.push($nc(this).parent().text());
            });
            return regions_str;
        }

        function updatePreview() {
            $nc('.banner_preview .title').html($nc('input[name="data[Title]"]').val().length ? $nc('input[name="data[Title]"]').val() : $nc('.banner_preview .title').attr('rel'));
            $nc('.banner_preview .text').html($nc('textarea[name="data[Text]"]').val().length ? $nc('textarea[name="data[Text]"]').val() : $nc('.banner_preview .text').attr('rel'));
            var href = $nc('input[name="data[Href]"]').val();
            if (href.length && href.indexOf('/') > -1) {
                href = href.substr(0, href.indexOf('/'));
            }
            $nc('.banner_preview .href').html(href.length ? href : $nc('.banner_preview .href').attr('rel'));
        }
        function setRegions() {
            $nc('input[name="data[Geo]"]').val(getRegions('#tree :checked').join(','));
        }
        function initForm() {
//            $nc('.geo_str').html(getRegionsStr('#tree :checked').join(','));
            
            updatePreview();
            $nc('textarea[name="data[Text]"]').after('<span class="counter" rel=75></span>');
            $nc('input[name="data[Title]"]').after('<span class="counter" rel=33></span>');

            $nc('input[name="data[Title]"], textarea[name="data[Text]"]').keypress(function() {
                changeInputLimit($nc(this), $nc(this).next());
            }).keyup(function() {
                changeInputLimit($nc(this), $nc(this).next());
            }).keydown(function() {
                changeInputLimit($nc(this), $nc(this).next());
            }).each(function() {
                changeInputLimit($nc(this), $nc(this).next());
            });
            $nc('input[name="data[Href]"]').keypress(function() {
                updatePreview();
            }).keyup(function() {
                updatePreview();
            }).keydown(function() {
                updatePreview();
            }).each(function() {
                updatePreview();
            });

        }

        function changeInputLimit(obj, counter) {
            var val = counter.attr('rel') - obj.val().length;
            if (val >= 0) {
                updatePreview();
            }
            counter.html(val > 0 ? val : 0);
            if (val <=0 ) {
                obj.val(obj.val().substr(0, counter.attr('rel')));
            }
        }
        initForm();
    })();
</script>
