<template>
    <hc-new-dialog v-model="isShow" title="租户授权配置" widths="400px" is-footer-center @close="dialogClose">
        <el-form ref="formRef" :model="formModel" :rules="formRules" label-position="left" label-width="auto">
            <el-form-item label="产品包：">
                <el-select v-model="formModel.packageId" placeholder="选择搜索类型" filterable block @change="packageChange">
                    <el-option v-for="item in packageData" :key="item.id" :label="item.packageName" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="菜单列表：" prop="menuIds">
                <el-tree-select
                    v-model="formModel.menuIds" :data="menuData" :render-after-expand="false" node-key="id"
                    :props="menuTreeProps" multiple show-checkbox clearable block placeholder="请选择菜单列表"
                />
            </el-form-item>
        </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-new-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { getArrValue, getObjValue } from 'js-fast-way'
import mainApi from '~api/system/tenant'
import menuApi from '~api/system/menu'
import { get } from '~api'

const props = defineProps({
    info: {
        type: Object,
        default: () => ({}),
    },
})

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

//双向绑定
// eslint-disable-next-line no-undef
const isShow = defineModel('modelValue', {
    default: false,
})

//监听可否编辑
const dataInfo = ref(props.info)
watch(() => props.info, (data) => {
    dataInfo.value = data
}, { immediate: true, deep: true })

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

//菜单表单
const formRef = ref(null)
const formModel = ref({})
const formRules = {}

//初始化表单
const initFormData = () => {
    submitLoading.value = false
    getMenuTreeApi()
    getPackageList()
    getPackageInfo()
    formModel.value = {
        tenantId: dataInfo.value.id,
    }
}

//菜单列表
const menuData = ref([])
const getMenuTreeApi = async () => {
    const { data } = await menuApi.getMenuTree()
    menuData.value = getArrValue(data)
}
//菜单树
const menuTreeProps = {
    label: 'title',
}

//获取产品包信息
const getPackageInfo = async () => {
    const { data } = await mainApi.packageInfo(dataInfo.value.id)
    let res = getObjValue(data)
    res.packageId = res.id
    res.menuIds = res.menuId?.split(',') ?? []
    formModel.value = res
}

//获取产品包列表
const packageData = ref([])
const getPackageList = async () => {
    const { data } = await get({
        url: '/api/blade-system/tenant-package/select',
    })
    packageData.value = getArrValue(data)
}

//产品包改变
const packageChange = (val) => {
    const item = packageData.value.find((item) => item.id === val)
    const menuId = item ? item.menuId : ''
    formModel.value.menuIds = menuId.split(',') ?? []
}

//提交表单
const submitLoading = ref(false)
const dialogSubmit = async () => {
    submitLoading.value = true
    //处理数据
    const form = formModel.value
    form.menuId = form.menuIds?.join(',') ?? ''
    //发起请求
    const { error, code, msg } = await mainApi.packageSetting(form)
    submitLoading.value = false
    if (!error && code === 200) {
        window?.$message?.success('操作成功')
        emit('finish')
        dialogClose()
    } else {
        window?.$message?.error(msg ?? '操作失败')
    }
}

//关闭弹窗
const dialogClose = () => {
    isShow.value = false
    submitLoading.value = false
    formModel.value = {}
}
</script>
