<!-- 编辑弹窗 -->
<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="场地名称" name="fieldName">
        <a-input
          allow-clear
          placeholder="请输入场地名称"
          v-model:value="form.fieldName"
        />
      </a-form-item>
      <a-form-item label="是否可预订半场" name="isHalf">
        <a-radio-group v-model:value="form.isHalf">
          <a-radio :value="1">可以</a-radio>
          <a-radio :value="2">不可以</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="是否可重复预定" name="isRepeat">
        <a-radio-group v-model:value="form.isRepeat">
          <a-radio :value="1">可以</a-radio>
          <a-radio :value="2">不可以</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="可重复预订数" name="num" v-if="form.isRepeat == 1">
        <a-input-number
          allow-clear
          style="width: 120px"
          placeholder="可重复预订数"
          v-model:value="form.num"
        />
      </a-form-item>

      <a-form-item label="是否是卫生间" name="isToilet">
        <a-radio-group v-model:value="form.isToilet">
          <a-radio :value="1">是</a-radio>
          <a-radio :value="2">否</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="是否支持儿童价" name="isChildren">
        <a-radio-group v-model:value="form.isChildren">
          <a-radio :value="1">支持</a-radio>
          <a-radio :value="2">不支持</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="显示在第几行" name="row">
        <a-input
          allow-clear
          placeholder="请输入显示在第几行"
          v-model:value="form.row"
        />
      </a-form-item>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入描述"
          v-model:value="form.comments"
        />
      </a-form-item>
      <a-form-item label="场地状态" name="isStatus">
        <a-radio-group v-model:value="form.isStatus">
          <a-radio :value="1">开启</a-radio>
          <a-radio :value="2">关闭</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="排序号" name="sortNumber">
        <a-input-number
          :min="0"
          :max="9999"
          class="ele-fluid"
          placeholder="请输入排序号"
          v-model:value="form.sortNumber"
        />
      </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 { addField, updateField } from '@/api/booking/field';
  import { Field } from '@/api/booking/field/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';

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

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 商户ID
    categoryId?: number;
    // 修改回显的数据
    data?: Field | 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<Field>({
    fieldId: undefined,
    fieldName: undefined,
    image: undefined,
    categoryId: 0,
    userId: 0,
    teacherId: 0,
    merchantId: 0,
    isHalf: 2,
    isRepeat: 2,
    isToilet: 2,
    isChildren: 2,
    row: undefined,
    num: 0,
    startTime: undefined,
    maxNumber: undefined,
    address: '',
    comments: '',
    isStatus: 1,
    status: 0,
    sortNumber: 0
  });

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

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

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

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        form.merchantId = props.categoryId;
        const formData = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateField : addField;
        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>
