<!-- 编辑弹窗 -->
<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="name">
        <a-input
          allow-clear
          placeholder="请输入仓库名称"
          v-model:value="form.name"
        />
      </a-form-item>
      <a-form-item label="仓库编号" name="code">
        <a-input
          allow-clear
          placeholder="请输入唯一标识"
          v-model:value="form.code"
        />
      </a-form-item>
      <a-form-item label="类型" name="type">
        <a-input
          allow-clear
          placeholder="请输入类型 中心仓,区域仓,门店仓"
          v-model:value="form.type"
        />
      </a-form-item>
      <a-form-item label="仓库地址" name="address">
        <a-input
          allow-clear
          placeholder="请输入仓库地址"
          v-model:value="form.address"
        />
      </a-form-item>
      <a-form-item label="仓库管理员" name="realName">
        <a-input
          allow-clear
          placeholder="请输入真实姓名"
          v-model:value="form.realName"
        />
      </a-form-item>
      <a-form-item label="联系电话" name="phone">
        <a-input
          allow-clear
          placeholder="请输入联系电话"
          v-model:value="form.phone"
        />
      </a-form-item>
      <a-form-item label="所在省份" name="province">
        <a-input
          allow-clear
          placeholder="请输入所在省份"
          v-model:value="form.province"
        />
      </a-form-item>
      <a-form-item label="所在城市" name="city">
        <a-input
          allow-clear
          placeholder="请输入所在城市"
          v-model:value="form.city"
        />
      </a-form-item>
      <a-form-item label="所在辖区" name="region">
        <a-input
          allow-clear
          placeholder="请输入所在辖区"
          v-model:value="form.region"
        />
      </a-form-item>
      <a-form-item label="经纬度" name="lngAndLat">
        <a-input
          allow-clear
          placeholder="请输入经纬度"
          v-model:value="form.lngAndLat"
        />
      </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="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 } from 'ele-admin-pro';
  import { addShopStoreWarehouse, updateShopStoreWarehouse } from '@/api/shop/shopStoreWarehouse';
  import { ShopStoreWarehouse } from '@/api/shop/shopStoreWarehouse/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;
    // 修改回显的数据
    data?: ShopStoreWarehouse | 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<ShopStoreWarehouse>({
    id: undefined,
    name: undefined,
    code: undefined,
    type: undefined,
    address: undefined,
    realName: undefined,
    phone: undefined,
    province: undefined,
    city: undefined,
    region: undefined,
    lngAndLat: undefined,
    userId: undefined,
    isDelete: undefined,
    tenantId: undefined,
    createTime: undefined,
    updateTime: undefined,
    comments: '',
    sortNumber: 100
  });

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

  // 表单验证规则
  const rules = reactive({
    shopStoreWarehouseName: [
      {
        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;
        const formData = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateShopStoreWarehouse : addShopStoreWarehouse;
        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);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    },
    { immediate: true }
  );
</script>
