<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('platformSettle.settle_sn')" type="string" v-model="baTable.form.items!.settle_sn" prop="settle_sn" :placeholder="t('Please input field', { field: t('platformSettle.settle_sn') })" />
                    <FormItem :label="t('platformSettle.order_id')" type="remoteSelect" v-model="baTable.form.items!.order_id" prop="order_id" :input-attr="{ pk: 'xuqiu_order.id', field: '需求订单编号', 'remote-url': '/admin/XuqiuOrder/index' }" :placeholder="t('Please select field', { field: t('platformSettle.order_id') })" />
                    <FormItem :label="t('platformSettle.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('platformSettle.supplier_id') })" />
                    <FormItem :label="t('platformSettle.total_amount')" type="number" prop="total_amount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.total_amount" :placeholder="t('Please input field', { field: t('platformSettle.total_amount') })" />
                    <FormItem :label="t('platformSettle.settle_status')" type="select" v-model="baTable.form.items!.settle_status" prop="settle_status" :data="{ content: { '未回款': t('platformSettle.settle_status 未回款'), '部分回款': t('platformSettle.settle_status 部分回款'), '已回款': t('platformSettle.settle_status 已回款') } }" :placeholder="t('Please select field', { field: t('platformSettle.settle_status') })" />
                    <FormItem :label="t('platformSettle.settled_amount')" type="number" prop="settled_amount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.settled_amount" :placeholder="t('Please input field', { field: t('platformSettle.settled_amount') })" />
                    <FormItem :label="t('platformSettle.invoice_status')" type="select" v-model="baTable.form.items!.invoice_status" prop="invoice_status" :data="{ content: { '未开票': t('platformSettle.invoice_status 未开票'), '部分开票': t('platformSettle.invoice_status 部分开票'), '已开票': t('platformSettle.invoice_status 已开票') } }" :placeholder="t('Please select field', { field: t('platformSettle.invoice_status') })" />
                    <FormItem :label="t('platformSettle.invoiced_amount')" type="number" prop="invoiced_amount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.invoiced_amount" :placeholder="t('Please input field', { field: t('platformSettle.invoiced_amount') })" />
                    <FormItem :label="t('platformSettle.doing_invoice_amount')" type="number" prop="doing_invoice_amount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.doing_invoice_amount" :placeholder="t('Please input field', { field: t('platformSettle.doing_invoice_amount') })" />
                </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({
    total_amount: [buildValidatorData({ name: 'float', title: t('platformSettle.total_amount') })],
    settled_amount: [buildValidatorData({ name: 'float', title: t('platformSettle.settled_amount') })],
    backing_invoice_amount: [buildValidatorData({ name: 'float', title: t('platformSettle.backing_invoice_amount') })],
    invoiced_amount: [buildValidatorData({ name: 'float', title: t('platformSettle.invoiced_amount') })],
    doing_invoice_amount: [buildValidatorData({ name: 'float', title: t('platformSettle.doing_invoice_amount') })],
    create_time: [buildValidatorData({ name: 'date', title: t('platformSettle.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('platformSettle.update_time') })],
})
</script>

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