<?php if ( count($this->templateOptions ) == 0 ) { ?>
<div class="alert alert-danger">
    <strong>
        <a href="index.php?fuse=admin&controller=settings&view=emailtemplates&settings=mail">
            <?php echo $this->user->lang('No email templates are defined.  Please create one here'); ?>
        </a>
    </strong>
</div>
<?php } ?>

<div class="mb-3">
    <label for="inputName" class="form-label fw-semibold">
        <?php echo $this->user->lang('Event Name'); ?>:
    </label>
    <input
        name="name"
        id="inputName"
        type="text"
        class="form-control required"
        value="<?php echo $this->name; ?>"
    />
</div>

<div class="mb-3">
    <label for="selectTemplateTarget" class="form-label fw-semibold">
        <?php echo $this->user->lang('Email Template'); ?>:
    </label>
    <select
        id="selectTemplateTarget"
        name="templateid"
        class="form-select required"
    >
        <?php foreach ($this->templateOptions as $option) { ?>
            <option value="<?php echo $option['id']; ?>">
                <?php echo $option['text']; ?>
            </option>
        <?php } ?>
    </select>
</div>

<div class="mb-3">
    <label for="selectActionTarget" id="labelAction" class="form-label fw-semibold">
        <?php echo $this->user->lang('Action'); ?>:
    </label>
    <select
        id="selectActionTarget"
        name="actionid"
        class="form-select required"
    >
        <?php foreach ($this->actionOptions as $option) { ?>
            <option value="<?php echo $option['id']; ?>">
                <?php echo $option['text']; ?>
            </option>
        <?php } ?>
    </select>
</div>

<div class="mb-3 fromDiv">
    <label for="fromName" class="form-label fw-semibold">
        <?php echo $this->user->lang('Custom From Name'); ?>:
    </label>
    <input
        name="fromName"
        id="fromName"
        type="text"
        class="form-control"
        value="<?php echo $this->fromName; ?>"
    />
</div>

<div class="mb-3 fromDiv">
    <label for="fromEmail" class="form-label fw-semibold">
        <?php echo $this->user->lang('Custom From Email'); ?>:
    </label>
    <input
        name="fromEmail"
        id="fromEmail"
        type="text"
        class="form-control"
        value="<?php echo $this->fromEmail; ?>"
    />
</div>

<hr class="my-3">
<div class="fw-bold"><?php echo $this->user->lang("Rules defining this notification");?></div>
<table>
    <tr>
        <td>
            <input style="margin-right:5px;" type="checkbox" name="buildCustomRule" id="buildCustomRule" onclick="manageRules(this.checked);" <?php echo $this->buildCustomRule? 'checked':''; ?> /><?php echo $this->user->lang('Build custom rule');?>&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
        <td>
            <label for="overrideOptOut" id="labelOverrideOptOut" style="width: 140px;" data-bs-toggle="tooltip" title="<?php echo $this->user->lang('Use this to force the emailing of this notification to clients who have opted out of announcements.');?>" data-bs-placement="right">
                <input style="margin-right:5px;" type="checkbox" name="overrideOptOut" id="overrideOptOut" <?php echo $this->overrideOptOut? 'checked':''; ?> />
                <span><?php echo $this->user->lang('Override Opt-Out?');?></span>
            </label>
        </td>
    </tr>
</table>

<div style="<?php echo ($this->buildCustomRule) ? "display:none;" : ""; ?>"  class="wrapper_match mb-3">
    <label style="display:block;" class="" for="selectMatchTarget"><?php echo $this->user->lang('Accounts will be notified if'); ?>:</label>
    <select id="selectMatchTarget" name="match" class="form-select required " style="width: 160px;">
        <?php foreach ( $this->matchOptions as $option ) { ?>
        <option value="<?php echo $option['id']; ?>"><?php echo $option['text']; ?></option>
        <?php } ?>
    </select>
</div>

