<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="title">
                    <el-input v-model="formData.title" clearable placeholder="请输入主题" />
                </el-form-item>
                <el-form-item label="创建人" prop="admin_id">
                    <el-input v-model="formData.admin_id" clearable placeholder="请输入创建人" />
                </el-form-item>
                <el-form-item label="任务角色" prop="task_role_id">
                    <el-select class="flex-1" v-model="formData.task_role_id" clearable placeholder="请选择任务角色">
                        <el-option 
                            v-for="(item, index) in dictData.create_task_template_select_role"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="任务类型" prop="type">
                    <el-input v-model="formData.type" clearable placeholder="请输入任务类型" />
                </el-form-item>
                <el-form-item label="一阶天数" prop="stage_day_one">
                    <el-input v-model="formData.stage_day_one" clearable placeholder="请输入一阶天数" />
                </el-form-item>
                <el-form-item label="第一阶段金额" prop="money">
                    <el-input v-model="formData.money" clearable placeholder="请输入第一阶段金额" />
                </el-form-item>
                <el-form-item label="一阶比例" prop="proportion_one">
                    <el-input v-model="formData.proportion_one" clearable placeholder="请输入一阶比例" />
                </el-form-item>
                <el-form-item label="二阶天数" prop="stage_day_two">
                    <el-input v-model="formData.stage_day_two" clearable placeholder="请输入二阶天数 0代表无天数" />
                </el-form-item>
                <el-form-item label="二阶比例" prop="proportion_two">
                    <el-input v-model="formData.proportion_two" clearable placeholder="请输入二阶比例" />
                </el-form-item>
                <el-form-item label="二阶段金额" prop="money_two">
                    <el-input v-model="formData.money_two" clearable placeholder="请输入二阶段金额" />
                </el-form-item>
                <el-form-item label="三阶段天数" prop="stage_day_three">
                    <el-input v-model="formData.stage_day_three" clearable placeholder="请输入三阶段天数" />
                </el-form-item>
                <el-form-item label="三阶段金额" prop="new_money_three">
                    <el-input v-model="formData.new_money_three" clearable placeholder="请输入三阶段金额" />
                </el-form-item>
                <el-form-item label="长期金额" prop="money_longtime">
                    <el-input v-model="formData.money_longtime" clearable placeholder="请输入长期金额" />
                </el-form-item>
                <el-form-item label="阶段类型" prop="types">
                    <el-input v-model="formData.types" clearable placeholder="请输入1循环2长期3单次" />
                </el-form-item>
                <el-form-item label="扩展字段" prop="extend">
                    <el-input v-model="formData.extend" clearable placeholder="请输入扩展字段" />
                </el-form-item>
                <el-form-item label="充值金额" prop="recharge">
                    <el-input v-model="formData.recharge" clearable placeholder="请输入充值金额" />
                </el-form-item>
                <el-form-item label="任务描述" prop="content">
                    <el-input v-model="formData.content" clearable placeholder="请输入任务描述" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="createTaskTemplateEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCreateTaskTemplateAdd, apiCreateTaskTemplateEdit, apiCreateTaskTemplateDetail } from '@/api/create_task_template'
import { timeFormat } from '@/utils/util'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑任务创建模板' : '新增任务创建模板'
})

// 表单数据
const formData = reactive({
    id: '',
    title: '',
    admin_id: '',
    task_role_id: '',
    type: '',
    stage_day_one: '',
    money: '',
    proportion_one: '',
    stage_day_two: '',
    proportion_two: '',
    money_two: '',
    stage_day_three: '',
    new_money_three: '',
    money_longtime: '',
    types: '',
    extend: '',
    recharge: '',
    content: '',
})


// 表单验证
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]
        }
    }
    
    
}

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


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