Files
PORTAL_COD2/src/front/src/views/safety/FireSafety.vue
T

121 lines
5.2 KiB
Vue

<template>
<v-container :fluid="true">
<v-card>
<v-tabs
v-model="tab"
bg-color="teal-darken-1"
slider-color="teal-darken-2"
:grow="true">
<v-tab selected-class="isActiveTab" value="document">Библиотека документов</v-tab>
<!-- <v-tab selected-class="isActiveTab" value="comission">Комиссии</v-tab> -->
</v-tabs>
</v-card>
<v-window v-model="tab">
<v-window-item value="document">
<v-row class="my-1">
<v-col cols="5" class="d-flex justify-start h-screen">
<v-card v-for="item in safetyData" :key="item.id" class="overflow-y-auto h-auto flex-grow-1 flex-shrink-1">
<v-card-title>{{ item.name }}</v-card-title>
<v-container
v-for="category in item.categories"
:key="item.id + category.id"
:fluid="true"
class="py-0">
<v-card-actions>
<v-btn @click="showMore[item.id + category.id] = !showMore[item.id + category.id]" variant="text">{{ category.name }}</v-btn>
<v-btn
:icon="showMore[item.id + category.id] ? 'mdi-chevron-up' : 'mdi-chevron-down'"
@click="showMore[item.id + category.id] = !showMore[item.id + category.id]">
</v-btn>
</v-card-actions>
<v-expand-transition>
<div v-show="showMore[item.id + category.id]">
<v-card-actions v-for="file in category.files" :key="file.id" class="overflow-y-auto">
<v-card-text>{{ file.name }}</v-card-text>
<v-btn icon="mdi-file-pdf-box" color="red" @click="openPDF(burl + file.url), loading=true"></v-btn>
<v-btn icon='mdi-file-download-outline' color="green" @click="downloadItem(burl + file.url, file.name)"></v-btn>
</v-card-actions>
</div>
</v-expand-transition>
</v-container>
</v-card>
</v-col>
<v-col cols="7" class="d-flex justify-start h-screen">
<v-card v-show="showFile" class="flex-grow-1 flex-shrink-1 overflow-y-auto">
<v-container v-if="loading">
<v-progress-linear
:rounded="5"
indeterminate
color="teal">
</v-progress-linear>
</v-container>
<VuePdfEmbed :source="pdfSource" width="720" @rendered="loading = false"/>
<v-card-actions>
<v-btn block @click="showFile = false">Закрыть</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-window-item>
</v-window>
</v-container>
</template>
<script setup>
import { ref, onBeforeMount, onUpdated } from 'vue';
import { pagecounter } from '@/utils/universal_functions';
import { axiosApiInstance, baseURL } from '@/utils/axios-api';
import { saveAs } from 'file-saver';
import VuePdfEmbed from 'vue-pdf-embed';
import { useRoute } from 'vue-router'
pagecounter();
const safetyData = ref([]);
const showMore = ref(false);
const showFile = ref(false);
let pdfSource = '';
const loading = ref(false);
const tab = ref('')
const burl = baseURL.slice(0,-1)
const route = useRoute()
// Вынес запрос в отдельную функцию, чтобы избежать дублирования одного и того же запроса на сервер.
function getSafety() {
axiosApiInstance.get(`api/safety/files/?chapter_id=${route.params.chapter_id}`).then(result => {
safetyData.value = result.data
})
}
onBeforeMount(() => getSafety());
// Хук обновления реализует возможность динамической подгрузки данных при переключении между вкладками раздела безопасность
onUpdated(() => getSafety())
// Проверяем по ID на какой категории срабатывает клик на раскрытии/скрытии, чтобы избежать срабатывания у всех категорий одновременно
showMore.value = safetyData.value.reduce((acc, value) => {
acc[value.id] = false
return acc
}, {});
const downloadItem = (url, fileName) => {
axiosApiInstance.get(url, {responseType: 'blob'})
.then(response => {
saveAs(response.data, fileName)
})
};
function openPDF(url) {
showFile.value = true
if (showFile) {
return pdfSource = url
}
};
</script>
<style>
.isActiveTab {
background-color: #68c3ff70;
}
</style>