<div class="modal-header">
  <button type="button" class="pull-right btn btn-danger btn-sm" data-dismiss="modal" aria-hidden="true"><i class="fa fa-close"></i></button>
  <h4 class="modal-title">{{ firstname }} {{ lastname }} - {{ tab_address }}</h4>
</div>
<div class="modal-body">
  <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-customer-address" class="form-horizontal">
	<input type="hidden" name="customer_group_id" value="{{ customer_group_id }}" />
	<div id="tab-affiliate">
      <fieldset>
        <legend>{{ text_affiliate }}</legend>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="input-company">{{ entry_company }}</label>
          <div class="col-sm-10">
            <input type="text" name="company" value="{{ company }}" placeholder="{{ entry_company }}" id="input-company" class="form-control" />
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="input-website">{{ entry_website }}</label>
          <div class="col-sm-10">
            <input type="text" name="website" value="{{ website }}" placeholder="{{ entry_website }}" id="input-website" class="form-control" />
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="input-tracking"><span data-toggle="tooltip" title="{{ help_tracking }}">{{ entry_tracking }}</span></label>
          <div class="col-sm-10">
            <input type="text" name="tracking" value="{{ tracking }}" placeholder="{{ entry_tracking }}" id="input-tracking" class="form-control" />
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label" for="input-commission"><span data-toggle="tooltip" title="{{ help_commission }}">{{ entry_commission }}</span></label>
          <div class="col-sm-10">
            <input type="text" name="commission" value="{{ commission }}" placeholder="{{ entry_commission }}" id="input-commission" class="form-control" />
          </div>
        </div>
        {% for custom_field in custom_fields %}
          {% if custom_field.location == 'affiliate' %}
            {% if custom_field.type == 'select' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <select name="custom_field[{{ custom_field.custom_field_id }}]" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control">
                    <option value="">{{ text_select }}</option>
                    {% for custom_field_value in custom_field.custom_field_value %}
                      {% if affiliate_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == affiliate_custom_field[custom_field.custom_field_id] %}
                        <option value="{{ custom_field_value.custom_field_value_id }}" selected="selected">{{ custom_field_value.name }}</option>
                        {% else %}
                        <option value="{{ custom_field_value.custom_field_value_id }}">{{ custom_field_value.name }}</option>
                      {% endif %}
                    {% endfor %}
                  </select>
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'radio' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <div> 
				    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="radio">
					    {% if affiliate_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id == affiliate_custom_field[custom_field.custom_field_id] %}
                          <label>
                            <input type="radio" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" checked="checked" />
                            {{ custom_field_value.name }}
						  </label>
                          {% else %}
                          <label>
                            <input type="radio" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ custom_field_value.custom_field_value_id }}" />
                            {{ custom_field_value.name }}
						  </label>
                        {% endif %}
					  </div>
                    {% endfor %}
				  </div>
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'checkbox' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <div> 
				    {% for custom_field_value in custom_field.custom_field_value %}
                      <div class="checkbox">
					    {% if affiliate_custom_field[custom_field.custom_field_id] and custom_field_value.custom_field_value_id in affiliate_custom_field[custom_field.custom_field_id] %}
                          <label>
                            <input type="checkbox" name="custom_field[{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" checked="checked" />
                            {{ custom_field_value.name }}
						  </label>
                          {% else %}
                          <label>
                            <input type="checkbox" name="custom_field[{{ custom_field.custom_field_id }}][]" value="{{ custom_field_value.custom_field_value_id }}" />
                            {{ custom_field_value.name }}
						  </label>
                        {% endif %}
					  </div>
                    {% endfor %}
				  </div>
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'text' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ affiliate_custom_field[custom_field.custom_field_id] ? affiliate_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'textarea' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <textarea name="custom_field[{{ custom_field.custom_field_id }}]" rows="5" placeholder="{{ custom_field.name }}" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control">{{ affiliate_custom_field[custom_field.custom_field_id] ? affiliate_custom_field[custom_field.custom_field_id] : custom_field.value }}</textarea>
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'file' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <button type="button" id="button-custom-field{{ custom_field.custom_field_id }}" data-loading-text="{{ text_loading }}" class="btn btn-default"><i class="fa fa-upload"></i> {{ button_upload }}</button>
                  <input type="hidden" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ affiliate_custom_field[custom_field.custom_field_id] ? affiliate_custom_field[custom_field.custom_field_id] }}" id="input-custom-field{{ custom_field.custom_field_id }}" />
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'date' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <div class="input-group date">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ affiliate_custom_field[custom_field.custom_field_id] ? affiliate_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" data-date-format="YYYY-MM-DD" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
                    <span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span>
				  </div>
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'time' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <div class="input-group time">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ affiliate_custom_field[custom_field.custom_field_id] ? affiliate_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" data-date-format="HH:mm" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
                    <span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span>
				  </div>
                </div>
              </div>
            {% endif %}
            {% if custom_field.type == 'datetime' %}
              <div class="form-group custom-field custom-field{{ custom_field.custom_field_id }}" data-sort="{{ custom_field.sort_order }}">
                <label class="col-sm-2 control-label" for="input-custom-field{{ custom_field.custom_field_id }}">{{ custom_field.name }}</label>
                <div class="col-sm-10">
                  <div class="input-group datetime">
                    <input type="text" name="custom_field[{{ custom_field.custom_field_id }}]" value="{{ affiliate_custom_field[custom_field.custom_field_id] ? affiliate_custom_field[custom_field.custom_field_id] : custom_field.value }}" placeholder="{{ custom_field.name }}" data-date-format="YYYY-MM-DD HH:mm" id="input-custom-field{{ custom_field.custom_field_id }}" class="form-control" />
                    <span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span>
				  </div>
                </div>
              </div>
            {% endif %}
          {% endif %}
        {% endfor %}
      </fieldset>
      <fieldset>
        <legend>{{ text_payment }}</legend>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-tax">{{ entry_tax }}</label>
            <div class="col-sm-10">
              <input type="text" name="tax" value="{{ tax }}" placeholder="{{ entry_tax }}" id="input-tax" class="form-control" />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">{{ entry_payment }}</label>
            <div class="col-sm-10">
              <div class="radio">
                <label>
				  {% if payment == 'cheque' %}
                    <input type="radio" name="payment" value="cheque" checked="checked" />
                    {% else %}
                    <input type="radio" name="payment" value="cheque" />
                  {% endif %}
                  {{ text_cheque }}
				</label>
              </div>
              <div class="radio">
                <label> 
				  {% if payment == 'paypal' %}
                    <input type="radio" name="payment" value="paypal" checked="checked" />
                    {% else %}
                    <input type="radio" name="payment" value="paypal" />
                  {% endif %}
                  {{ text_paypal }}
				</label>
              </div>
              <div class="radio">
                <label> 
				  {% if payment == 'bank' %}
                    <input type="radio" name="payment" value="bank" checked="checked" />
                    {% else %}
                    <input type="radio" name="payment" value="bank" />
                  {% endif %}
                  {{ text_bank }}
				</label>
              </div>
            </div>
          </div>
          <div id="payment-cheque" class="payment">
            <div class="form-group required">
              <label class="col-sm-2 control-label" for="input-cheque">{{ entry_cheque }}</label>
              <div class="col-sm-10">
                <input type="text" name="cheque" value="{{ cheque }}" placeholder="{{ entry_cheque }}" id="input-cheque" class="form-control" />
              </div>
            </div>
          </div>
          <div id="payment-paypal" class="payment">
            <div class="form-group required">
              <label class="col-sm-2 control-label" for="input-paypal">{{ entry_paypal }}</label>
              <div class="col-sm-10">
                <input type="text" name="paypal" value="{{ paypal }}" placeholder="{{ entry_paypal }}" id="input-paypal" class="form-control" />
              </div>
            </div>
          </div>
          <div id="payment-bank" class="payment">
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-bank-name">{{ entry_bank_name }}</label>
              <div class="col-sm-10">
                <input type="text" name="bank_name" value="{{ bank_name }}" placeholder="{{ entry_bank_name }}" id="input-bank-name" class="form-control" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-bank-branch-number">{{ entry_bank_branch_number }}</label>
              <div class="col-sm-10">
                <input type="text" name="bank_branch_number" value="{{ bank_branch_number }}" placeholder="{{ entry_bank_branch_number }}" id="input-bank-branch-number" class="form-control" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-bank-swift-code">{{ entry_bank_swift_code }}</label>
              <div class="col-sm-10">
                <input type="text" name="bank_swift_code" value="{{ bank_swift_code }}" placeholder="{{ entry_bank_swift_code }}" id="input-bank-swift-code" class="form-control" />
              </div>
            </div>
            <div class="form-group required">
              <label class="col-sm-2 control-label" for="input-bank-account-name">{{ entry_bank_account_name }}</label>
              <div class="col-sm-10">
                <input type="text" name="bank_account_name" value="{{ bank_account_name }}" placeholder="{{ entry_bank_account_name }}" id="input-bank-account-name" class="form-control" />
              </div>
            </div>
            <div class="form-group required">
              <label class="col-sm-2 control-label" for="input-bank-account-number">{{ entry_bank_account_number }}</label>
              <div class="col-sm-10">
                <input type="text" name="bank_account_number" value="{{ bank_account_number }}" placeholder="{{ entry_bank_account_number }}" id="input-bank-account-number" class="form-control" />
              </div>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-affiliate">{{ entry_status }}</label>
            <div class="col-sm-10">
              <select name="affiliate" id="input-affiliate" class="form-control">
                {% if affiliate %}
                  <option value="1" selected="selected">{{ text_enabled }}</option>
                  <option value="0">{{ text_disabled }}</option>
                  {% else %}
                  <option value="1">{{ text_enabled }}</option>
                  <option value="0" selected="selected">{{ text_disabled }}</option>
                {% endif %}
              </select>
            </div>
          </div>
      </fieldset>           
    </div>         
  </form>
