<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['add', 'edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <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="'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="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('test.weigh')" type="number" prop="weigh" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.weigh" :placeholder="t('Please input field', { field: t('test.weigh') })" />
                    <FormItem :label="t('test.status')" type="switch" v-model="baTable.form.items!.status" prop="status" :data="{ content: { 0: t('test.status 0'), 1: t('test.status 1') } }" />
                    <FormItem :label="t('test.remark')" type="textarea" v-model="baTable.form.items!.remark" prop="remark" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('test.remark') })" />
                    <FormItem :label="t('test.image')" type="image" v-model="baTable.form.items!.image" prop="image" />
                    <FormItem :label="t('test.images')" type="images" v-model="baTable.form.items!.images" prop="images" />
                    <FormItem :label="t('test.file')" type="file" v-model="baTable.form.items!.file" prop="file" />
                    <FormItem :label="t('test.files')" type="files" v-model="baTable.form.items!.files" prop="files" />
                    <FormItem :label="t('test.radio')" type="radio" v-model="baTable.form.items!.radio" prop="radio" :data="{ content: { opt0: t('test.radio opt0'), opt1: t('test.radio opt1') } }" :placeholder="t('Please select field', { field: t('test.radio') })" />
                    <FormItem :label="t('test.checkbox')" type="checkbox" v-model="baTable.form.items!.checkbox" prop="checkbox" :data="{ content: { opt0: t('test.checkbox opt0'), opt1: t('test.checkbox opt1') } }" :placeholder="t('Please select field', { field: t('test.checkbox') })" />
                    <FormItem :label="t('test.select')" type="select" v-model="baTable.form.items!.select" prop="select" :data="{ content: { opt0: t('test.select opt0'), opt1: t('test.select opt1') } }" :placeholder="t('Please select field', { field: t('test.select') })" />
                    <FormItem :label="t('test.selects')" type="selects" v-model="baTable.form.items!.selects" prop="selects" :data="{ content: { opt0: t('test.selects opt0'), opt1: t('test.selects opt1') } }" :placeholder="t('Please select field', { field: t('test.selects') })" />
                    <FormItem :label="t('test.switch')" type="switch" v-model="baTable.form.items!.switch" prop="switch" :data="{ content: { 0: t('test.switch 0'), 1: t('test.switch 1') } }" />
                    <FormItem :label="t('test.float')" type="number" prop="float" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.float" :placeholder="t('Please input field', { field: t('test.float') })" />
                    <FormItem :label="t('test.number')" type="number" prop="number" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.number" :placeholder="t('Please input field', { field: t('test.number') })" />
                    <FormItem :label="t('test.password')" type="password" v-model="baTable.form.items!.password" prop="password" :placeholder="t('Please input field', { field: t('test.password') })" />
                    <FormItem :label="t('test.color')" type="color" v-model="baTable.form.items!.color" prop="color" :placeholder="t('Please input field', { field: t('test.color') })" />
                    <FormItem :label="t('test.icon')" type="icon" v-model="baTable.form.items!.icon" prop="icon" :input-attr="{ placement: 'top' }" :placeholder="t('Please select field', { field: t('test.icon') })" />
                    <FormItem :label="t('test.city')" type="city" v-model="baTable.form.items!.city" prop="city" :placeholder="t('Please select field', { field: t('test.city') })" />
                    <FormItem :label="t('test.array')" type="array" v-model="baTable.form.items!.array" prop="array" :placeholder="t('Please input field', { field: t('test.array') })" />
                    <FormItem :label="t('test.timestamp')" type="datetime" v-model="baTable.form.items!.timestamp" prop="timestamp" :placeholder="t('Please select field', { field: t('test.timestamp') })" />
                    <FormItem :label="t('test.year')" type="year" v-model="baTable.form.items!.year" prop="year" :placeholder="t('Please select field', { field: t('test.year') })" />
                    <FormItem :label="t('test.datetime')" type="datetime" v-model="baTable.form.items!.datetime" prop="datetime" :placeholder="t('Please select field', { field: t('test.datetime') })" />
                    <FormItem :label="t('test.time')" type="time" v-model="baTable.form.items!.time" prop="time" :placeholder="t('Please select field', { field: t('test.time') })" />
                    <FormItem :label="t('test.date')" type="date" v-model="baTable.form.items!.date" prop="date" :placeholder="t('Please select field', { field: t('test.date') })" />
                    <FormItem :label="t('test.editor')" type="editor" v-model="baTable.form.items!.editor" prop="editor" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('test.editor') })" />
                </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 { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    update_time: [buildValidatorData({ name: 'date', title: t('test.update_time') })],
    create_time: [buildValidatorData({ name: 'date', title: t('test.create_time') })],
    float: [buildValidatorData({ name: 'float', title: t('test.float') })],
    number: [buildValidatorData({ name: 'number', title: t('test.number') })],
    password: [buildValidatorData({ name: 'password', title: t('test.password') })],
    timestamp: [buildValidatorData({ name: 'date', title: t('test.timestamp') })],
    year: [buildValidatorData({ name: 'date', title: t('test.year') })],
    datetime: [buildValidatorData({ name: 'date', title: t('test.datetime') })],
    date: [buildValidatorData({ name: 'date', title: t('test.date') })],
    editor: [buildValidatorData({ name: 'editorRequired', title: t('test.editor') })],
})
</script>

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