﻿<script lang="ts" setup>
    import { reactive, ref } from "vue";
    import { FormInstance } from "ant-design-vue";
    import Tools from "@/core/utils/Tools";
    import T_User_IntentOrderService from "@/services/apps/T_User_IntentOrders/T_User_IntentOrderService";

    //定义组件事件
    const props = defineProps<{ onSuccess: () => void }>();

    const state = reactive({
        vm: {
            id: "",
            form: {} as any,
        },
        visible: false,
        loading: false,
    });

    //表单实例
    const refForm = ref<FormInstance>();

    //向父级导出 函数
    defineExpose({
        /**
         * 打开表单初始化
         * @param key
         */
        open: (key: string = "") => {
            state.visible = true;
            if (state.visible) {
                state.vm.id = key;
            }
            refForm.value?.resetFields();
            //初始化表单数据
            state.loading = true;
        T_User_IntentOrderService.findForm(key).then((res) => {
                state.loading = false;
                if (res.code != 200) return;
                state.vm = res.data;
            });
        },
    });

    /**
     *保存数据
     */
    function save() {
        refForm.value?.validate().then(async () => {
            try {
                state.loading = true;
                const result = await T_User_IntentOrderService.saveForm(state.vm.id, state.vm.form);
                state.loading = false;
                if (result.code != 200) return;
                Tools.message.success("操作成功!");
                props.onSuccess();
                state.visible = false;
            } catch (error) {
                state.loading = false;
            }
        });
    }
</script>

<template>
<a-modal v-model:open="state.visible" :title="state.vm.id ? '编辑' : '新建'" centered @ok="state.visible = false" :width="800">
<template #footer>
<a-button type="primary" :loading="state.loading" @click="save()"> 提交</a-button>
<a-button @click="state.visible = false">关闭</a-button>
    </template>
<a-spin :spinning="state.loading">
<a-form ref="refForm" layout="vertical" :model="state.vm.form">
<a-row :gutter="[16, 0]">
<a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
<a-form-item label="项目" name="tenantId">
<hm-tenant-select v-model:value="state.vm.form.tenantId" />
                     </a-form-item>
                </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="创建时间" name="createdAt" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.createdAt" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="订单创建时间" name="intentDate" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.intentDate" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="支付方式" name="method" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.method" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="备注" name="notes" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.notes" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="订单Id" name="orderId" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.orderId" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="价格" name="price" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.price" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="产品id" name="productId" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.productId" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="数量" name="quantity" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.quantity" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="状态" name="status" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.status" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="修改时间" name="updatedAt" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.updatedAt" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        <a-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <a-form-item label="用户Id" name="userId" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <a-input v-model:value="state.vm.form.userId" placeholder="请输入" />
                            </a-form-item>
                        </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>