<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <!-- <el-form-item label="标识" prop="dataid">
                    <el-input v-model="formData.dataid" clearable placeholder="请输入" />
                </el-form-item> -->
                <el-form-item label="所属项目" prop="cost_project_id">
                    <el-select v-model="formData.cost_project_id" remote filterable :remote-method="queryCostProject"
                        :loading="loading" class="flex-1">
                        <el-option v-for="(item, index) in optionsData.projectList" :key="index" :label="item.projectinfo"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="任务编号" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入任务编号" v-type="'code'" />
                </el-form-item>
                <el-form-item label="任务名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入任务名称" />
                </el-form-item>
                <el-form-item label="上级类别" prop="superior_sort">
                    <el-select class="flex-1" v-model="formData.superior_sort" clearable placeholder="请选择上级类别">
                        <el-option v-for="(item, index) in dictData.superior_category" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="任务级次" prop="level">
                    <el-input v-model="formData.level" clearable placeholder="请输入任务级次" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="formData.remarks" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>

            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="taskTypeEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTaskTypeAdd, apiTaskTypeEdit, apiTaskTypeDetail } from '@/api/task_type'
import { useDictOptions } from "@/hooks/useDictOptions"
import type { PropType } from 'vue'
import { apiCostProjectDatas } from "@/api/cost_project";
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑任务类型' : '新增任务类型'
})

// 表单数据
const formData = reactive({
    id: '',
    dataid: '',
    cost_project_id: '',
    num: '',
    name: '',
    superior_sort: '',
    level: '',
    remarks: '',

})


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

})


const { optionsData } = useDictOptions<{
    projectList: any[]
}>({
    projectList: {
        api: apiCostProjectDatas,
    }
})


const loading = ref(false)

const queryCostProject = async (query: string) => {
    loading.value = true
    const projectList = await apiCostProjectDatas({
        name: query ?? ''
    })
    optionsData.projectList = projectList
    loading.value = false
}


// 获取详情
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.superior_sort = Number(formData.superior_sort)

}

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


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