<template>
    <hc-new-card>
        <template #header>
            <div class="w-40">
                <el-select v-model="searchForm.category" placeholder="请选择分类" filterable clearable block>
                    <el-option v-for="item in categoryData" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </div>
            <div class="ml-2 w-32">
                <el-select v-model="searchType" placeholder="选择搜索类型" filterable block>
                    <el-option label="资源编号" value="ossCode" />
                    <el-option label="accessKey" value="accessKey" />
                </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
            :column="tableColumn" :datas="tableData" :loading="tableLoading"
            :is-index="false" is-new is-check :check-style="{ width: 29 }"
            @selection-change="tableCheckChange"
        >
            <template #category="{ row }">{{ getCategoryName(row) }}</template>
            <template #status="{ row }">{{ row.statusName ?? '否' }}</template>
            <template #action="{ row }">
                <el-link type="warning" @click="editRowClick(row)">修改</el-link>
                <el-link v-del-com:[delRowClick]="row" type="danger">删除</el-link>
                <el-link type="success" @click="debugRowClick(row)">调试</el-link>
                <el-link v-yes-com:[enableRowClick]="row" type="primary">启用</el-link>
            </template>
        </hc-table>
        <template #action>
            <hc-pages :pages="searchForm" @change="pageChange" />
        </template>

        <!-- 新增/修改 -->
        <hc-new-dialog v-model="isDialogShow" widths="60rem" 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="category">
                            <div class="form-item-div">
                                <el-radio-group v-model="formModel.category">
                                    <el-radio v-for="item in categoryData" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
                                </el-radio-group>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="资源编号：" prop="ossCode">
                            <el-input v-model="formModel.ossCode" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="资源地址：" prop="endpoint">
                            <el-input v-model="formModel.endpoint" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="空间名：" prop="bucketName">
                            <el-input v-model="formModel.bucketName" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="accessKey：" prop="accessKey">
                            <el-input v-model="formModel.accessKey" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="secretKey：" prop="secretKey">
                            <el-input v-model="formModel.secretKey" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col v-if="formModel.category === 4" :span="12">
                        <el-form-item label="appId：">
                            <el-input v-model="formModel.appId" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col v-if="formModel.category === 4" :span="12">
                        <el-form-item label="region：">
                            <el-input v-model="formModel.region" clearable />
                        </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-dialog v-model="isDebugShow" widths="400px" :footer="false" title="上传调试" @close="debugClose">
            <el-form :model="debugForm" label-position="top" label-width="auto">
                <el-form-item label="资源编号：">
                    <el-input v-model="debugForm.ossCode" disabled />
                </el-form-item>
                <el-form-item label="上传图片：">
                    <el-upload class="avatar-uploader" :headers="getHeader()" :action="actionUrl" accept="image/*" :show-file-list="false" :on-success="handleAvatarSuccess">
                        <img v-if="debugForm.url" :src="debugForm.url" class="avatar" alt="">
                        <hc-icon v-else class="avatar-uploader-icon" name="add-large" />
                    </el-upload>
                </el-form-item>
                <el-form-item label="回调地址：">
                    <el-input v-model="debugForm.url" />
                </el-form-item>
            </el-form>
        </hc-new-dialog>
    </hc-new-card>
</template>

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

defineOptions({
    name: 'Oss',
})

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

const getDataApi = async () => {
    await getCategoryData()
    searchClick()
}

//搜索表单
const searchType = ref('ossCode')
const searchName = ref('')
const searchForm = ref({ category: null, current: 1, size: 30, total: 0 })

//获取字典数据
const categoryData = ref([])
const getCategoryData = async () => {
    categoryData.value = await getDictionaryData('oss')
}
const getCategoryName = ({ category }) => {
    if (isNullES(category)) return '-'
    const item = categoryData.value.find((item) => item.value === category)
    if (isNullES(item)) return category
    return item.label ?? category
}

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

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

//表格数据
const tableColumn = ref([
    { key: 'category', name: '分类', width: 80, align: 'center' },
    { key: 'ossCode', name: '资源编号', width: 120 },
    { key: 'endpoint', name: '资源地址' },
    { key: 'bucketName', name: '空间名' },
    { key: 'accessKey', name: 'accessKey' },
    { key: 'secretKey', name: 'secretKey' },
    { key: 'remark', name: '备注' },
    { key: 'status', name: '是否启用', width: 80, align: 'center' },
    { key: 'action', name: '操作', width: 160, align: 'center' },
])

//获取表格数据
const tableLoading = ref(true)
const tableData = ref([])
const getTableData = async () => {
    tableData.value = []
    tableLoading.value = true
    const { error, code, data } = await mainApi.page({
        ...searchForm.value,
        total: null,
    })
    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: '请选择存储分类',
    },
    ossCode: {
        required: true,
        trigger: 'blur',
        message: '请输入资源编号',
    },
    endpoint: {
        required: true,
        trigger: 'blur',
        message: '请输入资源地址',
    },
    bucketName: {
        required: true,
        trigger: 'blur',
        message: '请输入空间名',
    },
    accessKey: {
        required: true,
        trigger: 'blur',
        message: '请输入accessKey',
    },
    secretKey: {
        required: true,
        trigger: 'blur',
        message: '请输入secretKey',
    },
}


//新增
const addClick = () => {
    dialogTitle.value = '新增对象存储'
    formModel.value = { category: 1 }
    //显示表单弹窗
    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 { 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 isDebugShow = ref(false)
const debugForm = ref({})
const actionUrl = ref('')
const debugRowClick = (row) => {
    debugForm.value = { ossCode: row.ossCode, url: '' }
    actionUrl.value = '/api/blade-resource/oss/endpoint/put-file?code=' + row.ossCode
    isDebugShow.value = true
}
const handleAvatarSuccess = ({ code, data, msg }) => {
    if (code !== 200 || isNullES(data.link)) {
        window.$message.error(msg ?? '上传失败')
        return false
    }
    window.$message.success('上传成功')
    debugForm.value.url = data.link
}
const debugClose = () => {
    isDebugShow.value = false
    debugForm.value = {}
}


//启用
const enableRowClick = async ({ item }, resolve) => {
    const { code, msg } = await mainApi.enable(item.id)
    if (code === 200) {
        resolve()
        window.$message.success('启用成功')
        getTableData().then()
    } else {
        resolve()
        window.$message.error(msg ?? '启用失败')
    }
}

//删除
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 ?? '删除失败')
        }
    })
}
</script>

<style lang="scss">
.avatar-uploader {
    width: 100%;
    height: 178px;
    .el-upload {
        width: 100%;
        height: 178px;
        border: 1px dashed #dcdfe6;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
        border-color: var(--el-color-primary);
    }
    .avatar {
        width: 100%;
        height: 100%;
        border-radius: 6px;
        display: block;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
    }
}
</style>
