<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 projectType" :label="item.name" :value="item.key"/>
                </el-select>
            </div>
            <div class="w-40 ml-2">
                <el-select v-model="searchForm.projectType" block clearable placeholder="服务类型" size="large">
                    <el-option v-for="item in projectType" :label="item.name" :value="item.key"/>
                </el-select>
            </div>
            <div class="w-48 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="updateClick">
                <HcIcon name="refresh"/>
                <span>合同回款更新</span>
            </el-button>
            <el-button size="large" type="primary" hc-btn @click="addRowClick">
                <HcIcon name="add"/>
                <span>新增项目合同信息</span>
            </el-button>
        </template>

        <HcTable :isIndex="false" :column="tableColumn" :datas="tableData" :loading="tableLoading">
            <template #name="{row}">
                <span class="text-blue" @click="rowNameTap(row)">{{row.name}}</span>
            </template>
            <template #key4="{row}">
                <span >{{row.startTime+'~'+row.endTime}}</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">删除</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 contractApi from '~api/project/project-contract.js';
import {getArrValue} from "js-fast-way"

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

//项目类型
const projectType = ref([
    {name: '二级路', key: '二级路'},
    {name: '国道', key: '国道'},
    {name: '水利水电', key: '水利水电'},
    {name: '市政', key: '市政'},
])

//搜索表单
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: 'number', name: '合同编号', width: '120', align: 'center'},
    {key: 'name', name: '合同名称'},
    {key: 'contractTypeValue', name: '合同类型', width: '140', align: 'center'},
    {key: 'contractSignTime', name: '签订时间', width: '160', align: 'center'},
    {key: 'key4', name: '合同起止日期', width: '220', align: 'center'},
    {key: 'returnedMoney', name: '合同已履约回款', width: '140', align: 'center'},
    {key: 'unreturnedMoney', name: '合同未履约回款', width: '140', align: 'center'},
    {key: 'action', name: '操作', width: '180', align: 'center'},
]
const tableData = ref([])

const getTableData = async() => {
    tableLoading.value = true
    const {error, code, data} = await contractApi.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 rowNameTap = (row) => {
    router.push({
        name: 'project-contract-form',
        query: {
            id: row.id,
            type: 'view'
        }
    })
}

//新增预算
const addRowClick = () => {
    router.push({
        name: 'project-contract-form',
        query: {type: 'add'}
    })
}

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

//合同回款更新
const updateClick = () => {
    router.push({
        name: 'project-contract-update'
    })
}
</script>
