<!--
 * @Author: micky 1254597151@qq.com
 * @Date: 2023-10-23 15:13:31
 * @LastEditors: micky 1254597151@qq.com
 * @LastEditTime: 2023-11-06 17:30:35
 * @FilePath: \housekeeping-admin\src\views\master_worker\apply.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<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.applyStatusOptions" :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="handleNavigate">添加师傅资料</el-button>
        <div class="mt-3">
            <el-table ref="couponTable" :data="pager.lists" style="width: 100%" row-key="id">
                <el-table-column prop="id" label="ID号" min-width="80"></el-table-column>
                <el-table-column prop="name" label="师傅姓名" show-overflow-tooltip></el-table-column>
                <el-table-column prop="createTime" label="申请时间" min-width="100" />
                <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="拒绝理由" show-overflow-tooltip
                    :formatter="row => parseEmpty(row, 'refuseReason')" />
                <el-table-column label="操作" width="148" fixed="right">
                    <template #default="{ row }">
                        <div class="flex">
                            <el-button type="primary" link @click="handlePreviewStaffDetail(row.id)">详请</el-button>
                            <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>
</template>

<script lang="ts" setup>
import reasonDialog from './components/reasonDialog.vue'
import { usePaging } from '@/hooks/usePaging'
import { useAuthStaffOperation } from '@/hooks/useCommon'
import { applyStatusMap, OperationTextEnum, ApplyStatusEnum, tagFields } from '@/enums/modeEnum'
import { optionMap } from '@/config/status'
import { apiMasterWorkerApply } from '@/api/master_worker'
import { parseEmpty } from '@/utils/util'

const router = useRouter()

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

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: apiMasterWorkerApply,
    params: formData
})
const { handleOperation } = useAuthStaffOperation(reasonDialog, false, undefined, getLists)

const parseStatus = computed(() => {
    return (row: any) => {
        return applyStatusMap[row.status]
    }
})
const parseTagType = computed(() => (status: ApplyStatusEnum) => tagFields[status])
const isApplyPedding = computed(() => (status: ApplyStatusEnum) => status === ApplyStatusEnum.APPLY)

/**查看师傅详情 */
const handlePreviewStaffDetail = (id: number) => {
    router.push({ path: '/master_worker/detail', query: { id } })
}
/**跳转路由 */
const handleNavigate = () => {
    router.push({ path: '/master_worker/staff' })
}

onMounted(getLists)
</script>

<style lang="scss" scope></style>
