{{ header }}{{ column_left }}

<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="submit" form="form-account-combine" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa fa-save"></i></button>
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ul class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
        <li><a href="{{ breadcrumb['href'] }}">{{ breadcrumb['text'] }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  <div class="container-fluid">
    {% if error_warning %}
    <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
	{% else %}
    <div class="alert alert-danger" style="display:none"><i class="fa fa-exclamation-circle"></i> 
	<button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>	
    {% endif %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-pencil"></i> {{ text_edit }}</h3>
      </div>
      <div class="panel-body">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-account-combine" class="form-horizontal">
  <input type="hidden" name="account_combine_version" value="{{ account_combine_version }}" />
  <input type="hidden" name="account_combine_rform[website_textarea]" value="0" />
  <input type="hidden" name="account_combine_cform[website_textarea]" value="0" />
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-status">{{ entry_status }}</label>
            <div class="col-sm-10">
              <select name="account_combine_status" id="input-status" class="form-control">
                {% if account_combine_status %}
                <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>  
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-allow-own-aff"><span data-toggle="tooltip" title="{{ help_allow_own_aff_links }}">{{ entry_allow_own_aff_links }}</span></label>
            <div class="col-sm-10">
              <select name="account_combine_allow_own_aff" id="input-allow-own-aff" class="form-control triggers">
                {% if account_combine_allow_own_aff %}
                <option value="1" selected="selected">{{ text_yes }}</option>
                <option value="0">{{ text_no }}</option>
                {% else %}
                <option value="1">{{ text_yes }}</option>
                <option value="0" selected="selected">{{ text_no }}</option>
                {% endif %}
              </select>
            </div>
          </div> 	
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-own-parent"><span data-toggle="tooltip" title="{{ help_own_parent }}">{{ entry_own_parent }}</span></label>
            <div class="col-sm-10">
              <select name="account_combine_own_parent" id="input-own-parent" class="form-control triggers">
                {% if account_combine_own_parent %}
                <option value="1" selected="selected">{{ text_yes }}</option>
                <option value="0">{{ text_no }}</option>
                {% else %}
                <option value="1">{{ text_yes }}</option>
                <option value="0" selected="selected">{{ text_no }}</option>
                {% endif %}
              </select>
            </div>
          </div> 			  

          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-customer-group"><span data-toggle="tooltip" title="{{ help_customer_group }}">{{ entry_customer_group }}</span></label>
            <div class="col-sm-10">			
              <select name="account_combine_customer_group_id" id="input-customer-group" class="form-control">
				<option value="0">{{ text_none }}</option>
				{% for cg in customer_groups %}
				<option value="{{ cg['customer_group_id'] }}"{% if cg['customer_group_id'] == account_combine_customer_group_id %} selected="selected"{% endif %}>{{ cg['name'] }}</option>
				{% endfor %}
              </select> 
			  <a href="javascript:;" id="customer-group-now">{{ text_customer_group_now }}</a>
			  <div class="text-success" id="cgn-result-success" style="display:none;">{{ text_done }}</div>			  
            </div>
          </div>	  

	<fieldset>
		<legend>{{ heading_fields }}</legend>	  
		{% if tracking_input_info %}
		<div class="alert alert-info">{{ tracking_input_info }}</div>
		{% endif %}
		{% for field in aff_fields %}
          <div class="form-group">
            <label class="col-sm-2 control-label" for="f_{{ field }}">{{ entry_fields[field] }}</label>
            <div class="col-sm-10">
              <select name="account_combine_fields[{{ field }}]" id="f_{{ field }}" class="aff_f triggers form-control">
				<option value="y"{% if account_combine_fields[field] == 'y' %} selected="selected"{% endif %}>{{ text_yes }}</option>
				<option value="r"{% if account_combine_fields[field] == 'r' %} selected="selected"{% endif %}>{{ text_option_required }}</option>
				<option value="n"{% if account_combine_fields[field] == 'n' %} selected="selected"{% endif %}>{{ text_no }}</option>
              </select>
            </div>
          </div>
		{% endfor %}
          <div class="form-group">
            <label class="col-sm-2 control-label" for="f_payment_default">{{ entry_fields_payment_default }}</label>
            <div class="col-sm-10">
              <select name="account_combine_fields[payment_default]" id="f_payment_default" class="form-control">
			{% for method in payment_methods %}
				<option value="{{ method }}"{% if account_combine_fields['payment_default'] == method %} selected="selected"{% endif %}>{{ method|capitalize }}</option>
			{% endfor %}
              </select>
            </div>
          </div>
		{% for method in payment_methods %}
          <div class="form-group">
            <label class="col-sm-2 control-label" for="f_{{ method }}">{{ entry_fields_use[method] }}</label>
            <div class="col-sm-10">
              <select name="account_combine_fields[use_{{ method }}]" id="f_{{ method }}" class="f_payment triggers form-control">
				{% if account_combine_fields["use_#{method}"] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}
              </select>
            </div>
          </div>
		{% endfor %}
	  </fieldset>	  	
	  <fieldset>
	    <legend>{{ entry_fields_bank }}</legend>			  
		{% for field in bank_fields %}
          <div class="form-group">
            <label class="col-sm-2 control-label" for="bank_fields_{{ field }}">{{ text_bank[field] }}</label>
            <div class="col-sm-10">
              <select name="account_combine_fields[bank][{{ field }}]" id="bank_fields_{{ field }}" class="bank_fields form-control">
				{% if account_combine_fields['bank'][field] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}
              </select>
            </div>
          </div>
		{% endfor %}	
		</fieldset>		
		<fieldset>
		<legend>{{ heading_account_register }}</legend>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="rform-affiliate">{{ entry_form_affiliate_choice }}</label>
            <div class="col-sm-10">
              <select name="account_combine_rform[affiliate]" id="rform-affiliate" class="form-control">
				<option value="c"{% if account_combine_rform['affiliate'] == 'c' %} selected="selected"{% endif %}>{{ text_option_affiliate_choice }}</option>
				<option value="y"{% if account_combine_rform['affiliate'] == 'y' %} selected="selected"{% endif %}>{{ text_option_affiliate_force }}</option>
				<option value="n"{% if account_combine_rform['affiliate'] == 'n' %} selected="selected"{% endif %}>{{ text_no }}</option>	
              </select>
            </div>
          </div>	
<!--          <div class="form-group">
            <label class="col-sm-2 control-label" for="rform-website_textarea"><span data-toggle="tooltip" title="{{ help_form_website_textarea }}">{{ entry_form_website_textarea }}</span></label>
            <div class="col-sm-10">
              <select name="account_combine_rform[website_textarea]" id="rform-website_textarea" class="form-control">
				{% if account_combine_rform['website_textarea'] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}
              </select>
            </div>
          </div> -->
          <div class="form-group">
            <label class="col-sm-2 control-label" for="rform-agreement">{{ entry_form_agreement }}</label>
            <div class="col-sm-10">
              <select name="account_combine_rform[agreement]" id="rform-agreement" class="form-control">
				{% if account_combine_rform['agreement'] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}
              </select>
            </div>
          </div>
	  {% for field in aff_fields %}
          <div class="form-group">
            <label class="col-sm-2 control-label" for="rform_f_{{ field }}">{{ entry_form[field] }}</label>
            <div class="col-sm-10">
              <select name="account_combine_rform[show_{{ field }}]" id="rform_f_{{ field }}" class="form-control">
				{% if account_combine_rform["show_#{field}"] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}				
              </select>
            </div>
          </div>
	  {% endfor %}
	  </fieldset>					
		<fieldset>
		<legend>{{ heading_checkout_register }}</legend>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="cform-affiliate">{{ entry_form_affiliate_choice }}</label>
            <div class="col-sm-10">
              <select name="account_combine_cform[affiliate]" id="cform-affiliate" class="form-control">
				<option value="c"{% if account_combine_cform['affiliate'] == 'c' %} selected="selected"{% endif %}>{{ text_option_affiliate_choice }}</option>
				<option value="y"{% if account_combine_cform['affiliate'] == 'y' %} selected="selected"{% endif %}>{{ text_option_affiliate_force }}</option>
				<option value="n"{% if account_combine_cform['affiliate'] == 'n' %} selected="selected"{% endif %}>{{ text_no }}</option>	
              </select>
            </div>
          </div>
<!--          <div class="form-group">
            <label class="col-sm-2 control-label" for="cform-website_textarea"><span data-toggle="tooltip" title="{{ help_form_website_textarea }}">{{ entry_form_website_textarea }}</span></label>
            <div class="col-sm-10">
              <select name="account_combine_cform[website_textarea]" id="cform-website_textarea" class="form-control">
				{% if account_combine_cform['website_textarea'] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}		
              </select>
            </div>
          </div> -->
          <div class="form-group">
            <label class="col-sm-2 control-label" for="cform-agreement">{{ entry_form_agreement }}</label>
            <div class="col-sm-10">
              <select name="account_combine_cform[agreement]" id="cform-agreement" class="form-control">
				{% if account_combine_cform['agreement'] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}		
              </select>
            </div>
          </div>
	  {% for field in aff_fields %}
          <div class="form-group">
            <label class="col-sm-2 control-label" for="cform_f_{{ field }}">{{ entry_form[field] }}</label>
            <div class="col-sm-10">
              <select name="account_combine_cform[show_{{ field }}]" id="cform_f_{{ field }}" class="form-control">
				{% if account_combine_cform["show_#{field}"] %}
				<option value="1" selected="selected">{{ text_yes }}</option>
				<option value="0">{{ text_no }}</option>
				{% else %}
				<option value="1">{{ text_yes }}</option>
				<option value="0" selected="selected">{{ text_no }}</option>
				{% endif %}
              </select>
            </div>
          </div>
	  {% endfor %}
	  </fieldset>	  
     </form>
    </div>
  </div>
</div>
			

<script type="text/javascript"><!-- 
$(document).ready(function() {
	var _error_class = 'text-danger';
	var _payment_methods = {{ payment_methods_json }};

	var toggle_select = function(_el, enable) {
		if($(_el).attr('disabled') && enable) {
			var _hid = $(_el).data('hid');
			$('#'+_hid).remove();
			$(_el).attr('disabled', false);
			if($(_el).attr('_name') !== undefined && $(_el).attr('_name').length > 0) {
				$(_el).attr('name', $(_el).attr('_name'));
				$(_el).attr('_name', '');
			}
		}
		if(!$(_el).attr('disabled') && !enable) {
			var _hid = 'id'+parseInt(Math.random() * 10000000);
			$(_el).data('hid', _hid);
			$(_el).after('<input type="hidden" name="'+$(_el).attr('name')+'" value="'+$(_el).val()+'" id="'+_hid+'" />');
			$(_el).attr('_name', $(_el).attr('name'));
			$(_el).attr('name', '');
			$(_el).attr('disabled', true);
		}	
	}
	
	$(document).on('click', '#customer-group-now', function() {
		if(parseInt($('select[name="account_combine_customer_group_id"]').val()) < 1) {
			$('#cgn-result-success').after('<div class="'+_error_class+'" id="cgn-result-error">{{ text_customer_group_now_select }}</div>');					
			$('#cgn-result-success').hide();
			return;
		}
		
		if(!confirm('{{ text_customer_group_now_confirm }}')) return;
		
		$.ajax({
			url: '{{ link_customer_group_now }}', 
			data: {customer_group_id : $('select[name="account_combine_customer_group_id"]').val()}, 
			type: 'post',
			cache: false,			
			beforeSend: function() {
				$('#customer-group-now').button('loading');
				$('#cgn-result-success').hide();
				$('#cgn-result-error').remove();				
			},
			complete: function() {
				$('#customer-group-now').button('reset');
			},
			success: function(_r) {			
				if(_r < 0) {				
					$('#cgn-result-success').after('<div class="'+_error_class+'" id="cgn-result-error">{{ text_error }}</div>');									
				} else {				
					$('#cgn-result-success').show();
				}		
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}			
		});
	});
	
	$(document).on('change', '[name="account_combine_allow_own_aff"]', function() {
		if($(this).val() == '0') $('[name="account_combine_own_parent"]').val('0');
		toggle_select($('[name="account_combine_own_parent"]'), ($(this).val() == '0' ? false : true));
	});

	$(document).on('change', '[name="account_combine_own_parent"]', function() {
		if($(this).val() == '1') $('[name="account_combine_allow_own_aff"]').val('1');
		toggle_select($('[name="account_allow_own_aff"]'), ($(this).val() == '1' ? false : true));
	});
	
	
	$(document).on('change', '.aff_f', function() {
		var _id1 = '#rform_'+$(this).attr('id');
		var _id2 = '#cform_'+$(this).attr('id');
		toggle_select($(_id1), true);
		toggle_select($(_id2), true);		
		if($(this).val() != 'y') {
			$(_id1).val($(this).val() == 'n' ? '0' : '1');
			$(_id2).val($(this).val() == 'n' ? '0' : '1');
			toggle_select($(_id1), false);
			toggle_select($(_id2), false);			
		}		

		if($(this).attr('id') == 'f_payment') {
			var _val = $(this).val();
			if(_val == 'n') {
				$('.f_payment').val('0');
				$('.f_payment').trigger('change');				
			} 
			$('.f_payment').each(function(_i, _el) {
				toggle_select($(_el), (_val == 'n' ? false : true));
			});			
		}
	});	
	
	$(document).on('change', '.f_payment', function() {
		var _id = $(this).attr('id');
		if($(this).val() == '0') {
			if($('#f_payment').val() != 'n') {
				var _all_disabled = true;
				for(var _i = 0; _i < _payment_methods.length; _i++) {
					if($('#f_'+_payment_methods[_i]).val() == '1') {
						_all_disabled = false;
						break;
					}
				}
				if(_all_disabled === true) {
					$('#f_payment').val('n');
					$('#f_payment').trigger('change');
				} else {
					var _pm = _id.replace(/^f_/, '');
					if($('#f_payment_default').val() == _pm) {
						for(var _i = 0; _i < _payment_methods.length; _i++) {
							if($('#f_'+_payment_methods[_i]).val() == '1') {
								$('#f_payment_default').val(_payment_methods[_i]);
								break;
							}
						}
					}
				}
			}
		}
		
		if(_id == 'f_bank') {
			var _val = $(this).val();
			if(_val == '0')	$('.bank_fields').val('0');
			$('.bank_fields').each(function(_i, _el) {
				toggle_select($(_el), (_val == '0' ? false : true));
			});
		}
	});
		
	$('.triggers').trigger('change');		
	
	$('#form-account-combine').on('submit', function() {
		if($('#f_payment').val() != 'n') {
			var _all_disabled = true;
			for(var _i = 0; _i < _payment_methods.length; _i++) {
				if($('#f_'+_payment_methods[_i]).val() == '1') {
					_all_disabled = false;
					break;
				}
			}
			if(_all_disabled === true) {	
				$('.alert-danger').find('button').before('{{ error_enable_payment }}');
				$('.alert-danger').show();
				return false;
			}
		}
		return true;
	});
});
//--></script></div>
{{ footer }}
