<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('bank.weigh')"
                        type="number"
                        v-model="baTable.form.items!.weigh"
                        prop="weigh"
                        :input-attr="{ step: 1 }"
                        :placeholder="t('Please input field', { field: t('bank.weigh') })"
                    />
                    <FormItem
                        :label="t('bank.bank_name')"
                        type="string"
                        v-model="baTable.form.items!.bank_name"
                        prop="bank_name"
                        :placeholder="t('Please input field', { field: t('bank.bank_name') })"
                    />
                    <FormItem
                        :label="t('bank.bank_account')"
                        type="string"
                        v-model="baTable.form.items!.bank_account"
                        prop="bank_account"
                        :placeholder="t('Please input field', { field: t('bank.bank_account') })"
                    />
                    <FormItem
                        :label="t('bank.safe_alert')"
                        type="select"
                        v-model="baTable.form.items!.safe_alert"
                        prop="safe_alert"
                        :input-attr="{
                            content: {
                                '0': t('bank.safe_alert 0'),
                                '1': t('bank.safe_alert 1'),
                                '2': t('bank.safe_alert 2'),
                                '3': t('bank.safe_alert 3'),
                                '4': t('bank.safe_alert 4'),
                                '5': t('bank.safe_alert 5'),
                                '6': t('bank.safe_alert 6'),
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('bank.safe_alert') })"
                    />
                    <FormItem
                        :label="t('bank.tx_in')"
                        type="number"
                        v-model="baTable.form.items!.tx_in"
                        prop="tx_in"
                        :input-attr="{
                            step: 1,
                            disabled: baTable.form.items!.safe_alert == '0'
                        }"
                        :placeholder="t('Please input field', { field: t('bank.tx_in') })"
                    />
                    <FormItem
                        :label="t('bank.tx_out')"
                        type="number"
                        v-model="baTable.form.items!.tx_out"
                        prop="tx_out"
                        :input-attr="{
                            step: 1,
                            disabled: baTable.form.items!.safe_alert == '0'
                        }"
                        :placeholder="t('Please input field', { field: t('bank.tx_out') })"
                    />
                    <FormItem
                        :label="t('bank.fund_in')"
                        type="number"
                        v-model="baTable.form.items!.fund_in"
                        prop="fund_in"
                        :input-attr="{
                            step: 1,
                            disabled: baTable.form.items!.safe_alert == '0'
                        }"
                        :placeholder="t('Please input field', { field: t('bank.fund_in') })"
                    />
                    <FormItem
                        :label="t('bank.fund_out')"
                        type="number"
                        v-model="baTable.form.items!.fund_out"
                        prop="fund_out"
                        :input-attr="{
                            step: 1,
                            disabled: baTable.form.items!.safe_alert == '0'
                        }"
                        :placeholder="t('Please input field', { field: t('bank.fund_out') })"
                    />
                    <FormItem
                        :label="t('bank.label_color')"
                        type="color"
                        v-model="baTable.form.items!.label_color"
                        prop="label_color"
                        :placeholder="t('Please input field', { field: t('bank.label_color') })"
                    />
                    <FormItem
                        :label="t('bank.status')"
                        type="switch"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :input-attr="{ content: { '0': t('bank.status 0'), '1': t('bank.status 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, watch } 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({
    tx_in: [buildValidatorData({ name: 'number', title: t('bank.tx_in') })],
    tx_out: [buildValidatorData({ name: 'number', title: t('bank.tx_out') })],
    fund_in: [buildValidatorData({ name: 'number', title: t('bank.fund_in') })],
    fund_out: [buildValidatorData({ name: 'number', title: t('bank.fund_out') })],
    create_time: [buildValidatorData({ name: 'date', title: t('bank.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('bank.update_time') })],
})
watch(
    () => baTable.form.items!.safe_alert,
    (newVal) => {
        if (newVal == '0') {
            // 当选择为 0 时，强制重置相关数值（可选逻辑）
            baTable.form.items!.tx_in = null
            baTable.form.items!.tx_out = null
            baTable.form.items!.fund_in = null
            baTable.form.items!.fund_out = null
        }
    }
)
</script>

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