<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="500px"
    :visible="visible"
    :confirm-loading="loading"
    :title="`修改价格`"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form layout="horizontal">
      <a-form-item>
        <a-input-number :min="0" style="width: 200px" v-model:value="content" />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import { updateOrder } from '@/api/order';
  import { Order } from '@/api/order/model';
  import { createOrderNo } from "@/utils/common";
  // import { reloadPageTab } from '@/utils/page-tab-util';

  const useForm = Form.useForm;
  // 是否是修改
  const isUpdate = ref(false);
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    data?: Order | null;
    // 修改回显的数据
    field?: string | null;
    orderId?: number | 0;
    content?: number | 0;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  const content = ref<number>(0);
  const placeholder = ref('请输入订单金额');
  // 用户信息
  const form = reactive<Order>({
    orderId: 0,
    comments: '',
    payPrice: undefined
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  const { resetFields, validate } = useForm(form);

  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        // 判断更新字段
        form.orderId = props.orderId;
        if (props.field === 'payPrice') {
          form.payPrice = Number(content.value);
          form.totalPrice = Number(content.value);
          form.orderNo = createOrderNo();
        }
        updateOrder(form)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.orderId) {
          loading.value = false;
          content.value = props.content;
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }

      if (props.field == 'tenantCode') {
        placeholder.value = '请输入要绑定的主体编号';
        content.value = undefined;
      }
    }
  );
</script>
<style lang="less">
  .tab-pane {
    min-height: 300px;
  }
</style>
