<template>
    <div>
        <hc-dialog v-model="isShow" widths="90%" :padding="false" :title="`[${menuTitle}] 数据权限配置`" is-table :footer="false" @close="dialogClose">
            <hc-card>
                <template #header>
                    <div class="w-60">
                        <hc-search-input v-model="searchForm.scopeName" placeholder="请输入权限名称" @search="searchClick" />
                    </div>
                </template>
                <template #extra>
                    <el-button hc-btn type="primary" @click="addClick">新增</el-button>
                    <el-button hc-btn type="danger" @click="delClick">删除</el-button>
                </template>
                <hc-table
                    :column="tableColumn" :datas="tableData" :loading="tableLoading"
                    :is-index="false" is-check :check-style="{ width: 29 }"
                    @selection-change="tableCheckChange"
                >
                    <template #scopeType="{ row }">{{ getScopeTypeName(row) }}</template>
                    <template #action="{ row }">
                        <el-link type="warning" @click="editRowClick(row)">修改</el-link>
                        <el-link v-del-com:[delRowClick]="row" type="danger">删除</el-link>
                    </template>
                </hc-table>
                <template #action>
                    <hc-pages :pages="searchForm" @change="pageChange" />
                </template>
            </hc-card>
        </hc-dialog>

        <!-- 新增/修改 -->
        <hc-dialog v-model="isDialogShow" widths="50rem" is-footer-center :title="dialogTitle" @close="dialogFormClose">
            <el-form ref="formRef" :model="formModel" :rules="formRules" label-position="top" label-width="auto">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="权限名称：" prop="scopeName">
                            <el-input v-model="formModel.scopeName" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="权限编号：" prop="resourceCode">
                            <el-input v-model="formModel.resourceCode" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="权限字段：" prop="scopeColumn">
                            <el-input v-model="formModel.scopeColumn" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="规则类型：" prop="scopeType">
                            <el-select v-model="searchForm.scopeType" placeholder="选择规则类型" filterable clearable block>
                                <el-option v-for="item in scopeTypeData" :key="item.value" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="可见字段：" prop="scopeField">
                            <el-input v-model="formModel.scopeField" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="权限类名：" prop="scopeClass">
                            <el-input v-model="formModel.scopeClass" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col v-if="searchForm.scopeType === 5" :span="24">
                        <el-form-item label="规则值：">
                            <el-input v-model="formModel.scopeValue" :autosize="{ minRows: 5, maxRows: 5 }" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注说明：">
                            <el-input v-model="formModel.remark" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button hc-btn @click="dialogClose">取消</el-button>
                <el-button hc-btn type="primary" :loading="submitLoading" @click="dialogSubmit">提交</el-button>
            </template>
        </hc-dialog>
    </div>
</template>

<script setup>
import { nextTick, ref, watch } from 'vue'
import { arrToId, formValidate, getArrValue, isNullES } from 'js-fast-way'
import { getDictionaryData } from '~uti/tools'
import { HcDelMsg } from 'hc-vue3-ui'
import mainApi from '~api/authority/data'

const props = defineProps({
    mid: {
        type: [String, Number],
        default: '',
    },
    title: {
        type: [String, Number],
        default: '',
    },
})

//事件
const emit = defineEmits(['close'])

//双向绑定
const isShow = defineModel('modelValue', {
    default: false,
})

//监听内容
const menuId = ref(props.mid)
const menuTitle = ref(props.title)
watch(() =>[props.mid, props.title], ([id, title]) => {
    menuId.value = id
    menuTitle.value = title
}, { immediate: true, deep: true })

//监听显示
watch(isShow, (val) => {
    if (val) {
        getDataApi()
    }
})

//获取数据
const getDataApi = () => {
    if (isNullES(menuId.value)) return
    searchForm.value.menuId = menuId.value
    getScopeTypeData()
    searchClick()
}

