<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="社区名称" prop="communityName">
            <el-input v-model="queryParams.communityName" placeholder="请输入社区名称" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="社区标题" prop="communityTitle">
            <el-input v-model="queryParams.communityTitle" placeholder="请输入社区标题" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="主数据" prop="isMaster">
            <el-input v-model="queryParams.isMaster" placeholder="是否主数据" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              v-hasPermi="['system:community:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              v-hasPermi="['system:community:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              v-hasPermi="['system:community:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
              v-hasPermi="['system:community:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="communityList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="社区ID" align="center" prop="communityId" v-if="true" />
        <el-table-column label="社区名称" align="center" prop="communityName" />
        <el-table-column label="社区标题" align="center" prop="communityTitle" />
        <el-table-column label="主图展示" align="center" prop="url">
          <template #default="scope">
            <ImagePreview :width="100" :height="100" :src="scope.row.url" :preview-src-list="[scope.row.url]" />
          </template>
        </el-table-column>
        <el-table-column label="社区描述" align="center" prop="communityDesc" />
        <el-table-column label="主数据" align="center" prop="isMaster" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:community:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:community:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改社区对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="communityFormRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="9">
            <el-form-item label="社区名称" prop="communityName">
              <el-input v-model="form.communityName" placeholder="请输入社区名称" />
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="社区标题" prop="communityTitle">
              <el-input v-model="form.communityTitle" placeholder="请输入社区标题" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="主数据" prop="isMaster">
              <el-checkbox true-label="1" false-label="0" v-model="form.isMaster" label="主数据" size="large" border />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="社区描述" prop="communityDesc">
              <el-input v-model="form.communityDesc" placeholder="请输社区描述" show-word-limit type="textarea" rows="5"
                style="width: 800px;" />
              <!-- <el-input v-model="form.communityDesc" type="textarea" placeholder="请输入内容" /> -->
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="主数据" prop="isMaster">
          <el-input v-model="form.isMaster" placeholder="是否主数据" />
        </el-form-item> -->

        <el-upload v-model:file-list="fileList" :multiple="true" :action="upload.url" :on-preview="handlePreview"
          :on-remove="handleRemove" :before-remove="beforeRemove" :headers="upload.headers"
          :on-success="handleUploadSuccess" :before-upload="handleBeforeUpload" list-type="picture-card">
          上传社区图
          <template #tip>
            <div class="el-upload__tip">
              小于500KB的jpg/png/jpeg文件
            </div>
          </template>
          <template #file="{ file }">
            <div>
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
              <span class="el-upload-list__item-actions">
                <span @click="onClickDefaultPhoto(file)">
                  <el-icon v-if="file.defaults">
                    <Open />
                  </el-icon>
                  <el-icon v-else>
                    <TurnOff />
                  </el-icon>
                </span>
                <span v-if="!disabled" @click="onClickDeletePhoto(file)">
                  <el-icon>
                    <Delete />
                  </el-icon>
                </span>
              </span>
            </div>
          </template>
        </el-upload>

        <!-- 添加房型 -->
        <div id="app">
          <el-button @click="onClickOpen">添加房型</el-button>
          <el-form :model="datas.queryParams" ref="queryFormRefRoom" :inline="true" label-width="68px">
            <el-dialog title="房型列表" v-model="dialogVisible" close-on-click-modal="false" width="80%" top="5vh"
              :before-close="handleClose">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" :disabled="multiple" @click="handleAddRoom()"
                  v-hasPermi="['system:room:add']">添加房型</el-button>
              </el-col>

              <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
                <div class="search" v-show="showSearch">
                  <el-form :model="datas.queryParams" ref="queryFormRefRoom" :inline="true" label-width="68px">
                    <el-form-item label="房型名称" prop="roomName">
                      <el-input v-model="datas.queryParams.roomName" placeholder="请输入" />
                    </el-form-item>
                    <el-form-item label="房型标题" prop="roomTitle">
                      <el-input v-model="datas.queryParams.roomTitle" placeholder="请输入" />
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" icon="Search" @click="handleQueryRoom">搜索</el-button>
                      <el-button icon="Refresh" @click="resetQueryRoom">重置</el-button>
                    </el-form-item>
                  </el-form>
                </div>
              </transition>

              <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChangeRoom">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="房型id" align="center" prop="roomId" v-if="true" />
                <el-table-column label="房型名称" align="center" prop="roomName" />
                <el-table-column label="房型标题" align="center" prop="roomTitle" />
                <el-table-column label="主数据" align="center" prop="isMaster" />
                <el-table-column label="主图展示" align="center" prop="url">
                  <template #default="scope">
                    <ImagePreview :width="100" :height="100" :src="scope.row.url" :preview-src-list="[scope.row.url]" />
                  </template>
                </el-table-column>
                <el-table-column label="房型描述" align="center" prop="roomDesc" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template #default="scope">
                    <el-tooltip content="修改" placement="top">
                      <el-button link type="primary" icon="Edit" @click="handleUpdateRoom(scope.row)"
                        v-hasPermi="['system:room:edit']"></el-button>
                    </el-tooltip>
                  </template>
                </el-table-column>
              </el-table>

              <pagination v-show="roomTotal > 0" :total="roomTotal" v-model:page="datas.queryParams.pageNum"
                v-model:limit="datas.queryParams.pageSize" @pagination="onClickOpen" />
            </el-dialog>
          </el-form>
        </div>

        <!--小区房型展示列表-->
        <el-table v-loading="loading" :data="roomItem" v-show="showTable">
          <el-table-column label="房型id" align="center" prop="roomId" v-if="false" />
          <el-table-column label="房型名称" align="center" prop="roomName" />
          <el-table-column label="房型标题" align="center" prop="roomTitle" />
          <el-table-column label="主图展示" align="center" prop="url">
            <template #default="scope">
              <div v-if="scope.row.sysimg.find((item: any) => item.enumC === '0')">
                <div v-for="item in scope.row.sysimg">
                  <ImagePreview :width="100" :height="100" :src="item.url" v-if="item.enumC === '0'" />
                </div>
              </div>
              <div v-else>
                <ImagePreview :width="100" :height="100" :src="''" />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="描述" align="center" prop="roomDesc" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

            <template #default="scope">
              <el-tooltip content="修改" placement="top">
                <el-button link type="primary" icon="Edit" @click="handleUpdateRoom(scope.row)"
                  v-hasPermi="['system:room:edit']"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDeleteRoom(scope.row)"
                  v-hasPermi="['system:room:remove']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

      </el-form>

      <!--弹框房型详情-->
      <el-dialog :title="dialogs.title" v-model="dialogs.visible" width="1200px" append-to-body>

        <el-form ref="regionFormRef" :model="datas.form" :rules="rules" label-width="80px">
          <el-row>
          <el-col :span="12">
            <el-form-item label="户型名称" prop="roomName">
              <el-input v-model="datas.form.roomName" placeholder="请输入户型名称" style="width: 400px;" readonly="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户型标题" prop="roomTitle">
              <el-input v-model="datas.form.roomTitle" placeholder="请输入户型标题" style="width: 400px;" readonly="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="4">
            <el-form-item label="热门户型" prop="isHot">
              <el-checkbox true-label="1" false-label="0" v-model="datas.form.isHot" label="热门" size="large" border disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="楼层" prop="floorNum">
              <el-input v-model.number="datas.form.floorNum" placeholder="请输入楼层" type="number" style="width: 108px;" readonly="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="面积" prop="roomSize">
              <el-input v-model="datas.form.roomSize" placeholder="请输入面积" type="number" style="width: 110px;" readonly="true"/>
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="朝向" prop="direction">
              <el-input v-model="datas.form.direction" placeholder="请输入朝向" style="width: 155px;" readonly="true"/>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="主数据" prop="isMaster">
              <el-checkbox true-label="1" false-label="0" v-model="datas.form.isMaster" label="主数据" size="large" border disabled/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-form-item label="户型描述" prop="roomDesc">
          <el-input v-model="datas.form.roomDesc" maxlength="200" placeholder="请输入户型描述" show-word-limit type="textarea"
            rows="5" style="width: 800px;" readonly="true"/>
        </el-form-item>

          <!-- <el-form-item label="房型名称" prop="roomName">
            <el-input v-model="datas.form.roomName" placeholder="请输入" readonly="true" />
          </el-form-item>
          <el-form-item label="房型标题" prop="roomTitle">
            <el-input v-model="datas.form.roomName" placeholder="请输入" readonly="true" />
          </el-form-item>
          <el-form-item label="房型描述" prop="roomDesc">
            <el-input v-model="datas.form.roomDesc" placeholder="请输入小区描述" show-word-limit type="textarea"
              readonly="true" />
          </el-form-item> -->

          <el-upload v-model:file-list="communityImgList" :multiple="true" :action="upload.url"
            :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove"
            :headers="upload.headers" :on-success="handleUploadSuccess" :before-upload="handleBeforeUpload"
            list-type="picture-card" :disabled="true">
            上传房型图
            <template #tip>
              <div class="el-upload__tip">
                jpg/png files with a size less than 500KB.
              </div>
            </template>
            <template #file="{ file }">
              <div>
                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                <span class="el-upload-list__item-actions">
                </span>
              </div>
            </template>
          </el-upload>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="cancelCommunity">取 消</el-button>
          </div>
        </template>
      </el-dialog>

      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>

    </el-dialog>
  </div>
</template>

<script setup name="Community" lang="ts">
import { listCommunity, getCommunity, delCommunity, addCommunity, updateCommunity } from '@/api/system/community';
import { CommunityVO, CommunityQuery, CommunityForm } from '@/api/system/community/types';
import { getRoom, listRoom } from '@/api/system/room';
import { RoomForm, RoomQuery, RoomVO } from '@/api/system/room/types';
import { propTypes } from '@/utils/propTypes';
import { globalHeaders } from "@/utils/request";
import type { UploadProps } from 'element-plus'

const upload = reactive<UploadOption>({
  headers: globalHeaders(),
  url: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload',
});

const props = defineProps({
  modelValue: [String, Object, Array],
  // 数量限制
  limit: propTypes.number.def(5),
  // 大小限制(MB)
  fileSize: propTypes.number.def(5),
  // 文件类型, 例如['png', 'jpg', 'jpeg']
  fileType: propTypes.array.def(["png", "jpg", "jpeg"]),
  // 是否显示提示
  isShowTip: propTypes.bool.def(true),
});

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const communityList = ref<CommunityVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const communityFormRef = ref<ElFormInstance>();

const fileList = ref<any[]>([]);
const emit = defineEmits(['update:modelValue']);
const disabled = ref(false);
const number = ref(0);
const dialogVisible = ref(false); // 小区弹窗
const roomList = ref<RoomVO[]>([]);
const roomTotal = ref(0);
const roomItem = ref<any[]>([]);
const roomItemP = ref<any[]>([]);
const communityImgList = ref<any[]>([]);
let showTable = ref(false);
const queryFormRefRoom = ref<ElFormInstance>();

const dialogs = reactive<DialogOption>({
  visible: false,
  title: ''
});

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: CommunityForm = {
}

const data = reactive<PageData<CommunityForm, CommunityQuery>>({
  form: {
    communityId: undefined,
    communityName: undefined,
    communityTitle: undefined,
    communityDesc: undefined,
    isMaster: undefined,
    room: undefined,   // 房型
    sysimg: undefined,   // 图片
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    communityName: undefined,
    communityTitle: undefined,
    communityDesc: undefined,
    room: undefined,   // 房型
    sysimg: undefined,   // 图片
    isMaster: undefined,
    params: {
    }
  },
  rules: {
    communityId: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    communityName: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    communityTitle: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    communityDesc: [
      { required: true, message: "不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

const initFormDatas: CommunityForm = {
}
const datas = reactive<PageData<RoomForm, RoomQuery>>({
  form: {
    roomId: undefined,
    roomName: undefined,
    roomTitle: undefined,
    roomDesc: undefined,
    isHot: undefined,
    roomSize: undefined,
    direction: undefined,
    floorNum: undefined,
    region: undefined,
    sysimg: undefined,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomName: undefined,
    roomTitle: undefined,
    params: {
    }
  },
  rules: {

  }
});

/** 查询社区列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCommunity(queryParams.value);
  communityList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  communityFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: CommunityVO[]) => {
  ids.value = selection.map(item => item.communityId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加社区";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: CommunityVO) => {
  reset();
  const _communityId = row?.communityId || ids.value[0]
  const res = await getCommunity(_communityId);
  fileList.value = [];
  roomItem.value = [];
  res.data.sysimg.forEach((_s: any) => {
    const imgData = { fileName: _s.fileName, e: _s.enumC, url: _s.url, ossId: _s.ossId };
    fileList.value.push(imgData);
  })

  fileList.value.forEach(item => {
    console.info(item.e, 'item.e');
    if (item.e === '0') {
      item.defaults = true;
    }
  })

  if (res.data.room.length > 0) {
    res.data.room.forEach((_r: any) => {
      let imgData = {};
      _r.sysimg.forEach((_s: any) => {
        if (_s.enumC === '0') {
          imgData = {
            sysimg: _r.sysimg, roomId: _r.roomId, roomName: _r.roomName,
            roomTitle: _r.roomTitle, roomDesc: _r.roomDesc, url: _s.url
          }
        } else {
          imgData = {
            sysimg: _r.sysimg, roomId: _r.roomId, roomName: _r.roomName,
            roomTitle: _r.roomTitle, roomDesc: _r.roomDesc, url: null
          }
        }
      });
      roomItem.value.push(imgData);
    })
    console.info(roomItem.value, 'regionItem.value');
    showTable.value = true;
  }

  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改社区";
}

/** 提交按钮 */
const submitForm = () => {
  communityFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      data.form.sysimg = fileList.value;
      data.form.room = roomItem.value;
      data.form.isMaster = data.form.isMaster === '1' ? '1' : '0';
      console.info(form.value, 'data.form.room');
      if (form.value.communityId) {
        await updateCommunity(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addCommunity(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: CommunityVO) => {
  const _communityIds = row?.communityId || ids.value;
  await proxy?.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + _communityIds + '"的数据项？').finally(() => loading.value = false);
  await delCommunity(_communityIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/community/export', {
    ...queryParams.value
  }, `community_${new Date().getTime()}.xlsx`)
}

// 上传前校检格式和大小
const handleBeforeUpload = (file: any) => {
  // 校检文件类型
  if (props.fileType.length) {
    const fileName = file.name.split('.');
    const fileExt = fileName[fileName.length - 1];
    const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
    if (!isTypeOk) {
      proxy?.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`);
      return false;
    }
  }
  // 校检文件大小
  if (props.fileSize) {
    const isLt = file.size / 1024 / 1024 < props.fileSize;
    if (!isLt) {
      proxy?.$modal.msgError(`上传文件大小不能超过 ${props.fileSize} MB!`);
      return false;
    }
  }
  proxy?.$modal.loading("正在上传文件，请稍候...");
  number.value++;
  return true;
}

const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
  console.log(uploadFile)
}

const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
  console.log(file, uploadFiles)
}

const beforeRemove: UploadProps['beforeRemove'] = (uploadFile, uploadFiles) => {
  return ElMessageBox.confirm(
    `Cancel the transfer of ${uploadFile.name} ?`
  ).then(
    () => true,
    () => false
  )
}

const handleUploadSuccess = (res: any) => {
  // 如果上传成功
  if (res.code === 200) {
    proxy?.$modal.closeLoading();
  } else {
    proxy?.$modal.loading(res.msg);
    proxy?.$modal.closeLoading();
  }
}

const onClickDefaultPhoto = (file: any) => {
  if (file.defaults) {
    file.defaults = false;
    file.enumC = '1'; //主图标识
  } else {
    file.defaults = true;
    file.enumC = '0'; //主图标识
    fileList.value.forEach(item => {
      if (item.uid !== file.uid) {
        item.defaults = false;
        item.enumC = '1'; //主图标识
      }
    })
  }
}

const onClickDeletePhoto = (file: any) => {
  const index = fileList.value.findIndex((item) => {
    return item.uid === file.uid;
  })
  fileList.value.splice(index, 1);
  emit("update:modelValue", listToString(fileList.value));
}

// 对象转成指定字符串分隔
const listToString = (list: any[], separator?: string) => {
  let strs = "";
  separator = separator || ",";
  list.forEach(item => {
    if (item.ossId) {
      strs += item.ossId + separator;
    }
  })
  return strs != "" ? strs.substring(0, strs.length - 1) : "";
}

const onClickOpen = async () => {
  datas.queryParams.roomName = '';
  datas.queryParams.roomTitle = '';
  dialogVisible.value = true;

  loading.value = true;
  const res = await listRoom(datas.queryParams);
  console.log(res.rows, 'rows');
  roomList.value = res.rows;
  roomTotal.value = res.total;
  loading.value = false;
  datas.form = { ...initFormDatas };
}

const handleClose = (done: () => void) => {
  done();
}

const handleAddRoom = (row?: RoomVO) => {
  const unique = [...roomItem.value, ...roomItemP.value].reduce((acc, current) => {
    const x = acc.find((item: { roomId: any; }) => item.roomId === current.roomId);
    if (!x) {
      return acc.concat([current]);
    } else {
      return acc;
    }
  }, []);

  console.info(unique, 'unique');

  roomItem.value = [];
  unique.forEach((_i: any) => {
    roomItem.value.push(_i);
  });

  dialogVisible.value = false; //隐藏新增小区弹框
}

const handleQueryRoom = async () => {
  queryParams.value.pageNum = 1;
  const res = await listRoom(datas.queryParams);
  roomList.value = res.rows;
  roomTotal.value = res.total;
  loading.value = false;

  // onClickOpen();
}

const resetQueryRoom = () => {
  // queryFormRef.value?.resetFields();
  datas.queryParams.roomName = '';
  datas.queryParams.roomTitle = '';
  handleQueryRoom();
}

/** 多选框选中小区数据 */
const handleSelectionChangeRoom = (selection: RoomVO[]) => {
  roomItemP.value = [];
  roomItemP.value = selection.map(items => {
    const item = {
      sysimg: items.sysimg, roomId: items.roomId, roomName: items.roomName,
      roomTitle: items.roomTitle, roomDesc: items.roomDesc, url: items.url
    };
    return item;
  });
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  showTable.value = true;
}

const handleUpdateRoom = async (row?: RoomVO) => {
  const _roomId = row?.roomId || ids.value[0]
  const res = await getRoom(_roomId);
  Object.assign(datas.form, res.data);
  dialogs.visible = true;
  communityImgList.value = res.data.sysimg;
  dialog.title = "小区详情";
}

const handleDeleteRoom = async (row?: RoomVO) => {
  const _roomId = row?.roomId || ids.value;
  await proxy?.$modal.confirm('是否确认删小区编号为"' + _roomId + '"的数据项？').finally(() => loading.value = false);
  const index = roomItem.value.findIndex((item) => item.roomId === _roomId);
  if (index !== -1) {
    roomItem.value.splice(index, 1); //删除
  }
}

const cancelCommunity = () => {
  reset();
  dialogs.visible = false;
}

onMounted(() => {
  getList();
});
</script>
