<script>
    /*

start:
	if only one rate
		if hourly
			show minutes select
		else
			show reimb amount
		end if
	else
		show rate select
	end if

----

on rate select change
	if selected rate is hourly
		show minutes select
	else
		show reimb amount
	end if

----

on minutes select change
	show reimb amount

     */

    (function() {
        function init() {

            let selectedAmount = 0, selectedType = '';

            window.switchGenericRate = function (_trigger) {
                _trigger = $(_trigger);

                _trigger.closest('form').find('[calculated-generic-amount]').text('');

                let type = '', amount = '';
                if(_trigger.is('select')) {
                    let selectedOption = _trigger.find('option:selected').first();
                    if(selectedOption.length) {
                        type = selectedOption.attr('data-rate-type');
                        amount = +(selectedOption.attr('data-amount'));
                    }
                }
                else { // only one rate
                    type = _trigger.attr('data-rate-type');
                    amount = +(_trigger.attr('data-amount'));
                }

                if(!type || !amount) {
                    return false;
                }

                selectedType = type;
                selectedAmount = amount;

                let unitsSelect = $(_trigger).closest('form').find('[name="numberOfUnits"]'),
                    minutesSelect = $(_trigger).closest('form').find('[name="numMinutes"]');

                if(type === 'hourly') {
                    unitsSelect.val('');
                    minutesSelect.val('');
                    let maxNABillableAmount = 30,
                        amountPer5Minutes = amount / 12,
                        maxNumberOf5Minutes = Math.ceil(maxNABillableAmount / amountPer5Minutes),
                        maxHours = (maxNumberOf5Minutes * 5) / 60;
                    minutesSelect.find('option').each(function () {
                        if (this.value) {
                            if (+this.value > maxHours) {
                                $(this).hide();
                            }
                            else {
                                $(this).show();
                            }
                        }
                        else {
                            $(this).show();
                        }
                    });
                    minutesSelect.closest('.minutesSelectContainer').removeClass('d-none');
                }
                else { // flat
                    unitsSelect.val('1');
                    minutesSelect.closest('.minutesSelectContainer').addClass('d-none');
                    calculateGenericBillAmount(unitsSelect);
                }
            };
            window.minutesToUnits = function(_trigger) {
                _trigger = $(_trigger);

                if(!_trigger.val()) return false;
                let unitsSelect = $(_trigger).closest('form').find('[name="numberOfUnits"]');
                unitsSelect.val(+(_trigger.val()));
                calculateGenericBillAmount(unitsSelect);
            };
            window.calculateGenericBillAmount = function (_trigger) {
                _trigger = $(_trigger);

                let amountTarget = _trigger.closest('form').find('[calculated-generic-amount]').text('');
                let reimbursable = +(_trigger.val()) * selectedAmount;
                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-note-generic-add-bill', init);
    }).call(window);
</script>
