<template>
    <HcCard>
        <!-- <template #header>
            <div class="w-36">
                <el-select v-model="searchForm.planType" block clearable placeholder="年度" size="large">
                    <el-option v-for="item in planType" :label="item.name" :value="item.key"/>
                </el-select>
            </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 #name="{row}">
                <span class="text-blue text-hover" @click="rowNameClick(row)">{{row.name}}</span>
            </template>
            <template #key2="{row}">
                <span >{{row.budgetStartTime?row.budgetStartTime:''}}</span>
                <span  v-if="row.budgetEndTime">~</span>
                <span >{{row.budgetEndTime?row.budgetEndTime:''}}</span>
            </template>
            <template #action="{row,index}">
                <el-button plain size="small" type="primary" @click="editRowClick(row)">编辑</el-button>
                <el-button plain size="small" type="danger" @click="rowCancel(row)">删除</el-button>
            </template>
        </HcTable>
        <template #action>
            <HcPages :pages="searchForm" @change="pageChange"/>
        </template>
    </HcCard>
</template>

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

const router = useRouter()

// onMounted(()=>{
//     getTableData()
  
// })
onActivated(()=>{
    getTableData()

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

//搜索表单
const searchForm = ref({
    planType: null, 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: 'name', name: '预算名称'},
    {key: 'key2', name: '起止日期', width: '220', align: 'center'},
    {key: 'totalBudget', name: '总经营预算', width: '120', align: 'center'},
    {key: 'annualContractTarget', name: '年度合同额指标', width: '140', align: 'center'},
    {key: 'annualProfitTarget', name: '年度利润指标', width: '120', align: 'center'},
    {key: 'staffCost', name: '工资支出', width: '120', align: 'center'},
    {key: 'manageDisburse', name: '其他管理支出', width: '120', align: 'center'},
    {key: 'action', name: '操作', width: '150', align: 'center'},
]
const tableData = ref([
    {id: 1, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '111',  key4: '36', key5: '30', key6: '6', key7: '张三'},
    {id: 2, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '111',  key4: '36', key5: '30', key6: '6', key7: '张三'},
    {id: 3, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '111',  key4: '36', key5: '30', key6: '6', key7: '张三'},
    {id: 4, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '111',  key4: '36', key5: '30', key6: '6', key7: '张三'},
])

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

//名称被点击
const rowNameClick = (row) => {
    router.push({
        name: 'program-annual-view',
        query: {
            id: row.id,
            name:row.name
        }
    })
}

//新增计划
const addRowClick = () => {
    router.push({
        name: 'program-annual-form',
        query:{
            type:'add'
        }
    })
}

//编辑预算
const editRowClick = (row) => {
    router.push({
        name: 'program-annual-form',
        query: {
            id: row.id,
            type:'edit'
        }
    })
}
//删除
const rowCancel = (row) => {
    delMessage(async () => {
        const {error, code, msg} = await annualApi.deleteAnnualBudget({
            id: row.id
        })
        //判断状态
        if (!error && code === 200) {
            window.$message?.success(msg)
            getTableData().then()
        } else {
            window.$message?.error(msg)
        }
    })
}
</script>
