<!--大屏显示组件-->
<template>
    <div>
        <draggable
                v-for="item in currentBigshow"
                element="div"
                v-model="curSignalArr"
                :options="dragOptions2"
                :class="['split_screen', bigScreenObj[item.ID].splitStatus === 0 ? 'split_screen_one' : bigScreenObj[item.ID].splitStatus === 1 ? 'split_screen_four' : bigScreenObj[item.ID].splitStatus === 2 ? 'split_screen_nine' : 'split_screen_sixteen']"
                :style="{
                    float:'left',
                    width:item.Width*bigScale + 'px',
                    height:item.Height*bigScale + 'px',
                    left:item.Left*bigScale+'px',
                    top:item.Top*bigScale+'px',
                    zIndex:item.ZIndex,
                    display:item.IsVisibility ? 'black' : 'none',
                }"
                :id="item.ID"
        >
            <VueDragResize
                    v-for="itemV in bigScreenObj[item.ID].signalArr" :key="itemV.Id" class="sitem"
                    :w="itemV.width" :h="itemV.height" :parentLimitation="false"
                    :x="itemV.left" :y="itemV.top"
                    @dragstop="(obj) => dragStop(obj,itemV)"
                    @resizestop="(obj) => resizeSignal(obj,itemV)"
            >
                <div ref="signal">
                    {{itemV.deviceID}}
                    <!--<VideoPlayer
                            ref="videoPlayer"
                            :options="playerOptions"
                            :playsinline="true"
                    />-->
                    <div class="closeBtn" @click.stop="closeSignal(item,itemV)">×</div>
                </div>
            </VueDragResize>

            <div v-for="itemD in bigScreenObj[item.ID].bindList" :key="itemD" class="sitem2">
                {{itemD}}
            </div>
        </draggable>
    </div>
</template>

