hrms-mgt/src/modules/13_salary/components/salaryEmployeeChart/DialogEmployeeUpload.vue

304 lines
9 KiB
Vue

<script setup lang="ts">
import { ref, watch } from "vue";
import { checkPermission } from "@/utils/permissions";
import { useQuasar } from "quasar";
import axios from "axios";
import http from "@/plugins/http";
import config from "@/app.config";
/** importComponents*/
import Header from "@/components/DialogHeader.vue";
/** importStore*/
import { useCounterMixin } from "@/stores/mixin";
/**use*/
const $q = useQuasar();
const { showLoader, hideLoader, messageError, success, dialogRemove } =
useCounterMixin();
/**props*/
const modal = defineModel<boolean>("modal", { required: true });
const props = defineProps({
id: {
type: String,
defult: "",
},
isActive: {
type: Boolean,
defult: false,
},
});
const salaryId = ref<string>("");
const documentFile = ref<any>(null);
const itemsDocument = ref<any>([]);
/**
* function fetch ข้อมูลรายการ ไฟล์
* @param id
*/
async function fetchDocumentFile(id: string) {
showLoader();
await http
.get(config.API.salaryEmployeeChartFile(id))
.then((res) => {
const list = res.data.map((e: any) => ({ name: e.fileName }));
itemsDocument.value = list;
})
.catch(() => {})
.finally(() => {
hideLoader();
});
}
/** function ปืด Dialog*/
function closeDialog() {
modal.value = !modal.value;
}
/** function เรียก path อัปโหลดไฟล์*/
async function uploadDocumentFile() {
const fileName = documentFile.value.name.replace(/\.(xlsx|docx|pdf)$/, "");
showLoader();
const formData = new FormData();
formData.append("file", documentFile.value);
const body = {
replace: false,
fileList: {
fileName: fileName,
},
};
await http
.post(config.API.salaryEmployeeChartFile(salaryId.value), body)
.then((res) => {
console.log(res);
const foundKey: any = Object.keys(res.data).find(
(key) =>
res.data[key]?.fileName !== undefined &&
res.data[key]?.fileName !== ""
);
const link = res.data[foundKey]?.uploadUrl;
fileUpLoad(link);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
/**
* function อัปโหลดไฟล์
* @param url link อัปโหลด
*/
function fileUpLoad(url: string) {
axios
.put(url, documentFile.value, {
headers: { "Content-Type": documentFile.value?.type },
onUploadProgress: (e) => console.log(e),
})
.then(() => {
success($q, "อัปโหลดไฟล์สำเร็จ");
documentFile.value = null;
fetchDocumentFile(salaryId.value);
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {});
}
/**
* function ลบข้อมูลรายการไฟล์
* @param fileName ชื่่อไฟล์
*/
function onClickDeleteFile(fileName: string) {
dialogRemove($q, async () => {
showLoader();
await http
.delete(config.API.salaryEmployeeChartDelFile(salaryId.value, fileName))
.then(() => {
setTimeout(() => {
fetchDocumentFile(salaryId.value);
success($q, "ลบไฟล์สำเร็จ");
}, 1000);
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
setTimeout(() => {
hideLoader();
}, 1000);
});
});
}
/**
* function โหลดข้อมูลไฟล์
* @param fileName ชื่่อไฟล์
*/
async function onClickDonwload(fileName: string) {
showLoader();
await http
.get(config.API.salaryEmployeeChartDelFile(salaryId.value, fileName))
.then((res) => {
const data = res.data;
downloadFile(data.downloadUrl, data.fileType, fileName);
})
.catch((err) => {
messageError($q, err);
hideLoader();
});
}
/**
* function เรียกไฟล์ PDF
* @param url link PDF
* @param type ประเภทไฟล์
* @param fileName ชือไฟล์
*/
async function downloadFile(url: string, type: string, fileName: string) {
await axios
.get(url, {
method: "GET",
responseType: "blob",
headers: {
"Content-Type": "application/json",
Accept: type, // ถ้ามีการระบุเมื่ออัปโหลด
},
})
.then(async (res) => {
const a = document.createElement("a");
a.href = window.URL.createObjectURL(res.data);
a.download = fileName;
a.click();
})
.catch(async (e) => {
messageError($q, JSON.parse(await e.response.data.text()));
})
.finally(() => {
hideLoader();
});
}
/** callbackFunction ทำการ fetch ข้อมูลไฟล์เมื่อเปิด Dialog*/
watch(
() => modal.value,
async () => {
if (modal.value) {
if (props.id) {
salaryId.value = props.id;
await fetchDocumentFile(props.id);
}
}
}
);
</script>
<template>
<q-dialog v-model="modal" persistent>
<q-card class="col-12" style="width: 80%">
<Header :tittle="`อัปโหลดเอกสารอ้างอิง`" :close="closeDialog" />
<q-separator />
<q-card-section class="scroll" style="max-height: 70vh">
<div class="row col-12 q-col-gutter-sm">
<div class="col-md-12">
<div class="col-xs-12 col-md-3">
<div class="row col-12 q-col-gutter-y-sm">
<div class="col-12 row">
<div
v-if="
!props.isActive && checkPermission($route)?.attrIsUpdate
"
class="full-width"
>
<q-file
class="col-12"
outlined
dense
v-model="documentFile"
label="เอกสารอ้างอิง"
hide-bottom-space
>
<template v-slot:prepend>
<q-icon name="attach_file" color="primary" />
</template>
<template v-slot:after>
<q-btn
v-if="documentFile"
size="14px"
flat
round
dense
color="add"
icon="mdi-upload"
@click="uploadDocumentFile"
><q-tooltip>อัปโหลดเอกสาร</q-tooltip></q-btn
>
</template>
</q-file>
</div>
</div>
<div v-if="itemsDocument.length > 0" class="col-xs-12 row">
<q-list class="full-width rounded-borders" bordered separator>
<q-item
v-for="file in itemsDocument"
:key="file.id"
clickable
v-ripple
>
<q-item-section>{{ file.name }}</q-item-section>
<q-item-section avatar>
<div class="row q-col-gutter-md">
<div>
<q-btn
dense
flat
round
size="12px"
color="blue"
icon="mdi-download-outline"
@click="onClickDonwload(file.name)"
>
<q-tooltip>ดาวนโหลดไฟล</q-tooltip>
</q-btn>
</div>
<div
v-if="
!props.isActive &&
checkPermission($route)?.attrIsUpdate
"
>
<q-btn
dense
flat
round
size="12px"
color="red"
icon="mdi-delete-outline"
@click="onClickDeleteFile(file.name)"
><q-tooltip>ลบไฟล</q-tooltip></q-btn
>
</div>
</div>
</q-item-section>
</q-item>
</q-list>
</div>
<div class="col-12" v-else>
<q-card class="q-pa-md" bordered> ไมรายการเอกสาร </q-card>
</div>
</div>
</div>
</div>
</div>
</q-card-section>
</q-card>
</q-dialog>
</template>
<style lang="scss" scoped></style>