<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="org_name">
                    <el-input v-model="formData.org_name" clearable placeholder="请输入机构名称" />
                </el-form-item>
                <el-form-item label="备案类型" prop="filing_type">
                    <el-input v-model="formData.filing_type" clearable placeholder="请输入备案类型" />
                </el-form-item>
                <el-form-item label="法定代表人" prop="fddbr">
                    <el-input v-model="formData.fddbr" clearable placeholder="请输入法定代表人" />
                </el-form-item>
                <el-form-item label="联系电话" prop="telephone">
                    <el-input v-model="formData.telephone" clearable placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="营业执照编号" prop="yyzzbh">
                    <el-input v-model="formData.yyzzbh" clearable placeholder="请输入营业执照编号" />
                </el-form-item>
                <el-form-item label="资质证书编号" prop="zzzsbh">
                    <el-input v-model="formData.zzzsbh" clearable placeholder="请输入资质证书编号" />
                </el-form-item>
                <el-form-item label="组织机构代码证" prop="zzjgdmz">
                    <el-input v-model="formData.zzjgdmz" clearable placeholder="请输入组织机构代码证" />
                </el-form-item>
                <el-form-item label="总公司联系电话" prop="zgslxdh">
                    <el-input v-model="formData.zgslxdh" clearable placeholder="请输入总公司联系电话" />
                </el-form-item>
                <el-form-item label="主项监理范围" prop="zyxjlfw">
                    <el-input v-model="formData.zyxjlfw" clearable placeholder="请输入主项监理范围" />
                </el-form-item>
                <el-form-item label="增项监理范围" prop="zjxjlfw">
                    <el-input v-model="formData.zjxjlfw" clearable placeholder="请输入增项监理范围" />
                </el-form-item>
                <el-form-item label="本地办公地址" prop="bdbgdz">
                    <el-input v-model="formData.bdbgdz" clearable placeholder="请输入本地办公地址" />
                </el-form-item>
                <el-form-item label="本地公司联系电话" prop="bdgslxdh">
                    <el-input v-model="formData.bdgslxdh" clearable placeholder="请输入本地公司联系电话" />
                </el-form-item>
                <el-form-item label="人员信息" prop="ryxx">
                    <el-input v-model="formData.ryxx" clearable placeholder="请输入人员信息" />
                </el-form-item>
                <el-form-item label="机构注册地址" prop="jgzcdz">
                    <el-input v-model="formData.jgzcdz" clearable placeholder="请输入机构注册地址" />
                </el-form-item>
                <el-form-item label="备案开始期" prop="baksq">
                    <el-input v-model="formData.baksq" clearable placeholder="请输入备案开始期" />
                </el-form-item>
                <el-form-item label="备案结束期" prop="bajsq">
                    <el-input v-model="formData.bajsq" clearable placeholder="请输入备案结束期" />
                </el-form-item>
                <el-form-item label="备案时间" prop="basj">
                    <el-input v-model="formData.basj" clearable placeholder="请输入备案时间" />
                </el-form-item>
                <el-form-item label="经办人" prop="jbr">
                    <el-input v-model="formData.jbr" clearable placeholder="请输入经办人" />
                </el-form-item>
                <el-form-item label="年检时间" prop="njsj">
                    <el-input v-model="formData.njsj" clearable placeholder="请输入年检时间" />
                </el-form-item>
                <el-form-item label="负责人" prop="fzr">
                    <el-input v-model="formData.fzr" clearable placeholder="请输入负责人" />
                </el-form-item>
                <el-form-item label="负责人手机" prop="fzrsj">
                    <el-input v-model="formData.fzrsj" clearable placeholder="请输入负责人手机" />
                </el-form-item>
                <el-form-item label="技术负责人" prop="jsfzr">
                    <el-input v-model="formData.jsfzr" clearable placeholder="请输入技术负责人" />
                </el-form-item>
                <el-form-item label="技术负责人手机" prop="jsfzrsj">
                    <el-input v-model="formData.jsfzrsj" clearable placeholder="请输入技术负责人手机" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingBranchFilingEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingBranchFilingAdd, apiMarketingBranchFilingEdit, apiMarketingBranchFilingDetail } from '@/api/marketing_branch_filing'
