<template>
    <el-card class="!border-none" shadow="never">
        <el-form ref="formRef" class="mb-[-16px]" :model="formData" :inline="true">
            <el-form-item label="师傅姓名" prop="name">
                <el-input class="w-[280px]" v-model="formData.name" clearable placeholder="请输入师傅姓名" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select class="w-[280px]" v-model="formData.status" clearable>
                    <el-option label="全部" value="" />
                    <el-option v-for="option in optionMap.physicalStatusOptions" :key="option.value" :label="option.label"
                        :value="option.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="提交时间">
                <daterange-picker class="w-[385px]" type="datetimerange" format="YYYY-MM-DD HH:mm:ss"
                    v-model:startTime="formData.createTimeStart" v-model:endTime="formData.createTimeEnd" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getLists">查询</el-button>
                <el-button @click="resetParams">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card shadow="never" class="mt-4 !border-none">
        <el-button type="primary" @click="handleShowDialog">上传体检报告</el-button>
        <div class="mt-3">
            <el-table ref="couponTable" :data="pager.lists" style="width: 100%" row-key="id">
                <el-table-column prop="staffId" label="师傅编号" min-width="80"></el-table-column>
                <el-table-column prop="staffName" label="师傅姓名">
                    <template #default="{ row }">
                        {{ parseEmpty(row, 'staffName') }}
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="提交时间" min-width="100" />
                <el-table-column prop="physicalExamination" label="体检报告（图片和pdf）">
                    <template #default="{ row }">
                        <div class="method">
                            <el-image style="width: 30px; height: 30px" :src="reportImg"
                                :preview-src-list="previewSrcList(row)" fit="cover" :preview-teleported="true"
                                v-if="isVisibleImage(row)" />
                            <el-image style="width: 30px; height: 30px" :src="pdfImg" @click="handlePreviewPdf(row)"
                                fit="cover" :preview-teleported="true" v-if="isVisiblePdf(row)" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="状态">
                    <template #default="{ row }">
                        <el-tag :type="parseTagType(row.status)">{{ parseStatus(row) }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="refuseReason" label="拒绝理由">
                    <template #default="{ row }">
                        <el-tooltip placement="top" v-if="isShowToolTip(row.refuseReason)">
                            <template #content>
                                <span v-html="ellipsisReason(row.refuseReason)"></span>
                            </template>
                            <span class="ellipsis">{{ row.refuseReason }}</span>
                        </el-tooltip>
                        <span v-else>{{ parseEmpty(row, 'refuseReason') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="148" fixed="right">
                    <template #default="{ row }">
                        <div class="flex">
                            <el-button v-if="isApplyPedding(row.status)" type="primary" link
                                @click="handleOperation(row, OperationTextEnum.SUCCESS, '体检报告')">
                                通过
                            </el-button>
                            <el-button v-if="isApplyPedding(row.status)" type="primary" link
                                @click="handleOperation(row, OperationTextEnum.FAILED, '体检报告')">
                                拒绝
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="flex justify-end mt-4">
            <pagination v-model="pager" @change="getLists" />
        </div>
    </el-card>

    <physicalDialog ref="physicalDialogRef" />
</template>

<script setup lang="ts">
import { reactive, computed, ref } from 'vue'
import reasonDialog from './components/reasonDialog.vue'
import physicalDialog from './components/physicalDialog.vue'
import { usePaging } from '@/hooks/usePaging'
import { useAuthStaffOperation } from '@/hooks/useCommon'
import { apiMasterWorkerPhysical } from '@/api/master_worker'
import { ApplyStatusEnum, applyStatusMap, OperationTextEnum, physicalEnumMap, tagFields } from '@/enums/modeEnum'
import { optionMap } from '@/config/status'
import reportImg from '@/assets/images/report.png'
import pdfImg from '@/assets/icons/pdf.svg'
import { interceptText, parseEmpty } from '@/utils/util'

const router = useRouter()

const physicalDialogRef = ref()
const formData = reactive({
    name: '',
    status: '', //申请状态
    createTimeStart: '',
    createTimeEnd: ''
})

const isApplyPedding = computed(() => (status: ApplyStatusEnum) => status === ApplyStatusEnum.APPLY)

const parseStatus = computed(() => {
    return (row: any) => {
        return physicalEnumMap[row.status]
    }
})
const previewSrcList = computed(() => {
    return (row: any) => {
        const { physicalExamination } = row
        return physicalExamination.split(',').filter(item => !item.includes('pdf'))
    }
})
const isVisibleImage = computed(() => {
    return (row: any) => {
        return unref(previewSrcList)(row).length > 0
    }
})
const isVisiblePdf = computed(() => {
    return (row: any) => {
        const { physicalExamination } = row
        return physicalExamination.split(',').filter(item => item.includes('pdf')).length > 0
    }
})
const parseTagType = computed(() => (status: ApplyStatusEnum) => tagFields[status])
const isShowToolTip = computed(() => (text: string) => text !== null && text.length >= 20)
const ellipsisReason = computed(() => (text: string) => interceptText(text, 20, '<br />'))

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: apiMasterWorkerPhysical,
    params: formData
})
/**预览pdf */
const handlePreviewPdf = (row: any) => {
    const { physicalExamination } = row
    if (!physicalExamination) return
    const url = router.resolve({ path: '/report/pdf', query: { path: physicalExamination?.split(',').filter(item => item.includes('pdf')) } })
    window.open(url.href)
}
/**上传体检报告-弹框 */
const handleShowDialog = () => {
    physicalDialogRef.value.openDialog()
}
const { handleOperation } = useAuthStaffOperation(reasonDialog, false, undefined, getLists)

onMounted(getLists)
</script>

<style lang="scss" scoped>
.method {
    display: flex;
    gap: 10px;

    .iconfont {
        cursor: pointer;
    }
}

.ellipsis {
    max-width: 100%;
    text-overflow: ellipsis;
    display: -webkit-inline-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
</style>