<table class="table-borderless">
    <tbody id="systemEventTableBody">
        <tr id="systemEventRowTemplate" style="display:none;">
            <td>
                <div class="d-flex align-items-center gap-2 w-100 system-rule-row">
                    <select
                        name="systemEvent"
                        class="form-select required selectEventTarget"
                    >
                        <?php foreach ($this->systemRuleFieldOptions as $option) { ?>
                            <option value="<?php echo $option['id']; ?>">
                                <?php echo $option['text']; ?>
                            </option>
                        <?php } ?>
                    </select>

                    <select
                        name="systemOperator"
                        class="form-select required selectOperatorTarget"
                    >
                        <?php foreach ($this->systemOperatorOptions as $option) { ?>
                            <option value="<?php echo $option['id']; ?>">
                                <?php echo $option['text']; ?>
                            </option>
                        <?php } ?>
                    </select>

                    <input
                        name="systemValue"
                        type="text"
                        class="form-control required inputValue flex-grow-1"
                    />
                </div>
            </td>
        </tr>

    </tbody>
</table>




<span id="customEvent">
    <table border="0">
        <tbody id="eventTableBody" >
            <tr id="eventRowTemplate" style="display:none;">
                <td align="left">
                    <button class="btn btn-danger remove-button">-</button>
                    <select name="event[]" class="form-select required selectEventTarget disableSelect2AutoLoad" style="width: 300px;">
                        <optgroup label="Invoice Fields">
                        <?php foreach ( $this->customRuleInvoiceFieldOptions as $option ) { ?>
                            <option value="<?php echo $option['id']; ?>" data-date="<?php echo $option['date']? 1 : 0 ?>"><?php echo $option['text']; ?></option>
                        <?php } ?>
                        </optgroup>
                        <optgroup label="Package Fields">
                        <?php foreach ( $this->customRulePackageFieldOptions as $option ) { ?>
                            <option value="<?php echo $option['id']; ?>" data-date="<?php echo $option['date']? 1 : 0 ?>"><?php echo $option['text']; ?></option>
                        <?php } ?>
                        </optgroup>
                        <optgroup label="User Fields">
                        <?php foreach ( $this->customRuleUserFieldOptions as $option ) { ?>
                            <option value="<?php echo $option['id']; ?>" data-date="<?php echo $option['date']? 1 : 0 ?>"><?php echo $option['text']; ?></option>
                        <?php } ?>
                        </optgroup>
                    </select>

                    <select name="operator[]" class="form-select required selectOperatorTarget disableSelect2AutoLoad" style="width: 160px;">
                    <?php foreach ( $this->customOperatorOptions as $option ) { ?>
                        <option value="<?php echo $option['id']; ?>"><?php echo $option['text']; ?></option>
                    <?php } ?>
                    </select>

                    <select name="operator_dates[]" class="form-select required disableSelect2AutoLoad" style="display:none; width: 160px;">
                        <option value="<?php echo Notification::DATE_OPERATOR_WAS_EXACTLY ?>"><?php echo $this->user->lang('was exactly') ?></option>
                        <option value="<?php echo Notification::DATE_OPERATOR_WAS_LESS_THAN ?>"><?php echo $this->user->lang('was less than') ?></option>
                        <option value="<?php echo Notification::DATE_OPERATOR_WAS_MORE_THAN ?>"><?php echo $this->user->lang('was more than') ?></option>
                        <option value="<?php echo Notification::DATE_OPERATOR_WILL_OCCUR_IN ?>"><?php echo $this->user->lang('will occur exactly in') ?></option>
                        <option value="<?php echo Notification::DATE_OPERATOR_WILL_OCCUR_WITHIN ?>"><?php echo $this->user->lang('will occur within') ?></option>
                    </select>

                    <input name="value[]" class="form-control required inputValue d-inline-block"  style="width: 140px;" type="text" value="" />

                    <select name="operator_dates_units[]" class="form-select required disableSelect2AutoLoad" style="width: 100px;">
                        <option value="<?php echo Notification::DATE_UNIT_HOURS ?>"><?php echo $this->user->lang('hours') ?></option>
                        <option value="<?php echo Notification::DATE_UNIT_DAYS ?>"><?php echo $this->user->lang('days') ?></option>
                        <option value="<?php echo Notification::DATE_UNIT_WEEKS ?>"><?php echo $this->user->lang('weeks') ?></option>
                        <option value="<?php echo Notification::DATE_UNIT_MONTHS ?>"><?php echo $this->user->lang('months') ?></option>
                    </select>

                    <select name="invoice_sent[]" class="form-select required disableSelect2AutoLoad" style="width:200px; margin-bottom:10px">
                        <option value="0"><?php echo $this->user->lang('Not Sent') ?></option>
                        <option value="1"><?php echo $this->user->lang('Sent') ?></option>
                    </select>

                    <select name="invoice_status[]" class="form-select required disableSelect2AutoLoad" style="width:200px; margin-bottom:10px">
                        <?php /* these two correspond to INVOICE_STATUS_UNPAID AND INVOICE_STATUS_PARTIALLY_PAID */ ?>
                        <option value="<?php echo Notification::INVOICE_STATUS_NOT_PAID_PROCESSED ?>"><?php echo $this->user->lang('Failed') ?></option>
                        <option value="<?php echo Notification::INVOICE_STATUS_NOT_PAID ?>"><?php echo $this->user->lang('Not Paid') ?></option>

                        <option value="<?php echo INVOICE_STATUS_PAID  ?>"><?php echo $this->user->lang('Paid') ?></option>
                        <option value="<?php echo INVOICE_STATUS_VOID  ?>"><?php echo $this->user->lang('Void') ?></option>
                        <option value="<?php echo INVOICE_STATUS_REFUNDED  ?>"><?php echo $this->user->lang('Refunded') ?></option>
                        <option value="<?php echo INVOICE_STATUS_PENDING  ?>"><?php echo $this->user->lang('Pending') ?></option>
                        <option value="<?php echo INVOICE_STATUS_CREDITED  ?>"><?php echo $this->user->lang('Credited') ?></option>
                    </select>

                    <select name="user_status[]" class="form-select required disableSelect2AutoLoad" style="width:200px; margin-bottom:10px">
                        <?php $it = StatusAliasGateway::getInstance($this->user)->getUserAliases();
                            while ($status = $it->fetch()) {
                                echo '<option value="'.$status->statusid.'">'.$this->user->lang($status->name).'</option>';
                            }
                        ?>
                    </select>

                    <select name="package_status[]" class="form-select required disableSelect2AutoLoad" style="width:200px; margin-bottom:10px">
                        <?php $it = StatusAliasGateway::getInstance($this->user)->getPackageAliases();
                            while ($status = $it->fetch()) {
                                echo '<option value="'.$status->statusid.'">'.$this->user->lang($status->name).'</option>';
                            }
                        ?>
                    </select>

                    <span style="display:none" class="label ago label-ago-notifications"><?php echo $this->user->lang('ago') ?></span>
                </td>
            </tr>
        </tbody>
    </table>
    <button id="add-button" class="btn btn-danger mt-3" name="add">
        <i class="bi bi-plus-lg"></i>&nbsp;&nbsp;&nbsp;<?php echo $this->user->lang("Add new rule");?>
    </button>
