<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('aerocraft.flightPlan.user_id')" type="number" prop="user_id" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.user_id" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.user_id') })" />
                    <FormItem :label="t('aerocraft.flightPlan.business_id')" type="number" prop="business_id" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.business_id" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.business_id') })" />
                    <FormItem :label="t('aerocraft.flightPlan.business_certified_img')" type="string" v-model="baTable.form.items!.business_certified_img" prop="business_certified_img" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.business_certified_img') })" />
                    <FormItem :label="t('aerocraft.flightPlan.real_name')" type="string" v-model="baTable.form.items!.real_name" prop="real_name" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.real_name') })" />
                    <FormItem :label="t('aerocraft.flightPlan.real_name_status')" type="radio" v-model="baTable.form.items!.real_name_status" prop="real_name_status" :data="{ content: { 0: t('aerocraft.flightPlan.real_name_status 0'), 1: t('aerocraft.flightPlan.real_name_status 1'), 2: t('aerocraft.flightPlan.real_name_status 2') } }" :placeholder="t('Please select field', { field: t('aerocraft.flightPlan.real_name_status') })" />
                    <FormItem :label="t('aerocraft.flightPlan.certified')" type="radio" v-model="baTable.form.items!.certified" prop="certified" :data="{ content: { 0: t('aerocraft.flightPlan.certified 0'), 1: t('aerocraft.flightPlan.certified 1'), 2: t('aerocraft.flightPlan.certified 2') } }" :placeholder="t('Please select field', { field: t('aerocraft.flightPlan.certified') })" />
                    <FormItem :label="t('aerocraft.flightPlan.mobile')" type="number" prop="mobile" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.mobile" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.mobile') })" />
                    <FormItem :label="t('aerocraft.flightPlan.city')" type="city" v-model="baTable.form.items!.city" prop="city" :placeholder="t('Please select field', { field: t('aerocraft.flightPlan.city') })" />
                    <FormItem :label="t('aerocraft.flightPlan.departure_point')" type="number" prop="departure_point" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.departure_point" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.departure_point') })" />
                    <FormItem :label="t('aerocraft.flightPlan.type')" type="radio" v-model="baTable.form.items!.type" prop="type" :data="{ content: { 0: t('aerocraft.flightPlan.type 0'), 1: t('aerocraft.flightPlan.type 1'), 2: t('aerocraft.flightPlan.type 2') } }" :placeholder="t('Please select field', { field: t('aerocraft.flightPlan.type') })" />
                    <FormItem :label="t('aerocraft.flightPlan.range')" type="number" prop="range" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.range" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.range') })" />
                    <FormItem :label="t('aerocraft.flightPlan.height')" type="number" prop="height" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.height" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.height') })" />
                    <FormItem :label="t('aerocraft.flightPlan.start_time')" type="number" prop="start_time" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.start_time" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.start_time') })" />
                    <FormItem :label="t('aerocraft.flightPlan.end_time')" type="number" prop="end_time" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.end_time" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.end_time') })" />
                    <FormItem :label="t('aerocraft.flightPlan.staff')" type="string" v-model="baTable.form.items!.staff" prop="staff" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.staff') })" />
                    <FormItem :label="t('aerocraft.flightPlan.staff_license')" type="string" v-model="baTable.form.items!.staff_license" prop="staff_license" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.staff_license') })" />
                    <FormItem :label="t('aerocraft.flightPlan.aerocraft_sn')" type="textarea" v-model="baTable.form.items!.aerocraft_sn" prop="aerocraft_sn" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.aerocraft_sn') })" />
                    <FormItem :label="t('aerocraft.flightPlan.aerocraft_img')" type="string" v-model="baTable.form.items!.aerocraft_img" prop="aerocraft_img" :placeholder="t('Please input field', { field: t('aerocraft.flightPlan.aerocraft_img') })" />
                    <FormItem :label="t('aerocraft.flightPlan.status')" type="radio" v-model="baTable.form.items!.status" prop="status" :data="{ content: { 0: t('aerocraft.flightPlan.status 0'), 1: t('aerocraft.flightPlan.status 1'), 2: 'status 2' } }" :placeholder="t('Please select field', { field: t('aerocraft.flightPlan.status') })" />
                </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({
    user_id: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.user_id') })],
    mobile: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.mobile') })],
    departure_point: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.departure_point') })],
    range: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.range') })],
    height: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.height') })],
    start_time: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.start_time') })],
    end_time: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.end_time') })],
    updatetime: [buildValidatorData({ name: 'date', title: t('aerocraft.flightPlan.updatetime') })],
    createtime: [buildValidatorData({ name: 'date', title: t('aerocraft.flightPlan.createtime') })],
    deletetime: [buildValidatorData({ name: 'number', title: t('aerocraft.flightPlan.deletetime') })],
})
</script>

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