<template>
  <div class="seats_container">
    <div v-for="item in seatsList" class="seats_item" v-loading="item.loading" element-loading-text="加载中..."
         element-loading-svg-view-box="-10, -10, 50, 50"
         element-loading-background="rgba(122, 122, 122, 0.4)">
      <img src="@/assets/img/seats/service.png" alt="" class="seats_item_service">
      <p class="seats_item_tel">{{ item.telNo }}</p>
      <div class="seats_item_state">
        <img :src="getImageUrl('seats/'+item.state+'.png')" alt="">
        <span> {{ currentStatusText(item.state) }}</span>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import {onMounted, ref, onUnmounted, watch} from "vue";
import {getImageUrl} from "@/utils/tools";
import mittBus from '@/utils/mitt';
const props = defineProps({
  data: {
    type: Array,
    default: () => [],
  },
});
// 设置当前状态的值
const currentStatusText = (state: string) => {
  const statusMap: any = {
    logout: '签出',
    login: '签入',
    ready: '示闲',
    unready: '小休',
    busy: '通话中',
    acw: '整理中',
    held: '保持',
    treeWay: '三方会议',
  };
  return statusMap[state] || '';
};
const seatsList = ref<any>([])
watch(
  () => props.data,
  (newData:any) => {
    seatsList.value = newData;
  },
  { immediate: true }
);
onMounted(() => {
  // 接收消息
  mittBus.on('SeatState', (res: any) => {
    const item = seatsList.value.find((item: any) => item.telNo === res.telNo);
    item.loading = true;
    if (item) {
      setTimeout(() => {
        item.state = res.state;
        item.loading = false;
      }, 500);
    }
  });
});
onUnmounted(() => {
  mittBus.off('SeatState');
});
</script>
<style scoped lang="scss">
.seats_container {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fill, 100px);
  grid-gap: 21px;
  overflow: auto;

  .seats_item {
    width: 100px;
    height: 130px;
    border: 1px solid var(--el-color-primary-light-3);
    border-radius: 8px;
    text-align: center;
    padding: 7px 0;
    animation: bounce-out 0.3s ease;

    &_service {
      margin: 0 auto;
      width: 40px;
    }

    &_tel {
      font-size: var(--el-font-size-medium);
      color: var(--el-color-white);
      margin-top: 8px;
    }

    &_state {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 12px;

      img {
        width: 25px;
        height: 25px;
      }

      span {
        font-size: var(--el-font-size-medium);
        color: var(--el-color-white);
        margin-left: 5px;
      }
    }
  }
}
</style>