<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="custom_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.custom_name" @click="showDialog = true" clearable
                                placeholder="请选择客户" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目类型" prop="project_type"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.project_type" clearable placeholder="请选择项目类型">
                                <el-option v-for="(item, index) in protype" :key="index" :label="item.name"
                                    :value="parseInt(item.id)" />
                            </el-select>
                        </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="project_code">
                            <el-input v-model="formData.project_code" clearable disabled placeholder="系统自动生成" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="项目内容" prop="project_content">
                            <el-checkbox-group v-model="formData.project_content" placeholder="请选择项目内容">
                                <el-checkbox v-for="(item, index) in dictData.project_content" :key="index"
                                    :label="item.value">
                                    {{ item.name }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目估算" prop="project_estimation">
                            <el-input v-model="formData.project_estimation" clearable placeholder="请输入项目估算" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目所在地" prop="project_address">
                            <el-input v-model="formData.project_address" clearable placeholder="请输入项目所在地" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="预计工期" prop="estimated_construction">
                            <el-input v-model="formData.estimated_construction" clearable placeholder="请输入预计工期" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="投标时间" prop="bidding_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.bidding_time" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择投标时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="招标方式" prop="bidding_method">
                            <el-radio-group v-model="formData.bidding_method" placeholder="请选择招标方式">
                                <el-radio v-for="(item, index) in dictData.bidding_method" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人" prop="contacts">
                            <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人职位" prop="position">
                            <el-input v-model="formData.position" clearable placeholder="请输入联系人职位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系电话" prop="telephone">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人部门" prop="department">
                            <el-input v-model="formData.department" clearable placeholder="请输入联系人部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="负责人" prop="person">
                            <el-input v-model="formData.person" clearable placeholder="请输入负责人" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="发现时间" prop="discovery_time"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.discovery_time" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择投标时间">
                            </el-date-picker>
                        </el-form-item>

                        <!-- <el-form-item label="投标时间" prop="bidding_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.bidding_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="information_sources"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.information_sources" clearable
                                placeholder="请选择信息来源">
                                <el-option v-for="(item, index) in dictData.information_sources" :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="construction_payment_method">
                            <el-input v-model="formData.construction_payment_method" clearable placeholder="请输入建设方付款方式" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="竞争对手" prop="competitor">
                            <el-input v-model="formData.competitor" clearable placeholder="请输入竞争对手" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="关系度" prop="relationship">
                            <el-radio-group v-model="formData.relationship" placeholder="请选择关系度">
                                <el-radio v-for="(item, index) in dictData.relationship" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目建设资金来源" prop="construction_funds_sources">
                            <el-radio-group v-model="formData.construction_funds_sources" placeholder="请选择项目建设资金来源">
                                <el-radio v-for="(item, index) in dictData.construction_funds_sources" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="建设方财务状况" prop="construction_financial_status">
                            <el-radio-group v-model="formData.construction_financial_status" placeholder="请选择建设方财务状况">
                                <el-radio v-for="(item, index) in dictData.construction_financial_status" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设方对我方认可度" prop="construction_recognition">
                            <el-radio-group v-model="formData.construction_recognition" placeholder="请选择建设方对我方认可度">
                                <el-radio v-for="(item, index) in dictData.construction_recognition" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="我方对建设方认可度" prop="my_construction_recognition">
                            <el-radio-group v-model="formData.my_construction_recognition" placeholder="请选择我方对建设方认可度">
                                <el-radio v-for="(item, index) in dictData.my_construction_recognition" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="战略意义" prop="strategic_significance">
                            <el-radio-group v-model="formData.strategic_significance" placeholder="请选择战略意义">
                                <el-radio v-for="(item, index) in dictData.strategic_significance" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属行业" prop="industry">
                            <el-radio-group v-model="formData.industry" placeholder="请选择所属行业">
                                <el-radio v-for="(item, index) in dictData.industry" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单位性质" prop="unit_nature">
                            <el-radio-group v-model="formData.unit_nature" placeholder="请选择单位性质">
                                <el-radio v-for="(item, index) in dictData.unit_nature" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                            @delFile="delFileFn" />
                    </el-col>
                </el-row>

            </el-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择客户" width="70%">
            <customDialog @customEvent="customEvent"></customDialog>
        </el-dialog>
    </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 { apiProjectAdd, apiProjectEdit, apiProjectDetail } from '@/api/project'
import { getAllProjectTypes } from '@/api/projecttype'
import type { PropType } from 'vue'
// 上传文件
import annexUpload from "@/components/annexUpload/index.vue"
const handleAvatarSuccess_four = (response: any) => {
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};
// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }
// <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
//                                 @delFile="delFileFn" />


const protype = reactive([])
const formDataannex = reactive([])
const list1 = reactive([])
const list2 = reactive([])
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
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 customEvent = (e: any) => {
    formData.custom_id = e.id;
    formData.custom_name = e.name;
    showDialog.value = false;
};
//获取所有组织
const getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)
        if (res.length > 0 && !formData.org_id) {
            formData.org_id = res[0].id
            deptAll({ 'org_id': res[0].id }).then((res) => {
                if (res.length > 0) {
                    Object.assign(list2, res)
                    formData.dept_id = res[0].id
                }

            })
        }
    })
}
//获取部门
const deptrmt = (e: any) => {
    formData.dept_id = ''
    getlist1(e)

}
//获取所有部门
const getlist1 = (id: any) => {
    deptAll({ 'org_id': id }).then((res) => {
        list2.splice(0, list2.length, ...res)
    })
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目表' : '新增项目表'
})

// 表单数据
const formData = reactive({
    id: '',
    dept_id: '',
    org_id: '',
    name: '',
    custom_id: '',
    custom_name: '',
    project_type: '',
    project_code: '',
    project_content: [],
    project_estimation: '',
    project_address: '',
    estimated_construction: '',
    bidding_time: '',
    bidding_method: '',
    contacts: '',
    position: '',
    telephone: '',
    department: '',
    person: '',
    relationship: '',
    discovery_time: '',
    information_sources: '',
    competitor: '',
    construction_funds_sources: '',
    construction_payment_method: '',
    construction_financial_status: '',
    construction_recognition: '',
    my_construction_recognition: '',
    strategic_significance: '',
    industry: '',
    unit_nature: '',
    annex: [],
    status: '',
})


// 表单验证
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]
        }
    }
    if (data.dept_id) {
        getlist1(data.org_id)
    }
    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)

    }
    //@ts-ignore
    data.project_content && (formData.project_content = String(data.project_content).split(","))

}

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


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item) => item.uri)
    }
    await formRef.value?.validate()
    const data = { ...formData, project_content: formData.project_content.join(",") }
    mode.value == 'edit'
        ? await apiProjectEdit(data)
        : await apiProjectAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

        protype.splice(0, protype.length, ...res);
    })
}

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



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