<html xmlns:tal="http://xml.zope.org/namespaces/tal"
  xmlns:metal="http://xml.zope.org/namespaces/metal" i18n:domain="senaite.core">

<metal:view_macro define-macro="view"
  tal:define="innerJoin field/innerJoin;
      outerJoin field/outerJoin;
      subfieldViews python:field.getSubfieldViews(here,innerJoin)"
  tal:replace="structure python:outerJoin.join(subfieldViews)" />

<metal:define define-macro="edit">
  <metal:use use-macro="field_macro | here/widgets/field/macros/edit">
    <metal:fill fill-slot="widget_body">
      <tal:define define="addr python:request.form.get(fieldName, accessor());
                          possibles here/getPossibleAddresses;
                          country python:addr.get('country', '');
                          state python:addr.get('state','');
                          district python:addr.get('district','');
                          city python:addr.get('city','');
                          zip python:addr.get('zip','');
                          address python:addr.get('address','');
                          defcountry python:country or widget.getDefaultCountry();">
        <fieldset>
          <span tal:replace="structure context/@@authenticator/authenticator" />
          <legend i18n:translate=""
                  tal:content="python:widget.Label(here)"
                  tal:condition="widget/showLegend">
            Address Field
          </legend>
          <table>
            <tr>
              <!-- Country Label -->
              <td class="field">
                <label i18n:translate="" tal:attributes="for string:${fieldName}_country">Country</label>
              </td>
              <!-- Country Selection -->
              <td>
                <select size="1"
                        tal:attributes="name string:${fieldName}.country:record;
                                        id string:${fieldName}.country"
                        tal:define="values python:widget.getCountries();">
                  <option value=""></option>
                  <tal:x tal:repeat="value values">
                    <option tal:content="python:value[1]"
                            tal:attributes="value python:value[1];
                                            selected python:value[1] == country and 'selected' or (country=='' and value[0] == defcountry and 'selected' or '')">
                    </option>
                  </tal:x>
                </select>
              </td>
              <!-- Copy From -->
              <td class="field" style="padding-left: 2em;" tal:condition="widget/showCopyFrom">
                <label i18n:translate="">Copy from</label>
                <select size="1" id="" tal:attributes='id string:${fieldName}.selection;'>
                  <option></option>
                  <tal:possibles tal:repeat="possible possibles">
                    <option tal:content="possible"
                            tal:condition="python:possible != fieldName"
                            tal:attributes="value possible"></option>
                  </tal:possibles>
                </select>
              </td>
            </tr>

            <tr>
              <!-- State Label -->
              <td class="field">
                <label i18n:translate="" tal:attributes="for string:${fieldName}_state">State</label>
              </td>
              <!-- State Label -->
              <td>
                <select size="1"
                        tal:attributes="name string:${fieldName}.state:record;
                                        id string:${fieldName}.state"
                        tal:define="values python:widget.getStates(defcountry)">
                  <option value=''></option>
                  <tal:x tal:repeat="value values">
                    <option tal:content="python:value[2]"
                            tal:attributes="value python:value[2];
                                            selected python:value[2] == state and 'selected' or '';">
                    </option>
                  </tal:x>
                </select>
              </td>
            </tr>

            <tr tal:condition="widget/showDistrict">
              <!-- District Label -->
              <td class="field">
                <label i18n:translate="" tal:attributes="for string:${fieldName}_district">District</label>
              </td>
              <!-- District Selection -->
              <td>
                <select size="1"
                        tal:attributes="name string:${fieldName}.district:record;
                                        id string:${fieldName}.district"
                        tal:define="values python:widget.getDistricts(defcountry, state)">
                  <option value=''></option>
                  <tal:x tal:repeat="value values">
                    <option tal:content="python:value[2]"
                            tal:attributes="value python:value[2];
                                            selected python:value[2] == district and 'selected' or '';">
                    </option>
                  </tal:x>
                </select>
              </td>
            </tr>

            <tr tal:condition="widget/showCity">
              <!-- City Label -->
              <td class="field">
                <label i18n:translate="" tal:attributes="for string:${fieldName}_city">City</label>
              </td>
              <!-- City Input Field -->
              <td>
                <input tal:attributes="value city;
                                       name string:${fieldName}.city:record;
                                       id string:${fieldName}.city" />
              </td>
            </tr>

            <tr tal:condition="widget/showPostalCode">
              <!-- Postal Code Label -->
              <td class="field">
                <label i18n:translate="" tal:attributes="for string:${fieldName}_zip">Postal code</label>
              </td>
              <!-- Postal Code Input Field -->
              <td>
                <input tal:attributes="value zip;
                                       name string:${fieldName}.zip:record;
                                       id string:${fieldName}.zip" />
              </td>
            </tr>

            <tr tal:condition="widget/showAddress">
              <!-- Address Label -->
              <td class="field" valign="top">
                <label i18n:translate="">Address</label>
              </td>
              <td>
                <!-- Address Textarea -->
                <textarea cols="20" rows="3"
                          tal:attributes="name string:${fieldName}.address:record;
                                          id string:${fieldName}.address"
                          tal:content="address">
                </textarea>
              </td>
            </tr>

          </table>
        </fieldset>

      </tal:define>
    </metal:fill>
  </metal:use>
</metal:define>

<div metal:define-macro="search">
  <div metal:use-macro="here/widgets/string/macros/edit"></div>
</div>

</html>
