<template>
    <div class="underground-view">
        <StatusBar />

        <h2>{{ $t('falukant.underground.title') }}</h2>

        <SimpleTabs v-model="activeTab" :tabs="tabs" @change="onTabChange" />

        <div class="tab-content">
            <!-- Aktivitäten -->
            <div v-if="activeTab === 'activities'" class="tab-pane">
                <!-- Neues Activity-Formular -->
                <div class="create-activity">
                    <h3>{{ $t('falukant.underground.activities.create') }}</h3>

                    <label class="form-label">
                        {{ $t('falukant.underground.activities.type') }}
                        <select v-model="newActivityTypeId" class="form-control">
                            <option v-for="type in undergroundTypes" :key="type.id" :value="type.id">
                                {{ $t(`falukant.underground.types.${type.tr}`) }}
                                ({{ formatCost(type.cost) }})
                            </option>
                        </select>
                    </label>

                    <label class="form-label">
                        {{ $t('falukant.underground.activities.victim') }}
                        <input v-model="newVictimUsername" type="text" class="form-control"
                            :placeholder="$t('falukant.underground.activities.victimPlaceholder')" />
                    </label>

                    <!-- Bei sabotage: Ziel auswählen -->
                    <label v-if="selectedType && selectedType.tr === 'sabotage'" class="form-label">
                        {{ $t('falukant.underground.activities.sabotageTarget') }}
                        <select v-model="newSabotageTarget" class="form-control">
                            <option value="house">{{ $t('falukant.underground.targets.house') }}</option>
                            <option value="storage">{{ $t('falukant.underground.targets.storage') }}</option>
                        </select>
                    </label>

                    <!-- Bei corrupt_politician: Ziel erreichen -->
                    <label v-if="selectedType && selectedType.tr === 'corrupt_politician'" class="form-label">
                        {{ $t('falukant.underground.activities.corruptGoal') }}
                        <select v-model="newCorruptGoal" class="form-control">
                            <option value="elect">{{ $t('falukant.underground.goals.elect') }}</option>
                            <option value="tax_increase">{{ $t('falukant.underground.goals.taxIncrease') }}</option>
                            <option value="tax_decrease">{{ $t('falukant.underground.goals.taxDecrease') }}</option>
                        </select>
                    </label>

                    <button class="btn-create-activity" :disabled="!canCreate" @click="createActivity">
                        {{ $t('falukant.underground.activities.create') }}
                    </button>
                </div>
                <!-- /Neues Activity-Formular -->

                <div v-if="loading.activities" class="loading">
                    {{ $t('loading') }}
                </div>
                <div v-else class="activities-table">
                    <table>
                        <thead>
                            <tr>
                                <th>{{ $t('falukant.underground.activities.type') }}</th>
                                <th>{{ $t('falukant.underground.activities.victim') }}</th>
                                <th>{{ $t('falukant.underground.activities.cost') }}</th>
                                <th>{{ $t('falukant.underground.activities.additionalInfo') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="act in activities" :key="act.id">
                                <!-- Typ -->
                                <td>{{ $t(`falukant.underground.types.${act.type}`) }}</td>
                                <!-- Victim -->
                                <td>{{ act.victimName }}</td>
                                <!-- Cost -->
                                <td>{{ formatCost(act.cost) }}</td>
                                <!-- Zusätzliche Informationen -->
                                <td>
                                    <template v-if="act.type === 'sabotage'">
                                        {{ $t(`falukant.underground.targets.${act.target}`) }}
                                    </template>
                                    <template v-else-if="act.type === 'corrupt_politician'">
                                        {{ $t(`falukant.underground.goals.${act.goal}`) }}
                                    </template>
                                </td>
                            </tr>
                            <tr v-if="!activities.length">
                                <td colspan="4">{{ $t('falukant.underground.activities.none') }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Angriffe -->
            <div v-else-if="activeTab === 'attacks'" class="tab-pane">
                <div v-if="loading.attacks" class="loading">
                    {{ $t('loading') }}
                </div>
                <div v-else class="attacks-list">
                    <table>
                        <thead>
                            <tr>
                                <th>{{ $t('falukant.underground.attacks.source') }}</th>
                                <th>{{ $t('falukant.underground.attacks.date') }}</th>
                                <th>{{ $t('falukant.underground.attacks.success') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="atk in attacks" :key="atk.id">
                                <td>{{ atk.targetName }}</td>
                                <td>{{ formatDate(atk.date) }}</td>
                                <td>{{ atk.success ? $t('yes') : $t('no') }}</td>
                            </tr>
                            <tr v-if="!attacks.length">
                                <td colspan="3">
                                    {{ $t('falukant.underground.attacks.none') }}
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import StatusBar from '@/components/falukant/StatusBar.vue';
import SimpleTabs from '@/components/SimpleTabs.vue';
import apiClient from '@/utils/axios.js';

export default {
    name: 'UndergroundView',
    components: { StatusBar, SimpleTabs },
    data() {
        return {
            activeTab: 'activities',
            tabs: [
                { value: 'activities', label: 'falukant.underground.tabs.activities' },
                { value: 'attacks', label: 'falukant.underground.tabs.attacks' }
            ],
            undergroundTypes: [],
            activities: [],
            attacks: [],
            loading: { activities: false, attacks: false },

            // Neue Activity-Formfelder
            newActivityTypeId: null,
            newVictimUsername: '',
            newSabotageTarget: 'house',
            newCorruptGoal: 'elect'
        };
    },
    computed: {
        selectedType() {
            return this.undergroundTypes.find(t => t.id === this.newActivityTypeId) || null;
        },
        canCreate() {
            if (!this.newActivityTypeId || !this.newVictimUsername.trim()) return false;
            if (this.selectedType.tr === 'sabotage' && !this.newSabotageTarget) return false;
            if (this.selectedType.tr === 'corrupt_politician' && !this.newCorruptGoal) return false;
            return true;
        }
    },
    async mounted() {
        await this.loadUndergroundTypes();
        if (this.undergroundTypes.length) {
            this.newActivityTypeId = this.undergroundTypes[0].id;
        }
        await this.loadActivities();
    },
    methods: {
        onTabChange(tab) {
            if (tab === 'activities' && !this.activities.length) {
                this.loadActivities();
            }
            if (tab === 'attacks' && !this.attacks.length) {
                this.loadAttacks();
            }
        },

        async loadUndergroundTypes() {
            const { data } = await apiClient.get('/api/falukant/underground/types');
            this.undergroundTypes = data;
        },

        async loadActivities() {
            this.loading.activities = true;
            try {
                const { data } = await apiClient.get('/api/falukant/underground/activities');
                this.activities = data;
            } catch (err) {
                console.error('Error loading activities', err);
            } finally {
                this.loading.activities = false;
            }
        },

        async loadAttacks() {
            this.loading.attacks = true;
            try {
                const { data } = await apiClient.get('/api/falukant/underground/attacks');
                this.attacks = data;
            } catch (err) {
                console.error('Error loading attacks', err);
            } finally {
                this.loading.attacks = false;
            }
        },

        async createActivity() {
            if (!this.canCreate) return;
            const payload = {
                typeId: this.newActivityTypeId,
                victimUsername: this.newVictimUsername.trim()
            };
            // je nach Typ noch ergänzen:
            if (this.selectedType.tr === 'sabotage') {
                payload.target = this.newSabotageTarget;
            }
            if (this.selectedType.tr === 'corrupt_politician') {
                payload.goal = this.newCorruptGoal;
            }
            try {
                await apiClient.post('/api/falukant/underground/activities', payload);
                // zurücksetzen & neu laden
                this.newVictimUsername = '';
                this.newSabotageTarget = 'house';
                this.newCorruptGoal = 'elect';
                await this.loadActivities();
            } catch (err) {
                console.error('Error creating activity', err);
            }
        },

        formatDate(ts) {
            return new Date(ts).toLocaleDateString(this.$i18n.locale, {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit'
            });
        },

        formatCost(value) {
            return new Intl.NumberFormat(navigator.language, {
                minimumFractionDigits: 0,
                maximumFractionDigits: 0
            }).format(value);
        }
    }
};
</script>

<style scoped>
.underground-view {
    display: flex;
    flex-direction: column;
}

h2 {
    padding-top: 20px;
}

.tab-content {
    margin-top: 1rem;
}

.tab-pane {
    min-height: 200px;
}

.loading {
    font-style: italic;
    text-align: center;
    margin: 1em 0;
}

/* --- Create Activity --- */
.create-activity {
    border: 1px solid #ccc;
    padding: 1rem;
    margin-bottom: 1rem;
    border-radius: 4px;
    background: #fafafa;
    display: inline-block;
}

.create-activity h3 {
    margin-top: 0;
}

.form-label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: bold;
}

.form-control {
    display: block;
    width: 100%;
    padding: 0.4rem;
    margin-bottom: 1rem;
    border: 1px solid #ccc;
    border-radius: 4px;
}

.btn-create-activity {
    padding: 0.5rem 1rem;
    cursor: pointer;
    background: #4caf50;
    color: white;
    border: none;
    border-radius: 4px;
}

.btn-create-activity:disabled {
    background: #ccc;
    cursor: not-allowed;
}

/* --- Activities List --- */
.activities-list ul {
    list-style: disc;
    margin-left: 1.5em;
}

/* --- Attacks Table --- */
.attacks-list table {
    width: 100%;
    border-collapse: collapse;
}

.attacks-list th,
.attacks-list td {
    padding: 8px;
    border: 1px solid #ddd;
    text-align: left;
}
</style>