<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <template id="runbot.batch">
            <t t-call="runbot.layout">
                <div class="row g-0">
                    <div class="col-lg-6">
                    <t t-set="batch_class" t-value="'bg-info-subtle' if batch.state=='preparing' else 'bg-success-subtle' if not any(log.level != 'INFO' for log in batch.log_ids) else 'bg-warning-subtle'"/>
                    <table class="table table-stripped">
                        <tr>
                            <td>Bundle</td>
                            <td>
                                <a t-out="batch.bundle_id.name" t-attf-href="/runbot/bundle/{{batch.bundle_id.id}}"/>
                                &amp;emsp;
                                <a groups="runbot.group_runbot_advanced_user"
                                    t-attf-href="/odoo/runbot.batch/{{batch.id}}?menu_id={{env['ir.model.data']._xmlid_to_res_id('runbot.runbot_menu_root')}}" class="btn btn-default btn-sm" target="_blank" title="View Batch in Backend">
                                          <i class="fa fa-list"/>
                                </a>
                            </td>
                        </tr>
                        <tr t-if="batch.category_id.id != default_category">
                            <td>Category</td>
                            <td t-out="batch.category_id.name"></td>
                        </tr>
                        <tr>
                            <td>Version</td>
                            <td t-out="batch.slot_ids[0].params_id.version_id.name if batch.slot_ids else batch.bundle_id.version_id.name"/>
                        </tr>
                        <tr>
                            <td>Create date</td>
                            <td t-out="batch.create_date"/>
                        </tr>
                        <tr t-if="more">
                            <td>Last update</td>
                            <td>
                                <t t-out="batch.last_update"/>
                                <span class="badge text-bg-info" t-out="s2human(batch.last_update - batch.create_date)"/>
                            </td>
                        </tr>
                        <tr>
                            <td t-att-class="batch_class" >Commits</td>
                            <td t-att-class="batch_class" >
                                <div t-foreach="batch.commit_link_ids.sorted(key=lambda lc: (lc.commit_id.repo_id.sequence, lc.commit_id.repo_id.id))" t-as="commit_link">
                                    <t t-set="commit" t-value="commit_link.commit_id"/>
                                    <span/>
                                    <a t-attf-href="/runbot/commit/#{commit.id}">
                                        <i class="fa fa-fw fa-hashtag" t-if="commit_link.match_type == 'new'" title="This commit is a new head"/>
                                        <i class="fa fa-fw fa-link" t-if="commit_link.match_type == 'head'" title="This commit is an existing head from bundle branches"/>
                                        <i class="fa fa-fw fa-code-fork" t-if="commit_link.match_type == 'base_match'" title="This commit is matched from a base batch with matching merge_base"/>
                                        <i class="fa fa-fw fa-clock-o" t-if="commit_link.match_type == 'base_head'" title="This commit is the head of a base branch"/>
                                        <span class="label" t-out="commit.dname"/>
                                    </a>
                                    <a t-att-href="'https://%s/commit/%s' % (commit_link.branch_id.remote_id.base_url, commit_link.commit_id.name)" title="View Commit on Github"><i class="fa fa-github"/></a>
                                    <small t-if="commit_link.match_type and commit_link.match_type.startswith('base')">
                                        from base:
                                        <span t-out="commit_link.branch_id.name"/>
                                        <br/>
                                    </small>
                                    <small t-else="">
                                        found in branch
                                        <span t-out="commit_link.branch_id.name"/>
                                        <t t-if="batch.state != 'preparing'">
                                            <span t-out="'+%s' % commit_link.diff_add" class="text-success"/>
                                            <span t-out="'-%s' % commit_link.diff_remove" class="text-danger"/>
                                            <span class="text-info">
                                                (
                                                <span t-out="commit_link.file_changed"/>
                                                <i class="fa fa-file"/>
                                                )
                                                <span io="behind">(
                                                    <span t-out="'%s ahead' % commit_link.base_ahead" class="text-success"/>
                                                    ,
                                                    <span t-out="'%s behind' % commit_link.base_behind" class="text-danger"/>
                                                )</span>
                                            </span>
                                        </t>
                                        <br/>
                                        <t t-if="more">
                                            Base head:
                                            <span t-out="commit_link.base_commit_id.name"/>
                                            <br/>
                                            Merge base:
                                            <span t-out="commit_link.merge_base_commit_id.name"/>
                                            <br/>
                                        </t>
                                    </small>
                                    <b t-if="commit.rebase_on_id">Automatic rebase on <t t-out="commit.rebase_on_id.name"/><br/></b>
                                    <t t-if="more or not (commit_link.match_type and commit_link.match_type.startswith('base'))">
                                        Subject:
                                        <span t-out="commit.subject"/>
                                        <br/>
                                        Author:
                                        <span t-out="commit.author"/>
                                        (
                                        <span t-out="commit.author_email"/>
                                        )
                                        <br/>
                                        <t t-if="commit.author != commit.committer">
                                            Committer:
                                            <span t-out="commit.committer"/>
                                            (
                                            <span t-out="commit.committer_email"/>
                                            )
                                            <br/>
                                        </t>
                                        Commit date:
                                        <span t-out="commit.date"/>
                                        <br/>
                                    </t>
                                    <hr/>
                                </div>
                            </td>
                        </tr>
                        <tr t-if="more">
                            <t t-set="reference_batch_ids" t-value="batch.reference_batch_ids"/>
                            <td>
                                <t t-if="batch.bundle_id.is_base">
                                    Reference batches (is base)
                                </t>
                                <t t-else="">
                                    <t t-if="reference_batch_ids">
                                        Reference batches (custom, not using base)
                                    </t>
                                    <t t-else="">
                                        <t t-set="reference_batch_ids" t-value="batch.base_reference_batch_id.reference_batch_ids"/>
                                        Reference batches (from base)
                                    </t>
                                </t>
                            </td>
                            <td>
                                <t t-foreach="reference_batch_ids.sorted(lambda b: b.bundle_id.version_id.number, reverse=True)" t-as="reference_batch">
                                    <div t-attf-title="Created {{s2human(reference_batch.create_date - batch.create_date)}} before this one">
                                         <a t-attf-href="/runbot/batch/{{reference_batch.id}}"><t t-out="reference_batch.bundle_id.version_id.name"/> (<t t-out="reference_batch.id"/>)</a>
                                    </div>
                                </t>
                            </td>
                        </tr>
                    </table>
                    </div>
                    <div class="col-lg-6">
                    <table class="table table-stripped">
                        <tr>
                            <td>Builds</td>
                            <td>
                                <t t-foreach="batch.slot_ids.filtered(lambda s: not s.trigger_id.manual)" t-as="slot">
                                    <t t-call="runbot.slot_button"/>
                                </t>
                            </td>
                        </tr>
                        <tr>
                            <td>Manual</td>
                            <td>
                                <t t-foreach="batch.slot_ids.filtered(lambda s: s.trigger_id.manual)" t-as="slot">
                                    <t t-if="slot.build_id or (not slot.trigger_id.team_ids) or (user_id in slot.trigger_id.team_ids.user_ids)">
                                        <t t-call="runbot.slot_button"/>
                                    </t>
                                </t>
                            </td>
                        </tr>
                        <tr t-if="more">
                            <td>Old builds</td>
                            <td>
                                <t t-foreach="batch.with_context(active_test=False).slot_ids.filtered(lambda s: not s.active)" t-as="slot">
                                    <s>
                                        <t t-call="runbot.slot_button"/>
                                    </s>
                                </t>
                            </td>
                        </tr>
                    </table>
                    </div>
                </div>
                <t t-foreach="batch.log_ids" t-as="log">
                    <t t-set="logclass" t-value="dict(ERROR='danger', WARNING='warning', INFO='info').get(log.level, 'warning')"/>
                    <div t-attf-class="alert alert-{{logclass}}">
                        <b t-out="log.level"/>
                        --
                        <t t-out="log._markdown()"/>
                    </div>
                </t>
            </t>
        </template>

        <template id="runbot.trigger_report">
            <t t-call="runbot.layout">
                <t t-call="{{trigger.report_view_id.id}}">
                </t>
            </t>
        </template>
    </data>
</odoo>
