<template>
    <div class="account-cost-list">
        <el-card class="!border-none mb-4" shadow="never">
            <div class="flex flex-wrap">
                <div class="w-1/2 md:w-1/3">
                    <div class="leading-10">当前筛选天数</div>
                    <div class="text-4xl">{{ pager.extend.days_count ?? 0 }}</div>
                </div>
                <div class="w-1/2 md:w-1/3">
                    <div class="leading-10">累计账户消耗 (元)</div>
                    <div class="text-4xl">¥{{ pager.extend.total_amount ?? '0.00' }}</div>
                </div>
            </div>
        </el-card>

        <el-card class="!border-none" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="日期范围">
                    <daterange-picker
                        v-model:startTime="queryParams.start_date"
                        v-model:endTime="queryParams.end_date"
                    />
                </el-form-item>
                <el-form-item class="w-[280px]" label="备注/维护人">
                    <el-input
                        v-model="queryParams.remark"
                        placeholder="备注 / 创建人 / 修改人"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['finance.account_cost/add']" type="primary" @click="handleAdd">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>

            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="日期" prop="cost_date" min-width="120" />
                <el-table-column label="账户消耗" min-width="120">
                    <template #default="{ row }">¥{{ row.amount }}</template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" min-width="260" show-overflow-tooltip />
                <el-table-column label="创建人" prop="creator_name" min-width="100" />
                <el-table-column label="最后修改人" prop="updater_name" min-width="100" />
                <el-table-column label="更新时间" prop="update_time" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['finance.account_cost/edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>

        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="accountCostList">
import { accountCostLists } from '@/api/finance'
import { usePaging } from '@/hooks/usePaging'

import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const queryParams = reactive({
    start_date: '',
    end_date: '',
    remark: '',
})

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

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

const handleEdit = async (row: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(row)
}

const handleReset = () => {
    queryParams.start_date = ''
    queryParams.end_date = ''
    queryParams.remark = ''
    resetPage()
}

getLists()
</script>
