<!--大屏显示组件-->
<template>
    <div>
        <draggable
                v-for="item in currentBigshow"
                element="div"
                v-model="bigScreenObj[item.ID].signalArr"
                :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="() => dragStop(itemV)"
                    @resizestop="() => resizeSignal(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() {
            // 获取大屏在视口中的位置
            this.currentBigshow.forEach(item => {
                const screenDiv = document.getElementById(item.ID)
                this.screenPosition[item.ID] = {
                    top:getElementTop(screenDiv),
                    left:getElementLeft(screenDiv)
                }
            })
            
            // 获取当前操作大屏的信号源数组
            const bigScreenObj = this.$store.state.bigScreenObj
            const curOpeBigscreenId = this.$store.state.curOpeBigscreenId
            if(curOpeBigscreenId){
                this.signals = bigScreenObj[curOpeBigscreenId].signalArr
            }
        },

        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(item) {
                const arr2 = this.$store.state.signalPreList
                this.reqRefreshBigScreen(arr2)
            },

            // 缩放结束
            async resizeSignal(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']),

            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)
                    }
                }
            },
            
            signals: {
                get() {
                    return this.signals
                },
                
                set(arr) {
                    const val = this.$store.state.bigScreenObj
                    const bigscreenId = this.$store.state.curOpeBigscreenId
                    const bigScale = this.$store.state.bigScale
                    const positionNum = this.$store.state.positionNum
                    
                    let len = arr.length
                    if(len){
                        const arr2 = []
                        arr.forEach(item => {arr2.push(JSON.parse(JSON.stringify(item)))})
                        arr2.pop()
                        const lastObj = arr[len-1]
                        lastObj.Id = guId()
                        lastObj.splitScreenStatus = val[bigscreenId].splitStatus + 1
                        lastObj.width = val[bigscreenId].width * bigScale / lastObj.splitScreenStatus
                        lastObj.height = val[bigscreenId].height * bigScale / lastObj.splitScreenStatus
                        lastObj.widthScale = lastObj.width / (val[bigscreenId].width * bigScale)
                        lastObj.heightScale = lastObj.height / (val[bigscreenId].height * bigScale)
                        const positionInfo = elePosition(lastObj.width,lastObj.height,positionNum,this.screenPosition[bigscreenId].top,this.screenPosition[bigscreenId].left,val[bigscreenId].splitStatus+1)
                        lastObj.top = positionInfo.top - this.screenPosition[bigscreenId].top
                        lastObj.left = positionInfo.left - this.screenPosition[bigscreenId].left
                        console.log([...arr2,lastObj])
                        // val[bigscreenId].signalArr = [...arr2,lastObj]
                        // this.$store.dispatch('updateBigscreenObj',val)
                        /*signals.push(signalObj)
                        console.log(val)
                        this.$store.dispatch('updateBigscreenObj',val)*/
                    }
                }
            },
            
            // 拖拽选线配置
            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) {
                    const bigscreenId = this.$store.state.curOpeBigscreenId
                    if(bigscreenId){
                        console.log(val)
                    }

                    /*const bigscreenId = this.$store.state.curOpeBigscreenId
                    const bigScale = this.$store.state.bigScale
                    const positionNum = this.$store.state.positionNum
                    if(bigscreenId){
                        /!*const arr = val[bigscreenId].signalArr
                        let len = arr.length
                        if(len){
                            // 给新增的信号源进行赋值
                            const signalObj = arr[len-1]
                            signalObj.Id = guId()
                            signalObj.splitScreenStatus = val[bigscreenId].splitStatus + 1
                            signalObj.width = val[bigscreenId].width * bigScale / signalObj.splitScreenStatus
                            signalObj.height = val[bigscreenId].height * bigScale / signalObj.splitScreenStatus
                            signalObj.widthScale = signalObj.width / (val[bigscreenId].width * bigScale)
                            signalObj.heightScale = signalObj.height / (val[bigscreenId].height * bigScale)
                            const positionInfo = elePosition(signalObj.width,signalObj.height,positionNum,this.screenPosition[bigscreenId].top,this.screenPosition[bigscreenId].left,val[bigscreenId].splitStatus+1)
                            signalObj.top = positionInfo.top - this.screenPosition[bigscreenId].top
                            signalObj.left = positionInfo.left- this.screenPosition[bigscreenId].left
                        }*!/
                        
                        const arr = []
                        val[bigscreenId].signalArr.forEach(item => {
                            arr.push(JSON.parse(JSON.stringify(item)))
                        })

                        arr.forEach(item => {
                            item.Id = guId()
                            item.splitScreenStatus = val[bigscreenId].splitStatus + 1
                            item.width = val[bigscreenId].width * bigScale / item.splitScreenStatus
                            item.height = val[bigscreenId].height * bigScale / item.splitScreenStatus
                            item.widthScale = item.width / (val[bigscreenId].width * bigScale)
                            item.heightScale = item.height / (val[bigscreenId].height * bigScale)
                            const positionInfo = elePosition(item.width,item.height,positionNum,this.screenPosition[bigscreenId].top,this.screenPosition[bigscreenId].left,val[bigscreenId].splitStatus+1)
                            item.top = positionInfo.top - this.screenPosition[bigscreenId].top
                            item.left = positionInfo.left- this.screenPosition[bigscreenId].left
                        })
                    }*/
                },
                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>
