<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="120px" :rules="formRules" inline>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="project_name" clearable placeholder="请输入项目名称" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id">
                            <el-input v-model="project_code" clearable placeholder="请输入项目编码" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="姓名" prop="project_person_id" @click="showDialog1 = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="person_name" clearable placeholder="请选择项目人员" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="身份证号" prop="project_person_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="idcard" clearable placeholder="系统自动填写" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同类型" prop="contract_type"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.contract_type" clearable placeholder="请选择合同类型">
                                <el-option v-for="(item, index) in dictData.labor_contract_type" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同状态" prop="contract_status"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.contract_status" clearable placeholder="请选择合同状态">
                                <el-option v-for="(item, index) in dictData.labor_contract_status" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item> </el-col>


                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_title"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.contract_title" clearable placeholder="请选择合同名称">
                                <el-option v-for="(item, index) in dictData.labor_contract_name" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>

                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同签定日期" prop="signing_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.signing_date" 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="trial_start_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.trial_start_date" 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="per_daily_living">
                            <el-date-picker class="flex-1 !flex" v-model="formData.plan_start_date" 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="start_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.start_date" 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="end_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.end_date" 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="release_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.release_time" 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="release_reason">
                            <el-input v-model="formData.release_reason" clearable placeholder="请输入合同名称" />
                        </el-form-item> </el-col><el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" 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-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectDialog @customEvent="customEvent"></projectDialog>
        </el-dialog>
        <el-dialog v-model="showDialog1" title="选择项目人员" width="70%">
            <customDialog @customEvent="customEvent1" :project_id="formData.project_id"></customDialog>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { projectlaborcontractAdd, projectlaborcontractEdit, projectlaborcontractDetail } from '@/api/project_labor_contract'
import customDialog from '@/components/project_personnel/index.vue'
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 showDialog = ref(false)
const showDialog1 = ref(false)
const project_name = ref('')
const project_code = ref('')
const person_name = ref('')
const idcard = ref('')

import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const formDataannex = reactive([])
import feedback from '@/utils/feedback'

//获取值
const customEvent = (e) => {
    formData.project_id = e.id
    project_name.value = e.name
    project_code.value = e.project_code
    showDialog.value = false

}
const customEvent1 = (e) => {
    formData.project_person_id = e.id
    idcard.value = e.idcard
    person_name.value = e.name
    showDialog1.value = false

}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑劳动合同' : '新增劳动合同'
})
// 上传文件
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)
}
// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_person_id: '',
    contract_status: '',
    contract_type: '',
    contract_title: '',
    signing_date: '',
    trial_start_date: '',
    trial_end_date: '',
    start_date: '',
    end_date: '',
    release_time: '',
    release_reason: '',
    remark: '',
    annex: [],
})


// 表单验证
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
    idcard.value = data.person_idcard
    person_name.value = data.person_name

}

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


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item: any) => item.uri)
    }
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await projectlaborcontractEdit(data)
        : await projectlaborcontractAdd(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>
