<template>
    <hc-new-card>
        <template #header>
            <el-button hc-btn type="success" :disabled="tableCheckKeys.length <= 0" @click="handleSetting">授权配置</el-button>
            <el-button hc-btn type="success" :disabled="tableCheckKeys.length <= 0" @click="handleDatasource">数据源配置</el-button>
            <el-button hc-btn type="warning" :disabled="tableCheckKeys.length <= 0" @click="handlePackage">产品包配置</el-button>
            <el-button hc-btn type="warning" @click="handlePackageSetting">产品包管理</el-button>
        </template>
        <template #extra>
            <el-button hc-btn type="primary" @click="addClick">新增</el-button>
            <el-button hc-btn type="danger" @click="delClick">删除</el-button>
        </template>
        <template #search>
            <div class="w-32">
                <el-select v-model="searchType" placeholder="选择搜索类型" filterable block>
                    <el-option label="租户ID" value="tenantId" />
                    <el-option label="租户名称" value="tenantName" />
                    <el-option label="联系人" value="linkman" />
                    <el-option label="联系电话" value="contactNumber" />
                </el-select>
            </div>
            <div class="ml-2 w-60">
                <hc-search-input v-model="searchName" placeholder="请输入关键词" @search="searchClick" />
            </div>
        </template>
        <hc-table
            ref="tableRef" :column="tableColumn" :datas="tableData" :loading="tableLoading"
            is-new is-check :check-style="{ width: 29 }" :index-style="{ width: 60 }"
            @selection-change="tableCheckChange"
        >
            <template #accountNumber="{ row }">
                {{ row.accountNumber > 0 ? row.accountNumber : '不限制' }}
            </template>
            <template #expireTime="{ row }">
                {{ row.expireTime ? row.expireTime : '不限制' }}
            </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-new-dialog v-model="isDialogShow" widths="700px" is-footer-center :title="dialogTitle" @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="tenantName">
                            <el-input v-model="formModel.tenantName" clearable placeholder="请输入租户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="系统名称：" prop="tenantTitle">
                            <el-input v-model="formModel.tenantTitle" clearable placeholder="请输入系统名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系人：" prop="linkman">
                            <el-input v-model="formModel.linkman" clearable placeholder="请输入联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系电话：">
                            <el-input v-model="formModel.contactNumber" clearable placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系地址：">
                            <el-input v-model="formModel.address" clearable placeholder="请输入联系地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="绑定域名：" prop="domainUrl">
                            <el-input v-model="formModel.domainUrl" clearable placeholder="请输入绑定域名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="租户图片(透明黑色图)：">
                            <hc-form-upload type="preview" :src="formModel.tenantAvatar" :num="1" @upload="tenantUpload('avatar')" @change="avatarChange" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="租户图片文字(透明黑色图)：">
                            <hc-form-upload type="preview" :src="formModel.tenantAvatarText" :num="1" @upload="tenantUpload('text')" @change="textChange" />
                        </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>

        <!-- 授权配置 -->
        <HcSettingVue v-model="isSettingShow" :info="settingInfo" @finish="settingFinish" />

        <!-- 数据源配置 -->
        <HcDataSource v-model="isSourceShow" :info="sourceInfo" @finish="sourceFinish" />

        <!-- 产品包配置 -->
        <HcPackage v-model="isPackageShow" :info="packageInfo" @finish="packageFinish" />

        <!-- 产品包管理 -->
        <HcPackageData v-model="isPackageDataShow" />

        <!-- 上传文件 -->
        <hc-upload-file ref="uploadRef" :echo-params="uploadParams" :options="uploadOptions" @success="uploadSuccess" />
    </hc-new-card>
</template>

<script setup>
import { nextTick, onActivated, ref } from 'vue'
import { arrToId, formValidate, getArrValue } from 'js-fast-way'
import { HcDelMsg, getHeader } from 'hc-vue3-ui'
import HcSettingVue from './modules/tenant/setting.vue'
import HcDataSource from './modules/tenant/data-source.vue'
import HcPackageData from './modules/tenant/package-data.vue'
import HcPackage from './modules/tenant/package.vue'
import mainApi from '~api/system/tenant'

defineOptions({
    name: 'Tenant',
})

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

