<template>
  <div class="display-draft">
    <el-button-group>
      <el-button type="success" :disabled="isDisable" @click="selectBeginTime">{{ $t('display.plan.drivingByPlan') }}</el-button>
      <el-button type="danger" :disabled="!isDisable" @click="end">{{ $t('display.plan.exitPlan') }}</el-button>
      <el-button type="primary" @click="back">{{ $t('display.plan.back') }}</el-button>
    </el-button-group>
    <set-time ref="setTime" @ConfirmSelectBeginTime="start" />
  </div>
</template>

<script>
import SetTime from './demon/setTime';
import { Notification } from 'element-ui';
import { runDiagramIsStart, runDiagramGetTime, runDiagramOver, runDiagramStart } from '@/api/simulation';
import { timeFormat } from '@/utils/date';

// 琏计划 暂时不用
export default {
    name: 'MenuPlan',
    components: {
        SetTime
    },
    props: {
        group: {
            type: String,
            required: true
        },
        offset: {
            type: Number,
            required: true
        }
    },
    data() {
        return {
            isDisable: false
        };
    },
    watch: {
        '$store.state.training.simulationGroupCount': function () {
            this.initLoadPage();
        }
    },
    methods: {
        async initLoadPage() {
            try {
                const 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() {
            const 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(() => {
                this.isDisable = false;
                this.$messageBox(this.$t('display.plan.startPlanFail'));
            });
        },
        end() {
            this.isDisable = false;
            runDiagramOver(this.group).then(() => {
                this.$store.dispatch('training/over').then(() => {
                    this.$store.dispatch('training/setMapDefaultState').then(() => {
                        this.$store.dispatch('map/clearJlmapTrainView');
                        this.$store.dispatch('map/setTrainWindowShow', false);
                    });
                });
            }).catch(() => {
                this.isDisable = true;
                this.$messageBox(this.$t('display.plan.endPlanFail'));
            });
        },
        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-block;
        position: absolute;
        top: 17px;
        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;
        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 {
        position: absolute;
        float: right;
        right: 15px;
        bottom: 15px;
    }
</style>
