<div class="cover">
    <h1 class="zone xl margin-bottom-large">
        <a data-ls-attrs="href=/console/home?project={{router.params.project}}" class="back text-size-small link-return-animation--start"><i class="icon-left-open"></i> Home</a>
        <br />

        <span>Database</span>
    </h1>
</div>

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

            <div class="margin-top"
                data-service="database.listCollections"
                data-event="load,database.createCollection,database.updateCollection,database.deleteCollection"
                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/database/collection?id={{collection.$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="database.listCollections"
                        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="database.listCollections"
                        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="database.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/database/collection/settings?id={{serviceData.$id}}&project={{router.params.project}}"
                        data-success-param-trigger-events="database.createCollection"
                        data-failure="alert"
                        data-failure-param-alert-text="Failed to create collection"
                        data-failure-param-alert-classname="error">

                        <label for="collection-id">Collection ID</label>
                        <input
                            type="hidden"
                            data-custom-id
                            data-id-type="auto"
                            data-validator="database.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/database/usage?project={{router.params.project}}">
                <form class="pull-end margin-start-small margin-top-small" data-ls-if="{{usage.range}} !== '90d'"
                    data-service="database.getUsage"
                    data-event="submit"
                    data-name="usage"
                    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="database.getUsage"
                    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="database.getUsage"
                    data-event="submit"
                    data-name="usage"
                    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="database.getUsage"
                    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>
        </ul>
    </div>
</div>
