/**
 * ComptaFlow Financial Screen
 * Version 1.0.0
 */
(function($) {
    'use strict';

    $(document).ready(function() {
        ComptaFlow_Financial.init();
    });

    var ComptaFlow_Financial = {
        
        init: function() {
            this.initTypeChange();
            this.initManualAmountsToggle();
            this.initVATCodeSync();
        },

        initTypeChange: function() {
            var $select = $('#operation-type');
            if ($select.length === 0) return;

            function handleTypeChange() {
                var selectedType = $select.val();
                var $selectedOption = $select.find('option:selected');
                var hasVat = $selectedOption.data('has-vat') === 1;

                var $standardFields = $('#standard-fields');
                var $inKindFields = $('#in-kind-fields');
                var $bankTransferFields = $('#bank-transfer-fields');
                var $simpleAmountRow = $('#simple-amount-row');
                var $vatToggleRow = $('#vat-toggle-row');
                var $autoAmountRow = $('#auto-amount-row');
                var $vatRateRow = $('#vat-rate-row');

                if (selectedType === 'CAPITAL_IN_KIND') {
                    $standardFields.hide();
                    $inKindFields.css('display', 'table-row-group');
                } else {
                    $standardFields.css('display', 'table-row-group');
                    $inKindFields.hide();
                }

                if (selectedType === 'BANK_TRANSFER') {
                    $bankTransferFields.css('display', 'table-row');
                } else {
                    $bankTransferFields.hide();
                }

                if (hasVat) {
                    $simpleAmountRow.hide();
                    $vatToggleRow.css('display', 'table-row');
                    $autoAmountRow.css('display', 'table-row');
                    $vatRateRow.css('display', 'table-row');
                    $('#amount_simple').removeAttr('required');
                    $('#amount_ttc_auto').attr('required', 'required');
                } else {
                    $simpleAmountRow.css('display', 'table-row');
                    $vatToggleRow.hide();
                    $autoAmountRow.hide();
                    $vatRateRow.hide();
                    $('#amount_simple').attr('required', 'required');
                    $('#amount_ttc_auto').removeAttr('required');
                }
            }

            $select.on('change', handleTypeChange);
            handleTypeChange();
        },

        initManualAmountsToggle: function() {
            var $checkbox = $('#manual_amounts');
            if ($checkbox.length === 0) return;

            var $autoRow = $('#auto-amount-row');
            var $manualRow = $('#manual-amount-row');

            function toggle() {
                var isManual = $checkbox.is(':checked');
                $autoRow.toggle(!isManual);
                $manualRow.toggle(isManual);
            }

            $checkbox.on('change', toggle);
        },

        initVATCodeSync: function() {
            var $select = $('#vat_rate_select');
            var $hiddenCode = $('#vat_code');

            if ($select.length === 0 || $hiddenCode.length === 0) return;

            function sync() {
                var $selected = $select.find('option:selected');
                $hiddenCode.val($selected.data('code') || '');
            }

            $select.on('change', sync);
            sync();
        }
    };

    window.ComptaFlow_Financial = ComptaFlow_Financial;

})(jQuery);