<div class="cover margin-bottom-large">
    <h1 class="zone xl margin-bottom-large margin-top">
        Tasks
    </h1>
</div>
<div class="zone xl"
    data-service="projects.listTasks"
    data-scope="console"
    data-event="load,task-create,task-update,task-delete"
    data-name="console-tasks"
    data-param-project-id="{{router.params.project}}"
    data-success="trigger"
    data-success-param-trigger-events="project-load">

    <div data-ls-if="0 === {{console-tasks.length}} || undefined === {{console-tasks.length}}" class="box margin-top margin-bottom" style="display: none;">
        <h3 class="margin-bottom-small">No Tasks Found</h3>

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

    <div class="box y-scroll margin-bottom" data-ls-if="0 != {{console-tasks.length}}" style="display: none;">
        <table class="full">
            <thead>
                <tr>
                    <th width="220">Name</th>
                    <th></th>
                    <th width="140">Next Run</th>
                    <th width="140">Prev. Run</th>
                    <th></th>
                </tr>
            </thead>
            <tbody data-ls-loop="console-tasks" data-ls-as="task" class="list">
                <tr>
                    <td>
                        <div class="margin-bottom-tiny text-one-liner">
                            <span data-ls-bind="{{task.name}}"></span>

                            <span data-ls-if="0 === {{task.security}}" style="display: none;">
                                &nbsp; <span class="text-danger">SSL/TLS Disabled</span>
                            </span>
                            <span data-ls-if="0 !== {{task.failures}}" style="display: none;">
                                &nbsp; <span class="text-danger" data-ls-bind="({{task.failures}} errors)"></span>
                            </span>
                        </div>

                        <a data-ls-attrs="href={{task.httpUrl}}" data-ls-bind="{{task.httpUrl}}" target="_blank" class="text-one-liner"></a>
                    </td>
                    <td style="vertical-align: middle">
                        <span data-ls-if="'play' === {{task.status}}" style="display: none;">
                            <span class="tag green"><i class="icon-play"></i> Running &nbsp;</span>
                        </span>
                        <span data-ls-if="'play' !== {{task.status}}" style="display: none;">
                            <span class="tag red"><i class="icon-pause"></i> Paused &nbsp;</span>
                        </span>
                    </td>
                    <td style="vertical-align: middle">
                        <small data-ls-bind="{{task.next|date-time}}"></small>
                    </td>
                    <td style="vertical-align: middle">
                        <div data-ls-if="undefined !== {{task.previous}}">
                            <small data-ls-bind="{{task.previous|date-time}}"></small>

                            <div data-ls-if="undefined !== {{task.delay}} && 59 < {{task.delay}}" class="text-danger margin-top-tiny">
                                <small data-ls-bind="({{task.delay|ms2hum}} Delay)"></small>
                            </div>
                        </div>
                        <small data-ls-if="undefined === {{task.previous}}">
                            None.
                        </small>
                    </td>
                    <td>
                        <div data-ui-modal class="modal box close" data-button-text="" data-button-class="pull-end reverse round" data-button-icon="icon-cog">
                            <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                            <h1>Update Task</h1>

                            <form
                                data-service="projects.updateTask"
                                data-scope="console"
                                data-event="submit"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Updated task successfully"
                                data-success-param-trigger-events="task-update,modal-close"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to update task"
                                data-failure-param-alert-classname="error">

                                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
                                <input type="hidden" name="taskId" data-ls-bind="{{task.$uid}}" />

                                <label data-ls-attrs="for=name-{{task.$uid}}">Name</label>
                                <input type="text" class="full-width" data-ls-attrs="id=name-{{task.$uid}}" name="name" required autocomplete="off" data-ls-bind="{{task.name}}" />

                                <label data-ls-attrs="for=status-{{task.$uid}}" class="margin-bottom">Status
                                    <div class="margin-top-small">
                                        <input name="status" type="radio" checked="checked" required data-ls-bind="{{task.status}}" value="play" /> &nbsp; <span>Play</span>
                                        <input name="status" type="radio" required data-ls-bind="{{task.status}}" value="pause" /> &nbsp; <span>Pause</span>
                                    </div>
                                </label>

                                <label data-ls-attrs="for=schedule-{{task.$uid}}">Schedule (CRON Syntax)</label>
                                <input type="text" class="full-width" data-ls-attrs="id=schedule-{{task.$uid}}" name="schedule" required autocomplete="off" data-ls-bind="{{task.schedule}}" placeholder="* * * * *" />

                                <div class="row thin">
                                    <div class="col span-4">
                                        <label data-ls-attrs="for=httpMethod-{{task.$uid}}">HTTP Method</label>
                                        <select data-ls-attrs="id=httpMethod-{{task.$uid}}" name="httpMethod" required data-ls-bind="{{task.httpMethod}}">
                                            <option value="POST">POST</option>
                                            <option value="GET">GET</option>
                                            <option value="PUT">PUT</option>
                                            <option value="PATCH">PATCH</option>
                                            <option value="DELETE">DELETE</option>
                                            <option value="CONNECT">CONNECT</option>
                                            <option value="HEAD">HEAD</option>
                                            <option value="TRACE">TRACE</option>
                                            <option value="OPTIONS">OPTIONS</option>
                                        </select>
                                    </div>
                                    <div class="col span-8">
                                        <label data-ls-attrs="for=httpUrl-{{task.$uid}}">HTTP URL</label>
                                        <input type="url" class="full-width" data-ls-attrs="id=httpUrl-{{task.$uid}}" name="httpUrl" required autocomplete="off" placeholder="https://example.com/callback" data-ls-bind="{{task.httpUrl}}" />
                                    </div>
                                </div>

                                <div class="margin-bottom toggle" data-ls-ui-open>
                                    <i class="icon-plus pull-end margin-top-tiny"></i>
                                    <i class="icon-minus pull-end margin-top-tiny"></i>

                                    <h2 class="margin-bottom">
                                        Advanced Options
                                        <small class="text-size-small">(optional)</small>
                                    </h2>

                                    <label for="httpHeaders">HTTP Headers</label>

                                    <input type="hidden" id="httpHeadersInit" name="httpHeaders" data-cast-to="array-empty">
                                    
                                    <div class="margin-bottom-small">
                                        <div data-ls-loop="task.httpHeaders" data-ls-as="header" style="overflow: hidden">
                                            <div class="margin-bottom-small">
                                                <div data-forms-remove class="row thin">
                                                    <div class="col span-10">
                                                        <input type="hidden" name="httpHeaders" data-forms-headers data-ls-bind="{{header}}" data-cast-to="array" />
                                                    </div>
                                                    <div class="col span-2">
                                                        <button type="button" data-remove class="reverse danger round pull-end"><i class="icon-cancel"></i></button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <div data-forms-clone="" data-label="Add Header" data-first="0">
                                            <div class="">
                                                <div data-forms-remove class="row thin margin-bottom-small">
                                                    <div class="col span-10">
                                                        <input type="hidden" name="httpHeaders" data-forms-headers data-cast-to="array" />
                                                    </div>
                                                    <div class="col span-2">
                                                        <button type="button" data-remove class="reverse danger round pull-end"><i class="icon-cancel"></i></button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <label data-ls-attrs="for=security-{{task.$uid}}" class="margin-bottom">
                                        <div class="margin-bottom-small">SSL / TLS (Certificate verification)</div>
                                        
                                        <input name="security" type="radio" required data-ls-bind="{{task.security}}" value="1" /> &nbsp; <span>Enabled</span>
                                        <input name="security" type="radio" required data-ls-bind="{{task.security}}" value="0" /> &nbsp; <span>Disabled</span>
                                    </label>

                                    <p class="margin-bottom text-size-small"><span class="tag red">Warning</span> &nbsp; Untrusted or self-signed certificates may not be secure.
                                        <a href="https://en.wikipedia.org/wiki/Self-signed_certificate" target="_blank" rel="noopener">Learn more<i class="icon-link-ext"></i></a>
                                    </p>

                                    <label>HTTP Authentication <span class="tooltip" data-tooltip="Use to secure your endpoint from untrusted sources"><i class="icon-question"></i></span> &nbsp;<small>(optional)</small></label>

                                    <div class="row thin">
                                        <div class="col span-6">
                                            <label data-ls-attrs="for=httpUser-{{task.$uid}}">User</label>
                                            <input type="text" class="full-width" data-ls-attrs="id=httpUser-{{task.$uid}}" name="httpUser" autocomplete="off" data-ls-bind="{{task.httpUser}}" />
                                        </div>
                                        <div class="col span-6">
                                            <label data-ls-attrs="for=httpPass-{{task.$uid}}">Password</label>
                                            <input type="password" class="full-width" data-ls-attrs="id=httpPass-{{task.$uid}}" name="httpPass" autocomplete="off" data-ls-bind="{{task.httpPass}}" />
                                        </div>
                                    </div>
                                </div>

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

                            <form class="margin-top"
                                data-service="projects.deleteTask"
                                data-scope="console"
                                data-event="submit"
                                data-confirm="Are you sure you want to delete this task?"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Deleted task successfully"
                                data-success-param-trigger-events="task-delete,modal-close"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to delete task"
                                data-failure-param-alert-classname="error">

                                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
                                <input type="hidden" name="taskId" data-ls-bind="{{task.$uid}}" />

                                <button class="danger fill">Delete</button>
                            </form>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

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

            <h1>Add Task</h1>

            <form
                data-service="projects.createTask"
                data-scope="console"
                data-event="submit"
                data-success="alert,trigger,reset"
                data-success-param-alert-text="Created task successfully"
                data-success-param-trigger-events="task-create,modal-close"
                data-failure="alert"
                data-failure-param-alert-text="Failed to create task"
                data-failure-param-alert-classname="error">

                <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />

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

                <label data-ls-attrs="for=status" class="margin-bottom">Status
                    <div class="margin-top-small">
                        <input name="status" type="radio" value="play" checked="checked" required /> &nbsp; <span>Play</span>
                        <input name="status" type="radio" value="pause" required /> &nbsp; <span>Pause</span>
                    </div>
                </label>

                <label for="schedule">Schedule (CRON Syntax)</label>
                <input type="text" class="full-width" id="schedule" name="schedule" required autocomplete="off" placeholder="* * * * *" />

                <div class="row thin">
                    <div class="col span-4">
                        <label for="httpMethod">HTTP Method</label>
                        <select id="httpMethod" name="httpMethod" required>
                            <option value="POST">POST</option>
                            <option value="GET">GET</option>
                            <option value="PUT">PUT</option>
                            <option value="PATCH">PATCH</option>
                            <option value="DELETE">DELETE</option>
                            <option value="CONNECT">CONNECT</option>
                            <option value="HEAD">HEAD</option>
                            <option value="TRACE">TRACE</option>
                            <option value="OPTIONS">OPTIONS</option>
                        </select>
                    </div>
                    <div class="col span-8">
                        <label for="httpUrl">HTTP URL</label>
                        <input type="url" class="full-width" id="httpUrl" name="httpUrl" required autocomplete="off" placeholder="https://example.com/callback" />
                    </div>
                </div>
                
                <div class="margin-bottom toggle" data-ls-ui-open>
                    <i class="icon-plus pull-end margin-top-tiny"></i>
                    <i class="icon-minus pull-end margin-top-tiny"></i>
                    
                    <h2 class="margin-bottom">
                        Advanced Options
                        <small class="text-size-small">(optional)</small>
                    </h2>

                    <label for="httpHeaders">HTTP Headers</label>
                    
                    <input type="hidden" id="httpHeadersInitAdd" name="httpHeaders" data-cast-to="array-empty">

                    <div class="margin-bottom-small">
                        <div data-forms-clone="" data-label="Add Header" data-first="0">
                            <div class="">
                                <div data-forms-remove class="row thin margin-bottom-small">
                                    <div class="col span-10">
                                        <input type="hidden" name="httpHeaders" data-forms-headers data-cast-to="array" />
                                    </div>
                                    <div class="col span-2">
                                        <button type="button" data-remove class="reverse danger round pull-end"><i class="icon-cancel"></i></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <label for="secure" class="margin-bottom-small">
                        <div class="margin-bottom-small">SSL / TLS (Certificate verification)</div>
                        
                        <input name="security" data-ls-attrs="id=secure-yes" type="radio" value="1" checked="checked" required /> &nbsp; <span>Enabled</span>
                        <input name="security" data-ls-attrs="id=secure-no" type="radio" value="0" required /> &nbsp; <span>Disabled</span>
                    </label>

                    <p class="margin-bottom text-size-small"><span class="tag red">Warning</span> &nbsp; Untrusted or self-signed certificates may not be secure.
                        <a href="https://en.wikipedia.org/wiki/Self-signed_certificate" target="_blank" rel="noopener">Learn more<i class="icon-link-ext"></i></a>
                    </p>

                    <label>HTTP Authentication <span class="tooltip" data-tooltip="Use to secure your endpoint from untrusted sources"><i class="icon-question"></i></span> &nbsp;<small>(optional)</small></label>

                    <div class="row thin">
                        <div class="col span-6">
                            <label for="httpUser">User</label>
                            <input type="text" class="full-width" id="httpUser" name="httpUser" autocomplete="off" />
                        </div>
                        <div class="col span-6">
                            <label for="httpPass">Password</label>
                            <input type="password" class="full-width" id="httpPass" name="httpPass" autocomplete="off" />
                        </div>
                    </div>
                </div>

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