<?php

require_once '../../../includes/modal_header.php';

$client_id = intval($_GET['client_id'] ?? 0);
$contact_id = intval($_GET['contact_id'] ?? 0);
$asset_id = intval($_GET['asset_id'] ?? 0);

ob_start();

?>
<div class="modal-header bg-dark">
    <h5 class="modal-title"><i class="fa fa-fw fa-key me-2"></i>New Credential</h5>
    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<form action="post.php" method="post" autocomplete="off">
    <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

    <div class="modal-body">

        <ul class="nav nav-pills nav-justified mb-3">
            <li class="nav-item">
                <a class="nav-link active" data-bs-toggle="pill" href="#pills-credential-details">Details</a>
            </li>
            <?php if ($client_id) { ?>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-credential-relation">Relation</a>
            </li>
            <?php } ?>
            <li class="nav-item">
                <a class="nav-link" data-bs-toggle="pill" href="#pills-credential-notes">Notes</a>
            </li>
        </ul>

        <hr>

        <div class="tab-content">

            <div class="tab-pane fade show active" id="pills-credential-details">

                <?php if ($client_id) { ?>
                    <input type="hidden" name="client_id" value="<?= $client_id ?>">
                <?php } else { ?>

                    <div class="mb-3">
                        <label>Client <strong class="text-danger">*</strong></label>
                        <div class="input-group">
                                <span class="input-group-text"><i class="fa fa-fw fa-user"></i></span>
                            <select class="form-select select2" name="client_id" required>
                                <option value="">- Select Client -</option>
                                <?php

                                $sql = mysqli_query($mysqli, "SELECT client_id, client_name FROM clients WHERE client_archived_at IS NULL " . clientScopeSql('clients.client_id') . " ORDER BY client_name ASC");
                                while ($row = mysqli_fetch_assoc($sql)) {
                                    $client_id_select = intval($row['client_id']);
                                    $client_name = escapeHtml($row['client_name']); ?>
                                    <option <?php if ($client_id == $client_id_select) { echo "selected"; } ?> value="<?= $client_id_select ?>"><?= $client_name ?></option>

                                <?php } ?>
                            </select>
                        </div>
                    </div>

                <?php } ?>

                <div class="mb-3">
                    <label>Name <strong class="text-danger">*</strong> / <span class="text-secondary">Important?</span></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-key"></i></span>
                        <input type="text" class="form-control" name="name" placeholder="Name of Login" maxlength="200" required autofocus>
                            <div class="input-group-text">
                                <label class="star-toggle mb-0" title="Favorite">
                                    <input class="form-check-input" type="checkbox" name="favorite" value="1"><i class="far fa-star"></i>
                                </label>
                            </div>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Description</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-align-left"></i></span>
                        <input type="text" class="form-control" name="description" placeholder="Description" maxlength="500">
                    </div>
                </div>

                <div class="mb-3">
                    <label>Username / ID</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-user"></i></span>
                        <input type="text" class="form-control" name="username" placeholder="Username or ID" maxlength="350"> <!-- DB field is 500, 350 un-encrypted chars -->
                    </div>
                </div>

                <div class="mb-3">
                    <label>Password / Key <strong class="text-danger">*</strong></label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-lock"></i></span>
                        <input type="password" class="form-control" data-toggle="password" id="password" name="password" placeholder="Password or Key" required maxlength="350" autocomplete="new-password">
                            <span class="input-group-text"><i class="fa fa-fw fa-eye"></i></span>
                            <span class="btn btn-default"><i class="fa fa-fw fa-question" onclick="generatePassword()"></i></span>
                    </div>
                </div>

                <div class="mb-3">
                    <label>TOTP Seed</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-key"></i></span>
                        <input type="password" class="form-control" data-toggle="password" name="otp_secret" placeholder="Insert secret key" maxlength="200">
                            <span class="input-group-text"><i class="fa fa-fw fa-eye"></i></span>
                    </div>
                </div>

                <div class="mb-3">
                    <label>URI</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-link"></i></span>
                        <input type="text" class="form-control" name="uri" placeholder="http://192.168.1.1" maxlength="500">
                    </div>
                </div>

                <div class="mb-3">
                    <label>URI 2</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-link"></i></span>
                        <input type="text" class="form-control" name="uri_2" placeholder="https://server.company.com:5001" maxlength="500">
                    </div>
                </div>

            </div>

            <?php if ($client_id) { ?>
            <div class="tab-pane fade" id="pills-credential-relation">
                <div class="mb-3">
                    <label>Contact</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-user"></i></span>
                        <select class="form-select select2" name="contact">
                            <option value="">- Select Contact -</option>
                            <?php

                            $sql = mysqli_query($mysqli, "SELECT contact_id, contact_name FROM contacts WHERE contact_client_id = $client_id ORDER BY contact_name ASC");
                            while ($row = mysqli_fetch_assoc($sql)) {
                                $contact_id_select = intval($row['contact_id']);
                                $contact_name = escapeHtml($row['contact_name']);
                                ?>
                                <option
                                    <?php if ($contact_id == $contact_id_select) { echo "selected"; } ?>
                                    value="<?= $contact_id_select ?>"><?= $contact_name ?>
                                </option>

                                <?php
                            }
                            ?>
                        </select>
                    </div>
                </div>

                <div class="mb-3">
                    <label>Asset</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-tag"></i></span>
                        <select class="form-select select2" name="asset">
                            <option value="">- Select Asset -</option>
                            <?php

                            $sql = mysqli_query($mysqli, "SELECT asset_id, asset_name, location_name FROM assets LEFT JOIN locations on asset_location_id = location_id WHERE asset_client_id = $client_id AND asset_archived_at IS NULL ORDER BY asset_name ASC");
                            while ($row = mysqli_fetch_assoc($sql)) {
                                $asset_id_select = intval($row['asset_id']);
                                $asset_name = escapeHtml($row['asset_name']);
                                $asset_location = escapeHtml($row['location_name']);

                                $asset_display_string = $asset_name;
                                if (!empty($asset_location)) {
                                    $asset_display_string = "$asset_name ($asset_location)";
                                }

                                ?>
                                <option <?php if ($asset_id == $asset_id_select) { echo "selected"; } ?>
                                    value="<?= $asset_id_select ?>"><?= $asset_display_string ?></option>

                                <?php
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
            <?php } ?>

            <div class="tab-pane fade" id="pills-credential-notes">

                <div class="mb-3">
                    <textarea class="form-control" rows="12" placeholder="Enter some notes" name="note"></textarea>
                </div>

                <div class="mb-3">
                    <label>Tags</label>
                    <div class="input-group">
                            <span class="input-group-text"><i class="fa fa-fw fa-tags"></i></span>
                        <select class="form-select select2" name="tags[]" data-placeholder="Add some tags" multiple>
                            <?php

                            $sql_tags_select = mysqli_query($mysqli, "SELECT tag_id, tag_name FROM tags WHERE tag_type = 4 ORDER BY tag_name ASC");
                            while ($row = mysqli_fetch_assoc($sql_tags_select)) {
                                $tag_id_select = intval($row['tag_id']);
                                $tag_name_select = escapeHtml($row['tag_name']);
                                ?>
                                <option value="<?= $tag_id_select ?>"><?= $tag_name_select ?></option>
                            <?php } ?>

                        </select>
                            <button class="btn btn-secondary ajax-modal" type="button"
                                data-modal-url="../admin/modals/tag/tag_add.php?type=4">
                                <i class="fas fa-plus"></i>
                            </button>
                    </div>
                </div>

            </div>

        </div>
    </div>
    <div class="modal-footer">
        <button type="submit" name="add_credential" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Create</button>
        <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
    </div>
</form>

<script src="/agent/js/generate_password.js"></script>

<?php

require_once '../../../includes/modal_footer.php';
