<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<xsp:page language="java"
          xmlns:xsp="http://apache.org/xsp"
          xmlns:esql="http://apache.org/cocoon/SQL/v2">
  <page>
    <xsp:logic>
      String empID = request.getParameter("employee");
      String depID = request.getParameter("department");
      String name = request.getParameter("name");
      String message = (String) request.getAttribute("message");
    </xsp:logic>

    <resources>
      <resource type="file" href="employee.xml?cocoon-view=pretty-content">Descriptor</resource>
      <resource type="file" href="../schema.sql">Schema</resource>
      <resource type="doc" href="http://cocoon.apache.org/2.1/userdocs/database-actions.html">Actions</resource>
      <resource type="doc" href="http://cocoon.apache.org/2.1/userdocs/logicsheets/esql.html">ESQL</resource>
    </resources>

    <title>Manage Employees</title>
    <para style="backgroud-color:#ff0000"><xsp:expr>(message == null?"":message)</xsp:expr></para>
    <content>
      <form name="EmployeeSelection">
        <select size="1" name="employee"
          onChange="top.location.href=window.location.pathname + '?employee=' + document.forms['EmployeeSelection'].employee.options[document.forms['EmployeeSelection'].employee.options.selectedIndex].value">
          <option value="-1">-- New employee --</option>
          <esql:connection>
            <esql:pool>personnel</esql:pool>
            <esql:execute-query>
              <esql:query>select id, department_id, name from employee order by name</esql:query>
              <esql:results>
                <esql:row-results>
                 <option>
                  <xsp:logic>
                        if (<esql:get-string column="id"/>.equals(empID)) {
                            depID = <esql:get-string column="department_id"/>;
			      </xsp:logic>
			       	<xsp:attribute name="selected">selected</xsp:attribute>
			       <xsp:logic>
                        }
                  </xsp:logic>
                    <xsp:attribute name="value"><esql:get-int column="id"/></xsp:attribute>
                    <esql:get-string column="name"/>
                  </option>
                </esql:row-results>
              </esql:results>
            </esql:execute-query>
          </esql:connection>
        </select>
      </form>

      <form action="" method="POST" name="EmployeeEntry">
        <script>document.forms["EmployeeEntry"].action = window.location.pathname</script>
        <input type="hidden" name="employee">
          <xsp:attribute name="value"><xsp:expr>((empID != null) ? empID : "")</xsp:expr></xsp:attribute>
        </input>
        <para>Employee Name:
          <input name="name" type="text">
            <xsp:attribute name="value"><xsp:expr>((name != null) ? name : "")</xsp:expr></xsp:attribute>
          </input>
        </para>
        <para>Department:
          <select name="department">
            <option value="-1">-- Select department --</option>
            <esql:connection>
              <esql:pool>personnel</esql:pool>
              <esql:execute-query>
                <esql:query>select id, name from department order by name</esql:query>
                <esql:results>
                  <esql:row-results>
                  	<option>
                    <xsp:logic>
                        if (<esql:get-string column="id"/>.equals(depID)) {
			        </xsp:logic>
			       	  <xsp:attribute name="selected">selected</xsp:attribute>
			       	<xsp:logic>
                        }
                    </xsp:logic>
                      <xsp:attribute name="value"><esql:get-int column="id"/></xsp:attribute>
                      <esql:get-string column="name"/>
                    </option>
                  </esql:row-results>
                </esql:results>
              </esql:execute-query>
            </esql:connection>
          </select>
        </para>
        <xsp:logic>
          <![CDATA[
          if (empID != null && !empID.equals("-1")) {
          ]]>
            <input type="submit" name="cocoon-action-Update" value="click here to update"/>
            <input type="submit" name="cocoon-action-Delete" value="click here to delete"/>
          } else {
            <input type="submit" name="cocoon-action-Add" value="click here to add"/>
          }
        </xsp:logic>
      </form>
    </content>
  </page>
</xsp:page>
