<template>
    <el-card shadow="never" class="list-card">
        <template #header>
            <div class="search" v-show="showSearch">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
                    <el-form-item prop="value">
                        <el-input v-model="queryParams.name" placeholder="请输入值" clearable style="width: 240px"
                                  @keyup.enter="handleQuery"/>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </template>

        <Table ref="dictDetailTable"
               :tableParams="tableParams"
               :btnRole="leftBtn"
               @handleAdd="handleAdd"
               @handleEdit="handleUpdate"
               @selectionChange="handleSelectionChange"
        >
        </Table>
    </el-card>
    <!-- 添加或修改字典详情对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
        <el-form ref="dictDetailFormRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="文本" prop="label">
                <el-input v-model="form.label" placeholder="请输入文本"/>
            </el-form-item>
            <el-form-item label="值" prop="value">
                <el-input v-model="form.value" placeholder="请输入值"/>
            </el-form-item>

        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup name="DictDetail" lang="ts">
import {listDictDetail, getDictDetail, delDictDetail, addDictDetail, updateDictDetail, enabledDetail} from '@/api/system/dictDetail';
import {DictDetailVO, DictDetailQuery, DictDetailForm} from '@/api/system/dictDetail/types';
import auth from '@/plugins/auth'
import Table from '@/components/Table/index.vue'
import {tableTypes} from '@/components/Table/types'



const {proxy} = getCurrentInstance() as ComponentInternalInstance;

const props = defineProps({
    pDictId: {
        type: [String, Number],
        default: undefined
    }

})

const dictDetailList = ref<DictDetailVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);

const leftBtn = ref([
    { type: 'add', hasPermi: `system:dictDetail:add` },
    { type: 'edit', hasPermi: `system:dictDetail:edit` },
    { type: 'remove', hasPermi: `system:dictDetail:remove` },
    { type: 'import', hasPermi: `system:dictDetail:import` },
    { type: 'export', hasPermi: `system:dictDetail:export` }
])
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const dictDetailFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
    visible: false,
    title: ''
});


const tableParams = reactive<tableTypes<DictDetailQuery, DictDetailVO>>({
    tableColumn: [
        {show: true, label: '主键ID', prop: 'dictDetailId', sortable: false},
        {show: true, label: '字典id', prop: 'dictId', sortable: false},
        {show: true, label: '文本', prop: 'label', sortable: false},
        {show: true, label: '值', prop: 'value', sortable: false},
        {
            show: true, label: '是否启用', prop: 'enabled', render: ({column}: { column: DictDetailVO }) => {
                return [
                    h(ElSwitch, {
                        modelValue: column.enabled, onChange: async (v) => {
                            await enabledDetail({id: column.dictDetailId, enable: v})
                            proxy?.$modal.msgSuccess("修改成功");
                            handleQuery()
                        }
                    }),
                ]
            }
        },

        {
            show: true,
            label: '操作',
            width: 200,
            prop: 'operation',
            disabledMove: true,
            render: ({column}: { column: DictDetailVO }) => {
                return [ ]
            }
        },
    ],
    queryParams: {
        label: undefined,
        value: undefined,
        name: undefined,
        extField: undefined,
        ownerBy: undefined,
        isDelete: undefined,
        enabled: undefined,
        sort: undefined,
        dictId: props.pDictId
    },
    keyId: 'dictDetailId',
    listFn: listDictDetail,
    delFn: delDictDetail,
})


const initFormData: DictDetailForm = {
    dictDetailId: undefined,
    dictId: props.pDictId,
    label: undefined,
    value: undefined,
    extField: undefined,
    ownerBy: undefined,
    isDelete: undefined,
    enabled: undefined,
    sort: undefined
}
const data = reactive<PageData<DictDetailForm, DictDetailQuery>>({
    form: {...initFormData},
    rules: {
        dictDetailId: [
            {required: true, message: "主键ID不能为空", trigger: "blur"}
        ],
        dictId: [
            {required: true, message: "字典id不能为空", trigger: "blur"}
        ],
        label: [
            {required: true, message: "文本不能为空", trigger: "blur"}
        ],
        value: [
            {required: true, message: "值不能为空", trigger: "blur"}
        ],
        extField: [
            {required: true, message: "拥有者不能为空", trigger: "blur"}
        ],
        ownerBy: [
            {required: true, message: "拥有者不能为空", trigger: "blur"}
        ],
        isDelete: [
            {required: true, message: "是否删除：0未删除，1已删除不能为空", trigger: "blur"}
        ],
        enabled: [
            {required: true, message: "是否启用:1:启用,0:不启用不能为空", trigger: "blur"}
        ],
        sort: [
            {required: true, message: "显示顺序不能为空", trigger: "blur"}
        ]
    }
});

const {form, rules} = toRefs(data);

const {queryParams} = toRefs(tableParams);

/** 取消按钮 */
const cancel = () => {
    reset();
    dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
    form.value = {...initFormData};
    dictDetailFormRef.value?.resetFields();
}

const dictDetailTable = ref()
/** 搜索按钮操作 */
const handleQuery = () => {
    dictDetailTable.value.getData()
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: DictDetailVO[]) => {
    ids.value = selection.map(item => item.dictDetailId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "添加字典详情";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: DictDetailVO) => {
    reset();
    // const _dictDetailId = row?.dictDetailId || ids.value[0]
    // const res = await getDictDetail(_dictDetailId);
    Object.assign(form.value, row);
    dialog.visible = true;
    dialog.title = "修改字典详情";
}
const getList = () => {
    tableParams.queryParams.dictId = props.pDictId
    handleQuery()
    // dictDetailTable.value.getData()
}

const remove = ()=> {


}

/** 提交按钮 */
const submitForm = () => {
    dictDetailFormRef.value?.validate(async (valid: boolean) => {
        if (valid) {
            buttonLoading.value = true;
            form.value.dictId = props.pDictId
            await addDictDetail(form.value).finally(() => buttonLoading.value = false);
            proxy?.$modal.msgSuccess("操作成功");
            dialog.visible = false;
            await getList();
        }
    });
}

defineExpose({
    getList
})
onMounted(() => {
});
</script>
