<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.staffInfo" 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">
        <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">
                    <template #default="{ row }">
                        <el-button class="custom-btn" type="primary" text>
                            <router-link
                                :to="{
                                    path: '/service/evaluate',
                                    query: {
                                        staffInfo: row.staffSn
                                    }
                                }"
                            >
                                {{ row.staffSn }}
                            </router-link>
                        </el-button>
                    </template>
                </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 label="申诉原因">
                    <template #default="{ row }">
                        <el-tooltip placement="top" v-if="isShowToolTip(row.reason)">
                            <template #content>
                                <span v-html="ellipsisReason(row.reason)"></span>
                            </template>
                            <span class="ellipsis">{{ row.reason }}</span>
                        </el-tooltip>
                        <span v-else>{{ parseEmpty(row, 'reason') }}</span>
                    </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 label="回复内容">
                    <template #default="{ row }">
                        <el-tooltip placement="top" v-if="isShowToolTip(row.reply)">
                            <template #content>
                                <span v-html="ellipsisReason(row.reply)"></span>
                            </template>
                            <span class="ellipsis">{{ row.reply }}</span>
                        </el-tooltip>
                        <span v-else>{{ parseEmpty(row, 'reply') }}</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="handleAgree(row)">通过</el-button>
                            <el-button
                                v-if="isApplyPedding(row.status)"
                                type="primary"
                                link
                                @click="handleOperation(row, OperationTextEnum.FAILED, '意见反馈')"
                            >
                                拒绝
                            </el-button>
                            <el-button v-if="!isApplyPedding(row.status)" type="danger" link @click="handleDelete(row)">删除</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>
</template>

<script setup lang="ts">
import { reactive, computed } from 'vue'
import reasonDialog from './components/reasonDialog.vue'
import { usePaging } from '@/hooks/usePaging'
import { useAuthStaffOperation } from '@/hooks/useCommon'
import { apiFeedbackDel, apiFeedbackList, apiMasterWorkerDetail } from '@/api/master_worker'
import { ApplyStatusEnum, OperationTextEnum, physicalEnumMap, tagFields, getStaffOrderStatusEnum } from '@/enums/modeEnum'
import { optionMap } from '@/config/status'
import { interceptText, parseEmpty, toast } from '@/utils/util'
import feedback from '@/utils/feedback'

const router = useRouter()

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

const isApplyPedding = computed(() => (status: ApplyStatusEnum) => status === ApplyStatusEnum.APPLY)
const parseStatus = computed(() => (row: any) => physicalEnumMap[row.status])
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: apiFeedbackList,
    params: formData
})
/**删除操作 */
const handleDelete = async (row: any) => {
    const { staffName, id } = row
    try {
        await feedback.confirm(`您确定删除【${staffName}】师傅提交的申诉记录？`)
        await apiFeedbackDel({ id })
        toast('删除成功', 'success')
        getLists()
    } catch (error) {}
}

const handleAgree = async (row: any) => {
    const { isReceiveOrder } = await apiMasterWorkerDetail({ id: row.staffId })
    if (isReceiveOrder == getStaffOrderStatusEnum['CLOSED']) {
        await feedback.confirm(`该师傅评分过低，需要先修改订单评分。是否跳转服务评分页面？`)
        router.push({
            path: '/service/evaluate',
            query: {
                staffInfo: row.staffSn
            }
        })
    } else handleOperation(row, OperationTextEnum.SUCCESS, '意见反馈')
}
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;
}

:deep(.custom-btn) {
    flex: 1;
    overflow: hidden;
    margin-left: 10px;

    &.el-button:hover {
        color: #4a5dff;
        background-color: transparent;
    }

    span {
        display: block;
        width: 100%;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        text-align: left;
    }
}
</style>
