<?php
/**
 * /subs/parts/userarea/projects.php
 * @version 1.0
 * @desc File for deploying projects to the main site
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Systemtest.tk website's.
    Copyright (C) 2018  Fándly Gergő Zoltán

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/
?>

<div id="projectlist">
    <h2><?php echo $lang['projectlist'] ?></h2>
    <table class="footable">
        <thead>
            <tr>
                <th><?php echo $lang['id'] ?></th>
                <th><?php echo $lang['name'] ?></th>
                <th data-breakpoints="xs sm md"><?php echo $lang['description'] ?></th>
                <th data-breakpoints="xs sm"><?php echo $lang['owner'] ?></th>
                <th data-breakpoints="xs sm md"><?php echo $lang['path'] ?></th>
                <th data-breakpoints="xs sm md"><?php echo $lang['repository'] ?></th>
                <th><?php echo $lang['status'] ?> </th>
                <th data-breakpoints="xs sm"><?php echo $lang['operations'] ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql=$db->prepare("SELECT p.id, p.name, p.description, u.fullname AS owner, p.path, p.repo, p.status FROM projects AS p INNER JOIN users AS u ON (u.id=p.owner) ORDER BY p.name ASC");
            $sql->execute();
            while($row=$sql->fetch(PDO::FETCH_ASSOC)){
                echo "
                <tr>
                    <td>".$row['id']."</td>
                    <td>".$row['name']."</td>
                    <td>".$row['description']."</td>
                    <td>".$row['owner']."</td>
                    <td>".$row['path']."</td>
                    <td>".$row['repo']."</td>
                    <td>".$row['status']."</td>
                    <td>
                        <button type=\"button\" onclick=\"projectsEdit(".$row['id'].")\">".$lang['edit']."</button>
                        <button type=\"button\" onclick=\"projectsDelete(".$row['id'].", this)\">".$lang['delete']."</button>
                    </td>
                </tr>
                ";
            }
            ?>
        </tbody>
    </table>
    <hr class="placeholder">
    <button type="button" onclick="projectsNew()"><i class="fa fa-plus-circle"></i> <?php echo $lang['new'] ?></button>
</div>
<div id="editor" style="display: none">
    <form method="POST" class="ajaxform" onsubmit="projectsSave()" id="projectEditor">
        <input type="hidden" name="project_id" id="project_id">
        <fieldset style="width: 95%">
            <legend><?php echo $lang['editor'] ?></legend>
            <table style="width: 100%">
                <tr>
                    <td><?php echo $lang['name'].": " ?></td>
                    <td><input type="text" name="project_name" placeholder="<?php echo $lang['name']."..." ?>" required style="width: 95%"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['description'].": " ?></td>
                    <td><textarea name="project_desc" placeholder="<?php echo $lang['description']."..." ?>" required rows="5" style="width: 95%"></textarea></td>
                </tr>
                <tr>
                    <td><?php echo $lang['path'].": " ?></td>
                    <td><input type="text" name="project_path" placeholder="https://..." required style="width: 95%"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['repository'].": " ?></td>
                    <td><input type="text" name="project_repo" placeholder="https://..." required style="width: 95%"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['status'].": " ?></td>
                    <td><input type="text" name="project_status" placeholder="<?php echo $lang['status']."..." ?>" required style="width: 95%"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['image'].": " ?></td>
                    <td><input type="text" name="project_image" placeholder="https://..." required style="width: 95%"></td>
                </tr>
            </table>
            <br>
            <br>
            <button type="submit" class="green" form="projectEditor"><i class="fa fa-save"></i> <?php echo $lang['ok'] ?></button>
            <button type="button" class="red" onclick="projectsEditCancel()"><i class="fa fa-trash"></i><?php echo $lang['cancel'] ?></button>
        </fieldset>
    </form>
</div>