<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_code">
                            <el-input v-model="queryParams.contract_code" 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="合同类型 ">
                            <el-select v-model="queryParams.contract_type" class="flex-1 ">
                                <el-option label="主合同" :value="0"></el-option>
                                <el-option label="框架协议" :value="1"></el-option>
                                <el-option label="补充协议" :value="2"></el-option>
                            </el-select>
                        </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>
                            <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">
            <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_code" show-overflow-tooltip width="200" />
                    <el-table-column label="合同名称" prop="contract_name" show-overflow-tooltip />
                    <el-table-column label="合同类型" prop="contract_type_text" show-overflow-tooltip />
                    <el-table-column label="甲方单位" prop="part_a_name" show-overflow-tooltip />
                    <el-table-column label="乙方单位" prop="part_b" show-overflow-tooltip />
                    <el-table-column label="工程投资" prop="project_money" show-overflow-tooltip />
                    <el-table-column label="签订金额" prop="signed_amount" show-overflow-tooltip />
                    <el-table-column label="签订部门" prop="signed_dept_name" show-overflow-tooltip />
                    <el-table-column label="业务性质" prop="business_nature_text" 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/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_code: '',
    contract_type: '',
    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>
