<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('supplierStore.supplier_id')" type="remoteSelect" v-model="baTable.form.items!.supplier_id" prop="supplier_id" :input-attr="{ pk: 'supplier.id', field: 'name', 'remote-url': '/admin/Supplier/index' }" :placeholder="t('Please select field', { field: t('supplierStore.supplier_id') })" />
                    <FormItem :label="t('supplierStore.store_name')" type="string" v-model="baTable.form.items!.store_name" prop="store_name" :placeholder="t('Please input field', { field: t('supplierStore.store_name') })" />
                    <FormItem :label="t('supplierStore.store_type')" type="select" v-model="baTable.form.items!.store_type" prop="store_type" :data="{ content: { opt0: t('supplierStore.store_type opt0'), opt1: t('supplierStore.store_type opt1') } }" :placeholder="t('Please select field', { field: t('supplierStore.store_type') })" />
                    <FormItem :label="t('supplierStore.store_image')" type="image" v-model="baTable.form.items!.store_image" prop="store_image" />
                    <FormItem :label="t('supplierStore.store_address')" type="string" v-model="baTable.form.items!.store_address" prop="store_address" :placeholder="t('Please input field', { field: t('supplierStore.store_address') })" />
                    <FormItem :label="t('supplierStore.store_contacts')" type="string" v-model="baTable.form.items!.store_contacts" prop="store_contacts" :placeholder="t('Please input field', { field: t('supplierStore.store_contacts') })" />
                    <FormItem :label="t('supplierStore.tel')" type="string" v-model="baTable.form.items!.tel" prop="tel" :placeholder="t('Please input field', { field: t('supplierStore.tel') })" />
                    <FormItem :label="t('supplierStore.position')" type="string" v-model="baTable.form.items!.position" prop="position" :placeholder="t('Please input field', { field: t('supplierStore.position') })" />
                    <FormItem :label="t('supplierStore.string')" type="string" v-model="baTable.form.items!.string" prop="string" :placeholder="t('Please input field', { field: t('supplierStore.string') })" />
                </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({
    create_time: [buildValidatorData({ name: 'date', title: t('supplierStore.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('supplierStore.update_time') })],
    tel: [buildValidatorData({ name: 'mobile', title: t('supplierStore.tel') })],
    string: [buildValidatorData({ name: 'email', title: t('supplierStore.string') })],
})
</script>

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