<style type="text/css">
  .promotion-categories-box {
    max-height: 420px;
    overflow: auto;
    border: 1px solid #d6d6d6;
    border-radius: 4px;
    padding: 8px 10px;
    background: #fff;
  }

  .promotion-categories-tree {
    margin: 0;
    padding: 0 0 0 16px;
    list-style: none;
  }

  .promotion-category-item {
    margin: 2px 0;
  }

  .promotion-category-item.is-collapsed > ol {
    display: none;
  }

  .promotion-category-row {
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: 28px;
  }

  .promotion-tree-toggle,
  .promotion-tree-spacer {
    width: 18px;
    height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 0;
    background: transparent;
    padding: 0;
  }

  .promotion-tree-toggle {
    cursor: pointer;
    color: #666;
  }

  .promotion-category-item:not(.is-collapsed) > .promotion-category-row .promotion-tree-toggle i {
    transform: rotate(90deg);
  }

  .promotion-category-label {
    margin: 0;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }

  .js-promotion-row-hide {
    display: none;
  }
</style>

<script type="text/javascript">
  (function($) {
    if (!$) {
      return;
    }

    function updatePromotionConditionRows() {
      var conditionType = parseInt($('#condition_type').val(), 10) || 0;
      var $discountTypeRow = $('#discount_type').closest('.form-group, .row');
      var $amountRow = $('#amount').closest('.form-group, .row');
      var $group2Row = $('input[name="condition_categories[]"]').first().closest('.js-promotion-row');
      var $minCountRow = $('#min_product_count').closest('.form-group, .row');
      var $cheapestPriceRow = $('#price_cheapest_product').closest('.form-group, .row');

      if (conditionType === 3) {
        $discountTypeRow.addClass('js-promotion-row-hide');
        $amountRow.addClass('js-promotion-row-hide');
        $group2Row.addClass('js-promotion-row-hide');
        $minCountRow.removeClass('js-promotion-row-hide');
        $cheapestPriceRow.removeClass('js-promotion-row-hide');
        return;
      }

      $discountTypeRow.removeClass('js-promotion-row-hide');
      $amountRow.removeClass('js-promotion-row-hide');
      $group2Row.removeClass('js-promotion-row-hide');
      $minCountRow.addClass('js-promotion-row-hide');
      $cheapestPriceRow.addClass('js-promotion-row-hide');
    }

    $(document).off('change.shopPromotionCondition', '#condition_type');
    $(document).on('change.shopPromotionCondition', '#condition_type', function() {
      updatePromotionConditionRows();
    });

    $(document).off('click.shopPromotionTree', '.promotion-tree-toggle');
    $(document).on('click.shopPromotionTree', '.promotion-tree-toggle', function(e) {
      e.preventDefault();
      var $item = $(this).closest('.promotion-category-item');
      $item.toggleClass('is-collapsed');
      $(this).attr('aria-expanded', $item.hasClass('is-collapsed') ? 'false' : 'true');
    });

    $(function() {
      updatePromotionConditionRows();
    });
  })(window.jQuery);
</script>

