<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="140px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="机构名称" prop="org_name">
                            <el-input v-model="formData.org_name" clearable placeholder="请输入机构名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备案类型" prop="filing_type">
                            <el-select class='flex-1' v-model="formData.filing_type">
                                <el-option v-for="(item, index) in  dictData.filing_type " :key="index" :label="item.name"
                                    :value="parseInt(item.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="法定代表人">
                            <el-input v-model="formData.fddbr" clearable placeholder="请输入法定代表人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系电话" prop="telephone">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入联系电话" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="营业执照编号">
                            <el-input v-model="formData.yyzzbh" clearable placeholder="请输入营业执照编号" v-type="'code'" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质证书编号">
                            <el-input v-model="formData.zzzsbh" clearable placeholder="请输入资质证书编号" v-type="'code'" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="组织机构代码证">
                            <el-input v-model="formData.zzjgdmz" clearable placeholder="请输入组织机构代码证" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="总公司联系电话">
                            <el-input v-model="formData.zgslxdh" clearable placeholder="请输入总公司联系电话" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="主项监理范围">
                            <el-input v-model="formData.zyxjlfw" clearable placeholder="请输入主项监理范围" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="增项监理范围">
                            <el-input v-model="formData.zjxjlfw" clearable placeholder="请输入增项监理范围" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本地办公地址">
                            <el-input v-model="formData.bdbgdz" clearable placeholder="请输入本地办公地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本地公司联系电话" prop="bdgslxdh">
                            <el-input v-model="formData.bdgslxdh" clearable placeholder="请输入本地公司联系电话" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="人员信息">
                            <el-input v-model="formData.ryxx" clearable placeholder="请输入人员信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="机构注册地址" prop="jgzcdz">
                            <el-input v-model="formData.jgzcdz" clearable placeholder="请输入机构注册地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备案开始期" prop="baksq">
                            <!-- <el-input v-model="formData.baksq" clearable placeholder="请输入备案开始期" /> -->
                            <el-date-picker class="flex-1 !flex" v-model="formData.baksq" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择备案开始期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备案结束期" prop="bajsq">
                            <el-date-picker class="flex-1 !flex" v-model="formData.bajsq" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择备案结束期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备案时间" prop="basj">
                            <el-date-picker class="flex-1 !flex" v-model="formData.basj" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择备案时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="经办人">
                            <el-input v-model="formData.jbr" clearable placeholder="请输入经办人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="年检时间">
                            <el-date-picker class="flex-1 !flex" v-model="formData.njsj" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择年检时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="负责人">
                            <el-input v-model="formData.fzr" clearable placeholder="请输入负责人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="负责人手机" prop="fzrsj">
                            <el-input v-model="formData.fzrsj" clearable placeholder="请输入负责人手机" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="技术负责人">
                            <el-input v-model="formData.jsfzr" clearable placeholder="请输入技术负责人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="技术负责人手机" prop="jsfzrsj">
                            <el-input v-model="formData.jsfzrsj" clearable placeholder="请输入技术负责人手机" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件">
                            <upload-annex :form-data="formData"></upload-annex>
                        </el-form-item>


                    </el-col>

                </el-row>

            </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 type { PropType } from 'vue'
import { checkPhone } from '@/utils/validate'

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 checkDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.bajsq) < new Date(formData.baksq)) {
        callback(new Error('备案结束期不能早于备案开始期'))
    } else {
        callback()
    }
}


// 表单验证
const formRules = reactive<any>({
    org_name: [{
        required: true,
        message: '请输入机构名称',
        trigger: ['blur']
    }],
    filing_type: [{
        required: true,
        message: '请输入备案类型',
        trigger: ['blur']
    }],

    telephone: [
        {
            validator: checkPhone,
            trigger: ['blur']
        }
    ],

    zgslxdh: [{
        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']
    },
    {
        validator: checkDate,
        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: [{
        validator: checkPhone,
        trigger: ['blur']
    }],
    jsfzr: [{
        required: true,
        message: '请输入技术负责人',
        trigger: ['blur']
    }],
    jsfzrsj: [{
        validator: checkPhone,
        trigger: ['blur']
    }],
    remark: [{
        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>
