<script>
    (function() {
        function init() {
            let selectedGenericRate = 0;
            window.switchGenericRate = function (_trigger) {
                selectedGenericRate = 0;
                if ($(_trigger).find('option:selected').first().length) {
                    selectedGenericRate = +($(_trigger).find('option:selected').first().attr('data-amount'));
                }
                $(_trigger).closest('form').find('[name="numberOfUnits"]').val('');

                @if($genericRates && count($genericRates) && count($genericRates) === 1)
                    selectedGenericRate = {{ $genericRates[0]->amount }};
                @endif

                // max num units to limit at $15
                if ($(_trigger).attr('data-entity-type') === 'Note') {
                    let maxNABillableAmount = 30,
                        amountPer5Minutes = selectedGenericRate / 12,
                        maxNumberOf5Minutes = Math.ceil(maxNABillableAmount / amountPer5Minutes),
                        maxHours = (maxNumberOf5Minutes * 5) / 60,
                        unitsSelect = $(_trigger).closest('form').find('[name="numberOfUnits"]');

                    unitsSelect.find('option').each(function () {
                        if (this.value) {
                            if (+this.value > maxHours) {
                                $(this).hide();
                            }
                            else {
                                $(this).show();
                            }
                        }
                        else {
                            $(this).show();
                        }
                    });
                }

                calculateGenericBillAmount($(_trigger).closest('form').find('[name="numberOfUnits"]'));
            };
            window.calculateGenericBillAmount = function (_trigger) {
                _trigger = $(_trigger);
                let amountTarget = _trigger.closest('form').find('[calculated-generic-amount]');
                if (!_trigger.find('option:selected').length) {
                    amountTarget.text('');
                    return;
                }
                let hours = +_trigger.find('option:selected').attr('value'),
                    reimbursable = hours * selectedGenericRate;
                if (isNaN(reimbursable)) reimbursable = 0;
                amountTarget.html('<b>Reimbursable:</b> $' + reimbursable.toFixed(2));
            };
            window.updateGenericBillAction = function (_trigger) {
                let actions = [];
                $(_trigger).closest('.generic-bill-actions').find('.generic-bill-action:checked').each(function () {
                    actions.push($(this).attr('data-action'));
                });
                $(_trigger).closest('.generic-bill-actions').find('[name="genericDescription"]').val(actions.join("<br>"));
            };
            @if($genericRates && count($genericRates) && count($genericRates) === 1)
            switchGenericRate($('[name="code"][data-entity-type]'));
            @endif
        }
        addMCInitializer('init-generic-add-bill', init);
    }).call(window);
</script>
