javascript


jQuery(document).ready(function($) {
  function evaluateConditions() {
    $('[data-key]').each(function () {
      const $element = $(this);
      const attrs = this.attributes;
      const conditions = [];

      // Parse all data-cond-* attributes
      $.each(attrs, function () {
        if (this.name.startsWith('data-cond-')) {
          const match = this.name.match(/data-cond-(\d+)-(action|field|operator|value)/);
          if (match) {
            const index = match[1];
            const type = match[2];

            if (!conditions[index]) conditions[index] = {};
            conditions[index][type] = this.value;
          }
        }
      });

      // Evaluate all conditions (AND logic)
      let show = true;
      conditions.forEach(cond => {
        const $target = $('[data-key="' + cond.field + '"]');
        const val = $target.val();

        switch (cond.operator) {
          case 'equals':
            if (val !== cond.value) show = false;
            break;
          case 'not equals':
            if (val === cond.value) show = false;
            break;
          case 'empty':
            if (val !== '') show = false;
            break;
          case 'not empty':
            if (val === '') show = false;
            break;
          case 'greater than':
            if (!(parseFloat(val) > parseFloat(cond.value))) show = false;
            break;
          case 'less than':
            if (!(parseFloat(val) < parseFloat(cond.value))) show = false;
            break;
          default:
            console.warn('Unsupported operator:', cond.operator);
            show = false;
        }
      });

      // Apply the action (only "show" for now)
      const finalAction = conditions[0]?.action || 'show';
      if (finalAction === 'show') {
        $element.toggle(show);
      }
    });
  }

  // Bind input events to all controllers
  $('[data-key]').on('input change', evaluateConditions);

  // Initial check
  evaluateConditions();
});