<%--
    Document   : admin
    Created on : 20.4.2010, 16:11:51
    Author     : Pavel Jandera
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<c:if test="${RequestData.admin != true}">
    <c:redirect url="index.jsp" />
</c:if>

<sql:setDataSource
  var="dataSource"
  driver="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/videokonference?useUnicode=true&characterEncoding=UTF-8"
  user="videokonf"
  password="Purk118*"
/>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%--<%@ page contentType="text/html" pageEncoding="UTF-8"%>--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Úpravy uživatel</title>
        <link rel="stylesheet" type="text/css" href="style.css" />

    </head>
    <body>
        <div id="header"></div>
        <div id="content" style="min-width: 790px">

        <div id="menu">
            <a style="float: right" href="logout.jsp">Odhlásit</a>
            <a style="float: right" href="profile.jsp">Zpět na profil</a>
        </div>

        <h1>Úpravy uživatele</h1>
        <form style="padding: 0; width: 790px" action="" name="formEditUser" method="GET">
        <div id="register">
        <h3>Základní informace:</h3>

        <sql:query var="uzivatel" dataSource="${dataSource}">
            SELECT * FROM uzivatele WHERE login='${param.login}'
        </sql:query>
        <c:forEach var="row" items="${uzivatel.rows}">
                <p>Jméno: <input type="text" name="jmeno" size="30" value="${row.jmeno}" /><br /></p>
                <p>Příjmení: <input type="text" name="prijmeni" size="30" value="${row.prijmeni}" /><br /></p>
                <p>E-mail: <input type="text" name="mail" size="30" value="${row.mail}" /><br /></p>
                <p>URI: <input type="text" name="uri" size="30" value="${row.uri}" /><br /></p>
                <p>Telefonní číslo: <input type="text" name="telefon" size="30" value="${row.telefon}" /><br /></p>
        </c:forEach>
        </div>
        <div id="skupiny">
        <h3>Členství uživatele ve skupinách:</h3>
        <sql:query var="skupiny" dataSource="${dataSource}">
            SELECT DISTINCT skupiny.nazev,skupiny.id FROM skupiny, prirazeni
            WHERE (prirazeni.uzivatel='${RequestData.login}' AND skupiny.id=prirazeni.skupina) OR skupiny.id=1
        </sql:query>

        <sql:query var="jeClenem" dataSource="${dataSource}">
            SELECT skupiny.nazev FROM prirazeni, skupiny WHERE
            prirazeni.uzivatel='${param.login}' AND skupiny.id=prirazeni.skupina
        </sql:query>
        <c:forEach var="rowSkupiny" items="${skupiny.rows}">
            <c:set var="vygen" value="false" />
                <c:forEach var="rowJeClenem" items="${jeClenem.rows}">
                    <c:if test="${rowSkupiny.nazev == rowJeClenem.nazev}">
                        <input type="checkbox" name="skupina" value="${rowSkupiny.id}" checked> ${rowSkupiny.nazev}<br />
                        <c:set var="vygen" value="true" />
                    </c:if>
                </c:forEach>
                <c:if test="${vygen == 'false'}">
                    <input type="checkbox" name="skupina" value="${rowSkupiny.id}" /> ${rowSkupiny.nazev}<br />
                </c:if>
        </c:forEach>
        </div>
            <input style="float: right" type="submit" name="ulozit" value="Uložit změny" />
            <input type="hidden" name="login" value="${param.login}" />
        </form>
        </div>
<c:if test="${not empty param.ulozit}" >
    <sql:update dataSource="${dataSource}">
        UPDATE uzivatele SET jmeno='${param.jmeno}', prijmeni='${param.prijmeni}', mail='${param.mail}', uri='${param.uri}', telefon='${param.telefon}'
        WHERE login='${param.login}'
    </sql:update>

<%--***********************************ULOZENI VYBRANYCH SKUPIN**********************************************--%>

    <sql:update dataSource="${dataSource}">
        DELETE FROM prirazeni WHERE uzivatel='${param.login}'
    </sql:update>
    <c:forEach var="par" items="${paramValues.skupina}">
        <sql:update dataSource="${dataSource}">
            INSERT INTO prirazeni (uzivatel, skupina) VALUES ('${param.login}','${par}')
        </sql:update>
    </c:forEach>
        <c:if test="${empty paramValues.skupina}">
            <sql:update dataSource="${dataSource}">
                INSERT INTO prirazeni (uzivatel, skupina) VALUES ('${param.login}',1)
            </sql:update>
        </c:if>

    <c:redirect url="admin.jsp" />
</c:if>

    </body>
</html>
