{% extends "base.html" %}

{% block body %}
<form>
  <div class = "row">
    <div class = "col-md-3">
      Previous URCPP Funding Years
    </div>
    <div class = "col-md-6">
      <input type = "checkbox" name = "previousYears" id = "oneyr" value = "1" >Last Year
      <br> 
      <input type = "checkbox" name = "previousYears"  id = "twoyr" value = "2" >2 Years Ago
      <br>
      <input type = "checkbox" name = "previousYears" id = "threeToFiveyr" value = "3-5" >3 - 5 Years Ago 
      <br> 
      <input type = "checkbox" name = "previousYears"  id = "sixToTenyr" value = "5-10" >6 - 10 Years Ago
      <br>
      <input type = "checkbox" name = "previousYears" id = "elevenPlus" value = "11+" >11 or More Years
      <br>
      Will your project be involved with a non-religious non-profit, or government agency?
      <br>
      <input type = "radio" name = "relOrGov" id = "communityPartnerYes" value = "Yes" onChange = "">Yes
      <br> 
      <input type = "radio" name = "relOrGov"  id = "communityPartnerNo" value = "No" onChange = "">No
      <br>
      Does you project qualify as a service to the community?
      <br>
      <input type = "radio" name = "community" id = "isServiceToCommunityYes" value = "Yes" onChange = "">Yes
      <br> 
      <input type = "radio" name = "community"  id = "isServiceToCommunityNo" value = "No" onChange = "">No
      <br>
    </div>
  </div>
  <div class = "row">
    <div class = "col-md-6">
      Current/Pending Funding
    </div>
    <div class = "col-md-6">
      <textarea rows = "6" id = "currentPendingFunds">
        </textarea>
    </div>
  </div>
  <div class = "form-group"  >
    <div class = "col-sm-offset-8 col-sm-2" 
         id = "submitButton"
         style = "display: none;">
      <button id = "submit" 
              type = "button" 
              class="btn btn-block btn-primary"
              onClick = "submitData();"     
              class = "form-control"
              />
        Submit
      </button>
    </div>
  </div>
</form>

 <!--Is this a generic function for every submit button? -->
 <!--Submit data to server -->
<script type = "text/javascript">
/* global api, username, formToObj, show */
  var submitData = function () {
    var obj = formToObj(document.querySelector("form"));
    console.log("Sending data to server: " + JSON.stringify(obj) + "\n\n");
    // api.set.start(username, obj, function (data) { console.log (data); } ); 
    // api.go();
  };
</script>

<script type = "text/javascript">
  /* global urcpp, username */
  var api = urcpp('v1');
  
  var show = function (d) {
     console.log (d);
     $("#echo").text(JSON.stringify (d));
     };
  
  var setPreviousFunding = function (data) {
    // Set previous year checkboxes based on response
    /* global yearsFunded */
    if (data.response == "OK") {
      // Populates the checkboxes for previous years funded
      yearsFunded = JSON.parse(JSON.stringify(eval('(' + data.faculty.yearsFunded + ')')));
      
      if (yearsFunded.hasOwnProperty("oneyr")) {
        $("#oneyr").prop( "checked" , true);
      } 
      if (yearsFunded.hasOwnProperty("twoyr")) {
        $("#twoyr").prop( "checked" , true);
      } 
      if (yearsFunded.hasOwnProperty("threeToFiveyr")) {
        $("#threeToFiveyr").prop( "checked" , true);
      } 
      if (yearsFunded.hasOwnProperty("sixToTenyr")) {
        $("#sixToTenyr").prop( "checked" , true);
      } 
      if (yearsFunded.hasOwnProperty("elevenPlus")) {
        $("#elevenPlus").prop( "checked" , true);
      } 
      
      // Populates the current/pending funding textarea
      if (data.faculty.relatedFunding) {
        $("#currentPendingFunds").text(data.faculty.relatedFunding);
      }
    }
  }
  
  api.faculty.get(username, setPreviousFunding);
  
  var setAgencyRadios = function (data) {
    if (data.response == "OK") {
      // Populates the radios for Nonprofit etc.
      if (data.project.hasCommunityPartner) {
        $("#communityPartnerYes").prop("checked", true);
      } else {
       // Check the "no" box if user said no previous; need a third state (null) 
       // for never touched
      }
      
      // Populates the radios for service to the community
      if (data.project.isServiceToCommunity) {
        $("#isServiceToCommunityYes").prop("checked", true);
      } else {
       // Check the "no" box if user said no previous; need a third state (null) 
       // for never touched
      }  
    } 
  }

  
  api.projects.get(username, setAgencyRadios);
  
  api.go();
  
</script>

{% endblock %}