<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-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="请输入盖章次数" type="number" />
                        </el-form-item>
                    </el-col>
                    <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">
                            <el-select class="flex-1" v-model="formData.depar" clearable placeholder="用印部门">
                                <el-option v-for="(item, index) in deptList" :key="index" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <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="系统自动获取" disabled />
                        </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-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-col :span="8">
                        <el-form-item label="申请人" prop="applicant">
                            <el-input v-model="formData.applicant_name" @click="userclick('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_name" clearable placeholder="请输入盖章人"
                                @click="userclick('stampman')" />
                        </el-form-item>
                    </el-col>
                    <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_name" @click="userclick('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-col :span="8">
                        <el-form-item label="实际归还人" prop="returnee">
                            <el-input v-model="formData.returnee_name" @click="userclick('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 { cost_project } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    },
    deptList: Array
})
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑用章申请' : '新增用章申请'
})

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

})

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

let keys;

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

}


//确认
const submituser = (e: any) => {
    formData[keys] = e.id
    formData[keys + '_name'] = e.name
    showDialog3.value = false
}



const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.returndate) < new Date(formData.borrow)) {
        callback(new Error('印章借用日期不能早于结束借用日期'))
    } else {
        callback()
    }
}

const chekcDate1 = (rule: any, value: any, callback: any) => {
    if (new Date(formData.sjreturn) < new Date(formData.sjborrow)) {
        callback(new Error('实际归还日期不能早于实际借章日期'))
    } else {
        callback()
    }
}
// 表单验证
const formRules = reactive<any>({

    returndate: [{
        required: true,
        message: '请选择结束借用日期',
        trigger: ['blur']
    },
    {
        validator: chekcDate,
        trigger: ['blur']
    }],
    sjreturn: [{
        required: true,
        message: '请选择实际归还日期',
        trigger: ['blur']
    },
    {
        validator: chekcDate1,
        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]
        }
    }
    formData.project_id = formData.projectInfo.id
    formData.project_name = formData.projectInfo.project_name
    formData.principal = formData.projectInfo.principal


}

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()

}

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



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