<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/">
  <xi:include href="admin.html" />
  <head>
    <title>チケットサブミットポリシー</title>
  </head>
  <body>
    <h2>チケットサブミットポリシー</h2>
    <div py:if="not saved">
      ポリシーは保存されていません。<a href="#apply">'変更を適用'ボタン</a>をクリックして保存してください。
    </div>

    <form method="post">
      <fieldset>
	<legend>ポリシーの編集</legend>

    <div py:for="name in sorted(policies.keys())" style="background: #f7f7f7; border: 1px solid #d7d7d7; padding: 2px .5em;">
      <h2>${name}</h2>

      <input type="hidden" name="policy" value="${name}"/>

      <div>
	<h3>ポリシーの適用条件</h3>
	
	  <table>
	    <tr>
	      <th>フィールド</th><th>条件</th><th>値</th>
	    </tr>
	    <tr py:for="index, condition in enumerate(policies[name]['condition'])">
	      
	      <!-- field -->
	      <td class="field">
		<select name="condition_field_${index}_${name}">
		  <py:for each="field in fields">
		    <option py:attrs="{'selected': condition['field']
				      == field['name'] or None,
		                       'value': field['name']}">
		      ${field['label']}
		    </option>
		  </py:for>
		</select>
	      </td>
	      
	      <!-- comparitor -->
	      <td class="comparitor">
		<select name="condition_comparitor_${index}_${name}">
		  <py:for each="comparitor in comparitors">
		     <option py:attrs="{'selected':
				       condition['comparitor'] ==
				       comparitor or None}">
		       ${comparitor}
		     </option>
		  </py:for>
		</select>
	      </td>
	      
	      <!-- value(s) for comparison -->
	      <td>
		<input type="text"
		       name="condition_value_${index}_${name}" 
		       value="${isinstance(condition['value'], list) and ', '.join(condition['value']) or condition['value']}"/>
	      </td>

	      <td class="actions">
		<input type="submit" name="rm_condition_${index}_${name}" value="-"/>
	      </td>
	    </tr>
	  </table>
	  <input type="submit" name="add_condition_${name}" value="+"/>
      </div>

      <div>
	<h3>ポリシー</h3>
	
	<table class="wiki">
	<tr py:for="action in policies[name]['actions']">
	  <td style="width: 1.5em;">
	    <b>${action['label']}</b>
	  </td>
	  <td>
	    <table class="">
	      <tr py:for="row in group(fields, 4)">
		<py:for each="idx, field in enumerate(row)">
		  <py:if test="field">
		    <td>
		      <input type="checkbox"
			name="action_${action['name']}_${name}"
			value="${field['name']}"
			py:attrs="{'checked': field['name'] in
			action['args'] or None}"
			/>
		      <label for="action_${action['name']}_${name}">${field['label']}</label>
		    </td>
		  </py:if>
		</py:for>
	      </tr>
	    </table>
	  </td>
	  <td class="actions">
	    <input type="submit" name="rm_action_${action['name']}_${name}" value="-"/>
	  </td>
	</tr>
	</table>
	
	<!-- new policy checkbox -->
	<py:if test="len(policies[name]['actions']) !=
		     len(self_actions)">
	  ルールの追加: 
	  <select name="add_action_${name}">
	    <option></option>
	    <py:for each="action in [ i for i in self_actions
	  if i.name() not in [ j['name'] for j in policies[name]['actions']]]">
	      <option value="${action.name()}">${action.label()}</option>
	    </py:for>	      	      
	  </select>

	  <!-- dummy submit button for action adding -->
	  <input type="submit" name="actionbutton_${name}" value="+"/>
	</py:if>

      </div>

      <input type="submit" name="remove_${name}" value="削除"/>

    </div>

    <div>
      <input type="submit" name="apply" id="apply" value="変更を適用"/>
    </div>
  </fieldset>
      <div>
	<input type="text" name="new-policy"/>
	<input type="submit" name="add-policy" value="ポリシーを追加"/>
      </div>
    </form>

    <fieldset>
      <legend>現在のポリシー(trac.ini上の設定)</legend>
      <pre>${current_policy}</pre>
    </fieldset>
    
    
  </body>
</html>
