<template>
    <div class="assign-log-panel">
        <el-table v-loading="loading" :data="rows" border stripe empty-text="暂无指派记录">
            <el-table-column label="操作时间" width="175" prop="create_time_text" />
            <el-table-column label="原医助" min-width="120">
                <template #default="{ row }">
                    {{ assistantLogCellText(row, 'from') }}
                </template>
            </el-table-column>
            <el-table-column label="新医助" min-width="120">
                <template #default="{ row }">
                    {{ assistantLogCellText(row, 'to') }}
                </template>
            </el-table-column>
            <el-table-column label="继承" width="72" align="center">
                <template #default="{ row }">
                    <el-tag v-if="Number(row.is_inherit) === 1" type="success" size="small">是</el-tag>
                    <span v-else class="text-gray-400">否</span>
                </template>
            </el-table-column>
            <el-table-column label="快照·业务单创建人" min-width="130" show-overflow-tooltip>
                <template #default="{ row }">
                    {{ relatedPoCreatorCell(row) }}
                </template>
            </el-table-column>
            <el-table-column label="快照·业务单创建时间" width="190">
                <template #header>
                    <span>快照·业务单创建时间</span>
                    <el-tooltip
                        placement="top"
                        content="related_po_create_time：触发本次操作的处方业务订单 create_time；与原文助一致时表示医助创建订单时间"
                    >
                        <el-icon class="assign-log-col-hint"><QuestionFilled /></el-icon>
                    </el-tooltip>
                </template>
                <template #default="{ row }">
                    {{ relatedPoCreateTimeCell(row) }}
                </template>
            </el-table-column>
            <el-table-column label="操作人" width="110" prop="operator_name" />
            <el-table-column label="操作账号" width="120" prop="operator_account" show-overflow-tooltip />
            <el-table-column label="IP" width="130" prop="ip" show-overflow-tooltip />
        </el-table>
    </div>
</template>

<script setup lang="ts">
import { QuestionFilled } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { tcmDiagnosisAssignLogList } from '@/api/tcm'

const props = defineProps<{
    diagnosisId: number
}>()

const loading = ref(false)
const rows = ref<any[]>([])

/** 对应日志表 related_po_creator_id：触发本次释放/指派的业务订单 creator */
function relatedPoCreatorCell(row: Record<string, unknown>): string {
    const name = row.related_po_creator_name
    if (name != null && String(name).trim() !== '') {
        return String(name)
    }
    const id = Number(row.related_po_creator_id)
    if (Number.isFinite(id) && id > 0) {
        return `ID:${id}`
    }
    return '—'
}

/** 对应日志表 related_po_create_time（Unix）格式化为可读时间 */
function relatedPoCreateTimeCell(row: Record<string, unknown>): string {
    const t = row.related_po_create_time_text
    if (t != null && String(t).trim() !== '') {
        return String(t)
    }
    const ts = Number(row.related_po_create_time)
    if (!Number.isFinite(ts) || ts <= 0) {
        return '—'
    }
    const d = new Date(ts * 1000)
    if (Number.isNaN(d.getTime())) return '—'
    const pad = (n: number) => String(n).padStart(2, '0')
    return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
}

function assistantLogCellText(row: Record<string, unknown>, side: 'from' | 'to'): string {
    const nameKey = side === 'from' ? 'from_assistant_name' : 'to_assistant_name'
    const idKey = side === 'from' ? 'from_assistant_id' : 'to_assistant_id'
    const n = row[nameKey]
    if (n != null && String(n).trim() !== '' && String(n) !== '—') {
        return String(n)
    }
    const id = Number(row[idKey])
    if (Number.isFinite(id) && id > 0) {
        return `ID:${id}`
    }
    return '—'
}

const load = async () => {
    if (!props.diagnosisId) return
    loading.value = true
    try {
        const data = await tcmDiagnosisAssignLogList({ id: props.diagnosisId })
        const list = Array.isArray(data) ? data : []
        rows.value = list
    } catch (e) {
        console.error(e)
        rows.value = []
    } finally {
        loading.value = false
    }
}

watch(
    () => props.diagnosisId,
    () => {
        load()
    },
    { immediate: true }
)

defineExpose({ refresh: load })
</script>

<style scoped>
.assign-log-col-hint {
    margin-left: 4px;
    vertical-align: middle;
    color: var(--el-text-color-secondary);
    cursor: help;
}
</style>
