<span tal:replace="structure here/manage_page_header"/>
<span tal:replace="structure here/manage_tabs"/>

<p class="page-description">
	This form allows you to <strong>add/update/delete</strong> dynamic properties to objects of type 
	<strong tal:content="here/id" />. The properties will be added automatically to both existing objects 
	and to the once that will be created in the future. The existing objects will have the default value
	specified and the new ones will have this value if no other is provided.
</p>
<tal:block tal:define="param_id python:request.get('id', None); dp_data python:here.getDynamicPropertyData(param_id)">
<form method="post" tal:attributes="action python:test(dp_data[0]=='add', 'manageAddDynamicProperty', 'manageUpdateDynamicProperty')">
<table border="0" cellspacing="2" cellpadding="2" width="100%">
    <tr style="background-color: #EFEFEF">
        <td class="form-label" width="20%">Id</td>
        <td width="80%">
            <tal:block tal:condition="python:dp_data[0]=='update'">
                <strong tal:content="python:dp_data[1]">id</strong>
                <input type="hidden" name="id" tal:attributes="value python:dp_data[1]" />
            </tal:block>
            <tal:block tal:condition="python:dp_data[0]=='add'">
                <input type="text" name="id" size="20" tal:attributes="value python:dp_data[1]">
            </tal:block>
        </td>
    </tr>
    <tr>
        <td class="form-label">Searchable</td>
        <td><input type="checkbox" name="searchable" tal:attributes="checked python:dp_data[2]==1" /></td>
    </tr>
    <tr style="background-color: #EFEFEF">
        <td class="form-label">Name</td>
        <td><input type="text" name="name" size="30" tal:attributes="value python:dp_data[3]" /></td>
    </tr>
    <tr>
        <td class="form-label">Type</td>
        <td>
            <select id="type" name="type" onchange="javascript:show_selection()">
                <option tal:repeat="item here/getFieldTypesList"
                        tal:attributes="value item; selected python:item==dp_data[4]"
                        tal:content="item">type</option>
            </select>
        </td>
    </tr>
    <tr style="background-color: #EFEFEF">
        <td class="form-label">Required</td>
        <td><input type="checkbox" name="required" tal:attributes="checked python:dp_data[5]==1" /></td>
    </tr>
    <tr>
        <td class="form-label">Default value</td>
        <td><input type="text" name="defaultvalue" size="20" tal:attributes="value python:dp_data[6]" /></td>
     </tr>
    <tr style="background-color: #EFEFEF">
        <td class="form-label" style="vertical-align: top">Values</td>
        <td>
            <div id="sel_list" tal:define="reflists python:here.getPortletsTool().getRefTrees()">
                <p>Populate the selection with values from an existing <em>selection list</em>:</p>
                <em tal:condition="not:reflists">No selection lists defined</em>
                <select tal:condition="reflists" id="ref_list" name="ref_list" style="display: block" onchange="javascript:toggle_values(this)">
                    <option value=""></option>
                    <option tal:repeat="ref reflists"
                            tal:attributes="value ref/getId; selected python: ref.getId() in dp_data[9]"
                            tal:content="ref/title_or_id" />
                </select>
                <p>or provide the values in the textarea below, one on a row.</p>
            </div>
            <textarea id="values" name="values" rows="5" cols="70" tal:content="python:dp_data[7]">values</textarea>
        </td>
     </tr>
    <tr>
        <td class="form-label">Listing order</td>
        <td><input type="text" name="order" size="2" maxlength="2" tal:attributes="value python:dp_data[8]" /></td>
     </tr>
     <tr style="background-color: #EFEFEF">
        <td class="form-label">&nbsp;</td>
        <td><input type="submit" tal:attributes="value python:test(dp_data[0]=='add', 'Add', 'Update')" /></td>
     </tr>
</table>
</form>
</tal:block>

<form method="post" action="manageDeleteDynamicProperties">
<table border="1" cellspacing="0" cellpadding="2" width="100%">
    <tr class="row-header">
        <th class="cell-header">&nbsp;</th>
        <th class="cell-header">Id</th>
        <th class="cell-header">Searchable</th>
        <th class="cell-header">Name</th>
        <th class="cell-header">Type</th>
        <th class="cell-header">Required</th>
        <th class="cell-header">Default value</th>
        <th class="cell-header">Values</th>
        <th class="cell-header">Listing order</th>
    </tr>
    <tr tal:repeat="record here/getDynamicProperties" tal:attributes="class python:test(path('repeat/record/odd'), 'row-odd', 'row-even')">
        <td><input type="checkbox" name="id" tal:attributes="value record/id"></td>
        <td><a tal:attributes="href string:manage_properties_html?id=${record/id}" tal:content="record/id">Id</a></td>
        <td tal:content="record/searchable">Searchable</td>
        <td tal:content="record/name">Name</td>
        <td tal:content="record/type">Type</td>
        <td tal:content="record/required">Required</td>
        <td tal:content="record/defaultvalue">Default value</td>
        <td tal:content="record/getvalues">Values</td>
        <td tal:content="record/order">Listing order</td>
    </tr>
</table>
<br />
<input type="submit" value="Delete selected properties" />
</form>

<script language="JavaScript">
window.onload = function() {show_selection();}
function show_selection() {
    var type = document.getElementById('type');
    var rl = document.getElementById('sel_list');
    if (type.value != 'selection') rl.style.display = 'none';
    else{
		rl.style.display = 'block'
		toggle_values(document.getElementById('ref_list'))
	}
}

function toggle_values(elem) {
    document.getElementById('values').disabled = elem.value?true:false
}
</script>

<span tal:replace="structure here/manage_page_footer"/>