<template>
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="['Add', 'Edit'].includes(baTable.form.operate!)" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">{{ baTable.form.operate ? t(baTable.form.operate) : '' }}</div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-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="rules">
                    <FormItem :label="t('operation.operationNotice.title')" type="string" v-model="baTable.form.items!.title" prop="title" />
                    <FormItem :label="t('operation.operationNotice.content')" type="textarea" v-model="baTable.form.items!.content" prop="content" :input-attr="{ rows: 8 }" />
                    <FormItem
                        :label="t('operation.operationNotice.notice_type')"
                        type="radio"
                        v-model="baTable.form.items!.notice_type"
                        prop="notice_type"
                        :input-attr="{ content: { '0': t('operation.operationNotice.notice_type 0'), '1': t('operation.operationNotice.notice_type 1') } }"
                    />
                    <FormItem
                        :label="t('operation.operationNotice.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :input-attr="{ content: { '0': t('operation.operationNotice.status 0'), '1': t('operation.operationNotice.status 1') } }"
                    />
                    <FormItem :label="t('operation.operationNotice.publish_at')" type="datetime" v-model="baTable.form.items!.publish_at" prop="publish_at" />
                </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" @click="baTable.onSubmit(formRef)" type="primary">{{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormItemRule } from 'element-plus'
import { inject, reactive, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'

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

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    title: [{ required: true, message: t('Please input field', { field: t('operation.operationNotice.title') }) }],
})
</script>

<style scoped lang="scss"></style>
