<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="status" class="w-[160px]">
                    <el-select v-model="queryParams.status" placeholder="请选择状态" class="flex-1">
                        <el-option label="未到账" value="1" />
                        <el-option label="部分到账" value="2" />
                        <el-option label="全部到账" value="3" />
                    </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">
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="id" prop="id" show-overflow-tooltip />
                    <el-table-column label="到账状态" prop="is_cash_text" show-overflow-tooltip />
                    <el-table-column label="到账金额" prop="enter_amount" show-overflow-tooltip />
                    <el-table-column label="最新到账时间" prop="enter_time" show-overflow-tooltip />
                    <el-table-column label="发票抬头" prop="invoice_title" show-overflow-tooltip />
                    <el-table-column label="发票金额" prop="amount" show-overflow-tooltip />
                    <el-table-column label="开票类型" prop="type_text" show-overflow-tooltip />
                    <el-table-column label="申请开票人" prop="user_name" show-overflow-tooltip />
                    <el-table-column label="申请时间" prop="create_time" show-overflow-tooltip />
                    <el-table-column label="发票号码" prop="code" show-overflow-tooltip />
                    <el-table-column label="开票时间" prop="open_time" show-overflow-tooltip />
                    <el-table-column label="操作" width="170" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handEdit(row.id)">
                                到账管理
                            </el-button>
                            <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" @success="getLists(), showEdit = false" @close="showEdit = false" />
        <detailPopup v-if="showDetail" ref="detailRef" @success="showDetail = false, getLists()"
            @close="showDetail = false, getLists()" />
    </div>
</template>

<script lang="ts" setup name="oaSealCateLists">
import { usePaging } from '@/hooks/usePaging'
import detailPopup from './detail.vue'
import EditPopup from './edit.vue'
import { apiInvliceIncomeIndex, apiInvliceDetail, apiInvliceList2 } from '@/api/oa_financial'

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


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

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

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

// 获取字典数据

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

const handEdit = async (id: any) => {
    let res = await apiInvliceDetail({ id })
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(res)

}

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