<template>
    <div class="edit-popup">

        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="auto" :rules="formRules">
                <el-row :gutter="10">

                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id" @click="showDialog2 = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_name" clearable readonly placeholder="请选择项目" :disabled="project" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="文档大类" prop="classify_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="large_document" clearable readonly placeholder="请输入文档大类" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="文档中类" prop="classify_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="middle_document" readonly clearable placeholder="请输入文档中类" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="文档编号" prop="document_no">
                            <el-input v-model="document_no" disabled clearable placeholder="系统自动生成" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="文档名称" prop="name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.name" clearable placeholder="请输入文档名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="版本号" prop="version">
                            <el-input v-model="formData.version" clearable placeholder="请输入版本号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="文档描述" prop="describe">
                            <el-input v-model="formData.describe" type="textarea" clearable placeholder="请输入文档描述" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="附件" prop="field127">
                            <el-upload
                                accept="doc, docx, xls, xlsx, ppt, pptx, pdf, txt, zip, rar, tar, jpg, png, gif, jpeg, webp, wmv, avi, mpg, mpeg, 3gp, mov, mp4, flv, f4v, rmvb, mkv"
                                class="upload-demo" :show-file-list="false" aria-hidden="true"
                                :headers="{ Token: userStore.token }" :action="base_url + '/upload/file'"
                                :on-success="handleAvatarSuccess_four" ref="upload">
                                <el-button type="primary">
                                    上传
                                </el-button>
                            </el-upload>

                            <div>
                                <div v-for="(item, index) in formDataannex" style="margin-left: 5px;display: block;">
                                    <a style="margin-left: 10px; color: #4a5dff; align-self: flex-start" :href="item.uri"
                                        target="_blank">{{ item.name }}</a>
                                    <span style="cursor: pointer;margin-left: 5px;" @click="delFileFn(index)">x</span>
                                </div>
                            </div>

                        </el-form-item>
                    </el-col>
                </el-row>

                <el-dialog v-model="showDialog" title="选择文档" width="70%">
                    <documentDialog @customEvent="customEvent"></documentDialog>
                </el-dialog>


                <el-dialog v-model="showDialog2" title="选择项目" width="70%">
                    <projectDialog @customEvent="customEvent2"></projectDialog>
                </el-dialog>
                <personnelselector ref="personnel" @confirm="submituser" type="1"></personnelselector>
            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import customDialog from '@/components/custom-dialog/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import documentDialog from '@/components/project_document/index.vue'
import projectDialog from '@/components/project/index.vue'
import { apiProjectdocumentAdd, apiProjectdocumentEdit, apiProjectdocumentDetail } from '@/api/project_document'
import { getAllProjectTypes } from '@/api/projecttype'
import { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";

const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const formDataannex = reactive([])
const document_no = ref('')
const project_name = ref('')
const project_code = ref('')
const large_document = ref('')
const middle_document = ref('')
const personnel = ref<any>()
// console.log(userInfo, '222222')
// 上传文件
const handleAvatarSuccess_four = (
    response,
    uploadFile
) => {
    if (response.code == 0) {
        ElMessage.error(response.msg);
        return;
    }
    formDataannex.push(
        { uri: response.data.uri, name: response.data.name }


    );
};

// 删除上传的文件
const delFileFn = (index: number) => {
    formDataannex.splice(index, 1)
}
let props = defineProps({
    project: Object
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: "",
    dept_id: "",
    classify_id: '',
    name: '',
    describe: '',
    version: "",
    start_time: "",
    annex: [],

})


const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog = ref(false)
const showDialog2 = ref(false)

const customEvent2 = (e: any) => {
    formData.project_id = e.id;
    project_name.value = e.name;
    project_code.value = e.project_code;
    showDialog2.value = false;
};
if (props.project) customEvent2(props.project);




const customEvent = (e: any) => {
    formData.classify_id = e.id;
    large_document.value = e.large_category;
    middle_document.value = e.middle_category;
    showDialog.value = false;
};





// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目文档表' : '新增项目文档表'
})




// 表单验证
const formRules = reactive<any>({

})



// 获取详情
const setFormData = async (data: Record<any, any>) => {
    if (data.annex && data.annex.length > 0) {

        const arry1 = data.annex.map((item: any, index: any) => {
            return {
                name: `文件${index + 1}`,
                uri: item
            };
        });
        Object.assign(formDataannex, arry1)

    }


    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }

    project_name.value = data.project_name
    project_code.value = data.project_code
    document_no.value = data.code
    large_document.value = data.large_category;
    middle_document.value = data.middle_category;


}

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


// 提交按钮
const handleSubmit = async () => {
    formData.annex = formDataannex.map((item: any) => item.uri)

    await formRef.value?.validate()

    const data = { ...formData }
    mode.value == 'edit'
        ? await apiProjectdocumentEdit(data)
        : await apiProjectdocumentAdd(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>
