<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.dictName" :value="item.dictValue"/>
                </el-select>
            </div>
            <div class="w-40 ml-2">
                <el-select v-model="searchForm.projectServerType" block clearable placeholder="服务类型" size="large">
                    <el-option v-for="item in serverType" :label="item.dictName" :value="item.dictValue"/>
                </el-select>
            </div>
            <div class="w-36 ml-2">
                <el-select v-model="searchForm.implementPrincipal" block clearable placeholder="实施负责人" size="large">
                    <el-option v-for="item in projectUser" :label="item.name" :value="item.key"/>
                </el-select>
            </div>
            <div class="w-36 ml-2">
                <el-select v-model="searchForm.queryDate" block clearable placeholder="选择时间" size="large">
                    <el-option v-for="item in annuals" :label="item.name" :value="item.key"/>
                </el-select>
            </div>
            <div class="w-48 ml-2">
                <el-input v-model="searchForm.queryValue" clearable placeholder="请输入项目名称进行查询" @keyup="keyUpEvent" 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 :isIndex="false" :column="tableColumn" :datas="tableData" :loading="tableLoading">
            <template #name="{row}">
                <span class="text-blue text-hover" @click="rowClick(row)">{{row.name}}</span>
            </template>
            <template #key7="{row}">
                <span class="text-blue ">{{row.startTime?row.startTime:''}}</span>
                <span class="text-blue " v-if="row.endTime">~</span>
                <span class="text-blue ">{{row.endTime?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" @click="delRowClick(row)" :loading="row.delLoad||false">删除</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 projectApi from '~api/project/project-list.js';
import {getArrValue} from "js-fast-way"
import {delMessage} from "~uti/tools";

const router = useRouter()
onMounted(()=>{
    getTableData()
    getProjectType()
    getProjectServerTypeDict()
})
onActivated(()=>{
    getTableData()
})
//项目类型
const projectType = ref([
    // {name: '二级路', key: '二级路'},
    // {name: '国道', key: '国道'},
    // {name: '水利水电', key: '水利水电'},
    // {name: '市政', key: '市政'},
])

//项目服务类型
const serverType = ref([])

//实施负责人
const projectUser = ref([
    {name: '张三', key: '1'},
    {name: '李四', key: '2'},
])

//年度数据
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, contractType: null, projectUser: null, date: null, queryValue: '',
    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}
    getTableData()
}


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

//获取数据
const tableLoading = ref(false)
const tableColumn = [
    {key: 'number', name: '编号', width: '100', align: 'center'},
    {key: 'name', name: '项目名称', minWidth: '260'},
    {key: 'constructUnit', name: '客户单位', width: '200', align: 'center'},
    {key: 'projectTypeValue', name: '项目类型', width: '140', align: 'center'},
    {key: 'projectServerTypeValue', name: '服务类型', width: '140', align: 'center'},
    {key: 'contractMoney', name: '合同额(W)', width: '100', align: 'center'},
    {key: 'projectPrincipalName', name: '项目负责人', width: '100', align: 'center'},
    {key: 'implementPrincipalName', name: '实施负责人', width: '100', align: 'center'},
    {key: 'key7', name: '项目起止日期', width: '220', align: 'center'},
    {key: 'action', name: '操作', width: '160', align: 'center', fixed: 'right'},
]
const tableData = ref([
    {id: 1, key: 'GS-01', key1: '焦作至唐河高速公路方城至唐河段', key2: '研发-配置', key3: '2022-07-01', key4: '高速公路', key5: '软件合同', key6: '40', key8: '张三', key7: '2022-07-01 ~ 2022-04-01'},
    {id: 2, key: 'GS-01', key1: '焦作至唐河高速公路方城至唐河段', key2: '研发-配置', key3: '2022-07-01', key4: '高速公路', key5: '软件合同', key6: '40', key7: '2022-07-01 ~ 2022-04-01'},
    {id: 3, key: 'GS-01', key1: '焦作至唐河高速公路方城至唐河段', key2: '研发-配置', key3: '2022-07-01', key4: '高速公路', key5: '软件合同', key6: '40', key7: '2022-07-01 ~ 2022-04-01'},
])

const getTableData = async() => {
    tableLoading.value = true
    const {error, code, data} = await projectApi.getProjectList(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 getProjectType=async()=>{
    const {error, code, data} = await projectApi.getProjectTypeDict()
    if (!error && code === 200) {
        projectType.value=getArrValue(data)
    } else {
        projectType.value=[]
    }
}
//获取项目服务类型
const getProjectServerTypeDict=async()=>{
    const {error, code, data} = await projectApi.getProjectServerTypeDict()
    if (!error && code === 200) {
        serverType.value=getArrValue(data)
    } else {
        serverType.value=[]
    }
}
//查看
const rowClick = (row) => {
    router.push({
        name: 'project-list-info',
        query: {
            id: row.id,
            type: 'view'
        }
    })
}

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

//新增
const addRowClick = () => {
    router.push({
        name: 'project-list-info',
        query: {
            type: 'add'
        }
    })
}
//删除
const delRowClick = (row) => {
    delMessage(async() => {
            row.delLoad= true
            const {error, code, data,msg} = await projectApi.removeProjectInfo({id:row.id})
            row.delLoad= false
            if (!error && code === 200) {
                window.$message.success(msg)
                getTableData()
            } else {
                getTableData()
            }
    })
    

}
</script>
