<template>
    <hc-card>
        <template #header>
            <el-alert title="可独立修改私有项目的归档规则，不会影响其他项目归档规则" type="warning" :closable="false" />
        </template>
        <hc-table :column="tableColumn" :datas="tableData" :loading="tableLoading" :index-style="{ width: 60 }">
            <template #action="{ row }">
                <el-link type="primary" @click="tableRowClick(row)">配置规则</el-link>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>
        <!-- 配置规则 -->
        <HcDrawerTemp v-model="isDrawerTempShow" :data="tableInfo" @close="getTableData" />
    </hc-card>
</template>

<script setup>
import { onActivated, onDeactivated, ref } from 'vue'
import { getArrValue } from 'js-fast-way'
import HcDrawerTemp from './tree/drawer-temp.vue'
import mainApi from '~api/project/tree'

defineOptions({
    name: 'ProjectTree',
})

//激活
onActivated(() => {
    searchForm.value.current = 1
    getTableData()
})

//搜索条件
const searchForm = ref({ projectId: '', current: 1, size: 20, total: 0 })

//分页
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}

//表格数据
const tableColumn = ref([
    { key: 'projectName', name: '项目名称' },
    { key: 'action', name: '操作', width: 100, align: 'center' },
])
const tableData = ref([])

//获取表格数据
const tableLoading = ref(false)
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { data } = await mainApi.page({
        ...searchForm.value,
        total: null,
    })
    tableLoading.value = false
    tableData.value = getArrValue(data?.records)
    searchForm.value.total = data?.total ?? 0
}

//配置规则
const tableInfo = ref({})
const isDrawerTempShow = ref(false)
const tableRowClick = (row) => {
    tableInfo.value = row
    isDrawerTempShow.value = true
}

//离开了当前页面
onDeactivated(() => {
    isDrawerTempShow.value = false
})
</script>
