<template>
  <div class="l-flex--col">
    <grid-table
      ref="table"
      :schema="schema"
      size="large"
    >
      <grid-table-item v-slot="item">
        <camera-player
          v-if="isActivated"
          :key="item.identifier"
          :camera="item"
          autoplay
          controls
          @fullscreen="onFullScreen(item)"
        />
      </grid-table-item>
    </grid-table>
    <camera-dialog
      ref="cameraDialog"
      @closed="onClosed"
    />
  </div>
</template>

<script>
import { getCameras } from '@/api/external'
import { Camera } from '@/constant'

export default {
  name: 'DeviceTrafficCamera',
  props: {
    device: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      schema: {
        pageSize: 999,
        list: this.getCameras
      },
      isActivated: true
    }
  },
  methods: {
    getCameras () {
      return getCameras({
        deviceId: this.device.id,
        cameraType: Camera.LED
      }).then(({ data }) => {
        data = data.map(({ id, thirdPartyDevice }) => {
          return {
            ...thirdPartyDevice,
            id
          }
        })
        return { data }
      })
    },
    onFullScreen (camera) {
      this.isActivated = false
      this.$refs.cameraDialog.show(camera)
    },
    onClosed () {
      this.isActivated = true
    }
  }
}
</script>