import { timeFormat } from '@/utils/util'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑市场经营--分支机构备案' : '新增市场经营--分支机构备案'
})

// 表单数据
const formData = reactive({
    id: '',
    org_name: '',
    filing_type: '',
    fddbr: '',
    telephone: '',
    yyzzbh: '',
    zzzsbh: '',
    zzjgdmz: '',
    zgslxdh: '',
    zyxjlfw: '',
    zjxjlfw: '',
    bdbgdz: '',
    bdgslxdh: '',
    ryxx: '',
    jgzcdz: '',
    baksq: '',
    bajsq: '',
    basj: '',
    jbr: '',
    njsj: '',
    fzr: '',
    fzrsj: '',
    jsfzr: '',
    jsfzrsj: '',
    remark: '',
    annex: '',
})


// 表单验证
const formRules = reactive<any>({
    org_name: [{
        required: true,
        message: '请输入机构名称',
        trigger: ['blur']
    }],
    filing_type: [{
        required: true,
        message: '请输入备案类型',
        trigger: ['blur']
    }],
    fddbr: [{
        required: true,
        message: '请输入法定代表人',
        trigger: ['blur']
    }],
    telephone: [{
        required: true,
        message: '请输入联系电话',
        trigger: ['blur']
    }],
    yyzzbh: [{
        required: true,
        message: '请输入营业执照编号',
        trigger: ['blur']
    }],
    zzzsbh: [{
        required: true,
        message: '请输入资质证书编号',
        trigger: ['blur']
    }],
    zzjgdmz: [{
        required: true,
        message: '请输入组织机构代码证',
        trigger: ['blur']
    }],
    zgslxdh: [{
        required: true,
        message: '请输入总公司联系电话',
        trigger: ['blur']
    }],
    zyxjlfw: [{
        required: true,
        message: '请输入主项监理范围',
        trigger: ['blur']
    }],
    zjxjlfw: [{
        required: true,
        message: '请输入增项监理范围',
        trigger: ['blur']
    }],
    bdbgdz: [{
        required: true,
        message: '请输入本地办公地址',
        trigger: ['blur']
    }],
    bdgslxdh: [{
        required: true,
        message: '请输入本地公司联系电话',
        trigger: ['blur']
    }],
    ryxx: [{
        required: true,
        message: '请输入人员信息',
        trigger: ['blur']
    }],
    jgzcdz: [{
        required: true,
        message: '请输入机构注册地址',
        trigger: ['blur']
    }],
    baksq: [{
        required: true,
        message: '请输入备案开始期',
        trigger: ['blur']
    }],
    bajsq: [{
        required: true,
        message: '请输入备案结束期',
        trigger: ['blur']
    }],
    basj: [{
        required: true,
        message: '请输入备案时间',
        trigger: ['blur']
    }],
    jbr: [{
        required: true,
        message: '请输入经办人',
        trigger: ['blur']
    }],
    njsj: [{
        required: true,
        message: '请输入年检时间',
        trigger: ['blur']
    }],
    fzr: [{
        required: true,
        message: '请输入负责人',
        trigger: ['blur']
    }],
    fzrsj: [{
        required: true,
        message: '请输入负责人手机',
        trigger: ['blur']
    }],
    jsfzr: [{
        required: true,
        message: '请输入技术负责人',
        trigger: ['blur']
    }],
    jsfzrsj: [{
        required: true,
        message: '请输入技术负责人手机',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }],
    annex: [{
        required: true,
        message: '请输入',
        trigger: ['blur']
    }]
})


// 获取详情
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 apiMarketingBranchFilingDetail({
        id: row.id
    })
    setFormData(data)
}


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