<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="num">
                            <el-input v-model="formData.num" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project">
                            <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_num">
                            <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">

                        <el-form-item label="部门负责人" prop="bm_master">
                            <el-input v-model="formData.bm_master" clearable placeholder="请选择部门负责人" @click="userclick()" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="委托单位" prop="wt_unit">
                            <el-input v-model="formData.wt_unit" clearable placeholder="请输入委托单位" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目负责人" prop="xm_master">
                            <el-input v-model="formData.xm_master" clearable placeholder="请输入项目负责人" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="实际开始日期" prop="start_date">
                            <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">
                            <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="duration">
                            <el-input v-model="formData.duration" clearable placeholder="请输入工期（天）" type="number" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="工时" prop="working_hours">
                            <el-input v-model="formData.working_hours" clearable placeholder="请输入工时" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计已完成进度" prop="done_progress">
                            <el-input v-model="formData.done_progress" clearable placeholder="请输入累计已完成进度（%）"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本阶段完成进度(%)" prop="bjd_progress">
                            <el-input v-model="formData.bjd_progress" clearable placeholder="请输入本阶段完成进度(%)" type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="登记人" prop="djr">
                            <el-input v-model="formData.djr" clearable placeholder="请输入登记人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="登记时间" prop="apptime">
                            <el-date-picker class="flex-1 !flex" v-model="formData.apptime" 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="jdms">
                            <el-input v-model="formData.jdms" clearable placeholder="请输入形象进度描述" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="datas">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="fd_23">
                            <el-input v-model="formData.fd_23" clearable placeholder="请输入文本输入框" type="textarea" />
                        </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="jianliProjectProgressReportEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiJianliProjectProgressReportAdd, apiJianliProjectProgressReportEdit, } from '@/api/jianli_project_progress_report'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目进度汇报' : '新增项目进度汇报'
})

// 表单数据
const formData = reactive({
    id: '',
    dataid: '',
    num: '',
    project_id: '',
    project_name: '',
    project_num: '',
    bm_master: '',
    wt_unit: '',
    xm_master: '',
    start_date: '',
    end_date: '',
    duration: '',
    working_hours: '',
    done_progress: '',
    bjd_progress: '',
    djr: '',
    apptime: '',
    jdms: '',
    datas: '',
    fd_23: '',
    annex: [],
    projectInfo: {}
})

const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    formData.project_num = e.project_num
    formData.wt_unit = e.aunit
    showDialog.value = false
}

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

}


//确认
const submituser = (e: any) => {
    formData['bm_master'] = e.name
    // formData.project_change_visa_content[tableIndex.value][tableKey.value + '_id'] = e.id
    showDialog3.value = false
}


const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.end_date) < new Date(formData.start_date)) {
        callback(new Error('实际结束日期不能早于实际开始日期'))
    } else {
        callback()
    }
}



// 表单验证
const formRules = reactive<any>({
    end_date: [
        {
            validator: chekcDate,
            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.project_num = formData.projectInfo.project_num


}



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

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

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



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