  <?php 
  $condition_rules = isset($moduleData['condition_rules']) ? $moduleData['condition_rules'] : array() ;
  $conditions = isset($moduleData['conditions']) ? $moduleData['conditions'] : array() ;
   ?>

   <div class="row">
    <div class="col-sm-12">
      <h3 style="font-size:13px;">The promotion will be available if 
        <select name="condition_rules[condition_combine]" style="display:inline-block;width:70px;" class="form-control input-sm" id="condition_combine">    
          <option <?php echo isset($condition_rules['condition_combine']) && $condition_rules['condition_combine'] == 'All' ? 'selected' : ''; ?>  value="All">All</option>
          <option <?php echo isset($condition_rules['condition_combine']) && $condition_rules['condition_combine'] == 'Some' ? 'selected' : ''; ?> value="Some">Some</option>
        </select>
       of the conditions below are 
         <select name="condition_rules[condition_bool]" style="display:inline-block;width:70px;" class="form-control  input-sm" id="condition_bool">    
            <option <?php echo isset($condition_rules['condition_bool']) && $condition_rules['condition_bool']== 'True' ? 'selected' : ''; ?> value="True">True</option>
            <option <?php echo isset($condition_rules['condition_bool']) && $condition_rules['condition_bool']== 'False' ? 'selected' : ''; ?> value="False">False</option>
          </select>

         &nbsp;&nbsp;<a href="javascript:void(0)" data-toggle="modal" data-target="#condHelperModal"><i class="fa fa-info-circle" aria-hidden="true"></i></a>
      </h3>
    </div>
  </div>

  <div class="row">
    <div class="col-sm-12">
      <div id="conditions_group">
        <?php foreach ($conditions as $key => $cond) { ?>
            <div id="condition<?php echo $key; ?>" data-id="<?php echo $key; ?>" class="row"><hr>
              <div class="col-sm-3">
                <select id="mainSelect<?php echo $key; ?>" onchange="getFields(<?php echo $key; ?>);" name="conditions[<?php echo $key; ?>][condSelect]" class="form-control">
                  <option disabled="disabled" selected="selected">-- Select a condition --</option>
                  <optgroup label="Cart requirements">
                    <option <?php echoSelected($cond['condSelect'], 'cart_quantity'); ?> value="cart_quantity">Cart quantity</option>
                    <option <?php echoSelected($cond['condSelect'], 'cart_total'); ?> value="cart_total" >Cart total</option>
                    <option <?php echoSelected($cond['condSelect'], 'cart_weight'); ?> value="cart_weight">Cart weight (kg)</option>
                  </optgroup>
                  <optgroup label="Product requirements">
                    <option <?php echoSelected($cond['condSelect'], 'product_name'); ?> value="product_name">Product in cart (autocomplete)</option>
                  </optgroup>
                  <optgroup label="Customer requirements">
                    <option <?php echoSelected($cond['condSelect'], 'customer_name'); ?> value="customer_name">Customer name</option>
                    <option <?php echoSelected($cond['condSelect'], 'customer_order'); ?> value="customer_order">Customer order count</option>
                    <option <?php echoSelected($cond['condSelect'], 'customer_reg_date'); ?> value="customer_reg_date">Customer registred since</option>
                  </optgroup>
                  <optgroup label="Manufacturer requirements">
                    <option <?php echoSelected($cond['condSelect'], 'manufac_name'); ?> value="manufac_name">Product from manufacturer in cart</option>
                  </optgroup>
                  <optgroup label="Category requirements">
                    <option <?php echoSelected($cond['condSelect'], 'category_name'); ?> value="category_name">Product from category in cart</option>
                  </optgroup>
                  <optgroup label="Other requirements">
                    <option <?php echoSelected($cond['condSelect'], 'day'); ?> value="day">Day of the week</option>                    
                  </optgroup>
                </select>
             
            </div>

             <div class="col-sm-1" style="padding-left: 10px;font-weight: bold;width: 2%;line-height: 36px;">is</div>


              <div id="equal-input<?php echo $key; ?>" class="col-sm-2">
                <select name="conditions[<?php echo $key; ?>][condEquals]" class="form-control">
                  <option <?php echoSelected($cond['condEquals'], 'eq'); ?> value="eq" selected="selected">Equal to</option>
                  <option <?php echoSelected($cond['condEquals'], 'neq'); ?> value="neq">Not equal to</option>
                  <option <?php echoSelected($cond['condEquals'], 'gtr'); ?> value="gtr">Greater than</option>
                  <option <?php echoSelected($cond['condEquals'], 'lth'); ?> value="lth">Less than</option>
                </select>
              </div>

              <div id="condition-input<?php echo $key; ?>" class="col-sm-2">

                <input type="text" class="form-control" name="conditions[<?php echo $key; ?>][condition_input]" placeholder="Enter Condition" value="<?php echo isset($cond['condition_input']) ? $cond['condition_input'] : '' ?>" />
              </div>

              <div id="autocomplete-product<?php echo $key; ?>" style="display:none;">

              <div class="col-sm-2">
                <input type="text" class="form-control products-autocomplete" data-cond-id="<?php echo $key; ?>" readonly placeholder="Enter Product" value="<?php echo isset($cond['product_name']) ? $cond['product_name'] : '' ?>" />
                <input type="hidden" name="conditions[<?php echo $key; ?>][product_id]" value="<?php echo isset($cond['product_id']) ? $cond['product_id'] : '' ?>" />
                
              </div>
              <div class="col-sm-3">  
                <div class="input-group">
                  <div class="input-group-addon">Min. quantity:</div>
                    <input type="number" name="conditions[<?php echo $key; ?>][product_quantity]" class="form-control" value="<?php echo isset($cond['product_quantity']) ? $cond['product_quantity'] : '1' ?>" />
                </div>
              </div>
                
              </div>

              <div id="reg_date<?php echo $key; ?>" style="display:none;">

                <div class="col-sm-2 date">
                  <div class="input-group">
                    <input type="text" class="form-control" name="conditions[<?php echo $key; ?>][customer_date]" value="<?php echo isset($cond['customer_date']) ? $cond['customer_date'] : '' ?>" placeholder="YYYY-MM-DD" data-date-format="YYYY-MM-DD" />
                    <span class="input-group-btn">
                      <button class="btn btn-default" type="button"><i class="fa fa-calendar"></i></button>
                    </span>
                  </div>
                </div>
              </div>


              <div id="autocomplete-category<?php echo $key; ?>" style="display:none;">
                <div class="col-sm-2">
                  <input type="text" class="form-control categories-autocomplete" data-cond-id="<?php echo $key; ?>" readonly placeholder="Enter Category" value="<?php echo isset($cond['category_name']) ? $cond['category_name'] : '' ?>" />
                  <input type="hidden" name="conditions[<?php echo $key; ?>][category_id]" value="<?php echo isset($cond['category_id']) ? $cond['category_id'] : '' ?>" />
                </div>

                <div style="width: 12%;" class="col-sm-2">
                    Include parent categories
                    <input style="float:right;" type="checkbox" name="conditions[<?php echo $key; ?>][category_parent]" <?php echo isset($cond['category_parent']) && $cond['category_parent'] == 'yes' ? 'checked' : '' ?>  value="yes"> <br>
                    Include child categories
                    <input style="float:right;" type="checkbox" name="conditions[<?php echo $key; ?>][category_child]" <?php echo isset($cond['category_child']) && $cond['category_child'] == 'yes' ? 'checked' : '' ?> value="yes"> 
                </div>

                <div style="width: 13%;" class="col-sm-1">  
                  <div class="input-group">
                    <div class="input-group-addon">Min. quantity:</div>
                      <input type="number" name="conditions[<?php echo $key; ?>][category_quantity]" class="form-control" value="<?php echo isset($cond['category_quantity']) ? $cond['category_quantity'] : '1' ?>" />
                  </div>
                </div>

              </div>

              <div id="autocomplete-customer<?php echo $key; ?>" style="display:none;">
                <div class="col-sm-2">
                  <input type="text" class="form-control customer-autocomplete" readonly placeholder="Enter Customer" value="<?php echo isset($cond['customer_name']) ? $cond['customer_name'] : '' ?>" />
                  <input type="hidden" name="conditions[<?php echo $key; ?>][customer_id]" value="<?php echo isset($cond['customer_id']) ? $cond['customer_id'] : '' ?>" />
                </div>
              </div>


              <div id="autocomplete-manufacturer<?php echo $key; ?>" style="display:none;">
                <div class="col-sm-2">
                  <input type="text" class="form-control manufacturer-autocomplete" readonly placeholder="Enter Manufacturer" value="<?php echo isset($cond['manufacturer_name']) ? $cond['manufacturer_name'] : '' ?>" />
                  <input type="hidden" name="conditions[<?php echo $key; ?>][manufacturer_id]" value="<?php echo isset($cond['manufacturer_id']) ? $cond['manufacturer_id'] : '' ?>" />
                </div>
                <div class="col-sm-3">  
                  <div class="input-group">
                    <div class="input-group-addon"> Min. quantity:</div>
                      <input type="number" name="conditions[<?php echo $key; ?>][manufacturer_quantity]" class="form-control" value="<?php echo isset($cond['manufacturer_quantity']) ? $cond['manufacturer_quantity'] : '' ?>" />
                  </div>
                </div>
              </div>

              <div id="dayofweek<?php echo $key; ?>" style="display:none;">
                <div class="col-sm-3">
                <select name="conditions[<?php echo $key; ?>][dayofweek]" class="form-control">
                  <option <?php echoSelected($cond['dayofweek'], 'mon'); ?> value="mon" selected="selected">Monday</option>
                  <option <?php echoSelected($cond['dayofweek'], 'tue'); ?> value="tue">Tuesday</option>
                  <option <?php echoSelected($cond['dayofweek'], 'wed'); ?> value="wed">Wednesday</option>
                  <option <?php echoSelected($cond['dayofweek'], 'thu'); ?> value="thu">Thursday</option>
                  <option <?php echoSelected($cond['dayofweek'], 'fri'); ?> value="fri">Friday</option>
                  <option <?php echoSelected($cond['dayofweek'], 'sat'); ?> value="sat">Saturday</option>
                  <option <?php echoSelected($cond['dayofweek'], 'sun'); ?> value="sun">Sunday</option>
                </select>
                </div>
              </div>

              <div class="col-sm-3 ">
                <a data-remove-id="<?php echo $key; ?>" data-toggle="tooltip" title="Remove" class="btn btn-danger" onclick="remove_cond(<?php echo $key; ?>);" data-original-title="">
                  <i class="fa fa-trash"></i>
                </a>                
              </div>

            </div>
       <?php } ?>
        </div>
    </div>
  </div>
    
  <div class="row"><hr>
    <div class="col-sm-12">
      <a data-toggle="tooltip" title="Add a new condition" id="addnewcond" class="btn btn-primary" data-original-title=""><i class="fa fa-plus"></i> Add new condition</a>
    </div>
  </div>

  <div id="condHelperModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Conditions helper</h4>
        </div>
        <div class="modal-body">




          <span style="font-size:13px;" class="text"> The conditions tab defines the rules for the promotion. Before proceeding to the Actions Tab, where you can set the reward for the customer, you should assign conditions.<br>
          <br><br>

          Let’s explain each condition group:
          <hr>
          <ol>
            <li>The cart requirements are very simple. The customer must have the exact/lower/higher amount of items/weight/price in the cart in order to meet the condition.</li>
            <li>The product/category/manufacturer requirements demand that the customer must have the specified product in cart or product from specified category/manufacturer. Without this product (or product from the category/manufacturer), the condition will not be met.
            </li>
            <li>The customer requirements are useful for creating customer-specific promotions, which will be available only to certain customers.</li>
            <li>The "Day of the Week" requirement is obvious. The promotion will be valid on this day of the week (for promotions like "X% off every Monday/Friday").</li>
          </ol>
          <hr>
          In order to make great promotions you can combine different conditions to achieve a variety of promotions. <br><br>

          <strong>Warning:</strong> If you leave the promotion without conditions, it will always be valid (except if the customer has not fulfilled the general setting of the promotion).


          </span>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>




  

  <script type="text/javascript">
    function getFields(id){https://scontent-vie1-1.xx.fbcdn.net/v/t1.0-9/15181408_378806499118312_7678024550811527403_n.jpg?oh=44d31b4adf540b40bd85f4c7354a95ed&oe=58B8ADE7

      var mainSelect = $('#mainSelect'+id).val();

      if(mainSelect == 'cart_total') {
        html = '<div class="input-group"><div class="input-group-addon"><?php echo $currency; ?></div></div>';
        $('#condition-input'+id).append(html).find('input').appendTo('#condition-input'+id+' .input-group');
      } else {
        $('#condition-input'+id+' .input-group').find('input').prependTo('#condition-input'+id);
        $('#condition-input'+id+' .input-group').remove();
      }


  if(mainSelect == 'product_name'){
    $('#equal-input'+id).hide();
    $('#condition-input'+id).hide();
    $('#autocomplete-product'+id).show();
    $('#autocomplete-category'+id).hide();
    $('#autocomplete-customer'+id).hide();
    $('#autocomplete-manufacturer'+id).hide();
    $('#dayofweek'+id).hide();
    $('#reg_date'+id).hide();
  } else if(mainSelect == 'category_name') {
    $('#equal-input'+id).hide();
    $('#condition-input'+id).hide();
    $('#autocomplete-product'+id).hide();
    $('#autocomplete-category'+id).show();
    $('#autocomplete-customer'+id).hide();
    $('#autocomplete-manufacturer'+id).hide();
    $('#dayofweek'+id).hide();
    $('#reg_date'+id).hide();
  } else if(mainSelect == 'customer_name') {
    $('#equal-input'+id).hide();
    $('#condition-input'+id).hide();
    $('#autocomplete-product'+id).hide();
    $('#autocomplete-category'+id).hide();
    $('#autocomplete-customer'+id).show();
    $('#autocomplete-manufacturer'+id).hide();
    $('#dayofweek'+id).hide();
    $('#reg_date'+id).hide();
  } else if(mainSelect == 'customer_reg_date') {
    $('#equal-input'+id).hide();
    $('#condition-input'+id).hide();
    $('#autocomplete-product'+id).hide();
    $('#autocomplete-category'+id).hide();
    $('#autocomplete-customer'+id).hide();
    $('#reg_date'+id).show();
    $('#autocomplete-manufacturer'+id).hide();
    $('#dayofweek'+id).hide();
  }else if(mainSelect == 'manufac_name') {
    $('#equal-input'+id).hide();
    $('#condition-input'+id).hide();
    $('#autocomplete-product'+id).hide();
    $('#autocomplete-category'+id).hide();
    $('#autocomplete-customer'+id).hide();
    $('#autocomplete-manufacturer'+id).show();
    $('#dayofweek'+id).hide();
    $('#reg_date'+id).hide();
  }else if(mainSelect == 'day') {
    $('#equal-input'+id).hide();
    $('#condition-input'+id).hide();
    $('#autocomplete-product'+id).hide();
    $('#autocomplete-category'+id).hide();
    $('#autocomplete-customer'+id).hide();
    $('#autocomplete-manufacturer'+id).hide();
    $('#dayofweek'+id).show();
    $('#reg_date'+id).hide();
  }else {
    $('#equal-input'+id).show();
    $('#condition-input'+id).show();
    $('#autocomplete-product'+id).hide();
    $('#autocomplete-category'+id).hide();
    $('#autocomplete-customer'+id).hide();
    $('#autocomplete-manufacturer'+id).hide();
    $('#dayofweek'+id).hide();
    $('#reg_date'+id).hide();
  }
}




  $(document).ready(function () {

            $('#conditions_group div[id^=condition]').each(function () {
              getFields($(this).data('id'));
              applyAutocomplete($(this).data('id'));
            });

  });
  </script>