<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_id">
                            <el-input v-model="formData.contract_name" clearable placeholder="点击选择合同" readonly
                                @click="showDialog = true" />

                        </el-form-item>
                    </el-col>
                    <CreateUserLable :form-data="formData" name="录入" flag></CreateUserLable>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <UploadAnnex :form-data="formData"></UploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <formTable :formData="formData.detail" :config="tableConfig" :dict-data="dictData" />

                <el-dialog v-model="showDialog" title="选择合同" width="70%">
                    <dialogTable :config="marketing_contract" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingProjectFilingEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingProjectFilingAdd, apiMarketingProjectFilingEdit, apiMarketingProjectFilingDetail, apiMarketingProjectFilingDetailDelete, apiMarketingProjectFilingDetailLists } from '@/api/marketing_project_filing'
import { marketing_contract } 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: '',
    contract_id: '',
    remark: '',
    contract_name: "",
    annex: [],
    create_user: '',
    create_time: '',
    detail: []
})

const tableConfig = reactive(
    {
        title: "备案人员信息",
        tableConfig: [
            {
                label: "备案人员",
                value: 'filing_user',

            },
            {
                label: "备案角色",
                value: 'filing_role',
                select: "filing_role"
            },
            {
                label: "登记时间",
                value: 'reg_time',
                date: true
            },
            {
                label: "状态",
                value: 'status',
                select: "filing_status"

            },
        ],
        deleteApi: apiMarketingProjectFilingDetailDelete,
    }
)


const customEvent = (e) => {
    formData.contract_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]
        }
    }
    let res = await apiMarketingProjectFilingDetailLists({ project_filing_id: data.id })
    formData.detail = res.lists

}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiMarketingProjectFilingEdit(data)
        : await apiMarketingProjectFilingAdd(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>
