<template>
  <div class="profile-screen">
    <div class="profile-header">
      <div class="profile-info">
        <div class="avatar-container">
          <div v-if="!avatarUrl" class="avatar-placeholder">
            {{ initials }}
          </div>
          <img v-else :src="avatarUrl" class="avatar-image" alt="avatar">
        </div>
        <div class="profile-text">
          <h1 class="profile-name">{{ userData.name }}</h1>
          <p class="profile-email">{{ userData.email }}</p>
        </div>
      </div>
      <input 
        type="file" 
        ref="fileInput" 
        style="display: none" 
        accept="image/*"
        @change="handleFileChange"
      >
      <button class="edit-link" @click="triggerFileInput">Изменить</button>
    </div>

    <div class="profile-menu">
      <router-link to="/chats" class="menu-item">
        <span class="menu-icon">chat</span>
        <span class="menu-text">Обсуждения</span>
        <span class="menu-icon arrow">chevron_right</span>
      </router-link>
      <div class="menu-item">
        <span class="menu-icon">schedule</span>
        <span class="menu-text">История</span>
        <span class="menu-icon arrow">chevron_right</span>
      </div>
      <div class="menu-item">
        <span class="menu-icon">settings</span>
        <span class="menu-text">Настройки</span>
        <span class="menu-icon arrow">chevron_right</span>
      </div>
    </div>

    <button class="logout-button" @click="handleLogout">Выход</button>

    <BottomNav />
  </div>
</template>

<script>
import BottomNav from '@/components/BottomNav.vue'

export default {
  name: 'Profile',
  components: {
    BottomNav
  },
  data() {
    return {
      userData: {
        name: '',
        email: ''
      },
      avatarUrl: null
    }
  },
  computed: {
    initials() {
      if (!this.userData.name) return ''
      return this.userData.name
        .split(' ')
        .map(word => word[0])
        .join('')
        .toUpperCase()
    }
  },
  methods: {
    triggerFileInput() {
      this.$refs.fileInput.click()
    },
    handleFileChange(event) {
      const file = event.target.files[0]
      if (file) {
        const reader = new FileReader()
        reader.onload = (e) => {
          this.avatarUrl = e.target.result
          localStorage.setItem('userAvatar', e.target.result)
        }
        reader.readAsDataURL(file)
      }
    },
    handleLogout() {
      localStorage.removeItem('token')
      localStorage.removeItem('userData')
      localStorage.removeItem('userAvatar')
      this.$router.push('/signin')
    },
    loadUserData() {
      const userData = localStorage.getItem('userData')
      const avatarUrl = localStorage.getItem('userAvatar')
      
      if (userData) {
        this.userData = JSON.parse(userData)
      }
      if (avatarUrl) {
        this.avatarUrl = avatarUrl
      }
    }
  },
  created() {
    this.loadUserData()
  }
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');

.profile-screen {
  min-height: 100vh;
  background-color: #000000;
  color: white;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.profile-header {
  padding-bottom: 30px;
}

.profile-info {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.avatar-container {
  margin-right: 16px;
}

.avatar-placeholder, .avatar-image {
  width: 60px;
  height: 60px;
  border-radius: 30px;
}

.avatar-placeholder {
  background-color: #2196F3;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: bold;
}

.avatar-image {
  object-fit: cover;
}

.profile-text {
  flex: 1;
}

.profile-name {
  font-size: 20px;
  font-weight: bold;
  margin: 0;
}

.profile-email {
  font-size: 14px;
  color: #686868;
  margin: 4px 0 0;
}

.edit-link {
  color: #2196F3;
  background: none;
  border: none;
  font-size: 14px;
  padding: 0;
  cursor: pointer;
}

.profile-menu {
  margin: 0 -20px;
  padding: 0 20px;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid #1F1F1F;
  text-decoration: none;
  color: white;
}

.menu-icon {
  font-family: 'Material Icons';
  color: #686868;
  margin-right: 16px;
  font-size: 24px;
}

.menu-text {
  flex: 1;
  font-size: 16px;
}

.arrow {
  margin-right: 0;
}

.logout-button {
  margin: 0px 0px 100px;
  padding: 16px;
  background: none;
  border: 0.5px solid #686868;
  border-radius: 8px;
  color: #2196F3;
  font-size: 16px;
  cursor: pointer;
}
</style> 