<script>
    import {mapState} from 'vuex'
    import draggable from 'vuedraggable'
    import VueDragResize from 'vue-drag-resize'

    import '../assets/less/splitscreen.less'
    import {elePosition, getElementLeft, getElementTop, getStaticFile, guId} from "../../utils/tools"
    import {reqRefreshView} from "../api"
    import VideoPlayer from "./player.vue"

    export default {
        data() {
            return {
                streamWindows:[], // 传递出去的信号源位置信息
                currentBigshow:[], // 当前显示的大屏
                playerOptions: { // rtmp视频流配置
                    sources: [
                        {
                            "src": "rtmp://58.200.131.2:1935/livetv/hunantv",
                            "type": "rtmp/flv"
                        }
                    ],
                    techOrder: ['flash'],
                    autoplay: true,
                    controls: false,
                    fluid:true,
                    aspectRatio: '200:93',
                    preload: 'auto',
                    notSupportedMessage: '此视频暂无法播放，请稍后再试',
                },
                screenPosition:{}, // 大屏在视口中的位置
            }
        },

        components: {
            draggable,
            VueDragResize,
            VideoPlayer
        },

        async beforeCreate() {
            // 加载大屏json文件
            const bigScreenJson = await getStaticFile('EnityBigScreen.Data')
            // 过滤出显示的大屏
            this.currentBigshow = bigScreenJson.filter(item => item.IsVisibility === true)

            // 给大屏对象进行初始化显示
            const obj = {}
            this.currentBigshow.forEach(item => {
                obj[item.ID] = {
                    bindList:[],
                    splitStatus:0,
                    signalArr:[],
                    width:item.Width,
                    height:item.Height
                }
                const num = item.BindList.length
                // 拼接屏数量
                for(let i=0;i<num;i++){
                    obj[item.ID].bindList.push(i+1)
                }
                // 根据拼接屏数量得到分屏状态
                obj[item.ID].splitStatus = Math.floor(Math.sqrt(num)-1)
            })
            this.$store.dispatch('updateBigscreenObj',obj)
        },

        updated() {
            // 监视当前变动的大屏
            const bigScreenObj = this.$store.state.bigScreenObj
            const curOpeBigscreenId = this.$store.state.curOpeBigscreenId
            if(curOpeBigscreenId){
                const curSignalArr = bigScreenObj[curOpeBigscreenId].signalArr
                this.$store.dispatch('updateCurSignalArr',curSignalArr)
                // this.currentSignalArr = bigScreenObj[curOpeBigscreenId].signalArr
            }

            // 获取大屏在视口中的位置
            this.currentBigshow.forEach(item => {
                const screenDiv = document.getElementById(item.ID)
                this.screenPosition[item.ID] = {
                    top:getElementTop(screenDiv),
                    left:getElementLeft(screenDiv)
                }
            })
        },

        methods: {
            // 大屏开窗接口提交
            reqRefreshBigScreen(arr) {
                /*const streamWindows = []
                setTimeout(async () => {
                    const elementArr = this.$refs.signal ? this.$refs.signal.filter(item => item.clientWidth !== 0) : []
                    if(elementArr){
                        elementArr.forEach((item,index) => {
                            // 获取元素绝对位置的横坐标和纵坐标
                            const left = getElementLeft(item)
                            const top = getElementTop(item)
                            streamWindows.push({
                                left,
                                top,
                                width:arr[index].width,
                                height:arr[index].height,
                                id:0,
                                sourceId:arr[index].sourceId,
                                widthScale:arr[index].width/(this.currentBigshow[0].Width*this.$store.state.bigScale),
                                heightScale:arr[index].height/(this.currentBigshow[0].Height*this.$store.state.bigScale),
                                orginRect:{}
                            })
                        })
                        const data = {
                            bigScreenId:this.currentBigshow[0].ID,
                            streamWindows
                        }
                        this.streamWindows = streamWindows
                        // 调用大屏开窗接口
                        await reqRefreshView(data)
                    }
                })*/
            },

            // 拖动结束
            dragStop(obj,item) {
                const arr2 = this.$store.state.signalPreList
                this.reqRefreshBigScreen(arr2)
            },

            // 缩放结束
            async resizeSignal(obj,itemV) {
                const arr = this.$data.streamWindows
                // 找出当前改动的信号源
                const currentItem = arr.filter(item => item.sourceId === itemV.sourceId)
                currentItem[0].width = obj.width
                currentItem[0].height = obj.height
                currentItem[0].widthScale = obj.width/(this.currentBigshow[0].Width*this.$store.state.bigScale)
                currentItem[0].heightScale = obj.height/(this.currentBigshow[0].Height*this.$store.state.bigScale)
                const otherItems = arr.filter(item => item.sourceId !== itemV.sourceId)
                const newStreamWindows = [...currentItem,...otherItems]
                this.streamWindows = newStreamWindows
                this.reqRefreshBigScreen(newStreamWindows)
            },

            // 关闭信号源
            closeSignal(bigscreen,signal) {
                const bigScreenObj = this.$store.state.bigScreenObj
                // 1.找出是哪个大屏下的信号源发生了改变,过滤掉删除的那个
                const newSignalPreList = bigScreenObj[bigscreen.ID].signalArr.filter(item => item.Id !== signal.Id)
                // 2.重新赋值
                bigScreenObj[bigscreen.ID].signalArr = newSignalPreList
            }
        },

        computed: {
            ...mapState(['splitScreenStatus', 'bigScale','positionNum','bigScreenObj','curOpeBigscreenId','curSignalArr']),

            signalPreList: {
                get() {
                    return this.$store.state.signalPreList
                },

                set(arr) {
                    let len = arr.length
                    const positionNum = this.$store.state.positionNum
                    if(len>0){
                        // 对监视的信号源数组进行深度克隆，解决赋加Id一样的问题
                        const arr2 = []
                        arr.forEach(item => {arr2.push(JSON.parse(JSON.stringify(item)))})
                        // 删除数据的最后一项，重新赋值再添加
                        arr2.pop()

                        // 给新增的信号源进行赋值
                        const signalObj = arr[len-1]
                        signalObj.Id = guId()
                        signalObj.splitScreenStatus = this.$store.state.splitScreenStatus + 1
                        signalObj.width = this.currentBigshow[0].Width * this.$store.state.bigScale / (this.$store.state.splitScreenStatus+1)
                        signalObj.height = this.currentBigshow[0].Height * this.$store.state.bigScale / (this.$store.state.splitScreenStatus+1)
                        signalObj.widthScale = signalObj.width / (this.currentBigshow[0].Width * this.$store.state.bigScale)
                        signalObj.heightScale = signalObj.height / (this.currentBigshow[0].Height * this.$store.state.bigScale)

                        let width = this.currentBigshow[0].Width*this.$store.state.bigScale/(signalObj.splitScreenStatus)
                        let height = this.currentBigshow[0].Height*this.$store.state.bigScale/(signalObj.splitScreenStatus)
                        const res = elePosition(width,height,positionNum,this.screenPosition.top,this.screenPosition.left,this.$store.state.splitScreenStatus+1)
                        signalObj.top = res.top - this.screenPosition.top
                        signalObj.left = res.left - this.screenPosition.left
                        this.$store.dispatch('updateSignalPreList', [...arr2,signalObj])
                    }else {
                        this.$store.dispatch('updateSignalPreList', arr)
                    }
                }
            },
            
            // 当前变动的信号源数组
            curSignalArr: {
                get() {
                    return this.$store.state.curSignalArr
                },

                set(arr) {
                    const bigScreenObj = this.$store.state.bigScreenObj
                    const curOpeBigscreenId = this.$store.state.curOpeBigscreenId
                    const positionNum = this.$store.state.positionNum
                    const bigScale = this.$store.state.bigScale
                    const curBigScreen = bigScreenObj[curOpeBigscreenId]
                    let len = arr.length
                    if(len>0){
                        // 重新定义一个数组，对监视的信号源数组进行深度克隆，解决赋加Id一样的问题
                        const arr2 = []
                        arr.forEach(item => {arr2.push(JSON.parse(JSON.stringify(item)))})

                        // 给新增的信号源进行赋值
                        arr2[len-1].Id = guId()
                        arr2[len-1].splitScreenStatus = curBigScreen.splitStatus + 1
                        arr2[len-1].width = curBigScreen.width * bigScale / arr2[len-1].splitScreenStatus
                        arr2[len-1].height = curBigScreen.height * bigScale / arr2[len-1].splitScreenStatus
                        arr2[len-1].widthScale = arr2[len-1].width / (curBigScreen.width * bigScale)
                        arr2[len-1].heightScale = arr2[len-1].height / (curBigScreen.height * bigScale)
                        // 获取每个信号源的位置
                        const res = elePosition(arr2[len-1].width,arr2[len-1].height,positionNum,this.screenPosition[curOpeBigscreenId].top,this.screenPosition[curOpeBigscreenId].left,arr2[len-1].splitScreenStatus)
                        arr2[len-1].top = res.top - this.screenPosition[curOpeBigscreenId].top
                        arr2[len-1].left = res.left - this.screenPosition[curOpeBigscreenId].left

                        console.log(arr2)
                        this.$store.dispatch('updateCurSignalArr',arr2)
                    }
                }
            },       
            
            // 拖拽选线配置
            dragOptions1() {
                return {
                    animation: 0,
                    group: {
                        name: "description",
                        pull: 'clone',
                        put: false
                    },
                    ghostClass: "ghost",
                }
            },
            dragOptions2() {
                return {
                    animation: 0,
                    group: "description",
                    disabled:false,
                    sort:false,
                }
            },

            player() {
                return this.$refs.videoPlayer.player
            }
        },

        watch: {
            // 监视大屏对象的变化(深度监视)
            bigScreenObj: {
                handler: function (val, oldVal) {
                    /*const bigScale = this.$store.state.bigScale
                    const positionNum = this.$store.state.positionNum
                    for (const key in val) {
                        let len = val[key].signalArr.length
                        if(len){
                            const lastObj = val[key].signalArr[len-1]
                            lastObj.Id = guId()
                            lastObj.width = val[key].width*bigScale/(val[key].splitStatus+1)
                            lastObj.height = val[key].height*bigScale/(val[key].splitStatus+1)
                            const positionInfo = elePosition(lastObj.width,lastObj.height,positionNum,this.screenPosition[key].top,this.screenPosition[key].left,val[key].splitStatus+1)
                            lastObj.top = positionInfo.top - this.screenPosition[key].top
                            lastObj.left = positionInfo.left- this.screenPosition[key].left
                        }
                    }
                    this.$store.dispatch('updateBigscreenObj',val)*/
                },
                deep: true
            },

            // 监视大屏中的信号源，只要变动，就调用回调函数
            signalPreList: function (arr) {
                this.reqRefreshBigScreen(arr)
            },

            // 监视分屏的状态
            splitScreenStatus: function(num) {
                this.$data.bindList = []
                for (let i=0;i<(num+1)*(num+1);i++){
                    this.$data.bindList.push(i+1)
                }
                this.reqRefreshBigScreen(this.$data.streamWindows)
            }
        }
    }
</script>
