<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <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('mall.address.playx_user_asset_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.playx_user_asset_id"
                        prop="playx_user_asset_id"
                        :input-attr="{ pk: 'mall_user_asset.id', field: 'username', remoteUrl: '/admin/mall.UserAsset/select' }"
                        :placeholder="t('Please select field', { field: t('mall.address.playx_user_asset_id') })"
                    />
                    <FormItem
                        :label="t('mall.address.receiver_name')"
                        type="string"
                        v-model="baTable.form.items!.receiver_name"
                        prop="receiver_name"
                        :placeholder="t('Please input field', { field: t('mall.address.receiver_name') })"
                    />
                    <FormItem
                        :label="t('mall.address.phone')"
                        type="string"
                        v-model="baTable.form.items!.phone"
                        prop="phone"
                        :placeholder="t('Please input field', { field: t('mall.address.phone') })"
                    />
                    <FormItem
                        :label="t('mall.address.region')"
                        type="string"
                        v-model="baTable.form.items!.region"
                        prop="region"
                        :placeholder="t('mall.address.region_tip')"
                    />
                    <FormItem
                        :label="t('mall.address.detail_address')"
                        type="string"
                        v-model="baTable.form.items!.detail_address"
                        prop="detail_address"
                        :placeholder="t('Please input field', { field: t('mall.address.detail_address') })"
                    />
                    <FormItem
                        :label="t('mall.address.default_setting')"
                        type="switch"
                        v-model="baTable.form.items!.default_setting"
                        prop="default_setting"
                        :input-attr="{ content: { '0': t('mall.address.default_setting 0'), '1': t('mall.address.default_setting 1') } }"
                    />
                </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'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    phone: [buildValidatorData({ name: 'required', title: t('mall.address.phone') })],
    create_time: [buildValidatorData({ name: 'date', title: t('mall.address.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('mall.address.update_time') })],
})
</script>

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