<div class="cover margin-bottom-large">
    <h1 class="zone xl margin-bottom-large margin-top">
        Storage
    </h1>
</div>

<div class="zone xl">
    <div data-ui-modal class="box modal close" data-button-text="" data-button-class="fly round" data-button-icon="icon-plus">
        <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

        <h1>Upload File</h1>

        <form
            data-service="storage.createFile"
            data-event="submit"
            data-scope="sdk"
            data-success="alert,trigger,reset"
            data-success-param-alert-text="File uploaded successfully"
            data-success-param-trigger-events="file-create,modal-close"
            data-failure="alert"
            data-failure-param-alert-text="Failed to upload file"
            data-failure-param-alert-classname="error">
            <input type="hidden" name="folderId" id="files-folderId" data-cast-to="int" value="1">

            <label for="file-read">Files</label>
            <input type="file" name="files" id="file-files" required>

            <label for="file-read">Read Permissions</label>
            <input type="hidden" id="file-read" name="read" required data-forms-tags data-cast-to="json" value="<?php echo htmlentities(json_encode(['*'])); ?>" />
            
            <label for="file-write">Write Permissions</label>
            <input type="hidden" id="file-write" name="write" required data-forms-tags 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>

    <form class="box padding-small margin-bottom search"
        data-service="storage.listFiles"
        data-event="submit"
        data-param-search="{{router.params.search}}"
        data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
        data-param-offset=""
        data-param-order-type="DESC"
        data-scope="sdk"
        data-name="project-files"
        data-success="state"
        data-success-param-state-keys="search,offset">
        <div class="row thin responsive">
            <div class="col span-10">
                <input name="search" id="searchFiles" type="search" autocomplete="off" placeholder="Search" class="margin-bottom-no" data-ls-bind="{{router.params.search}}">
            </div>
            <div class="col span-2">
                <button class="fill" title="Search" aria-label="Search"><i class="icon-search"></i></button>
            </div>
        </div>
    </form>

    <div
        data-service="storage.listFiles"
        data-event="load,file-create,file-update,file-delete"
        data-param-search="{{router.params.search}}"
        data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
        data-param-offset="{{router.params.offset}}"
        data-param-order-type="DESC"
        data-scope="sdk"
        data-name="project-files">

        <div data-ls-if="0 == {{project-files.sum}}" class="box margin-bottom">
            <h3 class="margin-bottom">No Files Found</h3>

            <p class="margin-bottom-no">Upload your first file to get started</p>
        </div>

        <div data-ls-if="0 != {{project-files.sum}}" style="display: none;">
            <div class="margin-bottom-small margin-end-small text-align-end text-size-small"><span data-ls-bind="{{project-files.sum}}"></span> files found</div>

            <div class="box margin-bottom">
                <table class="vertical">
                    <thead>
                        <tr>
                            <th width="60"></th>
                            <th width="220">Filename</th>
                            <th width="180">Type</th>
                            <th>Size</th>
                            <th width="100">Created</th>
                        </tr>
                    </thead>
                    <tbody data-ls-loop="project-files.files" data-ls-as="file">
                        <tr>
                            <td class="hide">
                                <img src="" data-ls-attrs="src=//{{env.DOMAIN}}/v1/storage/files/{{file.$uid}}/preview?width=45&height=45&project={{router.params.project}}&mode=admin" class="pull-start avatar" loading="lazy" width="30" height="30" />
                            </td>
                            <td data-title="Name: ">
                                <div data-ui-modal class="box modal close" data-button-text="{{file.name}}" data-button-class="link">
                                    <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                                    <h1>Update File</h1>

                                    <form
                                        data-service="storage.updateFile"
                                        data-event="submit"
                                        data-scope="sdk"
                                        data-success="alert,trigger"
                                        data-success-param-alert-text="File updated successfully"
                                        data-success-param-trigger-events="file-update,modal-close"
                                        data-failure="alert"
                                        data-failure-param-alert-text="Failed to update file"
                                        data-failure-param-alert-classname="error">

                                        <label for="files-fileId">File UID</label>
                                        <div class="input-copy">
                                            <input data-forms-copy type="text" name="fileId" id="files-fileId" disabled data-ls-bind="{{file.$uid}}" />
                                        </div>
                                        <input type="hidden" name="folderId" id="files-folderId" data-cast-to="int" value="1">

                                        <label for="file-read">Read Permissions</label>
                                        <input type="hidden" id="file-read" name="read" required data-forms-tags data-cast-to="json" data-ls-bind="{{file.$permissions.read}}" />
                                        
                                        <label for="file-write">Write Permissions</label>
                                        <input type="hidden" id="file-write" name="write" required data-forms-tags data-cast-to="json" data-ls-bind="{{file.$permissions.write}}" />

                                        <hr />

                                        <div class="pull-end">
                                            <a href="" data-ls-attrs="href=//{{env.DOMAIN}}/v1/storage/files/{{file.$uid}}/preview?project={{router.params.project}}&mode=admin" target="_blank">Open preview in new window <i class="icon-link-ext"></i></a>
                                        </div>

                                        <button type="submit">Update</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                                    </form>
                                </div>
                            </td>
                            <td data-title="Type: ">
                                <span data-ls-bind="{{file.mimeType}}" class="tag"></span>
                            </td>
                            <td data-title="Size: ">
                                <small data-ls-bind="{{file.sizeOriginal|humanFileSize}}"></small>
                            </td>
                            <td data-title="Created: ">
                                <small data-ls-bind="{{file.dateCreated|date-text}}"></small>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="clear text-align-center paging">
            <form
                data-service="storage.listFiles"
                data-event="submit"
                data-param-search="{{router.params.search}}"
                data-param-limit="<?php echo APP_PAGING_LIMIT; ?>"
                data-param-order-type="DESC"
                data-scope="sdk"
                data-name="project-files"
                data-success="state"
                data-success-param-state-keys="search,offset">
                <button name="offset" data-paging-back data-offset="{{router.params.offset}}" data-sum="{{project-files.sum}}" 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-files.sum|pageTotal}}"></span>

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