<template>
    <!-- 对话框表单 -->
    <el-dialog
        custom-class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? baTable.form.operate=='edit'?'编辑' : '添加':'无标题'}}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem
                        label="父级组别"
                        v-model="baTable.form.items!.pid"
                        type="remoteSelect"
                        :input-attr="{
                            params: { isTree: true },
                            field: 'name',
                            'remote-url': baTable.api.actionUrl.get('select'),
                            placeholder: '请选择父级组别',
                            showRefresh:true
                        }"
                    />

                    <el-form-item prop="name" label="组别名称">
                        <el-input
                            v-model="baTable.form.items!.name"
                            type="string"
                            placeholder="请输入组别名称"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="auth" label="权限">
                        <el-tree
                            ref="treeRef"
                            :key="state.treeKey"
                            :default-checked-keys="state.defaultCheckedKeys"
                            :default-expand-all="true"
                            show-checkbox
                            node-key="id"
                            :props="{ children: 'children', label: 'title', class: treeNodeClass }"
                            :data="state.menuRules"
                        />
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-radio v-model="baTable.form.items!.status" label="0" :border="true">启用</el-radio>
                        <el-radio v-model="baTable.form.items!.status" label="1" :border="true">禁用</el-radio>
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">取消</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? '保存并编辑下一项' : '保存' }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, watch, inject } from 'vue'

import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { getMenuRules } from '/@/api/backend/auth/group'
import type { ElForm, ElTree, FormItemRule } from 'element-plus'
import { uuid } from '/@/utils/random'
import type Node from 'element-plus/es/components/tree/src/model/node'

interface MenuRules {
    id: number
    title: string
    children: MenuRules[]
}

const treeRef = ref<InstanceType<typeof ElTree>>()
const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass



const state: {
    treeKey: string
    defaultCheckedKeys: number[]
    menuRules: MenuRules[]
} = reactive({
    treeKey: uuid(),
    defaultCheckedKeys: [],
    menuRules: [],
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [
        {
            required: true,
            message: '请输入组别名称',
            trigger: 'blur',
        },
    ],
    auth: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                let ids = getCheckeds()
                if (ids.length <= 0) {
                    return callback(new Error(t('Please select field', { field: t('auth.group.jurisdiction') })))
                }
                return callback()
            },
        },
    ],
})

getMenuRules().then((res) => {
    state.menuRules = res.data.list
})

const getCheckeds = () => {
    return treeRef.value!.getCheckedKeys().concat(treeRef.value!.getHalfCheckedKeys())
}

const getHalfCheckeds = () => {
    return treeRef.value!.getHalfCheckedKeys()
}

const treeNodeClass = (data: anyObj, node: Node) => {

    if (node.isLeaf){
        return ''
    }
    let addClass = true
    for (const key in node.childNodes) {
        if (!node.childNodes[key].isLeaf) {
            addClass = false
        }
    }
    return addClass ? 'penultimate-node' : ''
}

defineExpose({
    getCheckeds,
    getHalfCheckeds,
})

watch(
    () => baTable.form.items!.arrayRules,
    () => {
        if (baTable.form.items!.arrayRules && baTable.form.items!.arrayRules.length) {
             state.defaultCheckedKeys = baTable.form.items!.arrayRules
        } else {
            state.defaultCheckedKeys = []
        }
        state.treeKey = uuid()
    }
)
</script>

<style scoped lang="scss">
:deep(.penultimate-node) {
    .el-tree-node__children {
        padding-left: 60px;
        white-space: pre-wrap;
        line-height: 12px;
        .el-tree-node {
            display: inline-block;
        }
        .el-tree-node__content {
            padding-left: 5px !important;
            padding-right: 5px;
            .el-tree-node__expand-icon {
                display: none;
            }
        }
    }
}
</style>
