<template>
    <div class="auto-assign-log-page">
        <el-card class="!border-none" shadow="never">
            <el-form :inline="true" class="log-filter-form">
                <el-form-item label="执行日期">
                    <el-date-picker
                        v-model="dateRange"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="YYYY-MM-DD"
                        clearable
                        @change="doSearch"
                    />
                </el-form-item>
                <el-form-item label="结果">
                    <el-select v-model="queryParams.action" clearable placeholder="全部" class="w-[120px]" @change="doSearch">
                        <el-option label="已分配" value="1" />
                        <el-option label="未分配" value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item label="关键词">
                    <el-input
                        v-model="queryParams.keyword"
                        placeholder="患者/手机号/医助/诊单ID"
                        clearable
                        class="w-[220px]"
                        @keyup.enter="doSearch"
                        @clear="doSearch"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="doSearch">查询</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-form-item>
            </el-form>
            <div class="log-hint">
                由每日定时任务「待分配诊单自动指派」写入：按上月二诊复诊接诊率分档轮询分配（&gt;70% 每人3条/日，60%~70% 每人2条/日，50%~60% 每人1条/日，&lt;50% 不分）。每条待指派诊单一行，含分配 / 不分配原因。
            </div>
        </el-card>

        <el-card class="!border-none mt-3" shadow="never">
            <el-table :data="pager.lists" v-loading="pager.loading" size="default" stripe>
                <el-table-column label="记录时间" width="160">
                    <template #default="{ row }">{{ row.create_time_text || '—' }}</template>
                </el-table-column>
                <el-table-column label="诊单" width="90" prop="diagnosis_id" />
                <el-table-column label="患者" min-width="110">
                    <template #default="{ row }">
                        <div>{{ row.patient_name || '—' }}</div>
                        <div class="cell-sub">{{ row.patient_phone || '' }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="结果" width="90" align="center">
                    <template #default="{ row }">
                        <el-tag :type="Number(row.action) === 1 ? 'success' : 'info'" size="small">
                            {{ row.action_text }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="医助" min-width="110">
                    <template #default="{ row }">
                        <template v-if="Number(row.assistant_id) > 0">
                            <div>{{ row.assistant_name || '#' + row.assistant_id }}</div>
                            <div class="cell-sub">上月二诊接诊率 {{ formatRate(row.visit2_rate) }}</div>
                        </template>
                        <span v-else>—</span>
                    </template>
                </el-table-column>
                <el-table-column label="档位" width="100" align="center">
                    <template #default="{ row }">{{ row.tier_text || '—' }}</template>
                </el-table-column>
                <el-table-column label="轮次" width="70" align="center">
                    <template #default="{ row }">{{ Number(row.round_no) > 0 ? '第' + row.round_no + '轮' : '—' }}</template>
                </el-table-column>
                <el-table-column label="原因" min-width="360" show-overflow-tooltip>
                    <template #default="{ row }">{{ row.reason }}</template>
                </el-table-column>
                <el-table-column label="统计月" width="90" prop="stat_month" align="center" />
                <el-table-column label="批次" width="180">
                    <template #default="{ row }">
                        <span class="cell-sub">{{ row.batch_no }}</span>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script setup lang="ts" name="statsAutoAssignLog">
import { autoAssignLogLists } from '@/api/stats'
import { usePaging } from '@/hooks/usePaging'
import { onMounted, reactive, ref, watch } from 'vue'

const dateRange = ref<[string, string] | null>(null)

const queryParams = reactive({
    start_date: '',
    end_date: '',
    action: '' as '' | '0' | '1',
    keyword: ''
})

watch(dateRange, (val) => {
    queryParams.start_date = val?.[0] ?? ''
    queryParams.end_date = val?.[1] ?? ''
})

const { pager, getLists, resetPage } = usePaging({
    fetchFun: autoAssignLogLists,
    params: queryParams
})

const doSearch = () => resetPage()

const handleReset = () => {
    dateRange.value = null
    queryParams.start_date = ''
    queryParams.end_date = ''
    queryParams.action = ''
    queryParams.keyword = ''
    resetPage()
}

const formatRate = (rate: number | null | undefined) =>
    rate === null || rate === undefined ? '—' : `${rate}%`

onMounted(() => getLists())
</script>

<style lang="scss" scoped>
.auto-assign-log-page {
    .log-filter-form {
        :deep(.el-form-item) {
            margin-bottom: 8px;
        }
    }

    .log-hint {
        font-size: 12px;
        color: var(--el-text-color-secondary);
        line-height: 1.6;
    }

    .cell-sub {
        font-size: 12px;
        color: var(--el-text-color-secondary);
    }
}
</style>
