<odoo>
    <template id="alerts" inherit_id="runbot_merge.alerts">
        <xpath expr="//div[@id='alerts']">
            <t t-set="fpcron" t-value="env(user=1).ref('forwardport.port_forward')"/>
            <div t-if="not fpcron.active" class="alert alert-warning col-12" role="alert">
                Forward-port is disabled, merged pull requests will not be forward-ported.
            </div>
        </xpath>
        <!-- key block (hopefully) -->
        <xpath expr="//div[@id='alerts']" position="inside">
            <t t-if="env['runbot_merge.pull_requests'].check_access_rights('read', False)">
                <t t-set="outstanding" t-value="env['runbot_merge.pull_requests'].search_count([
                    ('state', 'in', ['opened', 'validated', 'approved', 'ready', 'error']),
                    ('source_id.merge_date', '&lt;', datetime.datetime.now() - relativedelta(days=7)),
                ])"/>
                <div t-if="outstanding != 0" class="alert col-md-12 alert-warning mb-0">
                    <a href="/forwardport/outstanding">
                        <t t-out="outstanding"/> outstanding forward-ports (>1 week)
                    </a>
                </div>
            </t>
        </xpath>
    </template>

    <template id="pr_background">
        <t t-if="p.state == 'merged'">bg-success</t>
        <t t-elif="p.state == 'closed'">bg-light</t>
        <t t-elif="p.state == 'error'">bg-danger</t>
        <t t-else="">bg-warning</t>
    </template>

    <template id="outstanding" name="Outstanding forward ports">
        <t t-call="website.layout">
            <div id="wrap" class="oe_structure oe_empty"><div class="container-fluid">
                <div class="alert bg-light outstanding-partners">
                    <t t-foreach="groups" t-as="group">
                        <span>
                            <t t-if="group == current_group">
                                <span class="bg-primary text-bg-primary">
                                    <t t-out="group.display_name"/>
                                    (<t t-out="outstanding_per_group[group]"/>)
                                </span>
                                <a t-att-href="link()" class="btn fa fa-times p-0"/>
                            </t>
                            <t t-else="">
                                <a t-att-href="link(group=group.id, partner=0)">
                                    <t t-out="group.display_name"/>
                                    (<t t-out="outstanding_per_group[group]"/>)
                                </a>
                            </t>
                        </span>
                    </t>
                </div>
                <div class="alert bg-light outstanding-partners">
                    <t t-foreach="culprits" t-as="culprit">
                        <t t-set="approved" t-value="outstanding_per_reviewer[culprit]"/>
                        <t t-set="created" t-value="outstanding_per_author[culprit]"/>
                        <a t-att-href="link(partner=culprit.id)"
                           t-attf-title="approved {{approved}}, created {{created}}"
                           t-att-class="'bg-primary text-bg-primary' if culprit == env.user.partner_id else None"
                        ><t t-out="culprit.name"/>:
                            <t t-if="approved" t-out="approved"/>
                            <t t-if="approved and created"> + </t>
                            <t t-if="created" t-out="created"/>
                        </a>
                    </t>
                </div>
                <t t-if="not single">
                    by
                    <span class="btn-group btn-group-toggle">
                        <a t-att-href="link(authors=1, reviewers=1)"
                           t-attf-class="btn btn-sm btn-secondary {{'active' if authors and reviewers else ''}}">
                            both
                        </a>
                        <a t-att-href="link(authors=1, reviewers=0)"
                           t-attf-class="btn btn-sm btn-secondary {{'active' if authors and not reviewers else ''}}">
                            creators
                        </a>
                        <a t-att-href="link(reviewers=1, authors=0)"
                           t-attf-class="btn btn-sm btn-secondary {{'active' if reviewers and not authors else ''}}">
                            reviewers
                        </a>
                    </span>
                </t>
                <h1>List of pull requests with outstanding forward ports</h1>
                <h2 t-if="single">
                    for <span t-field="single.display_name" t-attf-title="@{{single.github_login}}"/>
                    <a t-att-href="link(partner=0)" title="All Users" class="btn fa fa-times"/>

                    <span class="btn-group btn-group-toggle">
                        <a t-att-href="link(authors=1, reviewers=1)"
                           t-attf-class="btn btn-sm btn-secondary {{'active' if authors and reviewers else ''}}">
                            both
                        </a>
                        <a t-att-href="link(authors=1, reviewers=0)"
                           t-attf-class="btn btn-sm btn-secondary {{'active' if authors and not reviewers else ''}}">
                            created
                        </a>
                        <a t-att-href="link(reviewers=1, authors=0)"
                           t-attf-class="btn btn-sm btn-secondary {{'active' if reviewers and not authors else ''}}">
                            reviewed
                        </a>
                    </span>
                </h2>

                <dl><t t-foreach="outstanding" t-as="x">
                    <t t-set="source" t-value="x['source']"/>
                    <t t-if="not single or source.reviewed_by == single or source.author == single">
                        <dt>
                            <a t-att-href="source.url"><span t-field="source.display_name"/></a>
                            created by <span t-field="source.author.display_name"
                                             t-attf-title="@{{source.author.github_login}}"/>
                            merged <span t-field="source.merge_date"
                                         t-options="{'widget': 'relative'}"
                                         t-att-title="source.merge_date"/>
                            by <span t-field="source.reviewed_by.display_name"
                                     t-attf-title="@{{source.reviewed_by.github_login}}"/>
                        </dt>
                        <dd>
                            Outstanding forward-ports:
                            <ul>
                                <li t-foreach="x['prs']" t-as="p">
                                    <a t-att-href="p.url"><span t-field="p.display_name"/></a>
                                    (<span t-field="p.state"/>)
                                    targeting <span t-field="p.target.name"/>
                                </li>
                            </ul>
                        </dd>
                    </t>
                </t></dl>
            </div></div>
        </t>
    </template>

    <template id="view_pull_request" inherit_id="runbot_merge.view_pull_request">
        <xpath expr="//dl[hasclass('runbot-merge-fields')]" position="inside">
            <t t-if="pr.state == 'merged'">
                <dt>merged</dt>
                <dd>
                    <span t-field="pr.merge_date" t-options="{'widget': 'relative'}"
                          t-att-title="pr.merge_date"/>
                    by <span t-field="pr.reviewed_by.display_name"
                             t-attf-title="@{{pr.reviewed_by.github_login}}"/>
                </dd>
            </t>
        </xpath>
    </template>

    <record model="ir.ui.view" id="project">
        <field name="name">Show forwardport project fields</field>
        <field name="inherit_id" ref="runbot_merge.runbot_merge_form_project"/>
        <field name="model">runbot_merge.project</field>
        <field name="arch" type="xml">
            <xpath expr="//sheet/group[2]" position="after">
                <group string="Forwardport Configuration">
                    <group>
                        <field string="Token" name="fp_github_token"/>
                    </group>
                    <group>
                        <field string="Bot Name" name="fp_github_name" readonly="0"/>
                    </group>
                </group>
            </xpath>
            <xpath expr="//field[@name='repo_ids']/tree" position="inside">
                <field string="FP remote" name="fp_remote_target"
                       help="Repository where forward port branches will be created"
                />
            </xpath>
        </field>
    </record>

    <record model="ir.ui.view" id="repository">
        <field name="name">Show forwardport repository fields</field>
        <field name="inherit_id" ref="runbot_merge.form_repository"/>
        <field name="model">runbot_merge.repository</field>
        <field name="arch" type="xml">
            <field name="branch_filter" position="after">
                <field string="FP remote" name="fp_remote_target"
                       help="Repository where forward port branches will be created"/>
            </field>
        </field>
    </record>

</odoo>
