<form class="{{cssClass}}" autocomplete="off" spellcheck="false" data-tooltip-class="pf2e">
    <header class="army-sheet-header">
        {{!-- Portrait --}}
        <section class="frame">
            <img src="{{actor.img}}" data-edit="img" data-tooltip="{{actor.name}}"/>
        </section>

        <div class="details">
            <input class="name" name="name" type="text" value="{{actor.name}}" placeholder="{{localize "PF2E.Kingmaker.Army.NamePlaceholder"}}" spellcheck="false" />
            {{!-- TRAITS --}}
            <div class="tags paizo-style">
                <select class="tag rarity {{data.traits.rarity}}" data-property="system.traits.rarity">
                    {{selectOptions rarityTraits selected=data.traits.rarity localize=true}}
                </select>
                <select name="system.traits.type" class="tag">
                    {{selectOptions armyTypes selected=data.traits.type localize=true}}
                </select>
            </div>
            <label class="level-label stats">
                {{localize "TYPES.Actor.army"}}
                <input class="level stats" type="number" value="{{data.details.level.value}}"/>
            </label>
            <div class="alliance">
                <span data-tooltip="PF2E.Kingmaker.Army.Alliance.Info">
                    {{#if (eq document.alliance "party")}}
                        {{localize "PF2E.Kingmaker.Army.Alliance.Kingdom"}}
                    {{else}}
                        {{localize "PF2E.Kingmaker.Army.Alliance.Enemy"}}
                    {{/if}}
                </span>

                {{#if (and (eq document.alliance "party") (not linked))}}
                    <a data-action="link-actor" data-tooltip="PF2E.Kingmaker.Army.Alliance.UnlinkedError">
                        <i class="fa-solid fa-link-slash error"></i>
                    </a>
                {{/if}}
            </div>
        </div>
    </header>

    <section class="army-sidebar">
        {{!-- AC --}}
        <section>
            <header>
                <i class="fa-solid fa-fw fa-shield-alt"></i>
                <h4 data-tooltip="{{ac.breakdown}}">{{localize "PF2E.ArmorClassLabel"}}</h4>
                <input type="number" class="stats {{ac.adjustmentClass}}" data-property="system.ac.value" value="{{ac.value}}" data-dtype="Number"/>
            </header>
        </section>

        {{!-- HP --}}
        <section>
            <header>
                <i class="fa-solid fa-fw fa-heart"></i>
                <h4>{{localize "PF2E.HitPointsShortLabel"}}</h4>
                <input type="number" data-property="system.attributes.hp.value" value="{{hitPoints.value}}" data-dtype="Number"/>
                <span class="slash">/</span>
                <input type="number" class="{{hitPoints.max.adjustmentClass}}" data-property="system.attributes.hp.max" value="{{hitPoints.max.value}}" data-dtype="Number"/>
            </header>
            <label {{#if document.routed}}class="routed" data-tooltip="{{localize "PF2E.Kingmaker.Army.RoutedDescription"}}"{{/if}}>
                <span>{{localize "PF2E.Kingmaker.Army.RoutThreshold"}}</span>
                <input type="number" class="{{hitPoints.routThreshold.adjustmentClass}}" data-property="system.attributes.hp.routThreshold" value="{{hitPoints.routThreshold.value}}" data-dtype="Number"/>
            </label>
        </section>

        <section class="saves">
            {{#each saves as |save|}}
                <div class="save" data-statistic="{{save.slug}}">
                    <a class="attribute-label rollable" data-tooltip="{{save.breakdown}}">{{localize save.label}}</a>
                    <input type="text" value="{{numberFormat save.mod decimals=0 sign=true}}" class="modifier adjustable {{save.adjustmentClass}}" data-property="system.saves.{{save.slug}}" data-dtype="Number" placeholder="0" />
                </div>
            {{/each}}
        </section>

        <section>
            <header>
                <i class="fa-solid fa-fw fa-eye"></i>
                <h4>{{localize "PF2E.Kingmaker.Army.Stats"}}</h4>
            </header>
            <div class="row">
                <span data-tooltip="{{document.scouting.breakdown}}">{{document.scouting.label}}</span>
                <section class="roll" data-statistic="scouting">
                    <a data-attribute="scouting" class="rollable roll-icon"><i class="fa-solid fa-dice-d20"></i></a>
                    <input class="stats modifier" type="text" data-property="system.scouting" value="{{numberFormat document.scouting.mod decimals=0 sign=true}}" data-dtype="Number"/>
                </section>
            </div>
            <label>
                <span>{{localize "PF2E.Kingmaker.Consumption.Label"}}</span>
                <input class="{{consumption.adjustmentClass}}" type="number" data-property="system.consumption" value="{{consumption.value}}"/>
            </label>
            <label>
                <span>{{localize "PF2E.Kingmaker.Army.RecruitmentDC"}}</span>
                <input type="number" data-property="system.recruitmentDC" value="{{data.recruitmentDC}}"/>
            </label>
            {{#if data.traits.senses.length}}
                <ol class="tags senses">
                    {{#each data.traits.senses as |sense slug|}}
                        {{#if sense.label}}<li class="tag tag_alt" data-slug="{{slug}}">{{sense.label}}</li>{{/if}}
                    {{/each}}
                </ol>
            {{/if}}
        </section>

        <section>
            <header>
                <i class="fa-solid fa-toolbox"></i>
                <h4>{{localize "PF2E.Kingmaker.Army.Gear.Label"}}</h4>
            </header>
            <div class="row gear">
                <span>{{localize "PF2E.Kingmaker.Army.Potions.Label"}}</span>
                {{#if (eq @root.data.resources.potions.value 0)}}
                    <span data-tooltip="{{localize "PF2E.Kingmaker.Army.Potions.OutOfPotions"}}" class="disabled"><i class="fa-light fa-champagne-glasses"></i></span>
                {{else if (gte @root.document.hitPoints.value @root.document.hitPoints.max)}}
                    <span data-tooltip="{{localize "PF2E.Kingmaker.Army.Potions.FullHP"}}" class="disabled"><i class="fa-light fa-champagne-glasses"></i></span>
                {{else}}
                    <a data-tooltip="{{localize "PF2E.Kingmaker.Army.Potions.UsePotion"}}" data-action="use-potion"><i class="fa-solid fa-champagne-glasses"></i></a>
                {{/if}}
                <a class="pips" data-action="change-resource" data-resource="potions">
                    {{#times @root.data.resources.potions.max as |idx|}}
                        <i class="{{#if (gt @root.data.resources.potions.value idx)}}fa-regular filled{{else}}fa-light empty{{/if}} fa-flask-round-potion"></i>
                    {{/times}}
                </a>
                {{#if document.kingdom}}
                    <a data-action="show-gear" data-gear="potions" data-tooltip="{{localize "PF2E.Kingmaker.Army.Gear.GearInfo"}}">
                        <i class="fa-regular fa-message"></i>
                    </a>
                {{/if}}
            </div>
            <div class="row gear">
                <span>{{localize "PF2E.Kingmaker.Army.Armor"}}</span>
                <a class="pips" data-action="change-magic-armor">
                    {{#times 3}}
                        <i class="{{#if (gt @root.data.ac.potency this)}}fa-regular fa-shield filled{{else}}fa-light fa-shield empty{{/if}}"></i>
                    {{/times}}
                </a>
                {{#if document.kingdom}}
                    <a data-action="show-gear" data-gear="armor" data-tooltip="{{localize "PF2E.Kingmaker.Army.Gear.GearInfo"}}">
                        <i class="fa-regular fa-message"></i>
                    </a>
                {{/if}}
            </div>
            <div class="row gear">
                <span>{{localize "PF2E.Kingmaker.Army.Strikes.melee"}}</span>
                <a class="pips" data-action="change-magic-weapon" data-weapon="melee">
                    <i class="{{#if data.weapons.melee}}fa-solid filled{{else}}fa-regular{{/if}} fa-circle split"></i>
                    <span>
                        {{#times 3}}
                            <i class="{{#if (gt @root.data.weapons.melee.potency this)}}fa-regular fa-sword filled{{else}}fa-light fa-sword{{/if}}"></i>
                        {{/times}}
                    </span>
                </a>
                {{#if document.kingdom}}
                    <a data-action="show-gear" data-gear="melee" data-tooltip="{{localize "PF2E.Kingmaker.Army.Gear.GearInfo"}}">
                        <i class="fa-regular fa-message"></i>
                    </a>
                {{/if}}
            </div>
            <div class="row gear">
                <span>{{localize "PF2E.Kingmaker.Army.Strikes.ranged"}}</span>
                <a class="pips" data-action="change-magic-weapon" data-weapon="ranged">
                    <i class="{{#if data.weapons.ranged}}fa-solid filled{{else}}fa-regular{{/if}} fa-circle split"></i>
                    <span>
                        {{#times 3}}
                            <i class="{{#if (gt @root.data.weapons.ranged.potency this)}}fa-regular fa-bow-arrow filled{{else}}fa-light fa-bow-arrow{{/if}}"></i>
                        {{/times}}
                    </span>
                </a>
                {{#if document.kingdom}}
                    <a data-action="show-gear" data-gear="ranged" data-tooltip="{{localize "PF2E.Kingmaker.Army.Gear.GearInfo"}}">
                        <i class="fa-regular fa-message"></i>
                    </a>
                {{/if}}
            </div>
        </section>
    </section>

    <section class="army-body sheet-body">
        <fieldset>
            <legend>
                <i class="fa-light fa-note"></i> {{localize "PF2E.Kingmaker.Army.Description"}}
                {{#if @root.options.editable}}
                    <div class="item-controls">
                        <a data-action="edit-description" data-tooltip="{{localize "PF2E.EditItemTitle"}}"><i class="fa-solid fa-fw fa-edit"></i></a>
                    </div>
                {{/if}}
            </legend>
            {{editor description target="system.details.description" button=true owner=owner editable=editable engine="prosemirror"}}
        </fieldset>

        {{!-- TOGGLES --}}
        {{#if toggles.actions}}
            <section class="toggles">
                {{> "systems/pf2e/templates/actors/partials/toggles.hbs" toggles=toggles.actions}}
            </section>
        {{/if}}

        <fieldset class="weapons">
            <legend>
                <i class="fa-light fa-swords"></i>
                {{localize "PF2E.Kingmaker.Army.Strikes.Label"}}
                {{#if document.strikes.ranged}}
                    <div class="ammunition">
                        (
                        <span>{{localize "PF2E.Kingmaker.Army.Ammunition"}}</span>
                        <a class="pips" data-action="change-resource" data-resource="ammunition" data-tooltip="{{localize "PF2E.Kingmaker.Army.Strikes.CurrentAmmo" current=data.resources.ammunition.value max=data.resources.ammunition.max}}">
                            {{#times @root.data.resources.ammunition.max}}
                                <i class="{{#if (gt @root.data.resources.ammunition.value this)}}fa-regular fa-dagger filled{{else}}fa-light fa-dagger empty{{/if}}"></i>
                            {{/times}}
                        </a>
                        <a data-action="reset-ammo" data-tooltip="{{localize "PF2E.Kingmaker.Army.Strikes.RestoreAmmunition"}}" >
                            <i class="{{#if (eq @root.data.weapons.ammunition.value 5)}}fa-light fa-refresh empty{{else}}fa-regular fa-refresh filled{{/if}}"></i>
                        </a>
                        )
                    </div>

                {{/if}}
            </legend>
            {{#unless (or document.strikes.melee document.strikes.ranged)}}
                <p>{{localize "PF2E.Kingmaker.Army.Strikes.None"}}</p>
            {{/unless}}
            {{#each document.strikes as |strike type|}}
                <div class="strike" data-strike="{{type}}">
                    {{#if (eq type "melee")}}
                        <i class="fa-solid fa-fw fa-sword"></i>
                    {{else}}
                        <i class="fa-solid fa-fw fa-bow-arrow"></i>
                    {{/if}}
                    <input class="name" type="text" name="system.weapons.{{type}}.name" value="{{strike.label}}" placeholder="{{localize (concat "PF2E.Kingmaker.Army.Strikes." type)}}" />
                    {{#each strike.variants as |variant index|}}
                        <button
                            type="button"
                            data-action="strike-attack"
                            data-variant-index="{{index}}"
                            class="attack"
                            {{#unless (or @root.data.resources.ammunition.value (ne type "ranged"))}}
                                disabled
                                data-tooltip="{{localize "PF2E.Kingmaker.Army.Strikes.OutOfAmmo"}}"
                            {{/unless}}>
                            {{#if (eq index 0)}}
                                <span class="name">{{localize "PF2E.WeaponStrikeLabel"}}</span>
                                <span class="action-glyph">{{strike.glyph}}</span>
                            {{/if}}
                            {{variant.label}}
                        </button>
                    {{/each}}
                    <button type="button" data-action="strike-damage" class="damage">{{localize "PF2E.DamageLabel"}}</button>
                    <button type="button" data-action="strike-damage" data-outcome="criticalSuccess" class="damage critical">{{localize "PF2E.CriticalDamageLabel"}}</button>
                </div>
            {{/each}}
        </fieldset>

        <fieldset class="conditions">
            <legend>
                <i class="fa-light fa-sparkles"></i> {{localize "PF2E.ConditionsLabel"}}
            </legend>
            <ol class="item-list effects-list">
                {{#each document.itemTypes.effect as |effect|}}
                    <li class="item" data-item-id="{{effect._id}}">
                        <a class="icon item-image" data-action="item-to-chat">
                            <img class="item-icon" src="{{effect.img}}" alt="{{effect.name}}" />
                            <i class="fa-solid fa-fw fa-message"></i>
                        </a>
                        <h4 class="item-name">
                            <a data-action="toggle-summary">{{effect.name}}</a>
                            {{#if (and (eq effect.type "effect") (eq effect.badge.type "counter"))}}
                                ({{coalesce effect.badge.label effect.badge.value}})
                            {{/if}}
                            {{#if (eq effect.type "condition")}}{{#unless effect.active}}<span> (Inactive)</span>{{/unless}}{{/if}}
                        </h4>
                        <div class="item-controls">
                            {{#if (and @root.options.editable (not effect.readonly))}}
                                {{#if (eq effect.badge.type "counter")}}
                                    <a class="increment" data-tooltip="PF2E.IncrementEffectTitle"><i class="fa-solid fa-fw fa-plus"></i></a>
                                    <a class="decrement" data-tooltip="PF2E.DecrementEffectTitle"><i class="fa-solid fa-fw fa-minus"></i></a>
                                {{/if}}

                                {{#if (eq effect.type "effect")}}
                                    <a data-action="edit-item" data-tooltip="PF2E.EditItemTitle"><i class="fa-solid fa-fw fa-edit"></i></a>
                                {{/if}}
                                <a data-action="delete-item" data-tooltip="PF2E.DeleteItemTitle"><i class="fa-solid fa-fw fa-trash"></i></a>
                            {{else if effect.readonly}}
                                <a class="info-only" data-tooltip="{{effect.breakdown}}"><i class="fa-solid fa-fw fa-info-circle"></i></a>
                            {{/if}}
                        </div>
                        <div class="item-summary" hidden></div>
                    </li>
                {{/each}}
            </ol>
        </fieldset>

        <fieldset class="actions">
            <legend>
                <i class="fa-light fa-diamond"></i> {{localize "PF2E.Kingmaker.Army.BasicWarActions"}}
            </legend>
            <ol class="item-list">
                {{#each basicWarActions as |action|}}
                    <li class="item basic-war-action" data-slug="{{action.slug}}">
                        <h4 class="item-name">
                            <a data-action="toggle-basic-war-action-summary">{{action.name}}</a>
                            {{{actionGlyph action.actionCost}}}
                        </h4>
                        <div class="item-controls">
                            <a data-action="use-basic-war-action" data-tooltip="PF2E.Kingmaker.Army.SendToChat">
                                <i class="fa-solid fa-fw fa-message"></i>
                            </a>
                        </div>
                        <div class="item-summary" hidden></div>
                    </li>
                {{/each}}
            </ol>
        </fieldset>

        {{#if warActions.length}}
            <fieldset class="actions">
                <legend>
                    <i class="fa-light fa-diamond"></i> {{localize "PF2E.Kingmaker.Army.TacticalWarActions"}}
                </legend>
                <ol class="item-list">
                    {{#each warActions as |action|}}
                        {{> actionItem action=action locked=(or action.granter (eq action.category "army-tactic"))}}
                    {{/each}}
                </ol>
            </fieldset>
        {{/if}}

        <fieldset data-group-id="{{document.tactics.id}}">
            <legend>
                <i class="fa-light fa-flag-swallowtail"></i>
                {{localize "PF2E.Kingmaker.Army.Tactics"}}
            </legend>
            <ol class="item-list">
                {{#each document.tactics.feats as |entry|}}
                    {{> featItem action=entry.feat slotId=entry.id}}
                {{/each}}
            </ol>
        </fieldset>

        {{#if document.bonusTactics.feats.length}}
            <fieldset>
                <legend>
                    <i class="fa-light fa-gift"></i>
                    {{localize "PF2E.Kingmaker.Army.TacticsFree"}}
                </legend>
                <ol class="item-list">
                    {{#each document.bonusTactics.feats as |entry|}}
                        {{> featItem action=entry.feat slotId=entry.id}}
                    {{/each}}
                </ol>
            </fieldset>
        {{/if}}
    </section>
</form>

{{#*inline "actionItem"}}
    {{#if action}}
        <li class="item" {{#if action}}data-item-id="{{action.id}}"{{/if}} {{#if slotId}}data-slot-id="{{slotId}}"{{/if}}>
            <h4 class="item-name">
                <a data-action="toggle-summary">{{action.name}}</a>
                {{{actionGlyph action.actionCost}}}
            </h4>
            {{#if action.system.frequency}}
                <section class="item-frequency">
                    <input type="number" value="{{action.system.frequency.value}}" data-item-id="{{action.id}}" data-item-property="system.frequency.value"/>
                    <span>
                        /
                        {{action.system.frequency.max}}
                        {{localize "PF2E.Frequency.per"}}
                        {{localize (lookup @root.frequencies action.system.frequency.per)}}
                    </span>
                </section>
            {{/if}}
            {{#if @root.editable}}
                <div class="item-controls">
                    <a data-action="item-to-chat" data-tooltip="PF2E.Kingmaker.Army.SendToChat">
                        <i class="fa-solid fa-fw fa-message"></i>
                    </a>
                    <a data-action="edit-item" data-tooltip="PF2E.EditItemTitle"><i class="fa-solid fa-fw fa-edit"></i></a>
                    {{#if locked}}
                        <i class="fa-solid fa-lock fa-fw" data-tooltip="{{localize "PF2E.Kingmaker.GrantedBy" granter=action.granter.name}}"></i>
                    {{else}}
                        <a data-action="delete-item" data-tooltip="PF2E.DeleteItemTitle"><i class="fa-solid fa-fw fa-trash"></i></a>
                    {{/if}}
                </div>
            {{/if}}
            <div class="item-summary" hidden></div>
        </li>
    {{/if}}
{{/inline}}

{{#*inline "featItem"}}
    {{#if action}}
        <li class="item" {{#if action}}data-item-id="{{action.id}}"{{/if}} {{#if slotId}}data-slot-id="{{slotId}}"{{/if}}>
            <a class="icon item-image">
                <img class="item-icon" src="{{action.img}}" alt="{{action.name}}" />
                <i class="fa-solid fa-fw fa-message"></i>
            </a>
            <h4 class="item-name">
                <a data-action="toggle-summary">{{action.name}}</a>
            </h4>
            {{#if action.system.frequency}}
                <section class="item-frequency">
                    <input type="number" value="{{action.system.frequency.value}}" data-item-id="{{action.id}}" data-item-property="system.frequency.value"/>
                    <span>
                        /
                        {{action.system.frequency.max}}
                        {{localize "PF2E.Frequency.per"}}
                        {{localize (lookup @root.frequencies action.system.frequency.per)}}
                    </span>
                </section>
            {{/if}}
            {{#if @root.editable}}
                <div class="item-controls">
                    <a data-action="edit-item" data-tooltip="PF2E.EditItemTitle"><i class="fa-solid fa-fw fa-edit"></i></a>
                    {{#if locked}}
                        <i class="fa-solid fa-lock fa-fw" data-tooltip="{{localize "PF2E.Kingmaker.GrantedBy" granter=action.granter.name}}"></i>
                    {{else}}
                        <a data-action="delete-item" data-tooltip="PF2E.DeleteItemTitle"><i class="fa-solid fa-fw fa-trash"></i></a>
                    {{/if}}
                </div>
            {{/if}}
            <div class="item-summary" hidden></div>
        </li>
    {{else}}
        <li class="item empty" {{#if slotId}}data-slot-id="{{slotId}}"{{/if}}>
            <div class="item-icon"></div>
            <div class="item-name"><span>{{localize "PF2E.EmptySlot"}}</span></div>
        </li>
    {{/if}}
{{/inline}}
