<style scoped>
.poll-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.poll-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  margin-bottom: 0.5rem;
  transition: all 0.3s ease;
}

.poll-item:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateX(5px);
}

.poll-title { 
  font-size: 1.5rem; 
  color: #667eea; 
  text-transform: uppercase; 
  letter-spacing: 1px;
  font-weight: bold;
  margin-bottom: 1rem;
}

.add-option-form {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 2rem;
}

.option-name { 
  font-weight: 500; 
  color: #fff;
}
.vote-section { display: flex; align-items: center; gap: 1rem; }
.vote-count { font-size: 0.9rem; color: rgba(255, 255, 255, 0.8); }
.vote-btn { 
  padding: 0.5rem 1rem; 
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border: none;
  border-radius: 8px;
  color: white;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(16, 185, 129, 0.4);
}
.vote-btn:hover { 
  background: linear-gradient(135deg, #059669 0%, #10b981 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(16, 185, 129, 0.6);
}

.vote-btn:disabled,
.vote-btn[disabled] { 
  background: rgba(136, 136, 136, 0.5);
  cursor: not-allowed;
}
.vote-btn:disabled:hover,
.vote-btn[disabled]:hover { 
  background: rgba(136, 136, 136, 0.7);
  transform: none;
}

p {
  color: rgba(255, 255, 255, 0.6);
}
</style>

<template>
    <div>
        <h2 class="poll-title">Poll: {{ activePollId }}</h2>
        <p v-if="Object.keys(pollData).length==0">Note: Add at least one Option to save the Poll.</p>
        <form @submit.prevent="handleAddNewOption" class="add-option-form" v-if="userid">
            <input v-model="newOption" placeholder="Enter a new poll option..." required />
            <button type="submit">Add Option</button>
        </form>

        <ul class="poll-list">
            <li v-for="(votes, optionName) in pollData" :key="optionName" class="poll-item">
                <span class="option-name">{{ optionName }}</span>
                <div class="vote-section">
                    <span class="vote-count">{{ votes.length }} {{ votes.length === 1 ? 'vote' : 'votes' }}</span>
                    <button @click="vote(String(optionName))" class="vote-btn" :disabled="userid==undefined || voted(votes)">+1</button>
                </div>
            </li>
        </ul>
    </div>
</template>

<script setup lang="ts">
    import type { PollProps, SignedData, VoteData } from '@/utils/types'
    const props = defineProps<PollProps>()

    const newOption = ref('');
    const handleAddNewOption = () => {
        props.addOption(newOption.value);
        newOption.value = '';
    };


    const voted = (votes: SignedData<VoteData>[]) => {
        for(let vote of votes){
            if(vote.data.userid == props.userid){
                return true;
            }
        }
        return false;
    }
</script>