<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">
                <!-- <div style="display: flex; flex-direction: row-reverse; justify-content: flex-start;margin-bottom: 30px;">

                    <el-select class="w-[180px]" v-model="formData.dept_id" clearable placeholder="请选择部门">
                        <el-option v-for="(item, index) in list2" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                    <el-select class="w-[180px]" 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>
                </div> -->


                <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 placeholder="请输入项目名称" />
                        </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="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="custom_name" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同编号" @click="showDialog1 = true">
                            <el-input v-model="contract_no" clearable placeholder="请选择合同" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="contract_name.length > 0">
                        <el-form-item label="合同名称" prop="contract_name">
                            <el-input v-model="contract_name" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目类型" prop="project_type_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.project_type_id" clearable placeholder="请选择项目类型">
                                <el-option v-for="(item, index) in protype" :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_manager" @click="userclick"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="manage_name" readonly clearable placeholder="请输入项目经理" />

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

                    <el-col :span="8">
                        <el-form-item label="项目金额" prop="amount"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.amount" clearable placeholder="请输入项目金额" />

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="立项日期" prop="project_approval_date">
                            <el-date-picker v-model="formData.project_approval_date" clearable type="date"
                                class="flex-1 !flex" 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 v-model="formData.start_date" clearable type="date" value-format="YYYY-MM-DD"
                                placeholder="选择项目开始日期" class="flex-1 !flex">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目交付日期" prop="delivery_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker v-model="formData.delivery_date" clearable type="date"
                                value-format="YYYY-MM-DD " placeholder="选择项目交付日期" class="flex-1 !flex">
                            </el-date-picker>

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

                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>


                    <el-col :span="24">

                        <el-form-item label="附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <div v-if="showDialog1">
                    <el-dialog v-model="showDialog1" title="选择合同" width="70%">
                        <salescontractDialog @customEvent="customEvent1" :project_id="formData.project_id">
                        </salescontractDialog>
                    </el-dialog>
                </div>


                <!-- <div v-if="showDialog2"> -->
                <el-dialog v-model="showDialog2" title="选择项目" width="70%">
                    <projectDialog @customEvent="customEvent2"></projectDialog>
                </el-dialog>
                <!-- </div> -->

                <personnelselector ref="personnel" @confirm="submituser" type="1"></personnelselector>
            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import salescontractDialog from '@/components/contract/index.vue'
import projectDialog from '@/components/project/index.vue'
import { appointmentAdd, appointmentEdit, appointmentDetail } from '@/api/appointment'
import type { PropType } from 'vue'
const protype = ref()
import { getAllProjectTypes } from '@/api/projecttype'

const contract_name = ref('')
const project_name = ref('')
const project_code = ref('')
const contract_no = ref('')
const custom_name = ref('')
const manage_name = ref('')
const personnel = ref<any>()


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 showDialog1 = ref(false)
const showDialog2 = ref(false)

const customEvent1 = (e: any) => {
    formData.contract_id = e.id;
    contract_name.value = e.contract_name;
    contract_no.value = e.contract_code
    showDialog1.value = false;
};

const customEvent2 = (e: any) => {
    formData.project_id = e.id;
    project_name.value = e.name;
    project_code.value = e.project_code;
    // formData.project_type_id = e.project_type;


    custom_name.value = e.custom_name;
    contract_name.value = ''
    contract_no.value = ''
    showDialog2.value = false;
};
//打开弹窗

const userclick = () => {
    personnel.value.open()

}
//确认
const submituser = (e: any) => {
    formData.project_manager = e.id;
    manage_name.value = e.name;
}




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

// 表单数据
const formData = reactive({
    id: '',
    org_id: "",
    dept_id: "",
    project_id: 0,
    contract_id: "",
    project_type_id: "",
    project_manager: '',
    amount: "",
    project_approval_date: "",
    start_date: "",
    delivery_date: "",
    remark: "",
    annex: [],
})


// 表单验证
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]
        }
    }
    custom_name.value = data.custom_name
    project_name.value = data.project_name
    project_code.value = data.project_code
    contract_no.value = data.contract_code
    manage_name.value = data.project_manager_name

    if (data && data.contract_name) {
        contract_name.value = data.contract_name;

    }

}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()

    const data = { ...formData }
    mode.value == 'edit'
        ? await appointmentEdit(data)
        : await appointmentAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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

getAllProjectTypes().then(res => {
    protype.value = res
})


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