<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="资产名称" prop="assetName">
                    <el-input class="w-[280px]" v-model="queryParams.assetName" />
                </el-form-item>
                <el-form-item label="规格型号" prop="model">
                    <el-input class="w-[280px]" v-model="queryParams.model" />
                </el-form-item>
                <el-form-item label="品牌" prop="brand">
                    <el-input class="w-[280px]" v-model="queryParams.brand" />
                </el-form-item>
                <el-form-item label="资产类别" prop="category">
                    <el-select
                        v-model="queryParams.category"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="全部" value="" />
                        <el-option
                            v-for="(item, index) in dictData.asset_classification"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否为固定资产" prop="isFixedAsset">
                    <el-input class="w-[280px]" v-model="queryParams.isFixedAsset" />
                </el-form-item>
                <el-form-item label="非固定资产库存信息ID" prop="consumableId">
                    <el-input class="w-[280px]" v-model="queryParams.consumableId" />
                </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 mt-4" shadow="never">
            <div>
                <el-button v-perms="['info: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="assetName" min-width="100" />
                <el-table-column label="规格型号" prop="model" min-width="100" />
                <el-table-column label="品牌" prop="brand" min-width="100" />
                <el-table-column label="资产类别" prop="category" min-width="100">
                    <template #default="{ row }">
                        <dict-value :options="dictData.asset_classification" :value="row.category" />
                    </template>
                </el-table-column>
                <el-table-column label="是否为固定资产" prop="isFixedAsset" min-width="100" />
                <el-table-column label="非固定资产库存信息ID" prop="consumableId" min-width="100" />
                <el-table-column label="资产描述" prop="description" min-width="100" />
                <el-table-column label="记录创建时间" prop="createdTime" min-width="100" />
                <el-table-column label="最后更新时间" prop="updatedTime" min-width="100" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['info:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['info:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </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"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="info">
import { infoDelete, infoLists } from '@/api/asset/info'
import { useDictData } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    assetName: '',
    model: '',
    brand: '',
    category: '',
    isFixedAsset: '',
    consumableId: '',
})

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

const { dictData } = useDictData<{
    asset_classification: any[]
}>(7)


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?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await infoDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
