<template>
  <div>
    <div class="display-draft">
      <el-button-group>
        <el-button type="primary" size="small" @click="back">返回</el-button>
        <el-button type="danger" size="small" @click="end">{{ $t('display.demon.initialize') }}</el-button>
        <el-button type="success" :disabled="isDisable" size="small" @click="selectBeginTime">{{ $t('display.demon.drivingByPlan') }}</el-button>
      </el-button-group>
    </div>
    <set-time ref="setTime" @ConfirmSelectBeginTime="start" />
  </div>
</template>
<script>
import SetTime from './demon/setTime';
import { Notification } from 'element-ui';
import { ranAsPlan, exitRunPlan } from '@/api/simulation';
export default {
    name:'MenuPlan',
    components: {
        SetTime
    },
    data() {
        return {

        };
    },
    computed:{
        group() {
            return this.$route.query.group;
        },
        isDisable() {
            return this.$store.state.training.started;
        }
    },
    methods:{
        back() {
            this.$store.dispatch('map/setShowCentralizedStationCode', '');
            history.go(-1);
            Notification.closeAll();
        },
        selectBeginTime() {
            this.$refs.setTime.doShow();
        },
        end() {
            exitRunPlan(this.group).then(() => {
                this.$store.dispatch('training/over').then(() => {
                    this.$store.dispatch('training/setMapDefaultState').then(() => {
                        this.$store.dispatch('map/resetActiveTrainList', false);
                        this.$store.dispatch('map/clearJlmapTrainView');
                        this.$store.dispatch('map/setTrainWindowShow', false);
                    });
                });
            }).catch(() => {
                this.$messageBox(this.$t('display.demon.endSimulationFail'));
            });
        },
        start(model) { // 开始仿真
            const data = {
                time: model.initTime
            };
            // if (this.$route.query.prdType === '04') {
            //     data.loadNumber = model.loadNum;
            // }
            ranAsPlan(data, this.group).then(res => {
                this.$store.dispatch('training/setInitTime', +new Date(`${new Date().toLocaleDateString()} ${model.initTime}`));
            }).catch(error => {
                if (error.code == '5001') {
                    this.$messageBox(this.$t('error.mapDataError') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '5002') {
                    this.$messageBox(this.$t('error.runningChartDataError') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '5003') {
                    this.$messageBox(this.$t('error.runningChartIsNotLoaded') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '5004') {
                    this.$messageBox(this.$t('error.runningDataError') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '5000') {
                    this.$messageBox(this.$t('error.systemError') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '4000') {
                    this.$messageBox(this.$t('error.simulationDoesNotExist') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '4001') {
                    this.$messageBox(this.$t('error.simulationOperationIsNotDefined') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '4002') {
                    this.$messageBox(this.$t('error.simulationOperationProcessingMethodNotFound') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '4003') {
                    this.$messageBox(this.$t('error.simulationOperationFailed') + ',' + this.$t('error.startSimulationFailed'));
                } else if (error.code == '4004') {
                    this.$messageBox(this.$t('error.operationConflict') + ',' + this.$t('error.startSimulationFailed'));
                } else {
                    this.$messageBox('按计划行车异常,请退出重试!');
                }
            });
        }
    }
};
</script>
<style lang="scss" scoped>
.display-draft {
        position: absolute;
        float: right;
        right: 15px;
        bottom: 15px;
    }
</style>