</div>
<div class="modal-footer">
  <div class="text-center"><a data-form="form-customer-address" class="button-save btn btn-primary"><i class="fa fa-save"></i> {{ button_save }}</a></div>
</div>
<script type="text/javascript"><!--
$(".button-save").on('click', function() {		
	$.ajax({
		url: $('#' + $(this).data('form')).attr('action'),
		type: 'post',
		dataType: 'json',
		data: $('#' + $(this).data('form') + ' input[type=\'text\'], #' + $(this).data('form') + ' input[type=\'hidden\'], #' + $(this).data('form') + ' input[type=\'radio\']:checked, #' + $(this).data('form') + ' input[type=\'checkbox\']:checked, #' + $(this).data('form') + ' select, #' + $(this).data('form') + ' textarea'),
		success: function(json) {
			if (json['error']) {
				$('#form-customer-address').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + '</div>');
				
				$('.alert-danger').delay(1500).fadeOut(500);	
			}
								
			if (json['success']) {				
				$('#form-customer-address').before('<div class="alert alert-success"><i class="fa fa-exclamation-circle"></i> ' + json['success'] + '</div>');
			
				setTimeout(function(){
					$('#modal-customer-edit').modal('hide');
				}, 1000)
			}
		},
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
	});
}); 
//--></script>
  <script type="text/javascript"><!--