</span>
<script data-cfasync="false" type="text/javascript">
    function manageRules(custom)
    {
        if(custom){
            $('.wrapper_match').show();
            $('#systemEventRowTemplate').hide();
            $("#customEvent").show();
        }else{
            $('.wrapper_match').hide();
            $("#customEvent").hide();
            $("#systemEventRowTemplate").show();
        }
    }

    $(document).ready(function(){
        var notifications = notifications || {};

        // --- Select2 v4 helpers ---
        // Make helpers global
        notifications.s2SetVal = function ($el, val) {
            if (!$el || !$el.length) return;
            $el.val(val).trigger('change');
        };

        notifications.s2Show = function ($el) {
            if (!$el || !$el.length) return;
            // v4: container is the .select2 sibling
            $el.next('.select2').show();
        };

        notifications.s2Hide = function ($el) {
            if (!$el || !$el.length) return;
            $el.next('.select2').hide();
        };

        notifications.s2Disable = function ($el) {
            if (!$el || !$el.length) return;
            $el.prop('disabled', true).trigger('change.select2');
        };

        notifications.s2Enable = function ($el) {
            if (!$el || !$el.length) return;
            $el.prop('disabled', false).trigger('change.select2');
        };

        var newEventIndex = 0;

        const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (tooltipTriggerEl) {
          return new bootstrap.Tooltip(tooltipTriggerEl);
        });

        function isIE() {
          const ua = navigator.userAgent;
          /* MSIE used to detect old browsers and Trident used to newer ones*/
          const is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;
          return is_ie;
        }

        function addEvent(object)
        {
            var newEventRow = document.getElementById("eventRowTemplate").cloneNode(true);
            newEventRow.setAttribute('id', 'filterRow_N' + newEventIndex);
            newEventRow.style.display = isIE()? 'block' : 'table-row';
            var newEventRow = document.getElementById("eventTableBody").appendChild(newEventRow);

            $('#filterRow_N' + newEventIndex + ' select').select2({
                minimumResultsForSearch: 10,
                width:'resolve',
                dropdownParent: $('.richwindow')
            });

            if ( typeof(object) !== 'undefined' ) {
                notifications.s2SetVal($("#filterRow_N" + newEventIndex + " select[name=event\\[\\]]"), object.event);

                var operator = $("#filterRow_N" + newEventIndex + " select[name=operator\\[\\]]");
                var operator_dates = $("#filterRow_N" + newEventIndex + " select[name=operator_dates\\[\\]]");
                var operator_dates_units = $("#filterRow_N" + newEventIndex + " select[name=operator_dates_units\\[\\]]");
                var invoice_sent = $("#filterRow_N" + newEventIndex + " select[name=invoice_sent\\[\\]]");
                var invoice_status = $("#filterRow_N" + newEventIndex + " select[name=invoice_status\\[\\]]");
                var user_status = $("#filterRow_N" + newEventIndex + " select[name=user_status\\[\\]]");
                var package_status = $("#filterRow_N" + newEventIndex + " select[name=package_status\\[\\]]");
                var value = $("#filterRow_N" + newEventIndex + " input[name=value\\[\\]]");
                var ago = $("#filterRow_N" + newEventIndex + " span.ago");

                notifications.s2SetVal(operator, object.operator);
                if (object.is_date) {
                    notifications.s2Hide(operator);
                    notifications.s2SetVal(operator_dates, object.operator_dates);
                    notifications.s2Show(operator_dates);
                    notifications.s2SetVal(operator_dates_units, object.operator_dates_units);
                    notifications.s2Show(operator_dates_units);
                    value.css('width', 70);
                    if (shouldShowAgo(object.operator_dates)) {
                        ago.show();
                    } else {
                        ago.hide();
                    }
                } else {
                    notifications.s2Show(operator);
                    notifications.s2Hide(operator_dates);
                    notifications.s2Hide(operator_dates_units);
                    value.css('width', 140);
                    ago.hide();
                }

                if (object.event == 'i_sent') {
                    notifications.s2SetVal(invoice_status, <?php echo Notification::INVOICE_STATUS_NOT_PAID_PROCESSED ?>);
                    notifications.s2Hide(invoice_status);
                    notifications.s2SetVal(user_status, <?php echo USER_STATUS_ACTIVE ?>);
                    notifications.s2Hide(user_status);
                    notifications.s2SetVal(package_status, <?php echo PACKAGE_STATUS_ACTIVE ?>);
                    notifications.s2Hide(package_status);
                    value.hide();

                    $("#filterRow_N" + newEventIndex + " input[name=value\\[\\]]").val('');
                    notifications.s2SetVal(invoice_sent, object.value);
                    notifications.s2Show(invoice_sent);
                    notifications.s2SetVal(operator, '=');
                    notifications.s2Disable(operator);
                } else if (object.event == 'i_status') {
                    notifications.s2SetVal(invoice_sent, 0);
                    notifications.s2Hide(invoice_sent);
                    notifications.s2SetVal(user_status, <?php echo USER_STATUS_ACTIVE ?>);
                    notifications.s2Hide(user_status);
                    notifications.s2SetVal(package_status, <?php echo PACKAGE_STATUS_ACTIVE ?>);
                    notifications.s2Hide(package_status);
                    value.hide();

                    $("#filterRow_N" + newEventIndex + " input[name=value\\[\\]]").val('');
                    notifications.s2SetVal(invoice_status, object.value);
                    notifications.s2Show(invoice_status);
                    notifications.s2SetVal(operator, '=');
                    notifications.s2Disable(operator);
                } else if (object.event == 'u_status') {
                    notifications.s2SetVal(invoice_sent, 0);
                    notifications.s2Hide(invoice_sent);
                    notifications.s2SetVal(package_status, <?php echo PACKAGE_STATUS_ACTIVE ?>);
                    notifications.s2Hide(package_status);
                    notifications.s2SetVal(invoice_status, <?php echo Notification::INVOICE_STATUS_NOT_PAID_PROCESSED ?>);
                    notifications.s2Hide(invoice_status);
                    value.hide();

                    $("#filterRow_N" + newEventIndex + " input[name=value\\[\\]]").val('');
                    notifications.s2SetVal(user_status, object.value);
                    notifications.s2Show(user_status);
                    notifications.s2SetVal(operator, '=');
                    notifications.s2Disable(operator);
                } else if (object.event == 'p_status') {
                    notifications.s2SetVal(invoice_sent, 0);
                    notifications.s2Hide(invoice_sent);
                    notifications.s2SetVal(user_status, <?php echo USER_STATUS_ACTIVE ?>);
                    notifications.s2Hide(user_status);
                    notifications.s2SetVal(invoice_status, <?php echo Notification::INVOICE_STATUS_NOT_PAID_PROCESSED ?>);
                    notifications.s2Hide(invoice_status);
                    value.hide();

                    $("#filterRow_N" + newEventIndex + " input[name=value\\[\\]]").val('');
                    notifications.s2SetVal(package_status, object.value);
                    notifications.s2Show(package_status);
                    notifications.s2SetVal(operator, '=');
                    notifications.s2Disable(operator);
                } else {
                    notifications.s2SetVal(invoice_sent, 0);
                    notifications.s2Hide(invoice_sent);
                    notifications.s2SetVal(invoice_status, <?php echo Notification::INVOICE_STATUS_NOT_PAID_PROCESSED ?>);
                    notifications.s2Hide(invoice_status);
                    notifications.s2SetVal(user_status, <?php echo USER_STATUS_ACTIVE ?>);
                    notifications.s2Hide(user_status);
                    notifications.s2SetVal(package_status, <?php echo PACKAGE_STATUS_ACTIVE ?>);
                    notifications.s2Hide(package_status);

                    $("#filterRow_N" + newEventIndex + " input[name=value\\[\\]]").val(object.value);
                    notifications.s2Enable(operator);
                    value.show();
                }
            } else {
                notifications.s2Show($("#filterRow_N" + newEventIndex + ' select[name=operator\\[\\]]'));
                notifications.s2Hide($("#filterRow_N" + newEventIndex + ' select[name=operator_dates\\[\\]]'));
                notifications.s2Hide($("#filterRow_N" + newEventIndex + ' select[name=operator_dates_units\\[\\]]'));
                notifications.s2Hide($("#filterRow_N" + newEventIndex + ' select[name=invoice_sent\\[\\]]'));
                notifications.s2Hide($("#filterRow_N" + newEventIndex + ' select[name=invoice_status\\[\\]]'));
                notifications.s2Hide($("#filterRow_N" + newEventIndex + ' select[name=user_status\\[\\]]'));
                notifications.s2Hide($("#filterRow_N" + newEventIndex + ' select[name=package_status\\[\\]]'));
            }

            $('.remove-button').click(function(e) {
                e.preventDefault();
                removeEvent(this.parentNode.parentNode)
            });

            $('select[name=event\\[\\]]').change(function() {
                var $operator          = $(this).siblings('select[name=operator\\[\\]]');
                var $operatorDates     = $(this).siblings('select[name=operator_dates\\[\\]]');
                var $operatorDatesUnit = $(this).siblings('select[name=operator_dates_units\\[\\]]');
                var $invoiceSent       = $(this).siblings('select[name=invoice_sent\\[\\]]');
                var $invoiceStatus     = $(this).siblings('select[name=invoice_status\\[\\]]');
                var $userStatus        = $(this).siblings('select[name=user_status\\[\\]]');
                var $packageStatus     = $(this).siblings('select[name=package_status\\[\\]]');

                if ($(this).find('option:selected').data('date')) {
                    notifications.s2Hide($operator);
                    notifications.s2Show($operatorDates);
                    notifications.s2Show($operatorDatesUnit);
                    $(this).siblings('input[name=value\\[\\]]').css('width', 70);
                    $(this).siblings('.ago').show();
                } else {
                    notifications.s2Show($operator);
                    notifications.s2Hide($operatorDates);
                    notifications.s2Hide($operatorDatesUnit);
                    $(this).siblings('input[name=value\\[\\]]').css('width', 140);
                    $(this).siblings('.ago').hide();
                }

                if ($(this).val() == 'i_sent') {
                    notifications.s2Hide($invoiceStatus);
                    notifications.s2Hide($userStatus);
                    notifications.s2Hide($packageStatus);
                    $(this).siblings('input[name=value\\[\\]]').hide();

                    notifications.s2Show($invoiceSent);
                    notifications.s2SetVal($operator, '=');
                    notifications.s2Disable($operator);
                } else if ($(this).val() == 'i_status') {
                    notifications.s2Hide($invoiceSent);
                    notifications.s2Hide($userStatus);
                    notifications.s2Hide($packageStatus);
                    $(this).siblings('input[name=value\\[\\]]').hide();

                    notifications.s2Show($invoiceStatus);
                    notifications.s2SetVal($operator, '=');
                    notifications.s2Disable($operator);
                } else if ($(this).val() == 'u_status') {
                    notifications.s2Hide($invoiceSent);
                    notifications.s2Hide($packageStatus);
                    notifications.s2Hide($invoiceStatus);
                    $(this).siblings('input[name=value\\[\\]]').hide();

                    notifications.s2Show($userStatus);
                    notifications.s2SetVal($operator, '=');
                    notifications.s2Disable($operator);
                } else if ($(this).val() == 'p_status') {
                    notifications.s2Hide($invoiceSent);
                    notifications.s2Hide($userStatus);
                    notifications.s2Hide($invoiceStatus);
                    $(this).siblings('input[name=value\\[\\]]').hide();

                    notifications.s2Show($packageStatus);
                    notifications.s2SetVal($operator, '=');
                    notifications.s2Disable($operator);
                } else  {
                    notifications.s2Hide($invoiceSent);
                    notifications.s2Hide($invoiceStatus);
                    notifications.s2Hide($userStatus);
                    notifications.s2Hide($packageStatus);

                    $(this).siblings('input[name=value\\[\\]]').show();
                    notifications.s2SetVal($operator, '=');
                    notifications.s2Enable($operator);
                }
            });

            $('select[name=operator_dates\\[\\]]').change(function() {
               if (shouldShowAgo($(this).val())) {
                    $(this).siblings('.ago').show();
               } else {
                    $(this).siblings('.ago').hide();
               }
            });

            newEventIndex++;
        }

        function removeEvent(tr)
        {
            tr.parentNode.removeChild(tr);
        }

        function shouldShowAgo(val)
        {
           return ($.inArray(val, [<?php echo "'".Notification::DATE_OPERATOR_WAS_EXACTLY."','".Notification::DATE_OPERATOR_WAS_LESS_THAN."','".Notification::DATE_OPERATOR_WAS_MORE_THAN."'" ?>]) > -1);
        }

        $('#add-button').click(function(e) {
            e.preventDefault();
            addEvent();
            document.getElementById('add-button').scrollIntoView();
        });

        notifications.selectTemplateTarget = $('#selectTemplateTarget');
        notifications.selectActionTarget = $('#selectActionTarget');
        notifications.selectMatchTarget = $('#selectMatchTarget');
        notifications.selectEventTarget    = $('.selectEventTarget');
        notifications.selectOperatorTarget = $('.selectOperatorTarget');

        $(notifications.selectTemplateTarget)
        .add(notifications.selectActionTarget)
        .add(notifications.selectMatchTarget)
        .add(notifications.selectEventTarget)
        .add(notifications.selectOperatorTarget)
        .focus(function() {
        });

        notifications.selectTemplateTarget.select2({
            placeholder: clientexec.lang('Select Email Template'),
            minimumResultsForSearch: 10,
            width: '100%',
            dropdownParent: $('.richwindow')
        });

        notifications.selectActionTarget.select2({
            placeholder: clientexec.lang('Select Action'),
            minimumResultsForSearch: 10,
            width: '100%',
            dropdownParent: $('.richwindow')
        });

        notifications.selectMatchTarget.select2({
            minimumResultsForSearch: 10,
            placeholder: clientexec.lang('Select Rule Option'),
            // width: '100%',
            dropdownParent: $('.richwindow')
        });

        var systemEventRow = document.getElementById("systemEventRowTemplate");
        $('#systemEventRowTemplate select').select2({
            minimumResultsForSearch: 10,
            width:'resolve',
            dropdownParent: $('.richwindow')
        });

        <?php if ( $this->id > 0 ) { ?>
            notifications.s2SetVal(notifications.selectTemplateTarget, '<?php echo $this->templateid; ?>');
            notifications.s2SetVal(notifications.selectActionTarget, '<?php echo $this->actionid; ?>');
            notifications.s2SetVal(notifications.selectMatchTarget, '<?php echo $this->match; ?>');
            <?php if($this->buildCustomRule){ ?>
                <?php foreach ( $this->rows as $key => $row ) { ?>
                var tempObject_<?php echo $key; ?> =  {};
                tempObject_<?php echo $key; ?>.event = '<?php echo $row['event']; ?>';
                tempObject_<?php echo $key; ?>.is_date = <?php echo @$row['is_date']? 'true' : 'false' ?>;
                tempObject_<?php echo $key; ?>.operator = '<?php echo $row['operator']; ?>';
                tempObject_<?php echo $key; ?>.operator_dates = '<?php echo $row['operator_dates']; ?>';
                tempObject_<?php echo $key; ?>.operator_dates_units = '<?php echo $row['operator_dates_units']; ?>';
                tempObject_<?php echo $key; ?>.value = '<?php echo $row['value']; ?>';
                addEvent(tempObject_<?php echo $key; ?>);
                manageRules(true);
                <?php } ?>
            <?php }else{ ?>
                $("#systemEventRowTemplate input[name^='systemValue']").val('<?php echo $this->rows[0]['value']; ?>');
                notifications.s2SetVal($("#systemEventRowTemplate select[name^='systemEvent']"), '<?php echo $this->rows[0]['event']; ?>');
                notifications.s2SetVal($("#systemEventRowTemplate select[name^='systemOperator']"), '<?php echo $this->rows[0]['operator']; ?>');
                addEvent();
                manageRules(false);
            <?php } ?>
        <?php } else { ?>
            addEvent();
            manageRules(false);
        <?php } ?>
    });

    $('#selectActionTarget').on('change', function(e){
        if ($(this).val() == 'ticket') {
            $('.fromDiv').hide();
        } else {
            $('.fromDiv').show();
        }
    });
</script>
<input type="hidden" name="id" value="<?php echo $this->id; ?>" />