<template>
    <SearchRow :title="t('permission.title')" :del="false" @query="query" @add="addPermission">
        <NoobInput v-model="example.code" :placeholder="t('permission.example.0')"></NoobInput>
    </SearchRow>
    <ListTable @query="query" :props="props" :example="example" :page="true" :data="result" rowKey="id"
        :treeProps="{ children: 'children' }">
        <template #status="{ row }">
            <div slot="reference" class="name-wrapper">
                <NoobTag v-if="row.status == 'A'">
                    {{ t('base.active') }}
                </NoobTag>
                <NoobTag v-else type="danger">
                    {{ t('base.inactive') }}
                </NoobTag>
            </div>
        </template>
        <template #action="{ row }">
            <TableAction :add="row.parent == null" @modify="modify(row)" @del="delate(row)" @add="addPermission(row)" />
        </template>
    </ListTable>

    <el-dialog :title="t('base.add') + t('permission.name')" v-model="flag.modify" :close-on-click-modal="false" top="15vh"
        width="40%" @keydown.enter.native="confirm">
        <ModifyForm ref="form" :param="permission" :rules="rules" :items="items" @confirm="confirm" :width="100"
            @cancel="flag.modify = false">
            <template #id>
                <NoobInput v-model="permission.id" full :placeholder="t('rule.pleaseEnter') + t('permission.prop.1')"
                    :disabled="!flag.add" />
            </template>
            <template #perOrder>
                <NoobSelect v-model="permission.perOrder" full :max-value="99"
                    :placeholder="t('rule.pleaseSelect') + t('permission.prop.4')" />
            </template>
        </ModifyForm>
    </el-dialog>
</template>

<script lang="ts" setup>
import { useStore } from "vuex";
import { reactive, onMounted, ref } from "vue";
import { Api, PageExample, PageResult, ListTable, SearchRow, NoobInput, TableAction, NoobTag, NoobSelect, ModifyForm, Element } from "noob-mengyxu";
import { useI18n } from "vue3-i18n";
const { t } = useI18n();
const { SimpleRequired } = Element;
const { state, dispatch } = useStore();
const { list, add, set, del } = Api.dictionary;
const result = ref(new PageResult());
const example = reactive<any>(new PageExample());
const flag = reactive({
    modify: false,
    add: false
})
const permission = ref<any>();
const form = ref();
const order = 99;
const nodeLevel = {
    module: 'group',
    group: 'view',
    view: 'action'
};

const props = [
    { i18n: 'permission.prop.0', code: 'name', width: 200, align: 'left', fixed: "left" },
    { i18n: 'permission.prop.1', code: 'id', width: 220 },
    { i18n: 'permission.prop.2', code: 'desc', slot: true, width: 80 },
    { i18n: 'permission.prop.3', code: 'perLevel', dict: 'per_level', width: 80 },
    { i18n: 'permission.prop.4', code: 'perOrder', width: 80 },
    { i18n: 'permission.prop.5', code: 'status', dict: 'able_status', width: 80 },
    { i18n: 'permission.prop.6', code: 'perContent', width: 230 },
    { i18n: 'permission.prop.7', code: 'action', slot: true, type: 'action', width: 180, fixed: 'right' },
]

const items = [
    { i18n: 'permission.prop.1', code: 'id', slot: true },
    { i18n: 'permission.parent', code: 'perParent', disabled: true },
    { i18n: 'permission.prop.3', code: 'perLevel', dict: 'per_level', disabled: true },
    { i18n: 'permission.prop.0', code: 'name' },
    { i18n: 'permission.prop.6', code: 'perContent' },
    { i18n: 'permission.prop.2', code: 'desc' },
    { i18n: 'permission.prop.4', code: 'perOrder', slot: true },
    { i18n: 'permission.prop.5', code: 'perStatus', dict: 'able_status' },
]

const rules = {
    id: [new SimpleRequired('dict.prop.1')],
    name: [new SimpleRequired('dict.prop.0')],
}

const query = () => {
    list(example).then((rsp: any) => result.value = rsp)
}

const addPermission = (row) => {
    permission.value = { perStatus: 'A' }
    if (row) {
        permission.value.perParent = row.id
        permission.value.perLevel = nodeLevel[row.perLevel]
    } else {
        permission.value.perLevel = 'group';
    }
    form.value?.clearValidate();
    flag.modify = true;
    flag.add = true;
}

const modify = (row) => {
    permission.value = JSON.parse(JSON.stringify(row));
    form.value?.clearValidate();
    flag.modify = true;
    flag.add = false;
}

const delate = (row) => {
    Element.confirm(t('dict.delete.0'), t('dict.delete.1')).then(() => {
        del(row).then(query);
    })
}

const confirm = () => {
    const api = flag.add ? add : set;
    api(permission.value).then(rsp => {
        if (rsp) {
            query();
            flag.modify = false;
        }
    })
}

onMounted(() => {
    dispatch('getDictMap', ['per_level', 'able_status'])
    query();
});
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
</style>