<template>
    <div class="content">
        <div class="search">
            <el-row :gutter="10">
                <el-col :span="6">
                    <el-row type="flex" align="middle">
                        <el-col :span="8">
                            <div class="lable-title">Vehicle Group:</div>
                        </el-col>
                        <el-col :span="16">
                            <el-select v-model="vehicleGroup" placeholder="Please choose Vehicle Group!" clearable>
                                <el-option v-for="item in vehicleGroupList" :key="item.code"
                                           :label="item.name"
                                           :value="item.code"></el-option>
                            </el-select>
                        </el-col>
                    </el-row>
                </el-col>
                <el-col :span="16">
                    <el-button type="primary" size="small">Search</el-button>
                </el-col>
            </el-row>
        </div>
        <div class="table-wrapper" style="margin-bottom: 20px;">
            <div class="title">
                <h6>Select Vehicle Group</h6>
            </div>
            <el-table :data="tableData1" style="width: 100%">
                <el-table-column prop="configurationId" label="Configuration ID"></el-table-column>
                <el-table-column prop="configurationMessage" label="Configuration Message"></el-table-column>
                <el-table-column prop="description" label="Description"></el-table-column>
                <el-table-column prop="linkGroup" label="Link Group">
                    <template slot-scope="scope">
                        <span class="table-row-btn">Link</span>
                    </template>
                </el-table-column>
                <template slot="empty" v-if="tableData1.length == 0">
                    <span>No data available</span>
                </template>
            </el-table>
            <el-pagination v-if="pager1.totalPage>1" :page-size="pager1.limit" :total="pager1.total" @current-change="handleCurrentChange1" background layout="prev, pager, next"></el-pagination>
        </div>
        <div class="table-wrapper">
            <div class="title">
                <h6>Issue Command</h6>
                <div>
                    <el-button type="primary" size="small" @click="dialogVisible = true">Send All Commands</el-button>
                </div>
            </div>
            <el-table :data="tableData2" style="width: 100%">
                <el-table-column prop="commandId" label="Command ID"></el-table-column>
                <el-table-column prop="groupName" label="Group Name"></el-table-column>
                <el-table-column prop="operate" label="Operate">
                    <template slot-scope="scope">
                        <span class="table-row-btn">Send to Vehicle</span>
                    </template>
                </el-table-column>
                <template slot="empty" v-if="tableData2.length == 0">
                    <span>No data available</span>
                </template>
            </el-table>
            <el-pagination v-if="pager2.totalPage>1" :page-size="pager2.limit" :total="pager2.total" @current-change="handleCurrentChange2" background layout="prev, pager, next"></el-pagination>
        </div>
        <el-dialog
            title="Group List"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">
            <el-table :data="dialogTableData">
                <el-table-column property="groupName" label="Group Name"></el-table-column>
                <el-table-column property="details" label="Details"></el-table-column>
                <el-table-column prop="link" label="Link">
                    <template slot-scope="scope">
                        <el-checkbox :value="scope.row.link"></el-checkbox>
                    </template>
                </el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">Cancel</el-button>
                <el-button type="primary" @click="submit">Submit</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data(){
        return {
            vehicleGroupList: [
                {code: 1, name: 'Group_001'},
                {code: 2, name: 'Group_002'},
                {code: 3, name: 'Group_003'},
            ],
            vehicleGroup: '',
            tableData1: [
                {configurationId: 'Id_0001', configurationMessage: 'Config_******', description: '-'},
                {configurationId: 'Id_0001', configurationMessage: 'Config_******', description: '-'},
                {configurationId: 'Id_0001', configurationMessage: 'Config_******', description: '-'},
                {configurationId: 'Id_0001', configurationMessage: 'Config_******', description: '-'},
                {configurationId: 'Id_0001', configurationMessage: 'Config_******', description: '-'}
            ],
            tableData2: [
                {commandId: 'CMD_0001', groupName: 'Group_Name_0001'},
                {commandId: 'CMD_0001', groupName: 'Group_Name_0001'},
                {commandId: 'CMD_0001', groupName: 'Group_Name_0001'},
                {commandId: 'CMD_0001', groupName: 'Group_Name_0001'},
                {commandId: 'CMD_0001', groupName: 'Group_Name_0001'}
            ],
            dialogVisible: false,
            dialogTableData: [
                {groupName: 'Group_0001', details: '-', link: true},
                {groupName: 'Group_0001', details: '-', link: false},
                {groupName: 'Group_0001', details: '-', link: true},
                {groupName: 'Group_0001', details: '-', link: false},
            ],
            maxHeight: 0,
            pager1: {
                page: 1,
                limit: 10,
                total: 300,
                totalPage: 10,
            },
            pager2: {
                page: 1,
                limit: 10,
                total: 300,
                totalPage: 10,
            }
        }
    },
    created() {
        this.getTableData1();
        this.getTableData2();
        // this.getMaxHeight();
        // window.addEventListener('resize', () => {
        //     this.getMaxHeight();
        // })
    },
    methods: {
        getTableData1(){

        },
        getTableData2(){

        },
        submit(){
            this.dialogVisible = false;
            this.$message.success('Group and configuration have been completed. Waiting for the command to be sent manually.');
        },
        handleClose(){
            this.dialogVisible = false;
        },
        handleCurrentChange1(val){
            this.page = val;
            this.getTableData1();
        },
        handleCurrentChange2(val){
            this.page = val;
            this.getTableData2();
        },
        getMaxHeight(){
            let bodyHeight = document.documentElement.clientHeight || document.body.clientHeight;
            this.maxHeight = bodyHeight - 194;
        }
    }
}
</script>
