<template>
  <div v-if="isOpen" class="modal-overlay">
    <div class="modal">
        <div class="modal-header">
            <h3>Add New Building</h3>
            <button class="close-btn" @click="close">×</button>
        </div>
        
        <div class="modal-content">
            <div class="setting-group">
                <label>Building ID</label>
                <input v-model="form.id" type="text" class="input-field" placeholder="e.g. H15" />
            </div>

            <div class="setting-group">
                <label>Type</label>
                <select v-model="form.type" class="input-field select-field">
                    <option value="house">House</option>
                    <option value="factory">Factory</option>
                    <option value="office">Office</option>
                    <option value="shop">Shop</option>
                </select>
            </div>

            <div class="setting-group">
                <label>Data Source (CSV Path)</label>
                <input v-model="form.csvPath" type="text" class="input-field" placeholder="e.g. data/H01.csv" />
            </div>
        </div>

        <div class="modal-actions">
            <button class="btn secondary" @click="close">Cancel</button>
            <button class="btn primary" @click="confirm">Add Building</button>
        </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { watch, reactive } from 'vue';
import { useSimulation } from '../composables/useSimulation';

const props = defineProps<{
    isOpen: boolean;
}>();

const emit = defineEmits(['close']);

const { addBuilding, state } = useSimulation();

const form = reactive({
    id: '',
    type: 'house',
    csvPath: 'data/H01.csv'
});

// Auto-generate ID when opened
watch(() => props.isOpen, (newVal) => {
    if (newVal) {
        const nextNum = state.value.buildings.length + 1;
        form.id = `H${nextNum.toString().padStart(2, '0')}`;
        form.type = 'house';
        form.csvPath = 'data/H01.csv';
    }
});

const close = () => {
    emit('close');
};

const confirm = () => {
    addBuilding({ ...form });
    close();
};
</script>

<style scoped>
/* Reuse styles from SettingsModal to maintain consistency */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.modal {
    background: #1a1a1a;
    border: 3px solid #000;
    width: 400px;
    max-width: 90%;
    color: white;
    font-family: 'Courier New', monospace;
    box-shadow: 10px 10px 0px rgba(0,0,0,0.5);
}

.modal-header {
    background: #FFD700;
    color: black;
    padding: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 3px solid #000;
}

.modal-header h3 {
    margin: 0;
    text-transform: uppercase;
    font-weight: 900;
    font-size: 1.1rem;
}

.close-btn {
    background: black;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 0 8px;
    font-weight: bold;
}

.close-btn:hover {
    color: #ff4444;
}

.modal-content {
    padding: 20px;
}

.setting-group {
    margin-bottom: 20px;
}

.setting-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
    text-transform: uppercase;
    color: #ffd700;
}

.input-field {
    width: 100%;
    padding: 8px;
    background: #000;
    border: 2px solid #333;
    color: white;
    font-family: monospace;
    font-size: 0.9rem;
    box-sizing: border-box;
}

.input-field:focus {
    outline: none;
    border-color: #FFD700;
}

.select-field {
    cursor: pointer;
}

.modal-actions {
    padding: 16px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    border-top: 2px solid #333;
    background: #222;
}

.btn {
    padding: 8px 16px;
    font-family: 'Courier New', monospace;
    text-transform: uppercase;
    font-weight: bold;
    cursor: pointer;
    border: 2px solid #000;
    box-shadow: 2px 2px 0px #000;
    transition: all 0.1s;
}

.btn:active {
    transform: translate(2px, 2px);
    box-shadow: none;
}

.btn.primary {
    background: #FFD700;
    color: black;
}

.btn.primary:hover {
    background: #ffea70;
}

.btn.secondary {
    background: #444;
    color: white;
}

.btn.secondary:hover {
    background: #555;
}
</style>
