<template>
    <div class="intellReport" :class="!theme ? 'themeDark' : 'themeLight'">
        <el-row :gutter="50" justify="space-evenly" :style="pageHeight">
            <el-col :span="8" class="warn-table">
                <existing @getExistingData="getData" :height="tableHeight" />
            </el-col>
            <el-col :span="2" style="text-align: center;">
                <div :style="btnHeight">
                    <el-button type="primary" @click="onAddItem1">
                        <el-icon class="el-icon--right">
                            <ArrowRight />
                        </el-icon>
                    </el-button>
                </div>
                <div :style="btnHeight">
                    <el-button type="primary" @click="onAddItem2">
                        <el-icon class="el-icon--right">
                            <ArrowRight />
                        </el-icon>
                    </el-button>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="listTop warn-table">
                    <el-table :data="tableData" :height="listHeight">
                        <el-table-column type="index" width="30" />
                        <el-table-column label="脚本列表">
                            <template #default="scope">
                                <el-button v-if="scope.row.scriptName !==''" type="warning" round>
                                    {{ scope.row.scriptName}}
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column align="right">
                            <template #default="scope">
                                <el-button type="primary" :disabled="btnloading" @click="handleRun(scope.$index)">运行
                                </el-button>
                                <el-button type="danger" @click="handleDelete(scope.$index)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-button class="run-all-script" type="primary" :disabled="btnloading" @click="runAllScript">运行所有脚本
                    </el-button>
                </div>

                <div class="listBot warn-table">
                    <p class="tableTit">每天运行一次</p>
                    <el-table :data="tableData4" :height="listHeight">
                        <el-table-column label="时间" width="300">
                            <el-time-select v-model="timeValue" start="00:05" step="00:05" end="23:55"
                                placeholder="选择时间" />
                        </el-table-column>
                        <el-table-column label="脚本名称">
                            <template #default="scope">
                                <el-button v-if="scope.row.scriptName !==''" type="warning" round>
                                    {{ scope.row.scriptName}}
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column align="right">
                            <template #default="scope">
                                <el-button type="primary" @click="handleDelete(scope.$index)">运行</el-button>
                                <el-button type="danger" @click="handleDelete(scope.$index)">取消
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-col>
        </el-row>
    </div>

</template>
<script>
    import {
        run
    } from '@/api/report';
    import existing from "../existing.vue";
    export default {
        components: {
            existing
        },
        data() {
            return {
                childCurrentRow: [],
                tableData: [],
                tableData4: [],
                timeValue: '',
                theme: null,
                tableHeight: document.documentElement.clientHeight - 115,
                listHeight: (document.documentElement.clientHeight - 160) / 2,
                btnloading: false
            }
        },
        watch: {
            '$store.state.theme': {
                handler: function (newVal, oldVal) {
                    this.theme = newVal
                },
                immediate: true
            }
        },
        computed: {
            pageHeight() {
                return {
                    'height': document.documentElement.clientHeight - 115 + 'px'
                }
            },
            btnHeight() {
                return {
                    'height': (document.documentElement.clientHeight - 160) / 4 + 'px',
                    'padding-top': (document.documentElement.clientHeight - 160) / 4 + 'px'
                }
            }
        },
        methods: {
            onAddItem1() {
                this.tableData.push(this.childCurrentRow)
            },
            onAddItem2() {
                this.tableData4.push(this.childCurrentRow)
            },
            runAllScript() {
                this.btnloading = true
                run(this.tableData).then(res => {
                    console.log(res)
                    this.btnloading = false
                });
            },
            handleRun(index) {
                this.btnloading = true
                run(new Array(this.tableData[index])).then(res => {
                    console.log(res)
                    this.btnloading = false
                })
            },
            handleDelete(index) {
                this.tableData.splice(index, 1)
            },
            getData(data) {
                console.log("data:", data)
                this.childCurrentRow = data
            }
        }
    }
</script>

<style scoped lang="less">
    .intellReport {
        .listTop {
            margin-bottom: 20px;
        }

        .run-all-script {
            position: absolute;
            right: 120px;
        }
    }

    .themeDark {
        padding: 20px 0;
        background: #161f1e;
        border-radius: 10px;

        .tableTit {
            color: #fff;
        }
    }

    .themeLight {
        padding: 20px 0;
        background: #edeffb;
        border-radius: 10px;

        .tableTit {
            color: #161f1e;
        }
    }
</style>