Files
PORTAL_COD2/src/front/src/views/VoteHomePage.vue
T
2025-01-14 07:43:49 +03:00

271 lines
8.5 KiB
Vue

<template>
<v-container>
<v-toolbar color="transparent">
<v-toolbar-title>Голосования</v-toolbar-title>
<template v-slot:extension>
<v-tabs v-model="currentTab">
<v-tab :key="0" value="current">Текущие голосования</v-tab>
<v-tab :key="1" value="completed">Завершенные голосования</v-tab>
<v-tab :key="2" value="constructor">Создать новое голосование</v-tab>
</v-tabs>
</template>
</v-toolbar>
<v-tabs-window v-model="currentTab">
<!-- Текущие голосования -->
<v-tabs-window-item value="current">
<v-container>
<v-row>
<v-col
v-for="voting in sortedCurrentVotes"
:key="voting.id"
cols="12" lg="3" md="4"
>
<v-card :class="{ 'voted-card': voting.voted }">
<v-card-title class="d-flex justify-space-between">{{ voting.title }}
<v-btn flat size="medium" @click="openEditVoteModal(voting)">
<v-icon color="orange-lighten-1">mdi-pencil</v-icon>
</v-btn>
</v-card-title>
<v-card-subtitle>
Окончание: {{ formatDate(voting.endDate) }}
</v-card-subtitle>
<v-card-text>
Осталось: {{ timeRemaining(voting.endDate) }}
</v-card-text>
<v-card-actions class="justify-space-between">
<v-btn
v-if="!voting.voted"
color="teal-darken-4"
size="medium"
@click="openVoteModal(voting)"
>
Проголосовать
</v-btn>
<v-btn v-else color="teal-darken-4" disabled size="medium">Проголосовали</v-btn>
<v-btn color="error" size="medium" @click="deleteVoting(voting.id)">
<v-icon>mdi-delete</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-container>
</v-tabs-window-item>
<!-- Завершенные голосования -->
<v-tabs-window-item value="completed">
<v-container>
<v-row>
<v-col
v-for="voting in sortedCompletedVotes"
:key="voting.id"
cols="12" lg="3" md="4"
>
<v-card>
<v-card-title>{{ voting.title }}</v-card-title>
<v-card-subtitle>
Завершено: {{ formatDate(voting.endDate) }}
</v-card-subtitle>
<v-card-actions class="justify-space-between">
<v-btn color="teal-darken-4" size="medium" @click="openResultsModal(voting)">
Результаты
</v-btn>
<v-btn color="error" size="medium" @click="deleteVoting(voting.id)">
<v-icon>mdi-delete</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-container>
</v-tabs-window-item>
<!-- Конструктор -->
<v-tabs-window-item value="constructor">
<VoteConstructor/>
</v-tabs-window-item>
</v-tabs-window>
<!-- Модальное окно для голосования -->
<v-dialog v-model="voteModal" max-width="1200px">
<v-card>
<v-card-actions>
<!--<v-btn color="teal-darken-4" @click="submitVote">Проголосовать</v-btn>-->
<v-btn color="red-darken-1" size="medium" @click="closeVoteModal">
<v-icon>mdi-close-thick</v-icon>
</v-btn>
</v-card-actions>
<v-card-text class="pt-0">
<CurrentVote
:description="votingData.description"
:endDate="votingData.endDate"
:questions="votingData.questions"
:startDate="votingData.startDate"
:title="votingData.title"
/>
</v-card-text>
</v-card>
</v-dialog>
<!-- Модальное окно для редактирования -->
<v-dialog v-model="editVoteModal" max-width="1200px">
<v-card>
<v-card-title>Голосование: {{ selectedVoting?.title }}</v-card-title>
<v-card-text>
<!-- <p>Редактирование голосования...</p> -->
<VoteConstructor/>
</v-card-text>
<v-card-actions>
<v-btn color="teal-darken-4">Сохранить</v-btn>
<v-btn @click="closeEditVoteModal">Закрыть</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
<!-- Модальное окно для результатов -->
<v-dialog v-model="resultsModal" max-width="1200px">
<v-card>
<v-card-title>Результаты голосования: {{ selectedVoting?.title }}</v-card-title>
<v-card-text>
<p>Здесь отображаются результаты голосования...</p>
</v-card-text>
<v-card-actions>
<v-btn @click="closeResultsModal">Закрыть</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-container>
</template>
<script>
import {computed, ref} from "vue";
import VoteConstructor from "@/components/vote/VoteConstructor.vue";
import CurrentVote from "@/components/vote/CurrentVote.vue";
import {votingData} from '@/test/testDataForVote'
export default {
computed: {
votingData() {
return votingData
}
},
components: {CurrentVote, VoteConstructor},
setup() {
const currentTab = ref("current");
const votes = ref([
{
id: 1,
title: "Голосование 1",
endDate: new Date(new Date().getTime() + 600000), // 10 минут
voted: false,
},
{
id: 2,
title: "Голосование 2",
endDate: new Date(new Date().getTime() - 600000), // Уже завершено
voted: true,
},
{
id: 3,
title: "Голосование 3",
endDate: new Date(new Date().getTime() + 3600000), // 1 час
voted: true,
},
]);
const voteModal = ref(false);
const editVoteModal = ref(false);
const resultsModal = ref(false);
const selectedVoting = ref(null);
const sortedCurrentVotes = computed(() => {
return votes.value
.filter((v) => new Date(v.endDate) > new Date())
.sort((a, b) => new Date(a.endDate) - new Date(b.endDate));
});
const sortedCompletedVotes = computed(() => {
return votes.value
.filter((v) => new Date(v.endDate) <= new Date())
.sort((a, b) => new Date(b.endDate) - new Date(a.endDate));
});
const timeRemaining = (endDate) => {
const now = new Date();
const diff = new Date(endDate) - now;
return diff > 0 ? `${Math.floor(diff / 60000)} минут` : "Завершено";
};
const formatDate = (date) => new Date(date).toLocaleString();
const openVoteModal = (voting) => {
selectedVoting.value = voting;
voteModal.value = true;
};
const closeVoteModal = () => {
voteModal.value = false;
selectedVoting.value = null;
};
const openEditVoteModal = (voting) => {
selectedVoting.value = voting;
editVoteModal.value = true;
};
const closeEditVoteModal = () => {
editVoteModal.value = false;
selectedVoting.value = null;
};
const submitVote = () => {
selectedVoting.value.voted = true;
closeVoteModal();
};
const openResultsModal = (voting) => {
selectedVoting.value = voting;
resultsModal.value = true;
};
const closeResultsModal = () => {
resultsModal.value = false;
selectedVoting.value = null;
};
const deleteVoting = (id) => {
votes.value = votes.value.filter((v) => v.id !== id);
};
return {
currentTab,
votes,
sortedCurrentVotes,
sortedCompletedVotes,
voteModal,
resultsModal,
selectedVoting,
timeRemaining,
formatDate,
openVoteModal,
closeVoteModal,
submitVote,
openResultsModal,
closeResultsModal,
deleteVoting,
editVoteModal,
openEditVoteModal,
closeEditVoteModal,
};
},
};
</script>
<style>
.voted-card {
opacity: 0.6;
}
</style>