//获取字典数据
const scopeTypeData = ref([])
const getScopeTypeData = async () => {
    scopeTypeData.value = await getDictionaryData('data_scope_type')
}
const getScopeTypeName = ({ scopeType }) => {
    if (isNullES(scopeType)) return '-'
    const item = scopeTypeData.value.find((item) => item.value === scopeType)
    if (isNullES(item)) return scopeType
    return item.label ?? scopeType
}

//搜索表单
const searchForm = ref({
    scopeName: null, menuId: '',
    current: 1, size: 30, total: 0,
})

//搜索
const searchClick = () => {
    searchForm.value.current = 1
    getTableData()
}

//分页
const pageChange = ({ current, size }) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}

//表格数据
const tableColumn = ref([
    { key: 'scopeName', name: '权限名称' },
    { key: 'resourceCode', name: '权限编号' },
    { key: 'scopeColumn', name: '权限字段' },
    { key: 'scopeType', name: '规则类型' },
    { key: 'scopeField', name: '可见字段' },
    { key: 'scopeClass', name: '权限类名' },
    { key: 'scopeValue', name: '规则值' },
    { key: 'remark', name: '备注' },
    { key: 'action', name: '操作', width: 90, align: 'center' },
])
const tableData = ref([])

//获取表格数据
const tableLoading = ref(true)
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { data } = await mainApi.page({
        ...searchForm.value,
        total: null,
    })
    tableLoading.value = false
    tableData.value = getArrValue(data?.records)
    searchForm.value.total = data?.total ?? 0
}

//表格被选择
const tableCheckKeys = ref([])
const tableCheckChange = (rows) => {
    tableCheckKeys.value = rows
}


//新增/修改 弹窗
const isDialogShow = ref(false)
const dialogTitle = ref('')

//表单
const formRef = ref(null)
const formModel = ref({})
const formRules = {
    scopeName: {
        required: true,
        trigger: 'blur',
        message: '请输入权限名称',
    },
    resourceCode: {
        required: true,
        trigger: 'blur',
        message: '请输入数据权限编号',
    },
    scopeColumn: {
        required: true,
        trigger: 'blur',
        message: '请输入数据权限编号',
    },
    scopeType: {
        required: true,
        trigger: 'blur',
        message: '请选择规则类型',
    },
    scopeField: {
        required: true,
        trigger: 'blur',
        message: '请输入数据权限可见的字段',
    },
    scopeClass: {
        required: true,
        trigger: 'blur',
        message: '请输入MybatisMapper对应方法的完整类名路径',
    },
}


//新增
const addClick = () => {
    dialogTitle.value = '新增数据权限'
    formModel.value = {}
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
    })
}

//修改
const editRowClick = (row) => {
    formModel.value = {}
    dialogTitle.value = '修改数据权限'
    formModel.value = { ...row }
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
    })
}


//提交表单
const submitLoading = ref(false)
const dialogSubmit = async () => {
    const formRes = await formValidate(formRef.value)
    if (!formRes) return false
    submitLoading.value = true
    //发起请求
    const { isRes } = await mainApi.submit(formModel.value)
    submitLoading.value = false
    if (!isRes) return
    dialogFormClose()
    window?.$message?.success('操作成功')
    getTableData().then()
}

//关闭弹窗
const dialogFormClose = () => {
    isDialogShow.value = false
    submitLoading.value = false
    formModel.value = {}
}

//删除
const delRowClick = async ({ item }, resolve) => {
    const { isRes } = await mainApi.del(item.id)
    resolve() //关闭弹窗
    if (!isRes) return
    window.$message.success('删除成功')
    getTableData().then()
}

//批量删除
const delClick = () => {
    const rows = tableCheckKeys.value
    if (rows.length <= 0) {
        window.$message.warning('请选择要删除的数据')
        return false
    }
    //确认删除菜单
    HcDelMsg(async (resolve) => {
        //发起请求
        const ids = arrToId(rows)
        const { isRes } = await mainApi.del(ids)
        resolve() //关闭弹窗
        if (!isRes) return
        window.$message.success('删除成功')
        getTableData().then()
    })
}

//关闭弹窗
const dialogClose = () => {
    isShow.value = false
    emit('close')
}
</script>
