<!-- 编辑弹窗 -->
<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="houseTitle">
        <a-input
          allow-clear
          placeholder="请输入房源标题"
          v-model:value="form.houseTitle"
        />
      </a-form-item>
      <a-form-item label="房产所在的城市" name="cityByHouse">
        <a-input
          allow-clear
          placeholder="请输入房产所在的城市"
          v-model:value="form.cityByHouse"
        />
      </a-form-item>
      <a-form-item label="户型" name="houseType">
        <a-input
          allow-clear
          placeholder="请输入户型"
          v-model:value="form.houseType"
        />
      </a-form-item>
      <a-form-item label="租赁方式" name="leaseMethod">
        <a-input
          allow-clear
          placeholder="请输入租赁方式"
          v-model:value="form.leaseMethod"
        />
      </a-form-item>
      <a-form-item label="租金" name="rent">
        <a-input
          allow-clear
          placeholder="请输入租金"
          v-model:value="form.rent"
        />
      </a-form-item>
      <a-form-item label="月租金" name="monthlyRent">
        <a-input
          allow-clear
          placeholder="请输入月租金"
          v-model:value="form.monthlyRent"
        />
      </a-form-item>
      <a-form-item label="佣金" name="commission">
        <a-input
          allow-clear
          placeholder="请输入佣金"
          v-model:value="form.commission"
        />
      </a-form-item>
      <a-form-item label="物业费" name="propertyFees">
        <a-input
          allow-clear
          placeholder="请输入物业费"
          v-model:value="form.propertyFees"
        />
      </a-form-item>
      <a-form-item label="面积" name="extent">
        <a-input
          allow-clear
          placeholder="请输入面积"
          v-model:value="form.extent"
        />
      </a-form-item>
      <a-form-item label="楼层" name="floor">
        <a-input
          allow-clear
          placeholder="请输入楼层"
          v-model:value="form.floor"
        />
      </a-form-item>
      <a-form-item label="房号" name="roomNumber">
        <a-input
          allow-clear
          placeholder="请输入房号"
          v-model:value="form.roomNumber"
        />
      </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="password">
        <a-input
          allow-clear
          placeholder="请输入进入房屋的密码"
          v-model:value="form.password"
        />
      </a-form-item>
      <a-form-item label="房屋朝向" name="toward">
        <a-input
          allow-clear
          placeholder="请输入房屋朝向"
          v-model:value="form.toward"
        />
      </a-form-item>
      <a-form-item label="房屋标签" name="houseLabel">
        <a-input
          allow-clear
          placeholder="请输入房屋标签"
          v-model:value="form.houseLabel"
        />
      </a-form-item>
      <a-form-item label="办公室配套" name="supporting">
        <a-input
          allow-clear
          placeholder="请输入办公室配套"
          v-model:value="form.supporting"
        />
      </a-form-item>
      <a-form-item label="房源视频" name="videoUrl">
        <a-input
          allow-clear
          placeholder="请输入房源视频"
          v-model:value="form.videoUrl"
        />
      </a-form-item>
      <a-form-item label="图片附件" name="files">
        <a-input
          allow-clear
          placeholder="请输入图片附件"
          v-model:value="form.files"
        />
      </a-form-item>
      <a-form-item label="房源介绍" name="content">
        <a-input
          allow-clear
          placeholder="请输入房源介绍"
          v-model:value="form.content"
        />
      </a-form-item>
      <a-form-item label="到期时间" name="expirationTime">
        <a-input
          allow-clear
          placeholder="请输入到期时间"
          v-model:value="form.expirationTime"
        />
      </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="area">
        <a-input
          allow-clear
          placeholder="请输入所在地区"
          v-model:value="form.area"
        />
      </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="longitude">
        <a-input
          allow-clear
          placeholder="请输入经度"
          v-model:value="form.longitude"
        />
      </a-form-item>
      <a-form-item label="纬度" name="latitude">
        <a-input
          allow-clear
          placeholder="请输入纬度"
          v-model:value="form.latitude"
        />
      </a-form-item>
      <a-form-item label="是否推荐" name="recommend">
        <a-input
          allow-clear
          placeholder="请输入是否推荐"
          v-model:value="form.recommend"
        />
      </a-form-item>
      <a-form-item label="是否必看" name="mustSee">
        <a-input
          allow-clear
          placeholder="请输入是否必看"
          v-model:value="form.mustSee"
        />
      </a-form-item>
      <a-form-item label="是否可溢价" name="premium">
        <a-input
          allow-clear
          placeholder="请输入是否可溢价"
          v-model:value="form.premium"
        />
      </a-form-item>
      <a-form-item label="租期" name="tenancy">
        <a-input
          allow-clear
          placeholder="请输入租期"
          v-model:value="form.tenancy"
        />
      </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="authentication">
        <a-input
          allow-clear
          placeholder="请输入是否实名认证"
          v-model:value="form.authentication"
        />
      </a-form-item>
      <a-form-item label="状态 10待审核 20驳回 30通过" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="0">显示</a-radio>
          <a-radio :value="1">隐藏</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-item label="用户ID" name="userId">
        <a-input
          allow-clear
          placeholder="请输入用户ID"
          v-model:value="form.userId"
        />
      </a-form-item>
      <a-form-item label="是否删除, 0否, 1是" name="deleted">
        <a-input
          allow-clear
          placeholder="请输入是否删除, 0否, 1是"
          v-model:value="form.deleted"
        />
      </a-form-item>
      <a-form-item label="修改时间" name="updateTime">
        <a-input
          allow-clear
          placeholder="请输入修改时间"
          v-model:value="form.updateTime"
        />
      </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 { addHouseInfo, updateHouseInfo } from '@/api/house/houseInfo';
  import { HouseInfo } from '@/api/house/houseInfo/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?: HouseInfo | null;
    // 类型
    type?: 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<HouseInfo>({
    houseId: undefined,
    houseTitle: undefined,
    cityByHouse: undefined,
    houseType: undefined,
    leaseMethod: undefined,
    rent: undefined,
    monthlyRent: undefined,
    commission: undefined,
    propertyFees: undefined,
    extent: undefined,
    floor: undefined,
    roomNumber: undefined,
    realName: undefined,
    phone: undefined,
    password: undefined,
    toward: undefined,
    houseLabel: undefined,
    supporting: undefined,
    videoUrl: undefined,
    files: undefined,
    content: undefined,
    expirationTime: undefined,
    province: undefined,
    city: undefined,
    region: undefined,
    area: undefined,
    address: undefined,
    longitude: undefined,
    latitude: undefined,
    recommend: undefined,
    mustSee: undefined,
    premium: undefined,
    tenancy: undefined,
    comments: undefined,
    authentication: undefined,
    status: undefined,
    sortNumber: undefined,
    userId: undefined,
    deleted: undefined,
    tenantId: undefined,
    createTime: undefined,
    updateTime: undefined,
    houseInfoId: undefined,
    houseInfoName: '',
    status: 0,
    comments: '',
    sortNumber: 100
  });

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

  // 表单验证规则
  const rules = reactive({
    houseInfoName: [
      {
        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 ? updateHouseInfo : addHouseInfo;
        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>
