<template>
    <HcCard>
        <template #header>
            <div class="w-36">
                <el-select v-model="searchForm.projectType" block clearable placeholder="项目类型" size="large">
                    <el-option v-for="item in planType" :label="item.name" :value="item.key"/>
                </el-select>
            </div>
            <div class="w-36 ml-2">
                <el-select v-model="searchForm.projectServerType" block clearable placeholder="服务类型" size="large">
                    <el-option v-for="item in department" :label="item.name" :value="item.key"/>
                </el-select>
            </div>
            <div class="w-56 ml-2">
                <el-input v-model="searchForm.name" clearable placeholder="请输入项目名称查询" @keyup="keyUpEvent" size="large"/>
            </div>
            <div class="ml-4">
                <el-button size="large" type="primary" @click="searchClick">
                    <HcIcon name="search-2"/>
                    <span>搜索</span>
                </el-button>
            </div>
            <div class="ml-2">
                <el-button size="large" @click="resetClick">
                    <HcIcon name="close-circle"/>
                    <span>重置</span>
                </el-button>
            </div>
        </template>
        <HcTable :isIndex="false" :column="tableColumn" :datas="tableData" :loading="tableLoading">
            <template #name="{row}">
                <span class="text-blue" @click="rowNameClick(row)">{{row.name}}</span>
            </template>
        </HcTable>
        <template #action>
            <HcPages :pages="searchForm" @change="pageChange"/>
        </template>
    </HcCard>
</template>

<script setup>

import {useRouter} from 'vue-router'
import {ref,onMounted,onActivated} from "vue";
import projectApi from '~api/program/project.js';
import {getArrValue} from "js-fast-way"

const router = useRouter()
onActivated(()=>{
    getTableData()

})
//计划类型
const planType = ref([
    {name: '临时计划', key: '1'},
    {name: '月度计划', key: '2'},
    {name: '年度计划', key: '3'},
])

//选择部门
const department = ref([
    {name: '研发部门', key: '1'},
    {name: '业务部门', key: '2'},
    {name: '人事部门', key: '3'},
])

//搜索表单
const searchForm = ref({
   
    current: 1, size: 20, total: 0
})

//搜索框回车
const keyUpEvent = (event) => {
    if (event.key === "Enter") {
        searchForm.value.current = 1;
        getTableData()
    }
}

//搜索
const searchClick = () => {
    searchForm.value.current = 1;
    getTableData()
}

//重置搜索表单
const resetClick = () => {
    searchForm.value = {current: 1, size: 20, total: 0}
}

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

//获取数据
const tableLoading = ref(false)
const tableColumn = [
    {key: 'number', name: '项目编号', width: '100'},
    {key: 'name', name: '项目名称'},
    {key: 'projectTypeValue', name: '项目类型'},
    {key: 'projectServerTypeValue', name: '项目服务类型'},
    {key: 'currentProcessName', name: '项目进程'},
]
const tableData = ref([
    {id: 1, key: 'JH-01', key1: '2023年5月度计划', key2: '高速公路', key3: '软件合同', key4: '产品-研发'},
    {id: 2, key: 'JH-01', key1: '2023年5月度计划', key2: '高速公路', key3: '软件合同', key4: '产品-研发'},
    {id: 3, key: 'JH-01', key1: '2023年5月度计划', key2: '高速公路', key3: '软件合同', key4: '产品-研发'},
])

const getTableData = async() => {
    tableLoading.value = true
    const {error, code, data} = await projectApi.getPage(searchForm.value)
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data)
        searchForm.value.total = data['total'] || 0
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}

//编辑预算
const rowNameClick = (row) => {
    router.push({
        name: 'program-project-form',
        query: {
            id: row.id,
            type:'edit'
        }
    })
}
</script>
