<template>
  <div ref="dragRef">
    <div class="camera" v-for="camera in cameraGroup.children" :key="camera.cameraGroupDetailId">
      <div class="IconAndCameraName">
        <div class="cameraIcon">
          <WarningFilled
            class="invalidCameraIcon"
            style="color: red"
            v-if="!carouselList.includes(camera) && playingGroup?.id === cameraGroup.id"
          />
          <img
            class="cameraImg"
            src="@/assets/icons//nine-square-grid/camera.png"
            v-if="!cameraInPlay.includes(camera)"
          />
          <img class="cameraImg" src="@/assets/icons/nine-square-grid/playingCamera.png" v-else />
        </div>

        <div
          class="cameraName"
          :class="{
            playingCamera: cameraInPlay.includes(camera),
            invalidCameraName: !carouselList.includes(camera) && playingGroup?.id === cameraGroup.id,
          }"
        >
          {{ camera.name }}
        </div>
      </div>

      <div class="cameraOperation">
        <img
          class="cameraOperationIcon"
          src="@/assets/icons/nine-square-grid/icon-delete.png"
          @click.stop="handleDelete(cameraGroup, camera)"
        />
      </div>

      <Thumbnail :imageUrl="camera.imageUrl" :code="camera.code" position="right">
        <div class="mask"></div>
      </Thumbnail>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { storeToRefs } from 'pinia';
  import type { Camera, CameraGroupType } from '@/views/disaster/monitor/splitScreenRetrieval/type';
  import { updateCameraOrderApi } from '@/api/nine-square-grid';
  import { useCameraGroupList } from '@/store/modules/useCameraGroupList';
  import { WarningFilled } from '@element-plus/icons-vue';
  import { ElMessageBox } from 'element-plus';
  import { useDraggable } from 'vue-draggable-plus';
  import Thumbnail from '@/components/thumbnail/Thumbnail.vue';

  const props = defineProps<{
    cameraGroup: CameraGroupType;
  }>();
  const dragRef = ref<HTMLElement | null>(null);
  const list = ref<Camera[]>([]);

  const { carouselList, playingGroup, cameraInPlay } = storeToRefs(useCameraGroupList());

  const { deleteCameraFromGroup, deleteCameraInPlaylist, restartPlay } = useCameraGroupList();

  function handleDelete(cameraGroup: CameraGroupType, camera: Camera) {
    const text = '删除后，相机数据不可恢复，是否确认删除？';
    ElMessageBox.confirm(text, '提示', {
      cancelButtonText: '取消',
      confirmButtonText: '确定',
      customClass: 'customMessageBox--warning',
      lockScroll: false,
    })
      .then(() => {
        if (cameraInPlay.value.includes(camera)) {
          deleteCameraInPlaylist(cameraGroup, camera);
        } else {
          deleteCameraFromGroup(cameraGroup, camera);
          restartPlay();
        }
      })
      .catch(() => {
        return;
      });
  }

  watch(
    () => props.cameraGroup.children,
    (children) => {
      list.value = children;
    },
    { immediate: true, deep: true },
  );

  useDraggable(dragRef, list, {
    animation: 150,
    ghostClass: 'ghost',
    onUpdate() {
      const updateIds = list.value.map((x, index) => {
        return {
          groupId: props.cameraGroup.id,
          cameraId: x.id,
          orderNum: index + 1,
        };
      });
      props.cameraGroup.children = list.value;
      updateCameraOrderApi(updateIds);
      restartPlay();
    },
  });
</script>

<style lang="scss" scoped>
  .camera {
    height: 32px;
    width: 100%;
    white-space: nowrap;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 72px;
    font-size: 14px;
    line-height: 32px;
    color: #333333;
    text-align: left;
    background: #f4f7ff;
    position: relative;
    &:hover {
      background-color: #cdd8ff;
    }
    &:hover .cameraOperation {
      visibility: visible;
      display: flex;
      align-items: center;
    }

    .IconAndCameraName {
      display: flex;
      align-items: center;
      .cameraIcon {
        display: flex;
        align-items: center;
        position: relative;
        .cameraImg {
          height: 24px;
          width: 24px;
          margin-right: 12px;
        }
        .invalidCameraIcon {
          height: 14px;
          color: #dd5869;
          position: absolute;
          right: 6px;
          top: 0px;
        }
      }
      .cameraName {
        width: fit-content;
        position: relative;
      }
      .invalidCameraName {
        color: #999999;
      }
    }

    .cameraOperation {
      visibility: hidden;
      margin-right: 4px;
      .cameraOperationIcon {
        height: 20px;
        width: 20px;
        margin-left: 10px;
        padding: 2px;
        z-index: 999;
        &:hover {
          background-color: rgb(197, 197, 197);
          border-radius: 4px;
          cursor: pointer;
        }
      }
    }

    .mask {
      height: 32px;
      width: 100%;
    }
  }

  :deep(.thumb-nail) {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9;
  }
</style>
