<script type="text/javascript">
	function upgrade_now() {
    var quickMode = $("#quick-upgrade").prop('checked');
    $("#upgrade-in-progress").show();      
    if (quickMode) { 
      $.getScript("/admin/upgrade?mode=quick");
    } else {
      $.getScript("/admin/upgrade?mode=full");
    }
	}
  
	function generate_random_server_api_key() {
	   var length = 25;
	   var result           = '';
	   var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	   var charactersLength = characters.length;
	   for ( var i = 0; i < length; i++ ) {
	      result += characters.charAt(Math.floor(Math.random() * charactersLength));
	   }
	   $("#server_api_key").val(result);
	   return result;
	}
  
	function clear_server_api_key() {
 	   $("#server_api_key").val("");		
	   return;
	}
  
</script>
	
<h2>
  <i class="fa fa-cog"></i>

	<%= I18n.t(:preferences) %>

  <button id="check-updates-btn" onclick="check_buildwise_updates();" class="btn btn-sm btn-info"><%= I18n.t(:check_updates) %></button>
  <button id="upgrade-btn" style="display:none;" onclick="upgrade_buildwise();" class="btn btn-sm btn-warning"><%= I18n.t(:upgrade) %></button>
  <img id="update-loading" style="display:none;" src="/images/loading.gif"/>
</h2>

<div class="modal fade" tabindex="-1" role="dialog" id="ruby-gems-modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title"><%= I18n.t(:installed_gems) %></h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<%= I18n.t(:close) %>"></button>
      </div>
      <div class="modal-body">
        <pre id="gem_list"></pre>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal" data-bs-dismiss="modal"><%= I18n.t(:close)%></button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<div class="modal fade" tabindex="-1" role="dialog" id="upgrade-modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title"><%= I18n.t(:upgrade_buildwise) %></h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<%= I18n.t(:close) %>"></button>
      </div>
      <div class="modal-body">
				
				<div id="new-version-available" class="alert alert-success"></div>			
				
				<div id="upgrade-in-progress" class="progress" style="display:none;">
				  <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
				</div>
				
      </div>
      <div class="modal-footer">
        
        <div class="pull-left"><input type="checkbox" id="quick-upgrade" checked name="quick-mode" value="value"/> <%=I18n.t(:quick_mode)%>
        </div>
        
        <button type="button" class="btn btn-sm btn-primary"  onclick="upgrade_now();"><%= I18n.t(:upgrade_now)%></button>
      </div>			
    </div><!-- /.modal-content -->
					
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<script type="text/javascript">
  function upgrade_buildwise() {
    bootbox.confirm("<%= I18n.t(:confirm_upgrade) %>", function(result) {
      if (result) {
        $("#update-loading").show();
        $.getScript("/admin/upgrade");
      }
    });
  }

  function delete_project(project_id) {
    bootbox.confirm("Delete the Project?", function(result) {
      if (result) {
        $.getScript("/admin/delete_project/" + project_id);
      }
    });
  }

  function show_project_for_public(project_id) {
    bootbox.confirm("Make this project viewable by the public?", function(result) {
      if (result) {
        $.getScript("/admin/show_project_for_public/" + project_id);
      }
    });
  }

  function hide_project_for_public(project_id) {
    bootbox.confirm("Hide project from the public?", function(result){
      if (result) {
        $.getScript("/admin/hide_project_for_public/" + project_id);
      }
    })
    
  }


  function check_buildwise_updates(){
    $("#update-loading").show();
    $.getScript("/admin/check-updates");
  }
	
	function select_locale(val) {
		if (val == 'jp') {
			$("#new-lang-intro").show();
		}	else {			
			$("#new-lang-intro").hide();
		}
	}

</script>

