<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="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="申请编号" prop="num">
                            <el-input v-model="formData.num" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章申请类型" prop="types">
                            <el-select class="flex-1" v-model="formData.types" clearable placeholder="请选择盖章申请类型">
                                <el-option v-for="(item, index) in dictData.apply_with_seal_type" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="文件类型" prop="filetype">
                            <el-select class="flex-1" v-model="formData.filetype" clearable placeholder="请选择文件类型">
                                <el-option v-for="(item, index) in dictData.apply_with_seal_file_type" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="骑缝盖章" prop="ridingseam">
                            <el-radio-group v-model="formData.ridingseam" placeholder="请选择骑缝盖章">
                                <el-radio v-for="(item, index) in dictData.isqfgz" :key="index" :label="item.value">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="印章名称" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入印章名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章次数" prop="frequency">
                            <el-input v-model="formData.frequency" clearable placeholder="请输入盖章次数" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="限制次数" prop="limit">
                            <el-input v-model="formData.limit" clearable placeholder="请输入限制次数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="组织" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.org_id" clearable placeholder="请选择组织"
                                @change="deptrmt">
                                <el-option v-for="(item, index) in list1" :key="index" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="用印部门" prop="depar"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" label-width="100px">
                            <el-select class="flex-1" v-model="formData.depar" clearable placeholder="用印部门">
                                <el-option v-for="(item, index) in list2" :key="index" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="关联项目" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目负责人" prop="project_director">
                            <el-input v-model="formData.project_director" clearable placeholder="请输入项目负责人" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="盖章内容" prop="content">
                            <el-input v-model="formData.content" clearable placeholder="请输入盖章内容" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="印章是否外借" prop="loan">
                            <el-radio-group v-model="formData.loan" placeholder="请选择骑缝盖章">
                                <el-radio label="1">
                                    是
                                </el-radio>
                                <el-radio label="0">
                                    否
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="印章借用日期" prop="borrow">
                            <el-date-picker class="flex-1 !flex" v-model="formData.borrow" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择印章借用日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="结束借用日期" prop="returndate">
                            <el-date-picker class="flex-1 !flex" v-model="formData.returndate" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择结束借用日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="申请人" prop="applicant">
                            <el-input v-model="formData.applicant" clearable placeholder="请输入申请人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="申请日期" prop="date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择申请日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="盖章人" prop="stampman">
                            <el-input v-model="formData.stampman" clearable placeholder="请输入盖章人"
                                @click="userclick('stampman')" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="实际借章日期" prop="sjborrow">
                            <el-date-picker class="flex-1 !flex" v-model="formData.sjborrow" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择实际借章日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实际借章人" prop="sjborrower">
                            <el-input v-model="formData.sjborrower" clearable placeholder="请输入实际借章人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实际归还日期" prop="sjreturn">
                            <el-date-picker class="flex-1 !flex" v-model="formData.sjreturn" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择实际归还日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="实际归还人" prop="returnee">
                            <el-input v-model="formData.returnee" clearable placeholder="请输入实际归还人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable :config="cost_project" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>
                <div v-if="showDialog3">
                    <personnelselector ref="personnel" @confirm="submituser" type="1">
                    </personnelselector>
                </div>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="applyWithSealEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiApplyWithSealAdd, apiApplyWithSealEdit, apiApplyWithSealDetail } from '@/api/apply_with_seal'
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
import { cost_project } 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 showDialog3 = ref(false)
const personnel = ref<any>()
const list1 = reactive([])
const list2 = reactive([])

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑用章申请' : '新增用章申请'
})

// 表单数据
const formData = reactive({
    id: '',
    dataid: '',
    num: '',
    types: '',
    filetype: '',
    ridingseam: '',
    name: '',
    frequency: '',
    limit: '',
    org_id: "",
    depar: '',
    stampman: '',
    project_id: '',
    project_name: '',
    project_director: '',
    content: '',
    loan: '',
    borrow: '',
    returndate: '',
    applicant: '',
    date: '',
    sjborrow: '',
    sjborrower: '',
    sjreturn: '',
    returnee: '',
    annex: [],
    projectInfo: {}

})

const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    formData.principal = e.contract.project_director
    showDialog.value = false
}

const tableKey = ref('')
//打开弹窗
const userclick = async (key) => {
    tableKey.value = key
    showDialog3.value = true
    await nextTick()
    personnel.value.open()

}
//确认
const submituser = (e: any) => {
    formData[tableKey.value] = e.name
    showDialog3.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]
        }
    }
    formData.project_id = formData.projectInfo.id
    formData.project_name = formData.projectInfo.project_name
    formData.principal = formData.projectInfo.principal
    getlist1(formData.depar)


}
//获取所有组织
const getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)

    })
}
//获取部门
const deptrmt = (e: any) => {
    formData.depar = ''
    getlist1(e)

}
//获取所有部门
const getlist1 = (id: any) => {
    deptAll({ 'org_id': id }).then((res) => {
        list2.splice(0, list2.length, ...res)
    })
}
const getDetail = async (row: Record<string, any>) => {
    const data = await apiApplyWithSealDetail({
        id: row.id
    })
    setFormData(data)
}


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

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

}

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



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