<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="项目编号" prop="project_num">
                    <el-input class="w-[280px]" v-model="queryParams.project_num" clearable placeholder="请输入项目编号" />
                </el-form-item>
                <el-form-item label="项目名称" prop="project_name">
                    <el-input class="w-[280px]" v-model="queryParams.project_name" clearable placeholder="请输入项目名称" />
                </el-form-item>
                <el-form-item label="咨询类型" prop="types">
                    <el-select class="w-[280px]" v-model="queryParams.types" clearable placeholder="请选择业务性质">
                        <el-option v-for="(item, index) in dictData.consultation_type" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="行业" prop="industry">
                    <el-select class="w-[280px]" v-model="queryParams.industry" 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-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export-data class="ml-2.5" :fetch-fun="apiCostProjectLists" :params="queryParams"
                        :page-size="pager.size" />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['cost_project.cost_project/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button v-perms="['cost_project.cost_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 :render-header="pager.calcWidth" label="项目名称" prop="project_name"
                        show-overflow-tooltip />
                    <!-- <el-table-column :render-header="pager.calcWidth" label="关联合同" prop="contract.contract_name"
                        show-overflow-tooltip /> -->
                    <el-table-column :render-header="pager.calcWidth" label="咨询类型" prop="types" show-overflow-tooltip>
                        <template #default="{ row }">
                            <dict-value :options="dictData.consultation_type" :value="row.types" />
                        </template>
                    </el-table-column>
                    <el-table-column :render-header="pager.calcWidth" label="行业" prop="industry" show-overflow-tooltip>
                        <template #default="{ row }">
                            <dict-value :options="dictData.cost_consultation_industry_nature" :value="row.industry" />
                        </template>
                    </el-table-column>
                    <el-table-column :render-header="pager.calcWidth" label="计划开始日期" prop="starting"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="计划结束日期" prop="endtime" show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="计划工期（天）" prop="jhgq" show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="项目负责人" prop="principal_name"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="项目相关人员" prop="person_text"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="项目总投资（元）" prop="invest"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="预算造价（元）" prop="budget" show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="送审造价（元）" prop="cost" show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="审定造价（元）" prop="approval"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="委托单位" prop="aunit" show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="委托方联系人" prop="Acontact"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="联系电话" prop="acontactnum"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="登记日期" prop="date" show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="项目规模及概括" prop="generalize"
                        show-overflow-tooltip />
                    <el-table-column :render-header="pager.calcWidth" label="注意事项" prop="note" show-overflow-tooltip />
                    <!-- <el-table-column label="备注" prop="remark" show-overflow-tooltip /> -->
                    <el-table-column label="操作" width="170" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['cost_project.cost_project/edit']" type="primary" link
                                @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-perms="['cost_project.cost_project/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button>
                            <el-button v-perms="['cost_project.cost_project/detail']" link @click="handleDetail(row.id)">
                                详情
                            </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 :deptList="deptList" 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="costProjectLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { useRoute } from 'vue-router'
import { apiCostProjectLists, apiCostProjectDelete, apiCostProjectDetail } from '@/api/cost_project'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import detailConfig from './detail'
import { deptLists } from "@/api/org/department"


const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const detailRef = ref('')
// 是否显示编辑框
const showEdit = ref(false)
const showDetail = ref(false)
const route = useRoute()


// 查询条件
const queryParams = reactive({
    project_num: '',
    project_name: '',
    types: '',
    industry: ''
})

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

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

// 获取字典数据
const { dictData } = useDictData('consultation_type,cost_consultation_industry_nature')

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

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

// 编辑
const handleEdit = async (data: any) => {
    let res = await apiCostProjectDetail({ 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 apiCostProjectDelete({ id })
    getLists()
}

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


if (route.query.contractId) {
    handleAdd()
}

const deptList = ref([])
const getDeptList = async () => {
    let res = await deptLists()
    deptList.value = res.lists
}

getLists()
getDeptList()
</script>