<form action="/admin/settings" method="POST">

  <div class="row">

    <div class="col-md-7">

      <div id="" style="padding: 20px; font-size: 15px;">

        	
	        <div class="form-group form-group-sm row">
	          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:company) %>: </label>
	          <div class="col-sm-8">
	            <input type="text" name="company_name" class="form-control"  autocomplete="off" value="<%= settings.company_name %>"/>							
	          </div>
	        </div>
					
        <div class="form-group form-group-sm row">
          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:language)%>: </label>
          <div class="col-sm-8">
            <select id='locale' name="locale" class="form-control" onchange='select_locale(this.value);'>
              <option value="en" <% if I18n.locale && I18n.locale.to_s == 'en' %> selected <% end %>>English</option>
              <option value="zh_cn" <% if I18n.locale && I18n.locale.to_s == 'zh_cn' %> selected <% end %> >Chinese </option>
              <option value="jp" <% if I18n.locale && I18n.locale.to_s == 'jp' %> selected <% end %> >Japanese </option>
            </select>
						<div id="new-lang-intro" style="display:none; margin-top: 10px; font-weight: 300;">The Japanese translation is newly added, if you know Japanese well, please help us to enhance the translation file <code>app/locale/jp.yml</code>, thanks.</div>
          </div>
        </div>

        <div class="form-group form-group-sm row">
          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:projects_per_page)%>: </label>
          <div class="col-sm-2">          
	            <input type="number" min="5" max="20" id="num_projects_per_page" name="num_projects_per_page" class="form-control"  autocomplete="off" value="<%= settings.num_projects_per_page %>"/>
          </div>
          <div class="col-sm-6" style="padding-top: 2px;">          
              <span class="hint" style="font-size: 0.9em;" >
                <i><%= I18n.t(:num_projects_per_page_hint) %></i>
              </span>
          </div>
        </div>
        
        <div class="form-group form-group-sm row">
          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:report)%></label>
          <div class="col-sm-8">
						
						<div class="input-group" style="margin-bottom: 10px;">
              <div class="input-group-prepend">
                <span class="input-group-text" style="width: 140px;"><%= I18n.t(:chart_height) %></span>
              </div>						  
						  <input type="text" class="form-control" placeholder="e.g. 480" aria-describedby="basic-addon1" name="chart_height" style="width: 40px;" value="<%= settings.chart_height %>" width="12"/>
						</div>						

						<div class="input-group">
              <div class="input-group-prepend">
                <span class="input-group-text" style="width: 140px;"><%= I18n.t(:max_test_history) %></span>
              </div>											  
							<input type="number" name="max_test_file_history" class="form-control" style="width: 100px;" autocomplete="off" value="<%= settings.max_test_file_history %>" width="12"/>
						</div>						

						<div class="input-group" style="margin-top: 10px;">
              <div class="input-group-prepend">
                <span class="input-group-text" style="width: 240px;"><%= I18n.t(:dynamic_chart_refresh_interval) %></span>
              </div>											  
							<input type="number" name="dynamic_chart_refresh_interval" class="form-control" style="width: 100px;" autocomplete="off" value="<%= settings.dynamic_chart_refresh_interval %>" width="12"/>
              <div class="input-group-append" style="margin-left: 0.5rem; color: #AAA; padding-top: 8px;">
                ms
              </div>
						</div>						


          </div>
        </div>

	        <div class="form-group form-group-sm row">
	          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:server_api_key) %>: 
        			<div style="margin-top: 8px;">
        			  <button id="generate_api_key" class="btn btn-default btn-xs" onclick="generate_random_server_api_key(); return false;">Generate</button>			
        			  <button id="disable_api" class="btn btn-default btn-xs" onclick="clear_server_api_key(); return false;">Disable</button>						
        			</div>
            </label>
	          <div class="col-sm-8">
	            <input type="text" id="server_api_key" name="server_api_key" class="form-control"  autocomplete="off" value="<%= settings.server_api_key %>"/>
              <span class="hint hint-below" style="font-size: 0.9em;">
                <i><%= I18n.t(:server_api_key_hint) %></i>
              </span>
	          </div>
	        </div>
          
				<% if $supported_scms.any?  %>
        <div class="form-group form-group-sm row">
          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:source_control) %></label>
          <div class="col-sm-9" style="margin-top: 5px;">
            <div style="margin-bottom: 10px;">
              <% $supported_scms.sort{|a,b| a[0] <=> b[0]}.each do |name, value| %>
                  <% if value %>
                      <span class="label label-pill label-info" style="font-size: 15px;" ><%= name.humanize %></span> &nbsp;
                  <% end %>
              <% end %>
            </div>
            <div class="hint hint-below"  style="font-size: 0.9em;">
              <i><%= I18n.t(:scm_check_intro) %></i>
            </div>

          </div>
        </div>
				<% end %>
      

        <div class="form-group form-group-sm row">
          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:testwise_integration) %></label>
          <div class="col-sm-9" style="margin-top: 5px;">
            <input type="hidden" name="testwise_integration" value="off" />
            <input type="checkbox" name="testwise_integration" <% if settings.testwise_integration %>checked<% end %> value="on"/>
            <span class="hint hint-below" style="font-size: 0.9em;">
              <i><%= I18n.t(:show_testwise_link_intro) %></i>
							<!-- local working folder is set and TestWise registry file is applied. -->
            </span>
          </div>
        </div>

        <div class="form-group form-group-sm row">
          <label class="col-sm-3 form-control-label" for="contact_header"><%= I18n.t(:execution_environment)%> </label>
          <div class="col-sm-9" style="margin-top: 5px;">
						<pre>RUBY_VERSION: <%= RUBY_VERSION %>