$.ajax({
    url: 'index.php?route=customer/customer/customfield&user_token={{ user_token }}&customer_group_id={{ customer_group_id }}',
    dataType: 'json',
    success: function(json) {
        $('.custom-field').hide();
        $('.custom-field').removeClass('required');

        for (i = 0; i < json.length; i++) {
			custom_field = json[i];

			$('.custom-field' + custom_field['custom_field_id']).show();

			if (custom_field['required']) {
				$('.custom-field' + custom_field['custom_field_id']).addClass('required');
			}
        }
    },
    error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
});
  //--></script> 
<script type="text/javascript"><!--
$('input[name="affiliate"]').on('change', function() {
    if ($(this).val() == '1') { 
		$('#tab-affiliate :input').not('input[name="affiliate"]').prop('disabled', false);
    } else {
		$('#tab-affiliate :input').not('input[name="affiliate"]').prop('disabled', true);
    }
}); 

$('input[name=\'affiliate\']:checked').trigger('change');
  //--></script> 
  <script type="text/javascript"><!--
$('#content').delegate('button[id^=\'button-custom-field\'], button[id^=\'button-address\']', 'click', function() {
    var node = this;

    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    if (typeof timer != 'undefined') {
        clearInterval(timer);
    }

    timer = setInterval(function() {
      if ($('#form-upload input[name=\'file\']').val() != '') {
        clearInterval(timer);

        $.ajax({
          url: 'index.php?route=tool/upload/upload&user_token={{ user_token }}',
          type: 'post',
          dataType: 'json',
          data: new FormData($('#form-upload')[0]),
          cache: false,
          contentType: false,
          processData: false,
          beforeSend: function() {
            $(node).button('loading');
          },
          complete: function() {
            $(node).button('reset');
          },
          success: function(json) {
            $(node).parent().find('.text-danger').remove();

            if (json['error']) {
              $(node).parent().find('input[type=\'hidden\']').after('<div class="text-danger">' + json['error'] + '</div>');
            }

            if (json['success']) {
              alert(json['success']);
            }

            if (json['code']) {
              $(node).parent().find('input[type=\'hidden\']').val(json['code']);
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      }
    }, 500);
});
  
$('.date').datetimepicker({
    language: '{{ datepicker }}',
    pickTime: false
});

$('.datetime').datetimepicker({
    language: '{{ datepicker }}',
    pickDate: true,
    pickTime: true
});

$('.time').datetimepicker({
    language: '{{ datepicker }}',
    pickDate: false
});
  
$('#tab-affiliate .form-group[data-sort]').detach().each(function() {
    if ($(this).attr('data-sort') >= 0 && $(this).attr('data-sort') <= $('#tab-affiliate .form-group').length) {
		$('#tab-affiliate .form-group').eq($(this).attr('data-sort')).before(this);
    }

    if ($(this).attr('data-sort') > $('#tab-affiliate .form-group').length) {
		$('#tab-affiliate .form-group:last').after(this);
    }

    if ($(this).attr('data-sort') < -$('#tab-affiliate .form-group').length) {
		$('#tab-affiliate .form-group:first').before(this);
    }
});
//--></script>
  <script type="text/javascript"><!--
$('input[name=\'payment\']').on('change', function() {
    $('.payment').hide();

    $('#payment-' + this.value).show();
});

$('input[name=\'payment\']:checked').trigger('change');
  //--></script>