<template>
  <div v-if="isMounted" id="app">
    <div>
      <h1>Ant Design Button Demo</h1>
      <a-button type="primary" @click="handleClick">Click Me</a-button>
    </div>

    <div>
      <h1>타입 선택</h1>
      <div class="select-container">
        <a-select v-model:value="selectedOption" placeholder="옵션을 선택하세요" style="width: 200px">
          <a-select-option value="option1">옵션 1</a-select-option>
          <a-select-option value="option2">옵션 2</a-select-option>
          <a-select-option value="option3">옵션 3</a-select-option>
        </a-select>
        <br>
        <p>선택한 옵션: {{ selectedOption }}</p>
      </div>
    </div>

    <div>
      <h2>옵션 선택</h2>
      <a-radio-group v-model:value="radioOption">
        <a-radio value="option1">옵션 1</a-radio>
        <a-radio value="option2">옵션 2</a-radio>
        <a-radio value="option3">옵션 3</a-radio>
      </a-radio-group>
      <br><br>
      <p>선택한 옵션: {{ radioOption }}</p>
    </div>

    <h1>사용자 목록</h1>
    <ul v-if="users.length">
      <li v-for="user in users" :key="user.id">
        {{ user.name }} - {{ user.phone }}
      </li>
    </ul>
    <p v-else>Loading...</p>
  </div>

  <div v-if="isMounted" class="container">
    <div class="search-area">
      <DateSearch @search="handleSearch" />
    </div>

    <div class="results-area">
      <ul v-if="results.length">
        <li v-for="(item, index) in results" :key="index">
          {{ item }}
        </li>
      </ul>
      <p v-else>검색 결과가 없습니다.</p>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import DateSearch from './components/DateSearch.vue';

const users = ref([]);
const selectedOption = ref('');
const radioOption = ref('option1');
const results = ref([]);
const isMounted = ref(false); // 마운트 상태를 확인하는 플래그

const handleClick = () => {
  alert('Button clicked!');
};

const handleSearch = async ({ startDate, endDate }) => {
  results.value.push(startDate);
  results.value.push(endDate);
};

const fetchUsers = async () => {
  try {
    const response = await axios.get('/api/all');
    users.value = response.data;
  } catch (error) {
    console.error('데이터를 가져오는 중 오류가 발생했습니다:', error);
  }
};

onMounted(() => {
  isMounted.value = true;
  fetchUsers();
});
</script>

<style scoped>
.select-container {
  display: flex;
  flex-direction: column;
  margin-top: 20px;
}

.search-area, .results-area {
  width: 80%;
  margin: 20px 0;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 8px;
}

.search-area {
  background-color: #f9f9f9;
}

.results-area {
  background-color: #ffffff;
}
</style>
