<template>
    <div class="subsection">
        <a-layout style="width: 900px">
            <a-layout style="width: 100%;height: 310px">
                <a-layout-sider width="110px">
                    <a-tabs default-active-key="1" tab-position="left" @change="changeTabLeft" v-if="total.row > nums.row">
                        <a-tab-pane 
                            v-for="index in latticeRow"
                            :key="index"
                        >
                            <span slot="tab" >
                                {{  getRandom(index, latticeRow, nums.row, total.row) }}
                                <a-icon type="close-circle" v-if="status.row[index] === 1" style="color: #d81e06"/>
                                <a-icon type="check-circle" v-else-if="status.row[index] === 2" style="color: #1afa29" />
                                <a-icon type="exclamation-circle" v-else style="color: #909399" />
                            </span>
                        </a-tab-pane>
                    </a-tabs>
                </a-layout-sider>
                <a-layout-content>
                    <!-- <slot :data="{select, nums, random}"></slot> -->
                    <div class="roadway-buttom">
                        <div class="roadway-box">
                            
                            <div class="line" v-for="(row,index) in total.row" :key="index"  v-if="row >= random.row[0] && row <= random.row[1]">
                                <div v-for="(column,index) in total.column" :key="index" class="el" v-if="column >= random.column[0] && column <= random.column[1]">

                                    <!--渲染默认巷道框架规格 定位浮在 已有巷道上做对应-->
                                    <span
                                        class="default"
                                        :id="`${shelveId}-${row}-${column}`"
                                        @click="tocheckPage(row,column)">
                                        {{row}}-{{column}}
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a-layout-content>
            </a-layout>
            <a-layout-footer>
                <!-- <a-tabs default-active-key="1" tab-position="bottom" @change="value => select.row = value" v-if="total.row > nums.row"> -->
                <a-tabs default-active-key="1" tab-position="bottom" @change="changeTab" v-if="total.column > nums.column">
                    <a-tab-pane
                        v-for="index in latticeColumn"
                        :key="index"
                    >
                            <span slot="tab">
                                {{  getRandom(index, latticeColumn, nums.column, total.column) }}
                                <!-- <a-icon :id="`${shelveId}-row-${getRandom(index,latticeRow,nums.row,total.row)}`"/> -->
                                <a-icon type="close-circle" v-if="status.column[index] === 1" style="color: #d81e06" />
                                <a-icon type="check-circle" v-else-if="status.column[index] === 2" style="color: #1afa29" />
                                <a-icon type="exclamation-circle" v-else style="color: #909399" />
                            </span>
                    </a-tab-pane>
                </a-tabs>
            </a-layout-footer>
        </a-layout>
    </div>
</template>

