<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="90px" :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-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="djrq">
                            <el-date-picker class="flex-1 !flex" v-model="formData.djrq" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </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>
                <div style="margin-bottom: 30px;">任务明细</div>
                <div style="margin-bottom: 30px;">
                    <el-table :data="formData.project_change_visa_content">
                        <template #empty>
                            <span class="cursor-pointer">暂无数据，点击添加 <el-button @click="handleAdd">+</el-button></span>
                        </template>
                        <el-table-column label="序号" v-if="mode == 'add'" width="150px">
                            <template #default="{ row }">
                                <el-button @click="handleAdd()">+</el-button>
                                <el-button @click="handleDelete(row)">-</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="所在目录" prop="level" width="200px">

                            <template #default="scope">
                                <el-input v-model="scope.row.directory" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                        <el-table-column label="单位名称" prop="unit_name" width="200px">

                            <template #default="{ row }">
                                <el-input v-model="row.unit_name" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                        <el-table-column label="时间" prop="time" width="200px">

                            <template #default="{ row }">
                                <el-date-picker class="flex-1 !flex" v-model="row.time" clearable type="date"
                                    value-format="YYYY-MM-DD" placeholder="请选择日期" :disabled="mode != 'add'">
                                </el-date-picker>
                            </template>
                        </el-table-column>
                        <el-table-column label="所属专业" prop="major" width="200px">

                            <template #default="{ row }">
                                <el-input v-model="row.major" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                        <el-table-column label="类别" prop="category" width="200px">

                            <template #default="scope">
                                <el-input v-model="scope.row.category" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                        <el-table-column label="审核内容" prop="review_content" width="200px">

                            <template #default="scope">
                                <el-input v-model="scope.row.review_content" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                        <el-table-column label="审核意见" prop="review_comments" width="200px">

                            <template #default="scope">
                                <el-input v-model="scope.row.review_comments" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                        <el-table-column label="审核人" prop="reviewer" width="200px">

                            <template #default="scope">
                                <el-input v-model="scope.row.reviewer" @click="userclick(scope.$index, 'reviewer')"
                                    :disabled="mode != 'add'" placeholder="点击选择" readonly />
                            </template>
                        </el-table-column>
                        <el-table-column label="施工单位申报值" prop="tc_amount" width="200px">

                            <template #default="{ row }">
                                <el-input v-model="row.declared_value" :disabled="mode != 'add'" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="咨询单位测算值" prop="calculated_value" width="200px">

                            <template #default="{ row }">
                                <el-input v-model="row.calculated_value" :disabled="mode != 'add'" type="number" />
                            </template>
                        </el-table-column>
                        <el-table-column label="备注" prop="remark" width="200px">

                            <template #default="{ row }">
                                <el-input v-model="row.remark" :disabled="mode != 'add'" />
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectChangeVisaEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectChangeVisaAdd, apiProjectChangeVisaEdit, } from '@/api/project_change_visa'
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: '',
    projectInfo: {},
    djr: '',
    djrq: '',
    project_change_visa_content: [
    ]

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

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

})

const tableIndex = ref(0)
const tableKey = ref('')
//打开弹窗
const userclick = async (e: any, key) => {
    tableIndex.value = e
    tableKey.value = key
    showDialog3.value = true
    await nextTick()
    personnel.value.open()

}

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

const handleAdd = () => {
    formData.project_change_visa_content.push({
        directory: '',
        unit_name: '',
        time: '',
        major: '',
        category: '',
        review_content: '',
        review_comments: '',
        reviewer: '',
        reviewer_id: '',
        declared_value: '',
        calculated_value: '',
        remark: '',
    })
}


const handleDelete = (row: any) => {
    const index = formData.project_change_visa_content.indexOf(row);
    formData.project_change_visa_content.splice(index, 1);
}


// 获取详情
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 getDetail = async (row: Record<string, any>) => {
    const data = await apiProjectChangeVisaDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectChangeVisaEdit(data)
        : await apiProjectChangeVisaAdd(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>
