<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit" @close="handleClose">
            <el-card>
                <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="考核类别" prop="type">
                                <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择考核类别">
                                    <el-option v-for="(item, index) in dictData.jxgl_check_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="name">
                                <el-input v-model="formData.name" clearable placeholder="请输入模版名称" readonly />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="创建人" prop="industry">
                                <el-input v-model="formData.admin_user" readonly />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>

            <el-card>
                <el-row>
                    <el-table :data="formData.detail">
                        <template #empty>
                            暂无数据，点击 <el-button @click="handleAdd" size="small">添加</el-button>
                        </template>
                        <el-table-column label="考核项" prop="progress" show-overflow-tooltip>
                            <template #default="{ row }">
                                <el-input v-model="row.admin_user" placeholder="请输入考核项" />
                            </template>
                        </el-table-column>
                        <el-table-column label="分数" prop="progress" show-overflow-tooltip>
                            <template #default="{ row }">
                                <el-input v-model="row.admin_user" placeholder="请输入分数" />
                            </template>
                        </el-table-column>
                        <el-table-column label="考核说明" prop="progress" show-overflow-tooltip>
                            <template #default="{ row }">
                                <el-input v-model="row.admin_user" placeholder="请输入考核说明" />
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" prop="progress" show-overflow-tooltip>
                            <template #default="{ row }">
                                <el-button @click="handleAdd" size="small">+</el-button>
                                <el-button @click="handleAdd" size="small">-</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-row>
            </el-card>
            <el-card>
                <el-descriptions :column="3" border>
                    <el-descriptions-item label="考核总分" label-align="left" align="left">
                        { formData.org_name }
                    </el-descriptions-item>
                </el-descriptions>
            </el-card>
        </popup>
    </div>
</template>
  
<script lang="ts" setup name="manageProjectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiManageProjectAdd, apiManageProjectEdit, apiManageProjectDetail } from '@/api/manage_project'
import type { PropType } from 'vue'
import { timeFormat } from '@/utils/util'
import useUserStore from "@/stores/modules/user";
import { apiCostApprovedProjectDetail } from "@/api/cost_approved_project";
import { marketing_custom } from "@/components/dialogTable/dialogTableConfig";


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 useStore = useUserStore().userInfo



// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑考核模版' : '新增考核模版'
})

// 表单数据
const formData = reactive({
    id: '',
    nature: '',
    name: '',
    admin: useStore.id,
    admin_user: useStore.name,
    detail: []

})

const handleAdd = () => {
    formData.detail.push({})
}


// 表单验证
const formRules = reactive<any>({
    project_name: [{
        required: true,
        message: '请输入项目名称',
        trigger: ['blur']
    }]
})

const setRules = () => {
    let lists = ['project_name']
    lists.forEach((item) => {
        formRules[item] = [{
            required: true,
            message: '不能为空',
            trigger: ['blur']
        }]
    })
}

setRules()


// 获取详情
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]
        }
    }


}


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

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

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


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