<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑' : '添加'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 4, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="关联订单表id" name="oid">
        <a-input
          allow-clear
          placeholder="请输入关联订单表id"
          v-model:value="form.oid"
        />
      </a-form-item>
      <a-form-item label="关联场馆id" name="sid">
        <a-input
          allow-clear
          placeholder="请输入关联场馆id"
          v-model:value="form.sid"
        />
      </a-form-item>
      <a-form-item label="关联场地id" name="fid">
        <a-input
          allow-clear
          placeholder="请输入关联场地id"
          v-model:value="form.fid"
        />
      </a-form-item>
      <a-form-item label="场馆" name="siteName">
        <a-input
          allow-clear
          placeholder="请输入场馆"
          v-model:value="form.siteName"
        />
      </a-form-item>
      <a-form-item label="场地" name="fieldName">
        <a-input
          allow-clear
          placeholder="请输入场地"
          v-model:value="form.fieldName"
        />
      </a-form-item>
      <a-form-item label="预约时间段" name="dateTime">
        <a-input
          allow-clear
          placeholder="请输入预约时间段"
          v-model:value="form.dateTime"
        />
      </a-form-item>
      <a-form-item label="单价" name="price">
        <a-input
          allow-clear
          placeholder="请输入单价"
          v-model:value="form.price"
        />
      </a-form-item>
      <a-form-item label="儿童价" name="childrenPrice">
        <a-input
          allow-clear
          placeholder="请输入儿童价"
          v-model:value="form.childrenPrice"
        />
      </a-form-item>
      <a-form-item label="成人人数" name="adultNum">
        <a-input
          allow-clear
          placeholder="请输入成人人数"
          v-model:value="form.adultNum"
        />
      </a-form-item>
      <a-form-item label="儿童人数" name="childrenNum">
        <a-input
          allow-clear
          placeholder="请输入儿童人数"
          v-model:value="form.childrenNum"
        />
      </a-form-item>
      <a-form-item label="1已付款，2未付款，3无需付款或占用状态" name="payStatus">
        <a-input
          allow-clear
          placeholder="请输入1已付款，2未付款，3无需付款或占用状态"
          v-model:value="form.payStatus"
        />
      </a-form-item>
      <a-form-item label="是否免费：1免费、2收费" name="isFree">
        <a-input
          allow-clear
          placeholder="请输入是否免费：1免费、2收费"
          v-model:value="form.isFree"
        />
      </a-form-item>
      <a-form-item label="是否支持儿童票：1支持，2不支持" name="isChildren">
        <a-input
          allow-clear
          placeholder="请输入是否支持儿童票：1支持，2不支持"
          v-model:value="form.isChildren"
        />
      </a-form-item>
      <a-form-item label="预订类型：1全场，2半场" name="type">
        <a-input
          allow-clear
          placeholder="请输入预订类型：1全场，2半场"
          v-model:value="form.type"
        />
      </a-form-item>
      <a-form-item label="组合数据:日期+时间段+场馆id+场地id" name="mergeData">
        <a-input
          allow-clear
          placeholder="请输入组合数据:日期+时间段+场馆id+场地id"
          v-model:value="form.mergeData"
        />
      </a-form-item>
      <a-form-item label="开场时间" name="startTime">
        <a-input
          allow-clear
          placeholder="请输入开场时间"
          v-model:value="form.startTime"
        />
      </a-form-item>
      <a-form-item label="下单时间" name="orderTime">
        <a-input
          allow-clear
          placeholder="请输入下单时间"
          v-model:value="form.orderTime"
        />
      </a-form-item>
      <a-form-item label="毫秒时间戳" name="timeFlag">
        <a-input
          allow-clear
          placeholder="请输入毫秒时间戳"
          v-model:value="form.timeFlag"
        />
      </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, uuid } from 'ele-admin-pro';
  import { addBookingOrderInfo, updateBookingOrderInfo } from '@/api/booking/bookingOrderInfo';
  import { BookingOrderInfo } from '@/api/booking/bookingOrderInfo/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { FileRecord } from '@/api/system/file/model';

  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: BookingOrderInfo | null;
  }>();

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

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);
  const images = ref<ItemType[]>([]);

  // 用户信息
  const form = reactive<BookingOrderInfo>({
    id: undefined,
    oid: undefined,
    sid: undefined,
    fid: undefined,
    siteName: undefined,
    fieldName: undefined,
    dateTime: undefined,
    price: undefined,
    childrenPrice: undefined,
    adultNum: undefined,
    childrenNum: undefined,
    payStatus: undefined,
    isFree: undefined,
    isChildren: undefined,
    type: undefined,
    mergeData: undefined,
    startTime: undefined,
    orderTime: undefined,
    timeFlag: undefined,
    tenantId: undefined
  });

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

  // 表单验证规则
  const rules = reactive({
    bookingOrderInfoName: [
      {
        required: true,
        type: 'string',
        message: '请填写名称',
        trigger: 'blur'
      }
    ]
  });

  const chooseImage = (data: FileRecord) => {
    images.value.push({
      uid: data.id,
      url: data.path,
      status: 'done'
    });
    form.image = data.path;
  };

  const onDeleteItem = (index: number) => {
    images.value.splice(index, 1);
    form.image = '';
  };

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

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const formData = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateBookingOrderInfo : addBookingOrderInfo;
        saveOrUpdate(formData)
          .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) {
        images.value = [];
        if (props.data) {
          assignObject(form, props.data);
          if(props.data.image){
            images.value.push({
              uid: uuid(),
              url: props.data.image,
              status: 'done'
            })
          }
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    },
    { immediate: true }
  );
</script>