RUBY_PATH: <%= RbConfig::CONFIG["bindir"] %>
BUILDWISE_HOME: <%= BUILDWISE_HOME %></pre>

            <button class="btn btn-sm btn-default" type="button"  onclick='$.getScript("/admin/gems")'>
                Gems <span class="badge badge-dark"><%= @local_gems.size rescue 0 %></span>
            </button>  <i style="font-size:0.9em;"><%= raw I18n.t(:update_gems_intro) %></i>

          </div>
        </div>

      </div>

    </div>


    <div class="col-md-5">

      <h4><%= I18n.t(:manage_projects) %></h4>
			<p><%= I18n.t(:manage_projects_intro) %></p>
      <table id="manage-project-table" class="table table-striped table-bordered table-sm" style="width: 96%">
				<tbody>
        <% Project.retrieve_all.each do |project| %>
            <tr id="project_<%= project.id %>_row">
              <td style="padding: 8px;"><%= project.name %></td>
              <td style="text-align: right; padding-top: 5px;" >
                <% if project.active %>
                    <a href="#" id="project_<%= project.id %>" onclick='$.getScript("/admin/suspend_project/<%=project.id %>")' class="change-project-btn btn btn-xs btn-default" data-toggle='tooltip' data-placement='top' title='<%= I18n.t(:click_to_hide_project)%>' ><%= I18n.t(:suspend) %></a>
                <% else %>
                    <a href="#" id="project_<%= project.id %>" onclick='$.getScript("/admin/activate_project/<%=project.id %>")' class="change-project-btn btn btn-xs btn-success" data-toggle='tooltip' data-placement='top' title='<%= I18n.t(:click_to_enable_project)%>' ><%= I18n.t(:activate) %></a>
                    <a href="#" id="project_<%= project.id %>" onclick='delete_project(<%=project.id %>)' class="change-project-btn btn btn-xs btn-danger"><%= I18n.t(:delete)%></a>
                <% end %>
								
                <% if project.public_viewable %>								
								<a href="#"  id="project_<%= project.id %>" onclick='hide_project_for_public(<%=project.id %>)' class="btn btn-default btn-xs"><i class="fa fa-eye-slash" aria-hidden="true" data-toggle='tooltip' data-placement='top' title='<%= I18n.t(:make_project_viewable_only_to_project_members)%>' ></i></a>								
								<% else %>
								<a href="#" id="project_<%= project.id %>" onclick='show_project_for_public(<%=project.id %>)' class="btn btn-default btn-xs "><i class="fa fa-eye" aria-hidden="true" data-toggle='tooltip' data-placement='top' title='<%= I18n.t(:make_project_viewable_to_public)%>'></i></a>																
								<% end %>
              </td>
            </tr>
        <% end %>
				</tbody>
      </table>

    </div>

  </div>


  <hr/>

  <div class="form-actions" align="center" style="padding-bottom: 20px;">
    <input type="submit" value="<%= I18n.t(:update) %>" class="btn btn-primary"/>
    <a href="/" class="btn btn-default"><%= I18n.t(:cancel) %></a>
  </div>

</form>
