<?php

$customDomainsEnabled = $this->getParam('customDomainsEnabled', false);
$customDomainsTarget = $this->getParam('customDomainsTarget', false);
$smtpEnabled = $this->getParam('smtpEnabled', false);

?>
<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>Settings</span>
    </h1>
</div>

<div class="zone xl"
    data-service="projects.get"
    data-scope="console"
    data-name="console-project"
    data-event="load"
    data-param-project-id="{{router.params.project}}"
    data-success="trigger"
    data-success-param-trigger-events="projects.get">

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

                <div class="row responsive margin-top-negative">
                    <div class="col span-8 margin-bottom">
                        <label>&nbsp;</label>

                        <div class="box margin-bottom-large">
                            <form
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Update Project"
                                data-service="projects.update"
                                data-scope="console"
                                data-event="submit"
                                data-param-project-id="{{router.params.project}}"
                                data-success="alert,trigger"
                                data-success-param-alert-text="Saved project successfully"
                                data-success-param-trigger-events="projects.update"
                                data-failure="alert"
                                data-failure-param-alert-text="Failed to update project"
                                data-failure-param-alert-classname="error">

                                <input name="$id" type="hidden" data-ls-bind="{{console-project.$id}}" />
                                
                                <label for="name">Name</label>
                                <input name="name" id="name" type="text" autocomplete="off" data-ls-bind="{{console-project.name}}" data-forms-text-direction required maxlength="128" />

                                <label for="logo">Project Logo</label>

                                <div class="text-align-center clear">
                                    <input type="hidden" name="logo" data-ls-bind="{{console-project.logo}}" data-read="<?php echo $this->escape(json_encode(['*'])); ?>" data-write="<?php echo $this->escape(json_encode(['team:{{console-project.teamId}}'])); ?>" data-accept="image/*" data-forms-upload="" data-label-button="Upload" data-preview-alt="Project Logo" data-scope="console" data-default="">
                                </div>

                                <hr />

                                <!-- <div data-ls-if="0 !== {{console-domains|activeDomainsCount}}">
                                    <label for="name">Custom API Endpoints</label>
                                    
                                    <ul data-ls-loop="console-domains" data-ls-as="domain">
                                        <li>
                                            <div class="input-copy" data-ls-if="true === {{domain.verification}} && {{domain.certificateId}}">
                                                <input data-forms-copy type="text" disabled data-ls-bind="{{env.PROTOCOL}}://{{domain.domain}}/v1" />
                                            </div>
                                        </li>
                                    </ul>
                                </div> -->
                                

                                <button class="" type="submit">Save</button>
                            </form>
                        </div>

                        <h3 class="text-danger">Danger Zone</h3>

                        <div class="box danger">
                            <p>This is the area where you can delete your project.</p>

                            <p>By deleting your project you will lose all your project metadata, resources and stats.</p>

                            <p>PLEASE NOTE: Project deletion is irreversible.</p>

                            <form class="inline"
                                data-analytics
                                data-analytics-activity
                                data-analytics-event="submit"
                                data-analytics-category="console"
                                data-analytics-label="Delete Project"
                                data-service="projects.delete"
                                data-scope="console"
                                data-event="submit"
                                data-confirmx="Are you sure you want to delete this project?"
                                data-success="trigger,redirect"
                                data-success-param-trigger-events="project.delete,reset,modal-close"
                                data-success-param-redirect-url="/console"
                                data-failure="alert,trigger,reset"
                                data-failure-param-alert-text="Project deletion failed"
                                data-failure-param-trigger-events="modal-close"
                                data-failure-param-alert-classname="error">

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

                                <div data-ui-modal class="modal box close width-small height-small" data-button-text="Delete Project" data-button-class="danger reverse">
                                    <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>    
                                    
                                    <h3>Confirm Password</h3>
                                    
                                    <hr />

                                    <label>Password</label>
                                    <input name="password" type="password" class="full-width" autocomplete="off" placeholder="" required>
                                    
                                    <hr />

                                    <button type="submit" class="margin-bottom-no danger fill">Delete Project</button>
                                </div>
                            </form>
                        </div>

                    </div>

                    <div class="col span-4 sticky-top">
                        <label for="name">Project ID</label>
                        <div class="input-copy">
                            <input data-forms-copy type="text" disabled data-ls-bind="{{console-project.$id}}" />
                        </div>

                        <label for="name">API Endpoint</label>
                        <div class="input-copy">
                            <input data-forms-copy type="text" disabled data-ls-bind="{{env.ENDPOINT}}/v1" />
                        </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> <a data-ls-attrs="href=/console/settings/domains?project={{router.params.project}}" class=" text-size-small">Add a custom endpoint</a></li>
                        </ul>
                    </div>
                </div>
            </li>
            <!-- <li data-state="/console/settings/privacy?project={{router.params.project}}">

                <form
                    data-service="projects.update"
                    data-scope="console"
                    data-event="submit"
                    data-param-project-id="{{router.params.project}}"
                    data-success="alert,trigger"
                    data-success-param-alert-text="Saved project successfully"
                    data-success-param-trigger-events="projects.update"
                    data-failure="alert"
                    data-failure-param-alert-text="Failed to update project"
                    data-failure-param-alert-classname="error">

                    <h2>Privacy & Legal</h2>

                    <div class="box margin-bottom">
                        <input name="$id" type="hidden" data-ls-bind="{{console-project.$id}}" />

                        <div class="row thin">
                            <div class="col span-6">
                                <label for="legalName">Legal Name</label>
                                <input name="legalName" id="legalName" type="text" autocomplete="off" data-ls-bind="{{console-project.legalName}}" data-forms-text-direction>

                                <label for="legalCountry">Country</label>
                                <select id="legalCountry" name="legalCountry" data-ls-bind="{{console-project.legalCountry}}" data-ls-loop="locale-countries" data-ls-as="option">
                                    <option data-ls-attrs="value={{$index}}" data-ls-bind="{{option}}"></option>
                                </select>

                                <label for="legalCity">City</label>
                                <input name="legalCity" id="legalCity" type="text" autocomplete="off" data-ls-bind="{{console-project.legalCity}}" data-forms-text-direction>
                            </div>

                            <div class="col span-6">
                                <label for="legalTaxId">Tax ID</label>
                                <input name="legalTaxId" id="legalTaxId" type="text" autocomplete="off" data-ls-bind="{{console-project.legalTaxId}}" data-forms-text-direction>

                                <label for="legalState">State</label>
                                <input name="legalState" id="legalState" type="text" autocomplete="off" data-ls-bind="{{console-project.legalState}}" data-forms-text-direction>

                                <label for="legalAddress">Address</label>
                                <input name="legalAddress" id="legalAddress" type="text" autocomplete="off" data-ls-bind="{{console-project.legalAddress}}" data-forms-text-direction>
                            </div>
                        </div>

                        <hr />

                        <button class="" type="submit">Save</button>
                    </div>
                </form>
            </li> -->
            <li data-state="/console/settings/domains?project={{router.params.project}}">
                
                <?php if(!$customDomainsEnabled): ?>
                    <h2 style="display: none;">Custom Domains</h2>

                    <div class="box line margin-bottom">
                        <h3>Enable Custom Domains</h3>
                        
                        <p>To enable <?php echo APP_NAME; ?>'s custom domain feature, you have to start your server instance with a public accessable domain name.</p>
                        
                        <p>Start your <?php echo APP_NAME; ?> server container with the <b>_APP_DOMAIN_TARGET</b> enviornment variable set with a public accessable domain name that resolves to your <?php echo APP_NAME; ?> server setup.</p>
                        <p class="margin-bottom-no">The <?php echo APP_NAME; ?> server will use your target domain to validate new custom domains and will automatically generate SSL certificates for your new domains using letsencrypt certbot.</p>
                    </div>
                <?php endif; ?>

                <?php if($customDomainsEnabled): ?>
                    <h2>Custom Domains</h2>

                    <div class="zone xl"
                        data-service="projects.listDomains"
                        data-scope="console"
                        data-event="load,projects.createDomain,projects.updateDomainVerification,projects.deleteDomain"
                        data-name="console-domains"
                        data-param-project-id="{{router.params.project}}"
                        data-success="trigger"
                        data-success-param-trigger-events="projects.listDomains">

                        <div data-ls-if="0 == {{console-domains.sum}} || undefined == {{console-domains.sum}}" class="box margin-top margin-bottom">
                            <h3 class="margin-bottom-small text-bold">No Custom Domains Added</h3>

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

                        <div class="box margin-bottom" data-ls-if="0 != {{console-domains.sum}}">
                            <table class="vertical">
                                <thead>
                                    <tr>
                                        <th width="120"></th>
                                        <th width="200">Domain Name</th>
                                        <th width="160">TLS</th>
                                        <th></th>
                                        <th width="40"></th>
                                    </tr>
                                </thead>
                                <tbody data-ls-loop="console-domains.domains" data-ls-as="domain">
                                    <tr>
                                        <td data-title="Status">
                                            <span class="text-size-small text-danger" data-ls-if="true !== {{domain.verification}}"><i class="icon-cancel-circled"></i> Unverified&nbsp;</span>
                                            <span class="text-size-small text-info" data-ls-if="true === {{domain.verification}}"><i class="icon-ok-circled"></i> Verified&nbsp;</span>
                                        </td>
                                        <td data-title="Domain: ">
                                            <span class="text-size-small" data-ls-bind="{{domain.domain}}"></span>
                                        </td>
                                        <td data-title="TLS: ">
                                            <span class="text-size-small text-fade" data-ls-if="!{{domain.certificateId}} && false === {{domain.verification}}"> &nbsp;Pending Verification&nbsp;</span>
                                            <span class="text-size-small text-fade" data-ls-if="!{{domain.certificateId}} && true === {{domain.verification}}"> &nbsp;In Progress&nbsp;</span>
                                            <span class="text-size-small text-success" data-ls-if="{{domain.certificateId}}"><i class="icon-ok-circled"></i> Enabled&nbsp;</span>
                                        </td>
                                        <td data-title="">
                                            <button class="link text-size-small" data-ls-if="true === {{domain.verification}}" data-ls-ui-trigger="dns-settings-{{domain.$id}}">DNS Settings</button>
                                            <button class="link text-size-small" data-ls-if="true !== {{domain.verification}}" data-ls-ui-trigger="dns-settings-{{domain.$id}}">Verify Domain</button>
                                            
                                            <div data-ui-modal class="modal box close" data-button-alias="none" data-open-event="dns-settings-{{domain.$id}}" xdata-close-event="dns-settings-close-{{domain.$id}}">
                                                <button type="button" class="close pull-end" data-ui-modal-close=""><i class="icon-cancel"></i></button>

                                                <h3 class="margin-bottom" data-ls-if="true === {{domain.verification}}" data-ls-ui-trigger="dns-settings-{{domain.$id}}">DNS Settings</h3>
                                                <h3 class="margin-bottom" data-ls-if="true !== {{domain.verification}}" data-ls-ui-trigger="dns-settings-{{domain.$id}}">Verify Domain</h3>

                                                <hr />

                                                <p>Add the following DNS records to your DNS provider settings to setup and verify your new custom domain.</p>

                                                <ol class="bullets">
                                                    <li>
                                                        <p>Add a new CNAME record in your DNS providers settings to point your new subdomain to your <?php echo APP_NAME; ?> server with the following value:</p>
                                                        
                                                        <div class="ide margin-bottom-small">
                                                            <pre class="line-numbers"><code class="prism language-javascript" data-prism><?php echo $this->print($customDomainsTarget, 'escape'); ?></code></pre>
                                                        </div>

                                                        <p>For example:</p>

                                                        <div class="ide margin-bottom-small">
                                                            <pre class="line-numbers"><code class="prism language-javascript" data-prism><?php echo strtolower(APP_NAME); ?>.myapp.com   CNAME   <?php echo $this->print($customDomainsTarget, 'escape'); ?></code></pre>
                                                        </div>
                                                    </li>

                                                    <li>
                                                        Confirm and verify your CNAME record values:
                                                        <form class="strip"
                                                            data-analytics
                                                            data-analytics-activity
                                                            data-analytics-event="submit"
                                                            data-analytics-category="console"
                                                            data-analytics-label="Update Domain Verification"
                                                            data-service="projects.updateDomainVerification"
                                                            data-scope="console"
                                                            data-event="submit"
                                                            data-loading="Verifying DNS records..."
                                                            data-success="alert,trigger"
                                                            data-success-param-alert-text="Verified domain successfully"
                                                            data-success-param-trigger-events="projects.updateDomainVerification"
                                                            data-failure="alert"
                                                            data-failure-param-alert-text="Failed to verify domain, check your DNS records"
                                                            data-failure-param-alert-classname="error">

                                                            <input type="hidden" name="projectId" data-ls-bind="{{router.params.project}}" />
                                                            <input type="hidden" name="domainId" data-ls-bind="{{domain.$id}}" />
                                                            
                                                            <button class="margin-top-small">Confirm & Verify</button>
                                                        </form>
                                                    </li>
                                                </ol>

                                                <div class="info margin-bottom">
                                                    <h4 class="margin-bottom-small">SSL Certificate</h4>
                                                    <p>After verification completes, <?php echo APP_NAME; ?> will automatically generate a secure SSL certificate for your domain. This process may take a few seconds. Certitficate renewals are automatically issued every 60 days.</p>
                                                </div>

                                                <button data-ui-modal-close="" type="button" class="reverse">Close Settings</button>
                                            </div>
                                        </td>
                                        <td data-title="">
                                            <form class="pull-end"
                                                data-analytics
                                                data-analytics-activity
                                                data-analytics-event="submit"
                                                data-analytics-category="console"
                                                data-analytics-label="Delete Project Domain"
                                                data-service="projects.deleteDomain"
                                                data-scope="console"
                                                data-event="submit"
                                                data-confirm="Are you sure you want to delete this domain?"
                                                data-success="alert,trigger"
                                                data-success-param-alert-text="Deleted domain successfully"
                                                data-success-param-trigger-events="projects.deleteDomain"
                                                data-failure="alert"
                                                data-failure-param-alert-text="Failed to delete domain"
                                                data-failure-param-alert-classname="error">

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

                                                <button class="danger round reverse small"><i class="icon-trash"></i></button>
                                            </form>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>

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

                                <h1>Add Domain</h1>

                                <form
                                    data-analytics
                                    data-analytics-activity
                                    data-analytics-event="submit"
                                    data-analytics-category="console"
                                    data-analytics-label="Create Project Domain"
                                    data-service="projects.createDomain"
                                    data-scope="console"
                                    data-event="submit"
                                    data-success="alert,trigger,reset"
                                    data-success-param-alert-text="Created domain successfully"
                                    data-success-param-trigger-events="projects.createDomain"
                                    data-failure="alert"
                                    data-failure-param-alert-text="Failed to create domain"
                                    data-failure-param-alert-classname="error">

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

                                    <label for="name">Domain Name</label>
                                    <input type="text" class="full-width" id="domain" name="domain" placeholder="appwrite.example.com" required autocomplete="off" title="Enter a valid domain name" pattern="^([a-zA-Z0-9][a-zA-Z0-9-_]*\.)*[a-zA-Z0-9]*[a-zA-Z0-9-_]*[[a-zA-Z0-9]+$" />
                                    
                                    <hr />

                                    <button type="submit">Create</button> &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                                </form>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </li>
            <li data-state="/console/settings/members?project={{router.params.project}}">
                <h2>Members</h2>

                <div class="zone xl"
                    data-service="teams.getMemberships"
                    data-scope="console"
                    data-event="load,teams.createMembership,teams.deleteMembership"
                    data-name="members"
                    data-param-team-id="{{console-project.teamId}}"
                    data-success="trigger"
                    data-success-param-trigger-events="teams.getMemberships">

                    <div class="box margin-bottom">
                        <ul data-ls-loop="members.memberships" data-ls-as="member" class="list">
                            <li class="clear">
                                <form class="pull-end"
                                    data-analytics
                                    data-analytics-activity
                                    data-analytics-event="submit"
                                    data-analytics-category="console"
                                    data-analytics-label="Delete Project Membership"
                                    data-service="teams.deleteMembership"
                                    data-scope="console"
                                    data-event="submit"
                                    data-success="alert,trigger"
                                    data-success-param-alert-text="Member Removed Successfully"
                                    data-success-param-trigger-events="teams.deleteMembership"
                                    data-failure="alert"
                                    data-failure-param-alert-text="Failed to Remove Member"
                                    data-failure-param-alert-classname="error">

                                    <input name="teamId" data-ls-attrs="id=leave-teamId-{{member.$id}}" type="hidden" data-ls-bind="{{console-project.teamId}}">
                                    <input name="membershipId" data-ls-attrs="id=leave-membershipId-{{member.$id}}" type="hidden" data-ls-bind="{{member.$id}}">

                                    <button class="danger">Leave</button>
                                </form>
                                
                                <div data-ls-if="false === {{member.confirm}}" class="pull-end margin-end">
                                    <form class="pull-end"
                                        data-analytics
                                        data-analytics-activity
                                        data-analytics-event="submit"
                                        data-analytics-category="console"
                                        data-analytics-label="Create Project Membership (resend)"
                                        data-service="teams.deleteMembership"
                                        data-scope="console"
                                        data-event="submit"
                                        data-success="trigger"
                                        data-success-param-trigger-events="teams.deleteMembership.resend"
                                        data-failure="alert"
                                        data-failure-param-alert-text="Failed to Resend Invitation"
                                        data-failure-param-alert-classname="error">

                                        <input name="teamId" data-ls-attrs="id=resend-teamId-{{member.$id}}" type="hidden" data-ls-bind="{{console-project.teamId}}">
                                        <input name="membershipId" data-ls-attrs="id=resend-membershipId-{{member.$id}}" type="hidden" data-ls-bind="{{member.$id}}">

                                        <button class="reverse">Resend</button>
                                    </form>

                                    <form class="pull-end"
                                        data-service="teams.createMembership"
                                        data-scope="console"
                                        data-event="teams.deleteMembership.resend"
                                        data-success="alert,trigger"
                                        data-success-param-alert-text="Invitation sent successfully"
                                        data-success-param-trigger-events="teams.createMembership.resent"
                                        data-failure="alert"
                                        data-failure-param-alert-text="Failed to send inivitation"
                                        data-failure-param-alert-classname="error">

                                        <input name="teamId" type="hidden" data-ls-bind="{{member.teamId}}">
                                        <input name="url" type="hidden" data-ls-bind="{{env.ENDPOINT}}/auth/join?project={{router.params.project}}" />
                                        <input name="email" type="hidden" data-ls-bind="{{member.email}}">
                                        <input name="name" type="hidden" data-ls-bind="{{member.name}}">
                                        <input name="roles" type="hidden" data-ls-bind="{{member.roles}}" data-cast-to="json">
                                    </form>
                                </div>

                                <img src="" data-ls-attrs="src={{member|avatar}}" data-size="200" alt="User Avatar" class="avatar pull-start margin-end" loading="lazy" width="60" height="60" />

                                <div class="margin-bottom-tiny">
                                    <span data-ls-bind="{{member.name}}"></span> &nbsp;&nbsp;<span class="tag" data-ls-bind="{{member.roles.0}}"></span> &nbsp;&nbsp;<span data-ls-if="false === {{member.confirm}}" class="tag red">Pending Approval</span>
                                </div>
                                <span class="text-size-small text-fade" data-ls-bind="{{member.email}}"></small>
                            </li>
                        </ul>
                    </div>

                    <div data-ui-modal class="modal box close width-medium" data-button-text="Invite Member" data-button-class="">
                        <button type="button" class="close pull-end" data-ui-modal-close><i class="icon-cancel"></i></button>

                        <h1>Invite Member</h1>

                        <form name="teams.createTeamMembership"
                            data-analytics
                            data-analytics-activity
                            data-analytics-event="submit"
                            data-analytics-category="console"
                            data-analytics-label="Create Project Membership"
                            data-service="teams.createMembership"
                            data-scope="console"
                            data-event="submit"
                            data-loading="Sending invitation, please wait..."
                            data-success="alert,trigger,reset"
                            data-success-param-alert-text="Invitation sent successfully"
                            data-success-param-trigger-events="teams.createMembership"
                            data-failure="alert"
                            data-failure-param-alert-text="Failed to send invite"
                            data-failure-param-alert-classname="error">

                            <input name="teamId" id="team-teamId" type="hidden" data-ls-bind="{{console-project.teamId}}">
                            <input name="url" type="hidden" data-ls-bind="{{env.ENDPOINT}}/auth/join?project={{router.params.project}}" />
                            
                            <label for="email">Email</label>
                            <input name="email" id="email" type="email" autocomplete="email" required>
                            
                            <label for="team-name">Name <small>(optional)</small></label>
                            <input name="name" id="team-name" type="text" autocomplete="name" maxlength="128" />

                            <label for="roles" style="display: none">Role</label>
                            <select id="roles" name="roles" required data-ls-loop="env.ROLES" data-ls-as="role" data-cast-to="array" style="display: none">
                                <option data-ls-attrs="value={{role.type}}" data-ls-bind="{{role.label}}"></option>
                            </select>

                            <?php if(!$smtpEnabled): ?>
                            <div class="box note padding-tiny warning margin-bottom text-align-center">
                                <i class="icon-warning"></i> SMTP connection is disabled. <a href="https://appwrite.io/docs/email-delivery" target="_blank" rel="noopener">Learn more <i class="icon-link-ext"></i></a>
                            </div>
                            <?php endif; ?>

                            <hr />

                            <div class="clear">
                                <button<?php if(!$smtpEnabled): ?> disabled<?php endif; ?>>Send Invite</button>
                                &nbsp; <button data-ui-modal-close="" type="button" class="reverse">Cancel</button>
                            </div>
                        </form>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>
