<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" label-width="80px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="合同名称">
                            <el-input v-model="queryParams.contract_name" clearable placeholder="请输入合同名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="合同编号">
                            <el-input v-model="queryParams.contract_code" 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="业务性质">
                            <el-select class='flex-1' v-model="queryParams.business_nature">
                                <el-option v-for="(item, index) in dictData.cost_consultation_business_nature "
                                    :key="index" :label="item.name" :value="parseInt(item.value)"></el-option>
                            </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>
                        </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 :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="合同名称" prop="contract_name" show-overflow-tooltip />
                    <el-table-column label="合同编号" prop="contract_code" 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="signed_amount" show-overflow-tooltip />
                    <el-table-column label="签订部门" prop="signed_dept_name" show-overflow-tooltip />
                    <el-table-column label="签订负责人" prop="signed_head_name" show-overflow-tooltip />
                    <el-table-column label="业务性质" prop="business_nature_text" show-overflow-tooltip />
                    <el-table-column label="签订时间" prop="create_time" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['marketing.marketing_contract/edit']" type="primary" link
                                @click="handleEdit(row)">
                                合同移交
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="marketingContractLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiMarketingHandoverContractLists, apiMarketingHandoverContractHandover, } from '@/api/marketing_handover_contract'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

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


// 查询条件
const queryParams = reactive({
    contract_name: '',
    contract_code: '',
    part_a: '',
    business_nature: '',
    industry_nature: '',
    fund_sources: '',
    const_area: '',
    signed_dept: '',
    contract_type: '',
})

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

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

// 获取字典数据
const { dictData } = useDictData('cost_consultation_business_nature,fund_sources,const_area,money_source')

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

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

// 编辑
const handleEdit = async (data: any) => {
    let res = await apiMarketingHandoverContractHandover({ id: data.id })
    getLists()
}


getLists()
</script>
