<template>
    <hc-new-card>
        <template #header>
            <div class="w-40">
                <el-select v-model="searchForm.tenantId" placeholder="选择所属租户" filterable clearable block>
                    <el-option v-for="item in tenantData" :key="item.tenantId" :label="item.name" :value="item.tenantId" />
                </el-select>
            </div>
            <div class="ml-2 w-32">
                <el-select v-model="searchType" placeholder="选择搜索类型" filterable block>
                    <el-option label="机构名称" value="deptName" />
                    <el-option label="机构全称" value="fullName" />
                </el-select>
            </div>
            <div class="ml-2 w-60">
                <hc-search-input v-model="searchName" 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
            ref="tableRef" :column="tableColumn" :datas="tableData" :loading="tableLoading"
            :is-index="false" is-new is-check :check-style="{ width: 29 }" lazy :load="tableLoad"
            @selection-change="tableCheckChange"
        >
            <template #tenantId="{ row }">{{ getTenantName(row) }}</template>
            <template #deptCategory="{ row }">{{ getDeptCategoryName(row) }}</template>
            <template #action="{ row }">
                <el-link type="warning" @click="editRowClick(row)">修改</el-link>
                <el-link type="success" @click="addRowClick(row)">新增子项</el-link>
                <el-link v-del-com:[delRowClick]="row" type="danger">删除</el-link>
            </template>
        </hc-table>

        <!-- 新增/修改 -->
        <hc-new-dialog v-model="isDialogShow" widths="620px" is-footer-center :title="iconDialogTitle" @close="dialogClose">
            <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="deptName">
                            <el-input v-model="formModel.deptName" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="机构全称：" prop="fullName">
                            <el-input v-model="formModel.fullName" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="上级机构：">
                            <el-tree-select
                                v-model="formModel.parentId" :disabled="isChildForm" :data="levelData" :props="levelDataProps"
                                clearable filterable check-strictly block :render-after-expand="false"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="机构类型：" prop="deptCategory">
                            <el-select v-model="formModel.deptCategory" filterable clearable block>
                                <el-option v-for="item in deptCategoryData" :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="sort">
                            <el-input-number v-model="formModel.sort" :min="1" block controls-position="right" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <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-new-dialog>
    </hc-new-card>
</template>

<script setup>
import { nextTick, onActivated, ref } from 'vue'
import { getDictionaryData, reloadPage } from '~uti/tools'
import { arrToId, formValidate, getArrValue, isNullES } from 'js-fast-way'
import { HcDelMsg } from 'hc-vue3-ui'
import mainApi from '~api/system/dept'
import tenantApi from '~api/system/tenant'

defineOptions({
    name: 'Dept',
})

//激活
onActivated(() => {
    getDataApi()
})

const getDataApi = async () => {
    await getTenantData()
    await getDeptCategory()
    searchClick()
}

//搜索表单
const searchType = ref('deptName')
const searchName = ref('')
const searchForm = ref({ tenantId: null })

//所属租户
const tenantData = ref([])
const getTenantData = async () => {
    let newArr = []
    const { data } = await tenantApi.getSelect()
    const res = getArrValue(data)
    for (let i = 0; i < res.length; i++) {
        newArr.push({
            id: res[i]['id'],
            name: res[i]['tenantName'],
            tenantId: res[i]['tenantId'],
        })
    }
    tenantData.value = newArr
}
const getTenantName = ({ tenantId }) => {
    if (isNullES(tenantId)) return '-'
    const item = tenantData.value.find((item) => item.tenantId === tenantId)
    if (isNullES(item)) return tenantId
    return item.name ?? tenantId
}

//获取机构类型
const deptCategoryData = ref([])
const getDeptCategory = async () => {
    deptCategoryData.value = await getDictionaryData('org_category')
}
const getDeptCategoryName = ({ deptCategory }) => {
    if (isNullES(deptCategory)) return '-'
    const item = deptCategoryData.value.find((item) => item.value === deptCategory)
    if (isNullES(item)) return deptCategory
    return item.label ?? deptCategory
}

//搜索
const searchClick = () => {
    getTableData()
}

