Files
PORTAL_COD2/src/front/src/components/vote/VoteResult.vue
T

409 lines
15 KiB
Vue

<template>
<v-container style="height: 85vh;">
<div class="text-end">
<v-btn class="" color="red-darken-1" density="compact" icon="mdi-close-thick" size="small"
@click="$emit('closer')">
</v-btn>
</div>
<v-card-title class="text-subtitle-1">Результат голосования: {{ title }}</v-card-title>
<v-card-subtitle class="text-subtitle-2">{{ description }}</v-card-subtitle>
<v-tabs v-model="activeTab" class="mb-4">
<v-tab
v-for="(question, questionIndex) in questions"
:key="questionIndex"
>
<template v-if="shouldShowTooltip(question.name || 'Без названия', 10)">
<v-tooltip bottom>
<template v-slot:activator="{ props }">
<span v-bind="props">
{{ truncateText(question.name || 'Без названия', 10) }}
</span>
</template>
<span>{{ question.name || 'Без названия' }}</span>
</v-tooltip>
</template>
<template v-else>
<span>{{ truncateText(question.name || 'Без названия', 10) }}</span>
</template>
</v-tab>
<v-tab>Не голосовали</v-tab>
</v-tabs>
<v-tabs-window v-model="activeTab">
<v-tabs-window-item v-for="(question, questionIndex) in questions" :key="questionIndex">
<v-row v-if="question.settings.onlyDep">
<v-col
v-for="(winner, department) in getWinnersByDepartment(question).winnersByDepartment"
:key="department"
>
<v-card
class="cursor-pointer"
@click="opendialogWinn(department, getWinnersByDepartment(question).groupedByDepartment[department])"
style="max-height: 50vh; overflow-y: auto;">
<v-card-title>
<strong class="text-teal-darken-4">{{ department }}</strong>
</v-card-title>
<v-card-subtitle>
Победитель: {{ winner.employee.fio || winner.name }} 🏆
</v-card-subtitle>
<v-card-text>
Количество голосов: {{ winner.count }}
</v-card-text>
</v-card>
</v-col>
</v-row>
<v-row v-else>
<v-col cols="12">
<h3 class="text-center">{{ question.name }}</h3>
<v-card-title class="d-flex ga-2 align-center">
<h4 class="text-teal-darken-4">Победитель:</h4>
<h3>{{ getWinner(question).employee?.fio || getWinner(question).name }} 🏆</h3>
</v-card-title>
<v-card-subtitle>
Количество голосов: {{ getWinner(question)?.count || 0 }}
</v-card-subtitle>
</v-col>
<v-col cols="12">
<v-card class="mb-4">
<v-card-title class="text-teal-darken-3">Распределение голосов:</v-card-title>
<v-card-text>
<v-list style="max-height: 45vh; overflow-y: auto;">
<v-list-item
v-for="(answer, answerIndex) in getTopResults(question).filter(el => el.count > 0)"
:key="answerIndex"
>
<p>
<strong class="mr-4">{{ answerIndex + 1 }}.</strong>
<v-avatar v-if="answer.file === null && answer.employee"
:image="checkremote(`${spravURL()}${answer.employee.login}.jpg`)"
class="cursor-pointer"
@click.stop="opendialog(checkremote(`${spravURL()}${answer.employee.login}.jpg`))"/>
<v-avatar v-if="answer.file !== null" :image="baseURL.slice(0, -1) + answer.file"
class="cursor-pointer"
@click.stop="opendialog(answer)"/>
<strong class="ml-4">{{ answer.employee ? answer.employee.fio : answer.name }}</strong>:
{{ answer.count }}
{{ voteCount(answer.count) }}
<template v-if="answer.employee">
<br/>
<small>Отдел: {{ answer.employee ? answer.employee.department : '' }}</small>
<br/>
<small>Должность: {{ answer.employee ? answer.employee.post : '' }}</small>
</template>
</p>
<v-progress-linear
:model-value="getPercentage(answer.count, question)"
color="teal-darken-4"
height="13"
rounded
striped
>
<template v-slot:default="{ value }">
<strong v-if="value < 50" class="text-grey-darken-3">{{ Math.ceil(value) }}%</strong>
<strong v-else class="text-grey-lighten-5">{{ Math.ceil(value) }}%</strong>
</template>
</v-progress-linear>
</v-list-item>
</v-list>
</v-card-text>
</v-card>
</v-col>
</v-row>
</v-tabs-window-item>
<v-tabs-window-item>
<NonVoteSad v-if="nonVoters.length > 0"/>
<v-card-title v-if="nonVoters.length > 0" class="text-red-darken-3">Число сотрудников, которые не проголосовали
<strong>{{ nonVoters.length }}</strong>
</v-card-title>
<v-card-item style="max-height: 50vh; overflow-y: auto;">
<NonVoteHappy v-if="nonVoters.length < 1"/>
<template v-for="(voters, department) in groupedByDepartment" :key="department">
<v-list-item-title class="text-teal-darken-4">{{ department }}</v-list-item-title>
<v-list-item v-for="(voter, voterIndex) in voters" :key="`${department}-${voterIndex}`">
<p>
<v-avatar
:image="checkremote(`${spravURL()}${voter.login}.jpg`)"
class="cursor-pointer"
@click="userCardIndex = `${department}-${voterIndex}`"
/>
<InfoUserCard
:author="voter"
:opendialog="userCardIndex === `${department}-${voterIndex}`"
@update:opendialog="userCardIndex = $event ? `${department}-${voterIndex}` : null"
/>
<strong class="ml-4">{{ voter.fio }}</strong>
</p>
</v-list-item>
</template>
</v-card-item>
</v-tabs-window-item>
</v-tabs-window>
</v-container>
<v-dialog v-model="dialog" max-width="100vh">
<v-card max-height="800">
<v-card-title class="v-row">
<div class="v-col-auto">
{{ activepick.answer }}
</div>
<v-spacer></v-spacer>
<div>
<v-btn color="red-darken-1" density="compact" icon="mdi-close-thick" size="small" @click="dialog=false"/>
</div>
</v-card-title>
<v-card-item>
<v-img :src="activepick.file !== undefined ? baseURL.slice(0, -1) + activepick.file : activepick"
max-height="600"></v-img>
</v-card-item>
</v-card>
</v-dialog>
<v-dialog v-model="dialogWinner" max-width="800">
<v-card>
<v-card-title class="d-flex">
<strong class="text-teal-darken-4">Результаты голосования: {{ activeDepartment }}</strong>
<v-spacer></v-spacer>
<v-btn color="red-darken-1" density="compact" icon="mdi-close-thick" size="small" @click="dialogWinner=false"/>
</v-card-title>
<v-card-text>
<v-list>
<v-list-item
v-for="(answer, index) in activeDepartmentResults.filter(el => el.count > 0)"
:key="index"
>
<p>
<strong class="mr-4">{{ index + 1 }}.</strong>
<v-avatar v-if="answer.file === null && answer.employee"
:image="checkremote(`${spravURL()}${answer.employee.login}.jpg`)"
class="cursor-pointer"
@click.stop="opendialog(checkremote(`${spravURL()}${answer.employee.login}.jpg`))"/>
<v-avatar v-if="answer.file !== null" :image="baseURL.slice(0, -1) + answer.file"
class="cursor-pointer"
@click.stop="opendialog(answer)"/>
<strong class="ml-4">{{ answer.employee ? answer.employee.fio : answer.name }}</strong>:
{{ answer.count }}
{{ voteCount(answer.count) }}
<template v-if="answer.employee">
<br/>
<small>Отдел: {{ answer.employee ? answer.employee.department : '' }}</small>
<br/>
<small>Должность: {{ answer.employee ? answer.employee.post : '' }}</small>
</template>
</p>
<v-progress-linear
:model-value="getPercentage(answer.count, questions[index])"
color="teal-darken-4"
height="13"
rounded
striped
>
<template v-slot:default="{ value }">
<strong v-if="value < 50" class="text-grey-darken-3">{{ Math.ceil(value) }}%</strong>
<strong v-else class="text-grey-lighten-5">{{ Math.ceil(value) }}%</strong>
</template>
</v-progress-linear>
</v-list-item>
</v-list>
</v-card-text>
</v-card>
</v-dialog>
</template>
<script>
import {computed, ref} from 'vue';
import {baseURL, spravURL} from "@/utils/axios-api";
import {checkremote} from "@/utils/universal_functions";
import NonVoteHappy from "@/components/vote/NonVoteHappy.vue";
import NonVoteSad from "@/components/vote/NonVoteSad.vue";
import InfoUserCard from "@/components/UI/fiousercard/infoUserCard.vue";
export default {
name: 'VoteResult',
components: {InfoUserCard, NonVoteSad, NonVoteHappy},
methods: {
checkremote,
spravURL() {
return spravURL
}
},
computed: {
baseURL() {
return baseURL
}
},
emits: ['closer'],
props: {
data: {
type: Object,
required: true,
},
},
setup(props) {
const activeTab = ref(0);
const questions = computed(() => props.data.questions || []);
const userCardIndex = ref(null);
const title = props.data.name;
const description = props.data.description;
const nonVoters = props.data.nonvoting;
const activeDepartment = ref(null);
const activeDepartmentResults = ref([]);
const dialog = ref(false);
const dialogWinner = ref(false);
const opendialogWinn = (department, results) => {
activeDepartment.value = department;
activeDepartmentResults.value = results;
dialogWinner.value = true;
};
const getWinner = (question) => {
return question.answers.reduce(
(max, answer) => (answer.count > max.count ? answer : max),
{count: 0, employee: {fio: '', department: '', post: ''}, name: ''}
);
};
const getTopResults = (question) => {
const sortedAnswers = [...question.answers].sort((a, b) => b.count - a.count);
// Присваиваем одинаковые индексы для одинаковых значений count
let rank = 1;
let previousCount = null;
return sortedAnswers.map((answer, index) => {
if (answer.count !== previousCount) {
rank = index + 1;
previousCount = answer.count;
}
return {
...answer,
rank
};
});
};
const truncateText = (text, maxLength) => {
if (!text) return '';
return text.length > maxLength ? text.slice(0, maxLength) + '…' : text;
};
const shouldShowTooltip = (text, maxLength) => {
return text && text.length > maxLength;
};
const voteCount = (count) => {
let currentVote = '';
const lastDigit = count % 10;
if (lastDigit === 1) currentVote = 'голос'
if (lastDigit > 1) currentVote = 'голоса'
if (lastDigit > 4 || count === 0 || count === 11 || count === 12 || count === 13 || count === 14) currentVote = 'голосов'
return currentVote
}
const getPercentage = (count, question) => {
const totalVotes = question.answers.reduce((sum, answer) => sum + answer.count, 0);
return totalVotes > 0 ? (count / totalVotes) * 100 : 0;
};
const activepick = ref(null)
// const dialog = ref(false)
const opendialog = (val) => {
dialog.value = true
activepick.value = val
}
const groupedByDepartment = computed(() => {
// Группируем пользователей по департаментам
const departments = nonVoters.reduce((groups, voter) => {
const department = voter.department || '';
if (!groups[department]) {
groups[department] = [];
}
groups[department].push(voter);
return groups;
}, {});
// Сортируем департаменты по id_dict_department
const sortedDepartments = Object.keys(departments).sort((a, b) => {
const idA = nonVoters.find(voter => voter.department === a)?.id_dict_department || 0;
const idB = nonVoters.find(voter => voter.department === b)?.id_dict_department || 0;
return idA - idB;
});
// Сортируем сотрудников внутри каждого департамента по id_dict_position
sortedDepartments.forEach(department => {
departments[department].sort((voterA, voterB) => {
return (voterA.id_dict_position || 0) - (voterB.id_dict_position || 0);
});
});
// Возвращаем отсортированные департаменты с пользователями
const sortedGroupedByDepartment = {};
sortedDepartments.forEach(department => {
sortedGroupedByDepartment[department] = departments[department];
});
return sortedGroupedByDepartment;
});
// const groupedByDepartment = {};
const getWinnersByDepartment = (question) => {
// Группируем ответы по департаментам
const groupedByDepartment = question.answers.reduce((groups, answer) => {
const department = answer.employee?.department;
if (!groups[department]) {
groups[department] = [];
}
groups[department].push(answer);
return groups;
}, {});
// Для каждого департамента находим победителя
const winnersByDepartment = {};
for (const department in groupedByDepartment) {
const answers = groupedByDepartment[department];
const winner = answers.reduce(
(max, answer) => (answer.count > max.count ? answer : max),
{count: 0, employee: {fio: '', department: '', post: ''}, name: ''}
);
winnersByDepartment[department] = winner;
}
return { groupedByDepartment, winnersByDepartment };
};
return {
opendialogWinn,
dialogWinner,
activeDepartmentResults,
activeDepartment,
getWinnersByDepartment,
userCardIndex,
activepick,
opendialog,
dialog,
activeTab,
questions,
getWinner,
getTopResults,
truncateText,
shouldShowTooltip,
voteCount,
title,
description,
getPercentage,
nonVoters,
groupedByDepartment
};
},
};
</script>