<div>
    {% from 'wallet/send/new/components/coin_selection_table.jinja' import coin_selection_table %}
    {{ coin_selection_table(wallet_utxo + rbf_utxo, specter.explorer, selected_coins, rbf_tx_id) }}
    <br>
</div>

<script>
    var coinSelectAmount = 0.0;
    function toggleExpand() {
        if (isCoinSelectionActive()) {
            setVisibility('coin_selection_table', 'none');
        } else {
            setVisibility('coin_selection_table', 'block');

            let coins = document.getElementsByClassName('coin_select_checkbox');
            // unselect all choices
            for(var i = 0; i < coins.length; i++){
                coins[i].checked = false;
            }
        }
        validateForm();
    }

    function updateCoinSelect(coin, amount) {
        let coinAmount = parseFloat(amount);
        if (coin.checked) {
            coinSelectAmount += coinAmount;
        } else {
            coinSelectAmount -= coinAmount;
        }
        coinSelectAmount = parseFloat(coinSelectAmount.toFixed(8));
        validateForm();
    }

    function isCoinSelectionActive() {
        return document.getElementById("coin_selection_table").style.display !== 'none';
    }

    function getSpendableAmount(unit) {
        let spendableAmount;
        if (!isCoinSelectionActive()) {
            if(unit == 'btc' || unit == 'sat'){
                spendableAmount = '{{ wallet.full_available_balance | btcamount }}';
            }else{
                if(unit in assetBalances){
                    return assetBalances[unit].balance;
                }else{
                    return 0;
                }
            }
        } else {
            spendableAmount = coinSelectAmount;
        }

        return (unit == 'sat' ? spendableAmount * 100000000 : spendableAmount);
    }

    function shouldSelectMoreCoins(unit, amount) {
        return (unit == 'sat' ? amount / 100000000 : amount) > coinSelectAmount && isCoinSelectionActive()
    }
</script>
