<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
            #foreach ($column in $columns)
            #if($column.isEdit)
            #if($table.treeParent!="" && $column.javaField==$table.treeParent)
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-tree-select
                        class="flex-1"
                        v-model="formData.${column.javaField}"
                        :data="treeList"
                        clearable
                        node-key="${table.treePrimary}"
                        :props="{label: ${table.treeName}, value: ${table.treePrimary}, children: 'children'}"
                        :default-expand-all="true"
                        placeholder="请选择${column.columnComment}"
                        check-strictly
                    />
                </el-form-item>
            #elseif($column.htmlType=="input")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-input v-model="formData.${column.javaField}" placeholder="请输入${column.columnComment}" />
                </el-form-item>
            #elseif($column.htmlType=="number")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-input-number v-model="formData.${column.javaField}" />
                </el-form-item>
            #elseif($column.htmlType=="textarea")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-input
                        v-model="formData.${column.javaField}"
                        placeholder="请输入${column.columnComment}"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
            #elseif($column.htmlType=="checkbox")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-checkbox-group v-model="formData.${column.javaField}" placeholder="请选择${column.columnComment}">
                        #if($column.dictType!="")
                        <el-checkbox
                            v-for="(item, index) in dictData.${column.dictType}"
                            :key="index"
                            :label="item.value"
                            :disabled="!item.status"
                        >
                            {{ item.name }}
                        </el-checkbox>
                        #else
                        <el-checkbox>请选择字典生成</el-checkbox>
                        #end
                    </el-checkbox-group>
                </el-form-item>
            #elseif($column.htmlType=="select")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-select v-model="formData.${column.javaField}" placeholder="请选择${column.columnComment}">
                        #if($column.dictType!="")
                        <el-option
                            v-for="(item, index) in dictData.${column.dictType}"
                            :key="index"
                            :label="item.name"
                            #if($column.javaType == "Integer")
                            :value="parseInt(item.value)"
                            #else
                            :value="item.value"
                            #end
                            :disabled="!item.status"
                        />
                        #else
                        <el-option label="请选择字典生成" value="" />
                        #end
                    </el-select>
                </el-form-item>
            #elseif($column.htmlType=="radio")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-radio-group v-model="formData.${column.javaField}" placeholder="请选择${column.columnComment}">
                        #if($column.dictType!="")
                        <el-radio
                            v-for="(item, index) in dictData.${column.dictType}"
                            :key="index"
                            #if($column.javaType == "Integer")
                            :label="parseInt(item.value)"
                            #else
                            :label="item.value"
                            #end
                            :disabled="!item.status"
                        >
                            {{ item.name }}
                        </el-radio>
                        #else
                        <el-radio label="0">请选择字典生成</el-radio>
                        #end
                    </el-radio-group>
                </el-form-item>
            #elseif($column.htmlType=="datetime")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <el-date-picker
                        v-model="form.${column.javaField}"
                        type="date"
                        clearable
                        value-format="YYYY-MM-DD"
                        placeholder="请选择${column.columnComment}"
                    />
                </el-form-item>
            #elseif($column.htmlType=="editor")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <editor v-model="formData.${column.javaField}" :height="500" />
                </el-form-item>
            #elseif($column.htmlType=="imageUpload")
                <el-form-item label="${column.columnComment}" prop="${column.javaField}">
                    <material-picker v-model="formData.${column.javaField}" />
                </el-form-item>
            #end
            #end
            #end
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
    #if($table.treePrimary && $table.treeParent)
    ${moduleName}Lists,
    #end
    ${moduleName}Edit,
    ${moduleName}Add,
    ${moduleName}Detail
} from '@/api/${moduleName}'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
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>>()
#if($table.treePrimary && $table.treeParent)
const treeList = ref<any[]>([])
#end
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑${functionName}' : '新增${functionName}'
})

const formData = reactive({
    #foreach ($column in $columns)
    #if($column.javaField==$primaryKey)
    ${primaryKey}: '',
    #elseif($column.isEdit)
    #if($column.htmlType=="checkbox")
    ${column.javaField}: [],
    #else
    ${column.javaField}: '',
    #end
    #end
    #end
})

const formRules = {
    #foreach ($column in $columns)
    #if($column.isEdit && $column.isRequired)
    ${column.javaField}: [
        {
            required: true,
            #if($column.htmlType=="checkbox" || $column.htmlType=="datetime" || $column.htmlType=="radio" || $column.htmlType=="select")
            message: '请选择${column.columnComment}',
            #else
            message: '请输入${column.columnComment}',
            #end
            trigger: ['blur']
        }
    ],
    #end
    #end
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = {...formData}
    #foreach ($column in $columns)
    #if($column.htmlType == "checkbox")
    data.${column.javaField} = data.${column.javaField}.join(",");
    #end
    #end
    mode.value == 'edit' ? await ${moduleName}Edit(data) : await ${moduleName}Add(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (row: Record<string, any>) => {
    const data = await ${moduleName}Detail({
        ${primaryKey}: row.${primaryKey}
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
            #foreach ($column in $columns)
            #if($column.htmlType == "checkbox")
            //@ts-ignore
            formData.${column.javaField} = String(data.${column.javaField}).split(',')
            #end
            #end
        }
    }
}

const handleClose = () => {
    emit('close')
}
#if($table.treePrimary && $table.treeParent)

// 树表
const getLists = async () => {
    const data: any = await ${moduleName}List()
    const item = { ${table.treePrimary}: 0, ${table.treeName}: '顶级', children: [] }
    item.children = data
    treeList.value.push(item)
}

getLists()
#end

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