<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="开始时间" prop="uid">
                    <el-date-picker class="flex-1 !flex" v-model="queryParams.start_time" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="选择开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="uid">
                    <el-date-picker class="flex-1 !flex" v-model="queryParams.end_time" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="状态" prop="quit_time">
                    <el-select v-model="queryParams.status" placeholder="请选择状态" class="flex-1">
                        <el-option label="全部" value="0" />
                        <el-option label="待审核" value="1" />
                        <el-option label="审核中" value="2" />
                        <el-option label="审批通过,待打款" value="3" />
                        <el-option label="审核不通过" value="4" />
                        <el-option label="撤销审核" value="5" />
                        <el-option label="审批通过,已打款" value="6" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type=" primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['works.rlzy.oa_personal_quit/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <!-- <el-button v-perms="['works.rlzy.oa_personal_quit/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="报销总金额(元)" prop="total_amount" show-overflow-tooltip />
                    <el-table-column label="状态" prop="check_status_text" show-overflow-tooltip />
                    <el-table-column label="报销人" prop="user_name" show-overflow-tooltip />
                    <el-table-column label="报销部门" prop="dept_name" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ row.dept_name || '全公司' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="报销保证编号" prop="code" show-overflow-tooltip />
                    <el-table-column label="原始单据日期" prop="expense_time" show-overflow-tooltip />
                    <el-table-column label="入账月份" prop="income_month" show-overflow-tooltip />
                    <el-table-column label="录入时间" prop="create_time" show-overflow-tooltip />
                    <el-table-column label="当前审核人" prop="check_admin_users" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ row.check_admin_users || '审核结束' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="打款人" prop="pay_admin_name" show-overflow-tooltip />
                    <el-table-column label="打款确认时间" prop="pay_time" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handDetail(row.id)">
                                详情
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :deptList="deptList" :dict-data="dictData" @success="getLists"
            @close="showEdit = false" />
        <detailPopup v-if="showDetail" ref="detailRef" :dict-data="dictData" @success="showDetail = false, getLists()"
            @close="showDetail = false, getLists()" />
    </div>
</template>

<script lang="ts" setup name="oaPersonalQuitLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiExpenseList, apiExpenseDetail } from '@/api/oa_financial'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import detailPopup from './detail.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

const detailRef = ref(null)
// 是否显示编辑框
const showDetail = ref(false)

// 查询条件
const queryParams = reactive({
    start_time: '',
    end_time: '',
    status: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('oa_approve_cate')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiExpenseList,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

const handDetail = async (id: any) => {
    let res = await apiExpenseDetail({ id })
    showDetail.value = true
    await nextTick()
    detailRef.value?.open()
    detailRef.value?.setFormData(res)
}

getLists()
</script>

