<template>
    <HcCard>
        <template #header>
            <div class=" ml-2">
                <el-input v-model="searchForm.queryValue" clearable placeholder="请输入项目名称进行查询" size="large"/>
            </div>
            <div class="ml-4">
                <el-button type="primary" @click="searchClick" size="large">
                    <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>
        <template #extra>
            <el-button size="large" type="primary" hc-btn @click="addRowClick">
                <HcIcon name="add"/>
                <span>新增项目成本测算</span>
            </el-button>
        </template>

        <HcTable :column="tableColumn" :datas="tableData" :loading="tableLoading">
            <template #projectName="{row}">
                <span class="text-blue text-hover" @click="rowNameTap(row)">{{row.projectName}}</span>
            </template>
            <template #action="{row,index}">
                <el-button plain size="small" type="success" @click="approvalRowClick(row)"  :disabled="row?.approveStatus!=='1'">提交审批</el-button>
                <el-button plain size="small" type="primary" @click="editRowClick(row)">编辑</el-button>
                <el-button plain size="small" type="danger" @click="delRowClick(row)">删除</el-button>
            </template>
        </HcTable>

        <template #action>
            <HcPages :pages="searchForm" @change="pageChange"/>
        </template>
    </HcCard>
</template>

<script setup>
import {ref,onMounted,onActivated} from "vue";
import {useRouter} from 'vue-router'
import costApi from '~api/project/cost.js';
import {getArrValue} from "js-fast-way"
import {delMessage} from "~uti/tools";


const router = useRouter()
onActivated(()=>{
    getTableData()
  
})
//项目类型
const projectType = ref([])

const serverType=ref([])




//年度数据
const annuals = ref([
    {name: '2023年5月', key: '2023-05'},
    {name: '2022年4月', key: '2022-04'},
    {name: '2021年3月', key: '2021-03'}
])

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

//搜索
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: 'projectName', name: '项目名称'},
    {key: 'projectCostTotal', name: '项目测算总金额', width: '120', align: 'center'},
    {key: 'devCostTotal', name: '研发部成本总金额', width: '120', align: 'center'},
    {key: 'businessCostTotal', name: '实施部成本总金额', width: '120', align: 'center'},
    {key: 'maintainCostTotal', name: '维护部成本总金额', width: '120', align: 'center'},
    {key: 'marketCostTotal', name: '市场部成本总金额', width: '120', align: 'center'},
    {key: 'manageCostTotal', name: '管理费成本总金额', width: '120', align: 'center'},
    {key: 'outsourceCostTotal', name: '外包劳务成本总金额', width: '120', align: 'center'},
    {key: 'action', name: '操作', width: '250', align: 'center', fixed: 'right'},
]
const tableData = ref([
    {id: 1, key: '焦作至唐河高速公路方城至唐河段', key1: '810232', key2: '203200', key3: '192000', key4: '120350', key5: '63540', key6: '320000', key7: '12000'},
    {id: 2, key: '焦作至唐河高速公路方城至唐河段', key1: '810232', key2: '203200', key3: '192000', key4: '120350', key5: '63540', key6: '320000', key7: '12000'},
    {id: 3, key: '焦作至唐河高速公路方城至唐河段', key1: '810232', key2: '203200', key3: '192000', key4: '120350', key5: '63540', key6: '320000', key7: '12000'},
])

const getTableData = async() => {
    tableLoading.value = true
    const {error, code, data} = await costApi.getprojectCostBudgetStats(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 addRowClick = () => {
    router.push({name: 'project-cost-form'})
}

//编辑报销记录
const editRowClick = (row) => {
    router.push({
        name: 'project-cost-form',
        query: {id: row.id}
    })
}
//删除成本预算deleteByStatsId

const delRowClick = async(row) => {
    delMessage(async() => {
        const {error, code, data,msg} = await costApi.deleteByStatsId({id:row.id})  
            if (!error && code === 200) {
                window.$message.success(msg)
                getTableData()
            } else {
                getTableData()
            }
    })
}
//名称被点击
const rowNameTap = (row) => {
    router.push({
        name: 'project-cost-data',
        query: {id: row.projectId}
    })
}

//提交审批
const approvalRowClick = async(row) => {
    const {error, code, data,msg} = await costApi.submitApprove({id:row.id})  
    if (!error && code === 200) {
        window.$message.success(msg)
        getTableData()
    } else {
        getTableData()
    }
}
</script>

<style lang="scss" scoped>

</style>
