<template>
    <div class="display-draft">
        <el-button-group>
            <el-button type="success" @click="selectBeginTime" :disabled="isDisable">按计划行车</el-button>
            <el-button type="danger" @click="end" :disabled="!isDisable">退出计划</el-button>
            <el-button type="primary" @click="back">返回</el-button>
        </el-button-group>
        <set-time ref="setTime" @ConfirmSelectBeginTime="start"></set-time>
    </div>
</template>

<script>
    import SetTime from './demon/setTime';
    import { mapGetters } from 'vuex';
    import { Notification } from 'element-ui';
    import { OperateMode, PermissionType } from '@/scripts/ConstDic';
    import { runDiagramIsStart, runDiagramGetTime, runDiagramOver, runDiagramStart } from '@/api/simulation';
    import { exitFullscreen } from '@/utils/screen';
    import { timeFormat } from '@/utils/date';

    export default {
        name: 'MenuPlan',
        props: {
            group: {
                type: String,
                required: true
            },
            offset: {
                type: Number
            }
        },
        components: {
            SetTime
        },
        data() {
            return {
                isDisable: false
            }
        },
        watch: {
            '$store.state.training.simulationGroupCount': function () {
                this.InitLoadPage();
            }
        },
        methods: {
            async InitLoadPage() {
                try {
                    let rest = await runDiagramIsStart(this.group);
                    if (rest && rest.data) {
                        this.isDisable = true;
                        this.$store.dispatch('training/simulationStart');
                    } else {
                        this.isDisable = false;
                        this.$store.dispatch('training/over');
                    }
                    await this.loadSystemTime();
                } catch (error) {
                    console.log(error);
                }
            },
            async loadSystemTime() {
                let rest = await runDiagramGetTime(this.group);
                if (rest && rest.code == 200) {
                    this.$store.dispatch('training/setInitTime', +new Date(`${new Date().getFullYear()} ${timeFormat(rest.data)}`));
                }
            },
            selectBeginTime() {
                this.$refs.setTime.doShow();
            },
            start(model) {
                this.isDisable = true;
                runDiagramStart(model, this.group).then(res => {
                    this.$store.dispatch('training/simulationStart').then(() => {
                        this.$store.dispatch('training/setInitTime', +new Date(`${new Date().getFullYear()} ${model.initTime}`));
                    })
                }).catch(error => {
                    this.isDisable = false;
                    this.$messageBox('开始计划失败，请返回重试');
                });
            },
            end() {
                this.isDisable = false;
                runDiagramOver(this.group).then(() => {
                    this.$store.dispatch('training/over').then(() => {
                        this.$store.dispatch('map/clearJlmapTrainView');
                        this.$store.dispatch('map/setTrainWindowShow', false);
                        this.$store.dispatch('training/setMapDefaultStatus');
                    })
                }).catch(error => {
                    this.isDisable = true;
                    this.$messageBox('结束计划失败，请返回');
                })
            },
            async back() {
                this.$store.dispatch('training/over').then(() => {
                    history.go(-1);
                    Notification.closeAll();
                    exitFullscreen();
                })
            }
        }
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/mixin.scss";

    .display-card {
        z-index: 9;
        display: inline;
        position: absolute;
        top: 17px;
        float: left;
        left: 160px;
        height: 32px;
    }

    .display-card .el-row {
        line-height: 32px !important;
    }

    .display-score {
        background-color: black;
        display: -moz-inline-box;
        display: inline-block;
        text-align: left;
        /* border: 1px solid lightskyblue; */
        /* width: 100px; */
        height: 32px;
        line-height: 24px;
        border-radius: 4px;
        padding-left: 2px;
        margin-left: 10px;
        font-family: "Microsoft" !important;
        font-size: 18px !important;
        color: #fff;
    }

    .display-draft {
        /* z-index: 1000; */
        position: absolute;
        float: right;
        right: 15px;
        bottom: 15px;
    }
</style>