//搜索表单
const searchType = ref('tenantId')
const searchName = ref('')
const searchForm = ref({ 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 tableRef = ref(null)
const tableColumn = ref([
    { key: 'tenantId', name: '租户ID', width: 80, align: 'center' },
    { key: 'tenantName', name: '租户名称' },
    { key: 'tenantTitle', name: '系统名称' },
    { key: 'domainUrl', name: '绑定域名' },
    { key: 'linkman', name: '联系人', width: 80 },
    { key: 'contactNumber', name: '联系电话', width: 110 },
    { key: 'accountNumber', name: '账号额度', width: 80, align: 'center' },
    { key: 'expireTime', name: '过期时间', width: 80, align: 'center' },
    { key: 'action', name: '操作', width: 100, align: 'center' },
])

//获取表格数据
const tableLoading = ref(true)
const tableData = ref([])
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const form = {
        ...searchForm.value,
        total: null,
    }
    if (searchName.value) {
        form[searchType.value] = searchName.value
    }
    const { error, code, data } = await mainApi.page(form)
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        searchForm.value.total = data['total']
    } else {
        tableData.value = []
        searchForm.value.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 = {
    category: {
        required: true,
        trigger: 'blur',
        message: '请选择岗位类型',
    },
    postCode: {
        required: true,
        trigger: 'blur',
        message: '请输入岗位编号',
    },
    postName: {
        required: true,
        trigger: 'blur',
        message: '请输入岗位名称',
    },
    sort: {
        required: true,
        trigger: 'blur',
        message: '请输入字典排序',
    },
}

//新增
const addClick = () => {
    dialogTitle.value = '新增租户'
    formModel.value = { sort: 1 }
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
    })
}

//修改菜单
const editRowClick = (row) => {
    formModel.value = {}
    dialogTitle.value = '修改租户'
    formModel.value = { ...row }
    //显示表单弹窗
    nextTick(() => {
        isDialogShow.value = true
    })
}

//删除菜单
const delRowClick = async ({ item }, resolve) => {
    const { code, msg } = await mainApi.del(item.id)
    if (code === 200) {
        resolve()
        window.$message.success('删除成功')
        getTableData().then()
    } 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('删除成功')
            getTableData().then()
        } else {
            window.$message.error(msg ?? '删除失败')
        }
    })
}


//上传文件
const uploadRef = ref(null)
const uploadParams = ref({})
const uploadOptions = ref({
    url: '/api/blade-resource/oss/endpoint/put-file2',
    accept: 'image/png,image/jpg,image/jpeg',
    accept_tip: '只能上传：图片(png、jpg、jpeg)',
    headers: getHeader(),
    multiple: false,
})

//文件上传
const tenantUpload = (type) => {
    uploadParams.value = { type: type }
    nextTick(() => {
        uploadRef.value?.selectFile()
    })
}

//文件改变
const avatarChange = (file) => {
    formModel.value.tenantAvatar = file
}
const textChange = (file) => {
    formModel.value.tenantAvatarText = file
}


// 文件上传成功的回调
const uploadSuccess = ({ echoParams, resData }) => {
    if (echoParams.type === 'avatar') {
        formModel.value.tenantAvatar = resData
    } else if (echoParams.type === 'text') {
        formModel.value.tenantAvatarText = resData
    }
    //关闭弹窗
    uploadRef.value?.setModalShow(false)
}


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

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

//授权配置
const isSettingShow = ref(false)
const settingInfo = ref({})
const handleSetting = () => {
    const rows = tableCheckKeys.value
    if (rows.length <= 0) {
        window.$message.warning('请选择要授权的租户')
        return false
    }
    //处理数据
    settingInfo.value = {
        accountNumber: rows[0].accountNumber,
        expireTime: rows[0].expireTime,
        ids: arrToId(rows),
    }
    //显示表单弹窗
    nextTick(() => {
        isSettingShow.value = true
    })
}
//授权配置完成
const settingFinish = () => {
    isSettingShow.value = false
    getTableData()
}

//数据源配置
const isSourceShow = ref(false)
const sourceInfo = ref({})
const handleDatasource = () => {
    const rows = tableCheckKeys.value
    if (rows.length <= 0) {
        window.$message.warning('请选择要授权的租户')
        return false
    }
    if (rows.length > 1) {
        window.$message.warning('只能选择一个租户')
        return false
    }
    //处理数据
    sourceInfo.value = { id: rows[0].id }
    //显示表单弹窗
    nextTick(() => {
        isSourceShow.value = true
    })
}
//数据源配置完成
const sourceFinish = () => {
    isSourceShow.value = false
    getTableData()
}


//产品包配置
const isPackageShow = ref(false)
const packageInfo = ref({})
const handlePackage = () => {
    const rows = tableCheckKeys.value
    if (rows.length <= 0) {
        window.$message.warning('请选择要授权的租户')
        return false
    }
    if (rows.length > 1) {
        window.$message.warning('只能选择一个租户')
        return false
    }
    //处理数据
    packageInfo.value = { id: rows[0].id }
    //显示表单弹窗
    nextTick(() => {
        isPackageShow.value = true
    })
}
//产品包配置完成
const packageFinish = () => {
    isPackageShow.value = false
    getTableData()
}

//产品包管理
const isPackageDataShow = ref(false)
const handlePackageSetting = () => {
    isPackageDataShow.value = true
}
</script>
