<template>    
    <div class="my-layout">
        <el-card class="mt8" shadow="never" :body-style="{ paddingBottom: '0' }">
            <el-form :inline="true" @submit.stop.prevent>
                <el-form-item label="项目标题">
                    <el-input v-model="state.filter.keywrods" placeholder="项目标题" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
                    <el-button v-auth="'api:admin:tenant:add'" type="primary" icon="ele-Plus" @click="onAdd"> 新增
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="my-fill mt8" shadow="never">
            <el-table v-loading="state.loading" :data="state.projectListData" row-key="id" height="'100%'"
                style="width: 100%; height: 100%">
                <el-table-column type="index" width="80" label="序号"></el-table-column>
                <el-table-column prop="logo" label="项目图标" width="100">
                    <template #default="{ row }">
                        <el-image style="width: 50px; height: 50px" :src="row.logo"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="项目标题" min-width="120" show-overflow-tooltip />
                <el-table-column prop="settleDay" label="结算方式" width="140" show-overflow-tooltip>
                    <template #default="{ row }">
                        T+{{ row.settleDay }}
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="状态" width="120" show-overflow-tooltip />             
                <el-table-column label="操作" width="180" header-align="center" align="center" fixed="right"> 
                    <template #default="{ row }">
                        <el-button v-auth="'api:admin:project:update'" icon="ele-EditPen" size="small" text type="primary" @click="onEdit(row)">编辑</el-button>
                    </template>                   
                </el-table-column>
            </el-table>

            <div class="my-flex my-flex-end" style="margin-top: 20px">
                <el-pagination v-model:currentPage="state.pageInput.currentPage"
                    v-model:page-size="state.pageInput.pageSize" :total="state.total" :page-sizes="[10, 20, 50, 100]" small
                    background @size-change="onSizeChange" @current-change="onCurrentChange"
                    layout="total, sizes, prev, pager, next, jumper" />
            </div>
        </el-card>

        <project-form ref="projectFormRef" :title="state.ProjectFormTitle"></project-form>
    </div>
</template>

<script lang="ts" setup name="admin/project">
import { ref, reactive, onMounted,  getCurrentInstance, onBeforeMount, defineAsyncComponent } from 'vue'
import { ProjectListOutput, PageInputProjectGetPageDto } from '/@/api/admin/data-contracts'
import { ProjectApi } from '/@/api/admin/project'
import eventBus from '/@/utils/mitt'
import { auth } from '/@/utils/authFunction'

// 引入组件
const ProjectForm = defineAsyncComponent(() => import('./components/project-form.vue'))

 const { proxy } = getCurrentInstance() as any

const projectFormRef = ref()

const state = reactive({
    loading: false,
    ProjectFormTitle: '',
    total: 0,
    filter: {
        keywrods: '',
        status: 0
    },
    pageInput: {
        currentPage: 1,
        pageSize: 20,
    } as PageInputProjectGetPageDto,
    projectListData: [] as Array<ProjectListOutput>,
})

onMounted(() => {
    onQuery()
    eventBus.off('refreshProject')
    eventBus.on('refreshProject', async () => {
        onQuery()
    })
})

onBeforeMount(() => {
    eventBus.off('refreshProject')
})

const onQuery = async () => {
    state.loading = true
    state.pageInput.filter = state.filter
    const res = await new ProjectApi().getPage(state.pageInput).catch(() => {
        state.loading = false
    })

    state.projectListData = res?.data?.list ?? []
    state.total = res?.data?.total ?? 0
    state.loading = false
}

const onAdd = () => {
    state.ProjectFormTitle = '新增项目'
    projectFormRef.value.open()
}

const onEdit = (row: ProjectListOutput) => {
    state.ProjectFormTitle = '编辑项目'
    projectFormRef.value.open(row)
}

// const onDelete = (row: ProjectListOutput) => {
//     proxy.$modal
//         .confirmDelete(`确定要删除【${row.name}】?`)
//         .then(async () => {
//             await new ProjectApi().delete({ id: row.id }, { loading: true, showSuccessMessage: true })
//             onQuery()
//         })
//         .catch(() => { })
// }

const onSizeChange = (val: number) => {
    state.pageInput.pageSize = val
    onQuery()
}

const onCurrentChange = (val: number) => {
    state.pageInput.currentPage = val
    onQuery()
}
</script>
