<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="所属合同" prop="project_filing_id">
                    <el-input v-model="formData.contract_name" clearable placeholder="点击选择项目备案所属合同" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="备案人员" prop="filing_user">
                    <el-input v-model="formData.filing_user" clearable placeholder="请输入备案人员" />
                </el-form-item>
                <el-form-item label="备案角色" prop="filing_role">
                    <el-select class=' flex-1' v-model="formData.filing_role">
                        <el-option v-for="(item, index) in  dictData.filing_role " :key="index" :label="item.name"
                            :value="parseInt(item.value)"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="登记时间" prop="reg_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.reg_time" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="选择登记时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select class=' flex-1' v-model="formData.status">
                        <el-option v-for="(item, index) in  dictData.filing_status " :key="index" :label="item.name"
                            :value="parseInt(item.value)"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择框架协议" width="70%">
                <dialogTable :config="marketing_project_filing" @customEvent="customEvent">
                </dialogTable>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingProjectFilingDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingProjectFilingDetailAdd, apiMarketingProjectFilingDetailEdit, apiMarketingProjectFilingDetailDetail } from '@/api/marketing_project_filing_detail'
import { marketing_project_filing } from "@/components/dialogTable/dialogTableConfig";

import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑市场经营--项目备案明细' : '新增市场经营--项目备案明细'
})

// 表单数据
const formData = reactive({
    id: '',
    project_filing_id: '',
    contract_name: '',
    filing_user: '',
    filing_role: '',
    reg_time: '',
    status: '',
})

const customEvent = (e) => {
    formData.project_filing_id = e.id
    formData.contract_name = e.contract_name
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiMarketingProjectFilingDetailDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiMarketingProjectFilingDetailEdit(data)
        : await apiMarketingProjectFilingDetailAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
