<!-- 搜索房间ID组件 -->
<template>
  <div class="search-room-input">
    <el-tooltip
      v-model="showTips"
      :manual="true"
      effect="dark"
      :content="tipsContent"
    >
      <el-input
        v-model.trim="content"
        v-loading="loading"
        :class="{
          success: roomProfile.id && roomProfile.id > 0,
          fail: (!roomProfile.id || roomProfile.id < 0) && failShow
        }"
        :placeholder="$t('pages.searchRoomInput.placeholder.roomId')"
        class="input-with-select"
        @blur="clickSearch"
      >
        <el-select
          slot="prepend"
          v-model="selectType"
          :placeholder="$t('pages.searchRoomInput.placeholder.type')"
          @change="clickSearch"
        >
          <el-option :label="$t('pages.searchRoomInput.option.shortId')" value="SHORT_ID" />
          <el-option :label="$t('pages.searchRoomInput.option.longId')" value="LONG_ID" />
        </el-select>
      </el-input>
    </el-tooltip>

    <el-dialog
      :title="$t('pages.searchRoomInput.dialog.title')"
      :visible="visibleSelecteDialog"
      :before-close="handleClose"
      :append-to-body="true"
    >
      <span>{{ $t('pages.searchRoomInput.dialog.selectRoom') }}</span>
      <div class="select-room">
        <div
          v-for="(item, index) in roomProfiles"
          :key="index"
          class="user-item flex-l"
        >
          <el-image
            style="width: 40px; height: 40px; margin-right:10px;border-radius: 100%; flex-shrink: 0;"
            :src="item.roomCover"
            :preview-src-list="[item.roomCover]"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
          <sys-origin-icon :icon="item.sysOrigin" />
          <div class="nickname nowrap-ellipsis">
            {{ item.roomName }}
          </div>
          <div class="selected-but">
            <el-button type="text" @click="clickSelected(item)">{{ $t('pages.searchRoomInput.action.select') }}</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRoomProfileBySysOriginAccount,
  listRoomProfileByAccount
} from "@/api/room";
export default {
  name: "SearchRoomInput",
  props: {
    sysOrigin: {
      type: String,
      require: false,
      default: ""
    },
    roomId: {
      type: String,
      require: false,
      default: ""
    }
  },
  data() {
    return {
      showTips: false,
      tipsContent: "",
      loading: false,
      content: "",
      selectType: "SHORT_ID",
      failShow: false,
      roomProfile: {},
      roomProfiles: [],
      visibleSelecteDialog: false
    };
  },
  watch: {
    roomId: {
      handler(val) {
        if (val === "") {
          return;
        }
        if (val.length > 15) {
          this.selectType = "LONG_ID";
          this.content = val;
          return;
        }
        if (val.length < 15) {
          this.selectType = "SHORT_ID";
          this.content = val;
          this.clickSearch();
          return;
        }
      },
      immediate: true
    }
  },
  methods: {
    // 关闭对话窗
    handleClose() {
      this.$emit("closeSelectDialog");
      this.visibleSelecteDialog = false;
    },

    // 选择房间
    clickSelected(item) {
      this.visibleSelecteDialog = false;
      this.$emit("success", item);
    },

    // 失败提示
    emitFail() {
      this.roomProfiles = [];
      this.roomProfile = {};
      this.failShow = true;
      this.$emit("fail");
    },

    // 失败信息提示
    failTipsContent(content) {
      this.emitFail();
      this.showTips = true;
      this.tipsContent = content;
    },

    // 搜索房间
    clickSearch() {
      const that = this;
      that.$emit("load");
      that.failShow = false;
      that.showTips = false;
      if (!that.content) {
        that.emitFail();
        that.showTips = false;
        that.failShow = false;
        return;
      }
      if (!/\d+/.test(that.content)) {
        that.failTipsContent(this.$t("pages.searchRoomInput.message.contentMustBePositiveInteger"));
        return;
      }
      if (that.content.length < 1) {
        that.failTipsContent(this.$t("pages.searchRoomInput.message.contentLengthInvalid"));
        return;
      }
      if (
        that.roomProfile &&
        (that.roomProfile.roomAccount === that.content ||
          that.roomProfile.roomAccount === that.content)
      ) {
        that.$emit("success", that.roomProfile);
        return;
      }
      if (this.selectType === "LONG_ID") return;

      that.loading = true;
      that
        .search(that.selectType, that.content)
        .then(res => {
          that.loading = false;
          const result = res.body;
          if (!result) {
            that.failTipsContent(this.$t("pages.searchRoomInput.message.roomNotFound"));
            return;
          }

          if (result.length > 0) {
            this.failShow = false;
            that.roomProfiles = result;
            if (that.roomProfiles.length === 1) {
              that.roomProfile = that.roomProfiles[0];
              that.$emit("success", that.roomProfile);
              return;
            }
            that.visibleSelecteDialog = true;
            return;
          }

          if (result.length === 0) {
            that.failTipsContent(this.$t("pages.searchRoomInput.message.roomNotFound"));
            return;
          }

          that.roomProfile = result;
          that.$emit("success", that.roomProfile);
        })
        .catch(er => {
          that.loading = false;
          console.error("clickSearch", er);
          that.failTipsContent(this.$t("pages.searchRoomInput.message.requestFailed"));
        });
    },

    // 调用搜索接口
    search(type, id) {
      if (type === "LONG_ID") {
        // return getRoomProfileByRoomId(id)
      }
      if (type === "SHORT_ID") {
        return this.sysOrigin
          ? getRoomProfileBySysOriginAccount(this.sysOrigin, id)
          : listRoomProfileByAccount(id);
      }
    }
  }
};
</script>
<style lang="scss">
.search-room-input {
  .el-select .el-input {
    width: 100px;
  }
  .success > .el-input__inner {
    border: 1px solid #67c23a;
  }
  .fail > .el-input__inner {
    border: 1px solid #f56c6c;
  }
}
</style>
<style scoped lang="scss">
.search-room-input {
  .select-room {
    .user-item {
      padding: 5px 0rem;
      img {
        border-radius: 100%;
      }
      .nickname {
        width: 100%;
        padding: 10px;
      }
    }
  }
}
</style>
