import { addDialog } from '@/components/ReDialog/index';
import {h, ref} from 'vue';
import {message, messageBox} from '@/utils/message';
import DeleteBatchDialog from "@/components/RePureTableBar/DeleteBatchDialog.vue";
import { use${classUppercaseName}Store } from '@/store/modules${webBasePath}/${classLowercaseName}';
import ${classUppercaseName}Dialog from '../components/${classLowercaseName}-dialog.vue';
import type { ${classUppercaseName}Info } from '@/types${webBasePath}/${classLowercaseName}/${classLowercaseName}DataType';
import type { FormItemProps } from 'element-plus';

export interface FormInline {
  formInline?: () => ${classUppercaseName}Info;
}

export const formRef = ref();
// 删除ids
export const deleteIds = ref([]);
const ${classLowercaseName}Store = use${classUppercaseName}Store();

/** 搜索初始化${comment} */
export async function onSearch() {
        ${classLowercaseName}Store.loading = true;
    await ${classLowercaseName}Store.fetch${classUppercaseName}Page();
        ${classLowercaseName}Store.loading = false;
}

/** 添加${comment} */
export function onAdd() {
    addDialog({
        title: `添加${comment}`,
        width: '30%',
        props: {
            formInline: {
                #foreach($item in $columnInfoList)
    $!{item.columnName}: undefined,
                #end
            },
        },
        draggable: true,
        fullscreenIcon: true,
        closeOnClickModal: false,
        contentRenderer: () => h(${classUppercaseName}Dialog, {ref: formRef}),
        beforeSure: (done, {options}) => {
            const form = options.props.formInline as FormItemProps;
            formRef.value.formRef.validate(async (valid: any) => {
                if (!valid) return;

                const result = await ${classLowercaseName}Store.create${classUppercaseName}(form);
                if (!result) return;
                done();
                await onSearch();
            });
        },
    });
}

/** 更新${comment} */
export function onUpdate(row: any) {
    addDialog({
        title: `修改${comment}`,
        width: '30%',
        props: {
            formInline: {
                #foreach($item in $columnInfoList)
    $!{item.lowercaseName}: row.$!{item.lowercaseName},
                #end
            }
        },
        draggable: true,
        fullscreenIcon: true,
        closeOnClickModal: false,
        contentRenderer: () => h(${classUppercaseName}Dialog, {ref: formRef}),
        beforeSure: (done, {options}) => {
            const form = options.props.formInline as FormItemProps;
            formRef.value.formRef.validate(async (valid: any) => {
                if (!valid) return;

                const result = await ${classLowercaseName}Store.update${classUppercaseName}ById({...form, id: row.id});
                if (!result) return;
                done();
                await onSearch();
            });
        },
    });
}

/** 删除${comment} */
export const onDelete = async (row: any) => {
    const id = row.id;

    // 是否确认删除
    const result = await messageBox({
        title: "确认删除${comment}？",
        showMessage: false,
        confirmMessage: undefined,
        cancelMessage: "已取消",
    });
    if (!result) return;

    // 删除数据
    await ${classLowercaseName}Store.batchDelete${classUppercaseName}([id]);
    await onSearch();
};

/** 批量删除 */
export const onDeleteBatch = async () => {
    const ids = deleteIds.value;
    const formDeletedBatchRef = ref();

    addDialog({
        title: "批量删除${comment}",
        width: '30%',
        props: {formInline: {confirmText: ''}},
        draggable: true,
        fullscreenIcon: true,
        closeOnClickModal: false,
        contentRenderer: () => h(DeleteBatchDialog, {ref: formDeletedBatchRef}),
        beforeSure: (done, {options}) => {
            formDeletedBatchRef.value.formDeletedBatchRef.validate(async (valid: any) => {
                if (!valid) return;

                const text = options.props.formInline.confirmText.toLowerCase();
                if (text === 'yes' || text === 'y') {
                    // 删除数据
                    await ${classLowercaseName}Store.batchDelete${classUppercaseName}(ids);
                    await onSearch();

                    done();
                } else message("批量删除失败", {type: 'warning'});
            });
        },
    });
};