<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('reqInvoiceApply.req_id')" type="remoteSelect" v-model="baTable.form.items!.req_id" prop="req_id" :input-attr="{ pk: 'req_order.id', field: 'name', 'remote-url': '/admin/ReqOrder/index' }" :placeholder="t('Please select field', { field: t('reqInvoiceApply.req_id') })" />
                    <FormItem :label="t('reqInvoiceApply.invoice_type')" type="select" v-model="baTable.form.items!.invoice_type" prop="invoice_type" :data="{ content: { '普通发票': t('reqInvoiceApply.invoice_type 普通发票'), '专用发票': t('reqInvoiceApply.invoice_type 专用发票') } }" :placeholder="t('Please select field', { field: t('reqInvoiceApply.invoice_type') })" />
                    <FormItem :label="t('reqInvoiceApply.invoice_corp')" type="string" v-model="baTable.form.items!.invoice_corp" prop="invoice_corp" :placeholder="t('Please input field', { field: t('reqInvoiceApply.invoice_corp') })" />
                    <FormItem :label="t('reqInvoiceApply.invoice_corp_sn')" type="string" v-model="baTable.form.items!.invoice_corp_sn" prop="invoice_corp_sn" :placeholder="t('Please input field', { field: t('reqInvoiceApply.invoice_corp_sn') })" />
                    <FormItem :label="t('reqInvoiceApply.invoice_amount')" type="number" prop="invoice_amount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.invoice_amount" :placeholder="t('Please input field', { field: t('reqInvoiceApply.invoice_amount') })" />
                    <FormItem :label="t('reqInvoiceApply.status')" type="switch" v-model="baTable.form.items!.status" prop="status" :data="{ content: { '': t('reqInvoiceApply.status '), 1: t('reqInvoiceApply.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 { 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({
    invoice_amount: [buildValidatorData({ name: 'float', title: t('reqInvoiceApply.invoice_amount') })],
    create_time: [buildValidatorData({ name: 'date', title: t('reqInvoiceApply.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('reqInvoiceApply.update_time') })],
})
</script>

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