<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="120px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="合同名称" prop="contract_name">
                            <el-input v-model="queryParams.contract_name" clearable placeholder="请输入合同名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="合同编号" prop="contract_num">
                            <el-input v-model="queryParams.contract_num" clearable placeholder="请输入合同编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="甲方签约单位" prop="part_a">
                            <el-input v-model="queryParams.part_a" clearable placeholder="请输入甲方签约单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="乙方签约单位" prop="part_b">
                            <el-input v-model="queryParams.part_b" clearable placeholder="请输入乙方签约单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="业务性质" prop="business_nature">
                            <el-select class="flex-1" v-model="queryParams.business_nature" clearable placeholder="请选择业务性质">
                                <el-option v-for="(item, index) in dictData.cost_consultation_business_nature" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="行业性质" prop="industry_nature">
                            <el-select class="flex-1" v-model="queryParams.industry_nature" clearable placeholder="请选择行业性质">
                                <el-option v-for="(item, index) in dictData.cost_consultation_industry_nature" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="资金来源" prop="fund_sources">
                            <el-select class="flex-1" v-model="queryParams.fund_sources" clearable placeholder="请选择资金来源">
                                <el-option v-for="(item, index) in dictData.cost_consultation_fund_sources" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="建设区域" prop="const_area">
                            <el-select class="flex-1" v-model="queryParams.const_area" clearable placeholder="请选择建设区域">
                                <el-option v-for="(item, index) in dictData.cost_consultation_const_area" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" @click="resetPage">查询</el-button>
                            <el-button @click="resetParams">重置</el-button>
                            <export-data class="ml-2.5" :fetch-fun="apiCostApprovedProjectLists" :params="queryParams"
                                :page-size="pager.size" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['cost_project.cost_approved_project/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button v-perms="['cost_project.cost_approved_project/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button>
            <div class="mt-4">
                <el-table border :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="合同编号" prop="contract_num" show-overflow-tooltip />
                    <el-table-column label="合同名称" prop="contract_name" show-overflow-tooltip />
                    <el-table-column label="甲方签约单位" prop="part_a" show-overflow-tooltip />
                    <el-table-column label="乙方签约单位" prop="part_b" show-overflow-tooltip />
                    <el-table-column label="合同计划开始日期" prop="start_date" show-overflow-tooltip />
                    <el-table-column label="合同计划结束日期" prop="end_date" show-overflow-tooltip />
                    <el-table-column label="工程总投资" prop="project_money" show-overflow-tooltip />
                    <el-table-column label="业务性质" prop="business_nature_text" show-overflow-tooltip />
                    <el-table-column label="行业性质" prop="industry_nature" show-overflow-tooltip>
                        <template #default="{ row }">
                            <dict-value :options="dictData.cost_consultation_industry_nature"
                                :value="row.industry_nature" />
                        </template>
                    </el-table-column>
                    <el-table-column label="签订部门" prop="dept" show-overflow-tooltip />
                    <el-table-column label="资金来源" prop="fund_sources_text" show-overflow-tooltip />
                    <el-table-column label="建设区域" prop="const_area_text" show-overflow-tooltip />
                    <el-table-column label="项目总监/经理" prop="project_director" show-overflow-tooltip />
                    <el-table-column label="监管部门" prop="regulators" show-overflow-tooltip />
                    <el-table-column label="操作" fixed="right" width="200" align="center">
                        <template #default="{ row }">
                            <el-button v-perms="['cost_project.cost_approved_project/edit']" type="primary" link
                                @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-perms="['cost_project.cost_approved_project/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button>
                            <el-button v-perms="['cost_project.cost_approved_project/detail']" link
                                @click="handleDetail(row.id)">
                                详情
                            </el-button>
                            <el-button v-perms="['cost_project.cost_approved_project/edit']" type='warning' link>
                                <router-link :to="{
                                    path: '/cost_consultation/cost_consultation_project/cost_project',
                                    query: {
                                        contractId: row.id,
                                        contractName: row.contract_name
                                    }
                                }">
                                    生成项目
                                </router-link>
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
        <detailPage v-if="showDetail" ref="detailRef" @close="showDetail = false" :detailConfig="detailConfig" />
    </div>
</template>

<script lang="ts" setup name="costApprovedProjectLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiCostApprovedProjectLists, apiCostApprovedProjectDelete, apiCostApprovedProjectDetail } from '@/api/cost_approved_project'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import detailConfig from './detail'


const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const detailRef = ref('')
// 是否显示编辑框
const showEdit = ref(false)
const showDetail = ref(false)
// 查询条件
const queryParams = reactive({
    contract_name: '',
    contract_num: '',
    part_a: '',
    part_b: '',
    business_nature: '',
    industry_nature: '',
    fund_sources: '',
    const_area: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('cost_contract_type,cost_consultation_business_nature,cost_consultation_industry_nature,cost_consultation_fund_sources,cost_consultation_const_area,cost_consultation_file_type,cost_consultation_way,cost_consultation_seal_name')


// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiCostApprovedProjectLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    let res = await apiCostApprovedProjectDetail({ id: data.id })
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(res)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiCostApprovedProjectDelete({ id })
    getLists()
}

// 生成项目
const handleCreation = async (id: number | any[]) => {
    await feedback.confirm('是否生成项目')
    // await apiCostApprovedProjectDelete({ id })
    getLists()
}

// 详情
const handleDetail = async (id: any) => {
    let res = await apiCostApprovedProjectDetail({ id })
    showDetail.value = true
    await nextTick()
    detailRef.value?.open()
    detailRef.value?.setFormData(res)
}

getLists()
</script>
