<div
    data-service="databases.get"
    data-param-database-id="{{router.params.id}}"
    data-scope="sdk"
    data-event="load,databases.update"
    data-name="project-database">
    <div class="cover">
        <h1 class="zone xl margin-bottom-large">
            <a data-ls-attrs="href=/console/databases?project={{router.params.project}}" class="back text-size-small link-return-animation--start"><i class="icon-left-open"></i> Database</a>
            <br />

            <span data-ls-bind="{{project-database.name}}">&nbsp;&nbsp;</span>
        </h1>
    </div>

    <div data-ui-modal class="modal width-large box close" data-button-hide="on" data-open-event="open-json">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

        <h2>JSON View</h2>

        <div class="margin-bottom">
            <input type="hidden" data-ls-bind="{{project-database}}" data-forms-code />
        </div>

        <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
    </div>

    <div class="zone xl">
        <ul class="phases clear" data-ui-phases data-selected="{{router.params.tab}}">
            <li data-state="/console/databases/database?id={{router.params.id}}&project={{router.params.project}}">
                <h2>Collections</h2>

                <div class="margin-top"
                    data-service="databases.listCollections"
                    data-event="load,databases.createCollection,databases.updateCollection,databases.deleteCollection"
                    data-param-database-id="{{router.params.id}}"
                    data-param-search="{{router.params.search}}"
                    data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                    data-param-offset="{{router.params.offset}}"
                    data-param-order-type="ASC"
                    data-scope="sdk"
                    data-name="project-collections">

                    <div data-ls-if="(!{{project-collections.total}})" class="box dashboard margin-bottom">
                        <div class="margin-bottom-small margin-top-small margin-end margin-start">
                            <h3 class="margin-bottom-small text-bold">No Collections Found</h3>

                            <p class="margin-bottom-no">You haven't created any collections for your project yet.</p>
                        </div>
                    </div>

                    <div data-ls-if="0 != {{project-collections.total}}">
                        <ul data-ls-loop="project-collections.collections" data-ls-as="collection" data-ls-append="" class="tiles cell-3 margin-bottom-small">
                            <li class="margin-bottom">
                                <a data-ls-attrs="href=/console/databases/collection?id={{collection.$id}}&databaseId={{router.params.id}}&project={{router.params.project}}" class="box">
                                    <div data-ls-bind="{{collection.name}}" class="text-one-liner margin-bottom text-bold">&nbsp;</div>

                                    <i class="icon-right-open"></i>
                                </a>
                            </li>
                        </ul>
                    </div>

                    <div class="pull-end text-align-center paging">
                        <form
                            data-service="databases.listCollections"
                            data-param-database-id="{{router.params.id}}"
                            data-event="submit"
                            data-param-search="{{router.params.search}}"
                            data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                            data-param-order-type="ASC"
                            data-scope="sdk"
                            data-name="project-collections"
                            data-success="state"
                            data-success-param-state-keys="search,offset">
                            <button name="offset" data-paging-back data-offset="{{router.params.offset}}" data-total="{{project-collections.total}}" class="margin-end round small" aria-label="Back"><i class="icon-left-open"></i></button>
                        </form>

                        <span data-ls-bind="{{router.params.offset|pageCurrent}} / {{project-collections.total|pageTotal}}"></span>

                        <form
                            data-service="databases.listCollections"
                            data-param-database-id="{{router.params.id}}"
                            data-event="submit"
                            data-param-search="{{router.params.search}}"
                            data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                            data-param-order-type="ASC"
                            data-scope="sdk"
                            data-name="project-collections"
                            data-success="state"
                            data-success-param-state-keys="search,offset">
                            <button name="offset" data-paging-next data-offset="{{router.params.offset}}" data-total="{{project-collections.total}}" class="margin-start round small" aria-label="Next"><i class="icon-right-open"></i></button>
                        </form>
                    </div>

                    <div data-ui-modal class="modal close box sticky-footer" data-button-text="Add Collection">
                        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                        <h1>New Collection</h1>

                        <form
                            data-analytics
                            data-analytics-activity
                            data-analytics-event="submit"
                            data-analytics-category="console"
                            data-analytics-label="Create Database Collection"
                            data-service="databases.createCollection"
                            data-event="submit"
                            data-scope="sdk"
                            data-success="alert,reset,redirect,trigger"
                            data-success-param-alert-text="Collection created successfully"
                            data-success-param-redirect-url="/console/databases/collection/settings?id={{serviceData.$id}}&databaseId={{router.params.id}}&project={{router.params.project}}"
                            data-success-param-trigger-events="databases.createCollection"
                            data-failure="alert"
                            data-failure-param-alert-text="Failed to create collection"
                            data-failure-param-alert-classname="error">
                            
                            <input type="hidden" name="databaseId" data-ls-bind="{{router.params.id}}" />

                            <label for="collection-id">Collection ID</label>
                            <input
                                type="hidden"
                                data-custom-id
                                data-id-type="auto"
                                data-validator="databases.getCollection"
                                required
                                maxlength="36"
                                pattern="^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$"
                                name="collectionId" />

                            <label for="collection-name">Name</label>
                            <input type="text" class="full-width" id="collection-name" name="name" required autocomplete="off" maxlength="128" />

                            <input type="hidden" id="collection-permission" name="permission" required value="collection" />
                            <input type="hidden" id="collection-read" name="read" required data-cast-to="json" value="<?php echo htmlentities(json_encode([])); ?>" />
                            <input type="hidden" id="collection-write" name="write" required data-cast-to="json" value="<?php echo htmlentities(json_encode([])); ?>" />

                            <hr />

                            <button type="submit">Create</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                        </form>
                    </div>

                </li>
                <li data-state="/console/databases/database/usage?project={{router.params.project}}&id={{router.params.id}}">
                    <form class="pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} !== '90d'"
                        data-service="databases.getDatabaseUsage"
                        data-event="submit"
                        data-name="usage"
                        data-param-database-id="{{router.params.id}}"
                        data-param-range="90d">
                        <button class="tick">90d</button>
                    </form>

                    <button class="tick pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} === '90d'" disabled>90d</button>

                    <form class="pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} !== '30d'"
                        data-service="databases.getDatabaseUsage"
                        data-param-database-id="{{router.params.id}}"
                        data-event="submit"
                        data-name="usage">
                        <button class="tick">30d</button>
                    </form>

                    <button class="tick pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} === '30d'" disabled>30d</button>

                    <form class="pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} !== '24h'"
                        data-service="databases.getDatabaseUsage"
                        data-event="submit"
                        data-name="usage"
                        data-param-database-id="{{router.params.id}}"
                        data-param-range="24h">
                        <button class="tick">24h</button>
                    </form>

                    <button class="tick pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} === '24h'" disabled>24h</button>

                    <h2>Usage</h2>

                    <div
                        data-service="databases.getDatabaseUsage"
                        data-param-database-id="{{router.params.id}}"
                        data-event="load"
                        data-name="usage">
                        <h3 class="margin-bottom-tiny">Objects</h3>
                        <p class="text-fade">Count of collections and documents over time</p>
                        <div class="box margin-bottom-small">
                            <div class="margin-start-negative-small margin-end-negative-small margin-top-negative-small margin-bottom-negative-small">
                                <div class="chart background-image-no border-no margin-bottom-no">
                                    <input 
                                    type="hidden" 
                                    data-ls-bind="{{usage}}" 
                                    data-forms-chart="Collections=collectionsCount,Documents=documentsCount" 
                                    data-show-y-axis="true"
                                    data-height="140" />
                                </div>
                            </div>
                        </div>

                        <ul class="chart-notes margin-bottom-large">
                            <li>Total Collections <span data-ls-bind="({{usage.collectionsCount|statsGetLast|statsTotal}})"></span></li>
                            <li>Total Documents <span data-ls-bind="({{usage.documentsCount|statsGetLast|statsTotal}})"></span></li>
                        </ul>


                        <h3 class="margin-bottom-tiny">Collections</h3>
                        <p class="text-fade">Count of collections create, read, update and delete operations over time</p>
                        <div class="box margin-bottom-small">
                            <div class="margin-start-negative-small margin-end-negative-small margin-top-negative-small margin-bottom-negative-small">
                                <div class="chart background-image-no border-no margin-bottom-no">
                                    <input 
                                        type="hidden"
                                        data-ls-bind="{{usage}}"
                                        data-forms-chart="Created=collectionsCreate,Read=collectionsRead,Updated=collectionsUpdate,Deleted=collectionsDelete"
                                        data-show-y-axis="true"
                                        data-colors="create,read,update,delete"
                                        data-height="140" />
                                </div>
                            </div>
                        </div>

                        <ul class="chart-notes margin-bottom-large crud">
                            <li>Created</li>
                            <li>Read</li>
                            <li>Updated</li>
                            <li>Deleted</li>
                        </ul>

                        <h3 class="margin-bottom-tiny">Documents</h3>
                        <p class="text-fade">Count of documents create, read, update and delete operations over time</p>
                        <div class="box margin-bottom-small">
                            <div class="margin-start-negative-small margin-end-negative-small margin-top-negative-small margin-bottom-negative-small">
                                <div class="chart background-image-no border-no margin-bottom-no">
                                    <input
                                        type="hidden"
                                        data-ls-bind="{{usage}}"
                                        data-forms-chart="Created=documentsCreate,Read=documentsRead,Updated=documentsUpdate,Deleted=documentsDelete"
                                        data-show-y-axis="true"
                                        data-colors="create,read,update,delete"
                                        data-height="140" />
                                </div>
                            </div>
                        </div>

                        <ul class="chart-notes margin-bottom-large crud">
                            <li>Created</li>
                            <li>Read</li>
                            <li>Updated</li>
                            <li>Deleted</li>
                        </ul>
                    </div>
                </li>
                <li data-state="/console/databases/database/settings?id={{router.params.id}}&project={{router.params.project}}">
                    <h2>Settings</h2>

                    <div class="row responsive margin-top-negative">
                        <div class="col span-8 margin-bottom">
                            <form
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update Database"
                                data-service="databases.update"
                                data-scope="sdk"
                                data-event="submit"
                                data-param-database-id="{{router.params.id}}"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Updated database successfully"
                                data-success-param-trigger-events="databases.update"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to update database"
                                data-failure-param-alert-classname="error">

                                <label>&nbsp;</label>

                                <div class="box">
                                    <label for="database-name">Name</label>
                                    <input name="name" id="database-name" type="text" autocomplete="off" data-ls-bind="{{project-database.name}}" data-forms-text-direction required placeholder="Database Name" maxlength="128" />

                                    <hr class="margin-top-no" />

                                    <button>Update</button>
                            </form>
                        </div>
                    </div>
                    <div class="col span-4 sticky-top">
                        <label>Database ID</label>
                        <div class="input-copy margin-bottom">
                            <input id="id" type="text" autocomplete="off" placeholder="" data-ls-bind="{{project-database.$id}}" disabled data-forms-copy class="margin-bottom-no" />
                        </div>

                        <ul class="margin-bottom-large text-fade text-size-small">
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> <button data-ls-ui-trigger="open-json" class="link text-size-small">View as JSON</button></li>
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> Last Updated: <span data-ls-bind="{{project-database.dateUpdated|dateText}}"></span></li>
                            <li class="margin-bottom-small"><i class="icon-angle-circled-right margin-start-tiny margin-end-tiny"></i> Created: <span data-ls-bind="{{project-database.dateCreated|dateText}}"></span></li>
                        </ul>

                            <form
                                name="databases.delete" class="margin-bottom"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Delete Database"
                                data-service="databases.delete"
                                data-event="submit"
                                data-param-database-id="{{router.params.id}}"
                                data-confirm="Are you sure you want to delete this Database?"
                                data-success="alert,trigger,redirect"
                                data-success-param-alert-text="Database deleted successfully"
                                data-success-param-trigger-events="databases.delete"
                                data-success-param-redirect-url="/console/databases?project={{router.params.project}}"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to delete database"
                                data-failure-param-alert-classname="error">

                            <button type="submit" class="danger fill">Delete Database</button>
                        </form>
                    </div>
                </div>
            </li>
            </ul>
        </div>
    </div>
</div>