<!-- 标准装置台账信息详情 校准证书 -->
<script name="StandardBookCertificate" lang="ts" setup>
import { onMounted, ref } from 'vue'
import { ElLoading, ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import type { IStandardCertificate } from '../book-interface'
import { useCheckList } from '@/commonMethods/useCheckList'
import { UploadFile } from '@/api/file'
import { addStandardCert, deleteStandardCert, getStandardCertList } from '@/api/equipment/standard/book'
const props = defineProps({
pageType: { // 页面类型 add新建 edit编辑 detail详情
type: String,
default: 'detail',
},
standardId: { // id
type: String,
},
})
// 查询条件
const listQuery = ref({
id: props.standardId!,
offset: 1,
limit: 20,
})
const loadingTable = ref(false) // 表格loading
const list = ref<IStandardCertificate[]>([]) // 表格数据
const checkoutList = ref<IStandardCertificate[]>([]) // 多选数据
const total = ref(0) // 数据总条数
const checkoutIds = ref<string[]>([])// 删除用的ids
const columns = [ // 表头
{ text: '证书编号', value: 'certificateNo', align: 'center', required: true, width: '160' },
{ text: '证书名称', value: 'certificateName', align: 'center', required: true },
{ text: '出具单位', value: 'provideCompany', align: 'center', required: true },
{ text: '证书日期', value: 'certificateDate', align: 'center', width: '120', required: true },
{ text: '证书有效期', value: 'certificateExpireDate', align: 'center', width: '120', required: true },
{ text: '附件', value: 'attachmentFile', align: 'center', required: true },
]
// -----------------------------------------methods--------------------------------------
// 数据查询
function fetchData(isNowPage = false) {
loadingTable.value = true
if (!isNowPage) {
// 是否显示当前页,否则跳转第一页
listQuery.value.offset = 1
}
getStandardCertList(listQuery.value).then((response) => {
list.value = response.data.rows.map((item: { attachmentFile: string; certificateExpireDate: string; certificateDate: string }) => {
return {
...item,
certificateDate: item.certificateDate ? dayjs(item.certificateDate).format('YYYY-MM-DD') : item.certificateDate, // 证书日期
certificateExpireDate: item.certificateExpireDate ? dayjs(item.certificateExpireDate).format('YYYY-MM-DD') : item.certificateExpireDate, // 证书有效期
}
})
total.value = parseInt(response.data.total)
loadingTable.value = false
}).catch(() => {
loadingTable.value = false
})
}
// 点击增加行
const add = () => {
if (!useCheckList(list.value, columns, '标准证书')) {
return false
}
list.value.unshift({
id: '', // 主键
attachmentFile: '', // 附件
certificateDate: '', // 证书日期
certificateExpireDate: '', // 证书有效期
certificateName: '', // 证书名称
certificateNo: '', // 证书编号
provideCompany: '', // 出具单位
standardId: '', // 标准装置id
})
}
// 点击删除行
const del = async () => {
if (!checkoutList.value.length) {
ElMessage.warning('请选中要删除的行')
return false
}
// 前端删除
list.value = list.value.filter(item => !checkoutList.value.includes(item))
// 接口删除
checkoutIds.value = checkoutList.value.map(item => item.id!)
checkoutIds.value = checkoutIds.value.filter(item => item)
if (checkoutIds.value.length) {
loadingTable.value = true
await deleteStandardCert({ ids: checkoutIds.value })
fetchData()
loadingTable.value = false
}
}
// 多选发生改变时
const handleSelectionChange = (e: any) => {
checkoutList.value = e
}
// 点击保存增加标准证书
const saveStandardCert = () => {
if (!list.value.length) {
return false
}
if (!useCheckList(list.value, columns, '标准证书')) {
return false
}
const tempParams = list.value.filter(item => !item.id) // 找到这次新增的数据(没有id的)
const params = tempParams.map((item) => {
return {
...item,
id: '',
standardId: props.standardId, // 标准装置表id
}
})
loadingTable.value = true
addStandardCert(params).then(() => {
ElMessage.success('新增标准证书成功')
fetchData()
}).catch(() => {
loadingTable.value = false
})
}
// 改变页容量
function handleSizeChange(val: number) {
listQuery.value.limit = val
fetchData()
}
// 改变当前页
function handleCurrentChange(val: number) {
listQuery.value.offset = val
fetchData(true)
}
// -------------------------------------------文件上传--------------------------------------
const fileRef = ref() // 文件上传input
const uploadIndex = ref(0) // 要上传文件到第几行
const onFileChange = (event: any) => {
if (event.target.files[0].type !== 'application/pdf') {
ElMessage.warning('请上传pdf格式')
return false
}
if (event.target.files?.length !== 0) {
// 创建formdata对象
const fd = new FormData()
fd.append('multipartFile', event.target.files[0])
const loading = ElLoading.service({
lock: true,
background: 'rgba(255, 255, 255, 0.8)',
})
UploadFile(fd).then((res) => {
if (res.code === 200) {
list.value[uploadIndex.value].attachmentFile = res.data[0]
// 重置当前验证
ElMessage.success('文件上传成功')
loading.close()
}
else {
ElMessage.error(res.message)
loading.close()
}
})
}
}
const upload = (index: number) => {
uploadIndex.value = index
fileRef.value.click()
}
// // 点击附件
// const openPdf = (row: any) => {
// }
// -------------------------------------------钩子-------------------------------------------------
onMounted(async () => {
fetchData()
})
defineExpose({
saveStandardCert,
})
</script>
<template>
<detail-block title="标准证书">
<template v-if="pageType !== 'detail'" #btns>
<el-button type="primary" @click="add">
增加行
</el-button>
<el-button type="info" @click="del">
删除行
</el-button>
</template>
<input v-show="false" ref="fileRef" type="file" @change="onFileChange">
<el-table
v-loading="loadingTable"
:data="list"
border
stripe
style="width: 100%;"
@selection-change="handleSelectionChange"
>
<el-table-column
v-if="pageType !== 'detail'"
type="selection"
width="38"
/>
<el-table-column label="序号" width="55" align="center">
<template #default="scope">
{{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
</template>
</el-table-column>
<el-table-column
v-for="item in columns"
:key="item.value"
:prop="item.value"
:label="item.text"
:width="item.width"
align="center"
show-overflow-tooltip
>
<template #header>
<span v-show="item.required" style="color: red;">*</span><span>{{ item.text }}</span>
</template>
<template #default="scope">
<span v-if="scope.row.id && item.value !== 'attachmentFile'">{{ scope.row[item.value] }}</span>
<show-photo v-if="item.value === 'attachmentFile' && scope.row.attachmentFile" :minio-file-name="scope.row.attachmentFile" />
<el-button
v-if="!scope.row.id && item.value === 'attachmentFile' && pageType !== 'detail'"
id="file"
type="primary"
:disabled="pageType === 'detail'"
:style="{ 'margin-left': scope.row.attachmentFile === '' ? '0px' : '20px' }"
@click="upload(scope.$index)"
>
{{ scope.row.attachmentFile === '' ? '上传' : '更换附件' }}
</el-button>
<el-input
v-if="!scope.row.id && pageType !== 'detail' && (item.value === 'certificateNo' || item.value === 'certificateName' || item.value === 'provideCompany')"
v-model="scope.row[item.value]"
:placeholder="`${item.text}`"
class="input"
/>
<el-date-picker
v-if="!scope.row.id && pageType !== 'detail' && (item.value === 'certificateDate' || item.value === 'certificateExpireDate')"
v-model="scope.row[item.value]"
type="date"
format="YYYY-MM-DD"
value-format="YYYY-MM-DD"
:placeholder="pageType === 'detail' ? ' ' : `${item.text}`"
:disabled="pageType === 'detail'"
class="full-width-input"
/>
</template>
</el-table-column>
</el-table>
<!-- 页码 -->
<el-pagination
style="width: 100%;margin-top: 10px;"
:current-page="listQuery.offset"
:page-sizes="[10, 20, 30]"
:page-size="listQuery.limit"
:total="total"
layout="total, sizes, prev, pager, next"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</detail-block>
</template>
<style lang="scss" scoped>
.link {
color: #5da0ff;
text-decoration: underline;
cursor: pointer;
margin-right: 8px;
}
</style>