<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 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 label="资金来源">
                            <el-select class='flex-1' v-model="queryParams.fund_sources">
                                <el-option v-for="(item, index) in  dictData.money_source " :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 label="合同类型 ">
                            <el-select v-model="queryParams.queryParams" 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>
                            <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="signed_amount" show-overflow-tooltip />
                    <el-table-column label="支付方式" prop="payment_method" 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="seal_name_text" show-overflow-tooltip />
                    <el-table-column label="是否限制" prop="is_limit_text" show-overflow-tooltip />
                    <el-table-column label="文件类型" prop="file_type_text" show-overflow-tooltip />
                    <el-table-column label="骑缝盖章" prop="plance_seal_text" show-overflow-tooltip />
                    <el-table-column label="合同类型" prop="contract_type_text" 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>

