<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            title="创建日程安排"
            :async="true"
            width="800px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form class="formdata" :model="detailsdt" label-width="120px">
                <el-form-item label="任务日期">
                    <el-date-picker v-model="formData.time" type="date" placeholder="选择日期" />
                </el-form-item>
                <el-form-item label="任务模板">
                    <el-input
                        v-model="formData.template_name"
                        @click="isShow = true"
                        clearable
                        placeholder="请输入任务模板"
                    />
                </el-form-item>
            </el-form>
        </popup>
        <el-dialog v-model="isShow" title="选择任务" width="60%">
            <DialogIndex @customEvent="customEvent" />
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="taskEidt">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTaskAdd } from '@/api/task'
import { reactive, onUpdated, type PropType } from 'vue'
import DialogIndex from '@/views/task_template/list_two.vue'
const route = useRoute()

const emit = defineEmits(['success', 'close'])
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const detailsdt = ref({})
const isShow = ref(false)

// 表单数据
const formData = reactive({
    time: '',
    template_id: '',
    scheduling_id: '',
    template_name: ''
})
function customEvent(data: any) {
    isShow.value = false
    formData.template_id = data.id
    formData.template_name = data.title
}
if (route.query.id) {
    formData.scheduling_id = route.query.id.toString()
}

// 提交按钮
const handleSubmit = async () => {
    const data = { ...formData }
    console.log(data)
    mode.value == 'edit' ? await apiTaskAdd(data) : await apiTaskAdd(data)
    popupRef.value?.close()
    emit('success')
}
//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

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

defineExpose({
    open
})
</script>
<style lang="scss" scoped>
.formdata {
    .el-form-item {
        .el-date-picker {
            width: 100%;
        }
    }
}
</style>
