<!-- +-------------------------------------------------+
 - © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
 - +-------------------------------------------------+
 - $Id: SourceFormZ3950.vue,v 1.1 2026/07/16 gneveu Exp $
-->
<template>
    <div class="prefillingform-source-z3950">
        <fieldset class="widget-fieldset">
            <legend class="widget-legend">
                {{ messages.get('prefillingform_admin', 'prefillingform_source_connector') }}
            </legend>
            <div class="row">
                <select id="connectorSelect" v-model.number="form.connector">
                    <option :value="0">{{ messages.get('prefillingform_admin', 'prefillingform_source_connector_choice') }}</option>
                    <option v-for="c in connectors" :key="c.id" :value="c.id">{{ c.name }}</option>
                </select>
                <label for="testIsbn">{{ messages.get('prefillingform_admin', 'prefillingform_source_test_isbn') }}</label>
                <input type="text" id="testIsbn" class="saisie-20em" v-model="testIsbn" />
                <input type="button" class="bouton" :disabled="!form.connector || testing" @click="test"
                    :value="messages.get('prefillingform_admin', 'prefillingform_source_test')" />
                <a href="./admin.php?categ=connecteurs" target="_blank">
                    {{ messages.get('prefillingform_admin', 'prefillingform_source_connector_manage') }}
                </a>
            </div>
            <div class="row" v-if="testResult">
                <span v-if="testResult.ok">
                    {{ messages.get('prefillingform_admin', 'prefillingform_source_test_ok') }} ({{ testResult.ms }} ms)
                </span>
                <span v-else>{{ messages.get('prefillingform_admin', 'prefillingform_source_test_failed') }}</span>
                <template v-if="testResult.ok && !testResult.unimarc">
                    {{ messages.get('prefillingform_admin', 'prefillingform_source_test_no_record') }}
                </template>
            </div>
            <div class="row" v-if="testResult && testResult.unimarc">
                <pre class="prefillingform-test-unimarc">{{ testResult.unimarc }}</pre>
            </div>
        </fieldset>

        <fieldset class="widget-fieldset">
            <legend class="widget-legend">
                {{ messages.get('prefillingform_admin', 'prefillingform_input_fields_title') }}
            </legend>
            <div class="row">
                <inputchips :selected="form.inputFields" :options="inputOptions"></inputchips>
            </div>
        </fieldset>

        <fieldset class="widget-fieldset">
            <legend class="widget-legend">
                {{ messages.get('prefillingform_admin', 'prefillingform_output_fields_title') }}
            </legend>
            <div class="row">
                <outputfieldsaccordion :selected="form.outputFields" :blocs="blocs"></outputfieldsaccordion>
            </div>
        </fieldset>
    </div>
</template>

<script>
import inputchips from "./InputChips.vue";
import outputfieldsaccordion from "./OutputFieldsAccordion.vue";

export default {
    props: {
        form: {
            type: Object,
            required: true,
        },
        connectors: {
            type: Array,
            default: () => [],
        },
        blocs: {
            type: Object,
            default: () => ({}),
        },
        testIsbnDefault: {
            type: String,
            default: '',
        },
    },
    components: { inputchips, outputfieldsaccordion },
    data: function () {
        return {
            testing: false,
            testResult: null,
            testIsbn: this.testIsbnDefault,
            inputOptions: [
                { code: 'isbn', labelKey: 'prefillingform_input_isbn' },
                { code: 'ark', labelKey: 'prefillingform_input_ark' },
                { code: 'titre', labelKey: 'prefillingform_input_titre' },
                { code: 'auteur', labelKey: 'prefillingform_input_auteur' },
            ],
        };
    },
    methods: {
        test: async function () {
            this.testing = true;
            this.testResult = null;
            let formData = new FormData();
            formData.append('data', JSON.stringify({ connectorId: this.form.connector, isbn: this.testIsbn }));

            const response = await fetch('./ajax.php?module=admin&categ=prefillingform&sub=sources&action=testconnector', {
                method: 'POST',
                body: formData,
            });
            this.testResult = await response.json();
            this.testing = false;
        },
    },
};
</script>
