<template>
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate === 'Edit'"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ t('Edit') }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form ba-Edit-form"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="formRules"
                >
                    <el-alert class="ba-table-alert" type="info" :closable="false" show-icon>
                        {{ t('config.depositChannel.form_tip') }}
                    </el-alert>

                    <el-form-item :label="t('config.depositChannel.code')" prop="code">
                        <el-input :model-value="String(baTable.form.items!.code ?? '')" readonly />
                    </el-form-item>
                    <el-form-item :label="t('config.depositChannel.display_name')">
                        <el-input :model-value="registryDisplayName" readonly />
                    </el-form-item>

                    <el-form-item :label="t('config.depositChannel.sort')" prop="sort">
                        <el-input-number v-model="baTable.form.items!.sort" :min="0" :max="9999" :controls="true" class="w100" />
                    </el-form-item>
                    <el-form-item :label="t('config.depositChannel.status')" prop="status">
                        <el-switch v-model="baTable.form.items!.status" :active-value="1" :inactive-value="0" />
                    </el-form-item>

                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" type="primary" @click="baTable.onSubmit(formRef)">
                    {{ t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { computed, inject, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = useTemplateRef<FormInstance>('formRef')
const baTable = inject('baTable') as baTable
const { t } = useI18n()

const registryDisplayName = computed(() => {
    const code = String(baTable.form.items?.code ?? '')
    const reg = baTable.table.extend?.registry as Record<string, { name?: string }> | undefined
    if (!code || !reg || !reg[code]) {
        return code
    }
    const n = reg[code].name
    return typeof n === 'string' && n !== '' ? n : code
})

const formRules = {}
</script>

<style scoped lang="scss">
.w100 {
    width: 100%;
}
</style>
