<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="合同类型" prop="contract_type">
                            <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="contract_name">
                            <el-input class="w-[280px]" 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 class="w-[280px]" v-model="queryParams.contract_code" 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">
                                <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="行业性质" prop="industry_nature">
                            <el-select class='flex-1' v-model="queryParams.industry_nature">
                                <el-option v-for="(item, index) in  dictData.cost_consultation_industry_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="资金来源" prop="fund_sources">
                            <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="建设区域" prop="const_area">
                            <el-select class='flex-1' v-model="queryParams.const_area">
                                <el-option v-for="(item, index) in  dictData.const_area " :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="签订金额" prop="signed_amount">
                            <el-input class="w-[280px]" v-model="queryParams.signed_amount" clearable placeholder="请输入签订金额"
                                type="number" />
                        </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">
            <el-button v-perms="['marketing.marketing_contract/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button v-perms="['marketing.marketing_contract/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button>
            <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="part_a" show-overflow-tooltip />
                    <el-table-column label="乙方签约单位" prop="part_b" show-overflow-tooltip />
                    <el-table-column label="乙方签约人" prop="part_b_signatory" show-overflow-tooltip />
                    <el-table-column label="业务性质" prop="business_nature" show-overflow-tooltip />
                    <el-table-column label="行业性质" prop="industry_nature" show-overflow-tooltip />
                    <el-table-column label="资金来源" prop="fund_sources" show-overflow-tooltip />
                    <el-table-column label="建设区域" prop="const_area" show-overflow-tooltip />
                    <el-table-column label="协议性质" prop="agreement_nature" show-overflow-tooltip />
                    <el-table-column label="工程总投资" prop="project_money" show-overflow-tooltip />
                    <el-table-column label="工程规模及概况" prop="project_scale" show-overflow-tooltip />
                    <el-table-column label="签订金额" prop="signed_amount" show-overflow-tooltip />
                    <el-table-column label="合同签订费率" prop="signed_rate" show-overflow-tooltip />
                    <el-table-column label="合同服务工期" prop="service_duration" 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="performance_money" show-overflow-tooltip />
                    <el-table-column label="履约金到期时间" prop="performance_money_expiration_time" show-overflow-tooltip />
                    <el-table-column label="合同付款方式" prop="payment_method" show-overflow-tooltip />
                    <el-table-column label="合同主要内容" prop="content" show-overflow-tooltip />
                    <el-table-column label="附件" prop="annex" show-overflow-tooltip />
                    <el-table-column label="签订部门" prop="signed_dept" show-overflow-tooltip />
                    <el-table-column label="签订负责人" prop="signed_head" show-overflow-tooltip />
                    <el-table-column label="监管部门" prop="supervise_dept" show-overflow-tooltip />
                    <el-table-column label="项目总监/经理" prop="project_manager" show-overflow-tooltip />
                    <el-table-column label="实施方式" prop="implement_method" show-overflow-tooltip />
                    <el-table-column label="盖章名称" prop="seal_name" show-overflow-tooltip />
                    <el-table-column label="是否限制" prop="is_limit" show-overflow-tooltip />
                    <el-table-column label="限制次数" prop="limit_num" show-overflow-tooltip />
                    <el-table-column label="文件类型" prop="file_type" show-overflow-tooltip />
                    <el-table-column label="盖章份数" prop="seal_num" show-overflow-tooltip />
                    <el-table-column label="盖章人" prop="seal_user" show-overflow-tooltip />
                    <el-table-column label="骑缝盖章" prop="plance_seal" show-overflow-tooltip />
                    <el-table-column label="发出日期" prop="send_date" show-overflow-tooltip />
                    <el-table-column label="邮寄编号" prop="mailing_number" show-overflow-tooltip />
                    <el-table-column label="评审状态 0-待移交 1-已移交" prop="review_status" show-overflow-tooltip />
                    <el-table-column label="立项状态 0-待立项 1-已立项" prop="status" show-overflow-tooltip />
                    <el-table-column label="录入人" prop="create_user" 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>
                            <el-button v-perms="['marketing.marketing_contract/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </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"
            :deptList="deptList" />
    </div>
</template>

<script lang="ts" setup name="marketingContractLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiMarketingContractLists, apiMarketingContractDelete } from '@/api/marketing_contract'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import { deptLists } from "@/api/org/department"
import EditPopup from './edit.vue'

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


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

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

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

// 获取字典数据
const { dictData } = useDictData('cost_consultation_business_nature,cost_consultation_industry_nature,money_source,const_area,cost_consultation_way,cost_consultation_seal_name,cost_consultation_file_type,isqfgz')

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

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

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

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

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

getLists()
getDeptList()

</script>
