<template>
    <hc-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-input-number v-model="formModel.accountNumber" block controls-position="right" />
            </el-form-item>
            <el-form-item label="过期时间：">
                <el-date-picker
                    v-model="formModel.expireTime" type="datetime" class="block"
                    format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
                />
            </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-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { deepClone } from 'js-fast-way'
import mainApi from '~api/system/tenant'

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
    formModel.value = deepClone(dataInfo.value)
}

//提交表单
const submitLoading = ref(false)
const dialogSubmit = async () => {
    submitLoading.value = true
    const { isRes } = await mainApi.setting(formModel.value)
    submitLoading.value = false
    if (!isRes) return
    window?.$message?.success('操作成功')
    emit('finish')
    dialogClose()
}

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