<script>
export default {
    name: "Subsection",
    components: {},
    props: {
        // 默认行第一个  列第一个
        select: {
            type: Object,
            default: () => {
                return {
                    row: 1,
                    column: 1
                }
            }
        },
        // 总共数据
        total:{
            type: Object,
            default:()=>{
                return {}
            }
        },
        nums: {
            // 行20个 列10个
            type: Object,
            default: () => {
                return {
                    row: 10,
                    column: 15
                }
            }
        },
        shelveId: {
            type: String,
            default: () => {
                return {}
            }
        },
        streetId: {
            type: Number,
            default: () => {
                return {}
            }
        },
        streetName: {
            type: String,
            default: () => {
                return {}
            }
        },
        type: {
            type: String,
            default: () => {
                return {}
            }
        },
        inOut: {
            type: String,
            default: () => {
                return {}
            }
        },
        
    },
    computed: {
        // 应有的格子数
        latticeRow() {
            return  parseInt(this.total.row / this.nums.row) + (this.total.row % this.nums.row > 0 ? 1 : 0);
        },
        latticeColumn() {
            return  parseInt(this.total.column / this.nums.column) + (this.total.column % this.nums.column > 0 ? 1 : 0);
        },
        random() {
            console.log(this.getRandomRow(this.select.row))
            return {
                
                row: this.getRandomRow(this.select.row),
                column: this.getRandomColumn(this.select.column),
            }
        },
        watchShelveIdInfo(){
            return this.shelveId;
        },    
    },
    watch: {
        watchShelveIdInfo() {
            this.getStatus();
        },
       
        immediate: true
    },
    data() {
        return {
            status: {
                row: {},
                column: {}
            },
            timer: null
           
            
        }
    },
    mounted() {
        
        this.$nextTick(() => {
            this.getStatus();
        })
        
        this.timer = window.setInterval(this.getStatus, 5000);
        this.$once('hook:beforeDestroy', () => {//页面关闭
            console.log('hook:beforeDestroy')
            window.clearInterval(this.timer);//停止
            this.timer = null;
        });
    },
    beforeDestroy() {
        console.log("beforeDestroy")
        window.clearInterval(this.timer);        
        this.timer = null;
    },
    created() {
        
    },
    destroyed() {
        clearInterval(this.timer);
    },
    methods: {
        changeTab(value){
            this.select.column = value
            this.getStatus()
        },
        changeTabLeft(value){
            this.select.row = value
            this.getStatus()

        },
        // 获取行或列的范围字符串
        getRandom(index, lattice, num, total) {
            if(index !== lattice) {
                return `${(index - 1) * num + 1} - ${index * num}`;
            }else {
                return `${(index - 1) * num + 1} - ${total}`;
            }
        },
        // 获取行和列的范围数组
        getRandomRow(val) {
            return this.getRandom(val, this.latticeRow, this.nums.row, this.total.row).split(' - ').map(item => Number(item));
        },
        getRandomColumn(val) {
            return this.getRandom(val, this.latticeColumn, this.nums.column, this.total.column).split(' - ').map(item => Number(item));
        },
        
        getStatusBg(row,column,status) {
            var dom = document.getElementById(this.shelveId+"-"+row+'-'+column);
            var color = status == 1 ?'#d81e06': status == 2 ? '#1afa29': '#bfbfbf';
            dom.style.background = color
        },
        getStatusTab(tab,status,rowColumn){
            var dom = document.getElementById(this.shelveId+'-'+rowColumn+'-'+tab);
            console.log(dom)
            console.log(this.shelveId+'-'+rowColumn+'-'+tab)
            dom.style.type ="close-circle"
            
            var color = status == 1 ?'#d81e06': status == 2 ? '#1afa29': '#bfbfbf';
            dom.style.color = color
            console.log(dom)
        },

        getStatus(){
            if(JSON.stringify(this.shelveId) == "{}"){
                return;
            }
            var columnTab = []
            for(let c = 1; c <= this.latticeColumn ;c++){
                let tab = this.getRandom(c,this.latticeColumn,this.nums.column,this.total.column)
                columnTab.push(tab)
            }
            var rowTab = []
            for(let r = 1; r <= this.latticeRow ;r++){
                let tab = this.getRandom(r,this.latticeRow,this.nums.row,this.total.row)
                rowTab.push(tab)
            }
            this.$api.httpApi.getStatusByRowColumn({
                data: {
                    columnStart: this.random.column[0],
                    columnEnd: this.random.column[1],
                    rowStart: this.random.row[0],
                    rowEnd: this.random.row[1],
                    shelveId: this.shelveId,
                    rowTabs: rowTab,
                    columnTabs: columnTab
                }
            }).then(res => {
                if(res.code == 200){
                    if(res.data.stocks){
                        for(let a of res.data.stocks){
                            this.getStatusBg(a.row,a.column,a.status)
                        }
                    }
                
                    var i = 1;
                    for(let a in res.data.columnTabStatus){
                        this.$set(this.status.column, i, res.data.columnTabStatus[a]);
                        // this.status.column[i] = res.data.columnTabStatus[a]
                        i++;
                    }
                    
                    var j = 1;
                    for(let a in res.data.rowTabStatus){
                        this.$set(this.status.row, j, res.data.rowTabStatus[a]);
                        // this.status.row[j] = res.data.rowTabStatus[a]
                        j++;
                    } 
                }
            }).catch(err => {

            });
        },
        tocheckPage(row,column){
            this.$router.push({
                name: 'checkOperation',
                query: {row: row, column: column, shelveId: this.shelveId, id: this.streetId, name:this.streetName, type: this.type, type2: this.inOut}
            }) 
        }
    }
}
</script>

<style scoped lang="scss">
.subsection /deep/{
    width: 100%;
    height: 360px;
    overflow: hidden;
    .ant-tabs-nav .ant-tabs-tab {
        // padding: 0;
        margin-right: 0;
    }
    .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled) {
        &:hover{
            background: #40a9ff;
            border-color: #40a9ff;
        }
        background: #40a9ff;
        border-color: #40a9ff;
        box-shadow: -1px 0 0 0 #40a9ff;
    }
    .ant-radio-button-wrapper-checked {
        box-shadow: -1px 0 0 0 #40a9ff;
    }
    .radio-button-cell {
        width: 100px;
        text-align: center;
    }
    .ant-layout-sider {
        height: 100%;
        .ant-tabs {
            height: 300px;
        }

        .ant-tabs-tab {
            padding: 8px;
            margin-bottom: 5px;
            width: 104px;
        }
    }
    .ant-layout-footer {
        // width: 500px;
        padding: 0 0 0 110px;
        .ant-tabs {
            // width: 735px;
        }
        .ant-tabs-bottom-bar {
            margin-top: 0;
        }
    }
    .ant-layout, .ant-layout-sider, .ant-layout-footer {
        background-color: #ffffff;
    }
    roadway-buttom {
        padding: 5px 0;
        width: 100%;
    }

    .roadway-box {
        //transform: rotateX(180deg);
        padding: 10px 10px 0 0;
        .line {
            display: flex;
            //transform: rotateX(180deg); //两次垂直镜像翻转让原本由上到下排列的div 更改为由下到上

            .el {
                width: 46px;
                height: 25px;
                line-height: 25px;
                margin: 2px;
                font-size: 12px;
                text-align: center;
                border-radius: 4px;
                cursor: pointer;
                position: relative;
                .default {
                    position: absolute;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                }
            }
        }
    }
}
</style>