//表格数据
const tableRef = ref(null)
const tableColumn = ref([
    { key: 'deptName', name: '机构名称' },
    { key: 'tenantId', name: '所属租户', width: 160 },
    { key: 'fullName', name: '机构全称' },
    { key: 'deptCategory', name: '机构类型', width: 100 },
    { key: 'sort', name: '排序', width: 80, align: 'center' },
    { key: 'action', name: '操作', width: 200, align: 'center' },
])

//获取表格数据
const tableLoading = ref(true)
const tableData = ref([])
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const form = searchForm.value
    if (searchName.value) {
        form[searchType.value] = searchName.value
    }
    const { data } = await mainApi.getLazyList({
        ...form,
        parentId: 0,
    })
    tableData.value = getArrValue(data)
    tableLoading.value = false
}

//懒加载表格
const tableLoad = async (row, node, resolve) => {
    const form = searchForm.value
    if (searchName.value) {
        form[searchType.value] = searchName.value
    }
    const { data } = await mainApi.getLazyList({
        ...form,
        parentId: row.id,
    })
    resolve(getArrValue(data))
}

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

//上级菜单
const levelDataProps = { label: 'deptName' }
const levelData = ref([])
const getlevelData = async () => {
    const { data } = await mainApi.getDeptTree()
    levelData.value = getArrValue(data)
}

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

//菜单表单
const formRef = ref(null)
const formModel = ref({})
const formRules = {
    deptName: {
        required: true,
        trigger: 'blur',
        message: '请输入机构名称',
    },
    fullName: {
        required: true,
        trigger: 'blur',
        message: '请输入机构全称',
    },
    deptCategory: {
        required: true,
        trigger: 'blur',
        message: '请选择机构类型',
    },
    sort: {
        required: true,
        trigger: 'blur',
        message: '请输入菜单排序',
    },
}

//新增机构
const addClick = () => {
    isChildForm.value = false
    iconDialogTitle.value = '新增机构'
    formModel.value = { parentId: null, deptCategory: null, sort: 1 }
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
        getlevelData()
    })
}

//修改机构
const editRowClick = (row) => {
    formModel.value = {}
    isChildForm.value = false
    iconDialogTitle.value = '修改机构'
    formModel.value = { ...row }
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
        getlevelData()
    })
}

//新增子项
const isChildForm = ref(false)
const addRowClick = (row) => {
    isChildForm.value = true
    iconDialogTitle.value = '新增子机构'
    formModel.value = { parentId: row.id, deptCategory: null, sort: 1 }
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
        getlevelData()
    })
}

//提交表单
const submitLoading = ref(false)
const dialogSubmit = async () => {
    const formRes = await formValidate(formRef.value)
    if (!formRes) return false
    submitLoading.value = true
    //处理数据
    const form = formModel.value
    form.parentId = form.parentId ?? '0'
    form.tenantId = form.tenantId ?? ''
    //发起请求
    const { error, code, msg } = await mainApi.submit(form)
    submitLoading.value = false
    if (!error && code === 200) {
        dialogClose()
        window?.$message?.success('操作成功')
        reloadPage()
    } else {
        window?.$message?.error(msg ?? '操作失败')
    }
}

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

//删除
const delRowClick = async ({ item }, resolve) => {
    const { code, msg } = await mainApi.del(item.id)
    if (code === 200) {
        resolve()
        window.$message.success('删除成功')
        reloadPage()
    } else {
        resolve()
        window.$message.error(msg ?? '删除失败')
    }
}

//批量删除
const delClick = () => {
    const rows = tableCheckKeys.value
    if (rows.length <= 0) {
        window.$message.warning('请选择要删除的数据')
        return false
    }
    //确认删除菜单
    HcDelMsg({}, async (instance, resolve) => {
        instance.confirmButtonLoading = true
        instance.confirmButtonText = '删除中...'
        //发起请求
        const ids = arrToId(rows)
        const { code, msg } = await mainApi.del(ids)
        //关闭弹窗的回调
        resolve()
        instance.confirmButtonLoading = false
        //处理结果
        if (code === 200) {
            window.$message.success('删除成功')
            reloadPage()
        } else {
            window.$message.error(msg ?? '删除失败')
        }
    })
}
</script>
