<template>
  <div class="plan-tool" style="width: 100%; height: 100%;">
    <menu-bar
      ref="menuBar"
      :plan-convert="PlanConvert"
      :run-plan-list="runPlanList"
      :load-run-plan-id="loadRunPlanId"
      @doClose="doClose"
      @dispatchDialog="dispatchDialog"
      @loadingRunPlan="loadingRunPlan"
      @checkIsLoadRunPlan="checkIsLoadRunPlan"
      @modifyRunPlanName="modifyRunPlanName"
    />
    <schedule
      ref="schedule"
      :plan-convert="PlanConvert"
      :load-run-plan-id="loadRunPlanId"
      :load-run-plan-name="loadRunPlanName"
    />
    <status-bar ref="statusBar" :load-run-plan-id="loadRunPlanId" @dispatchDialog="dispatchDialog" @showTrain="showTrain" />
    <parameter ref="parameter" />
    <off-line ref="offLine" @handleConfirm="handleConfirm" @dispatchDialog="dispatchDialog" />
    <add-planning-train ref="addPlanningTrain" :load-run-plan-id="loadRunPlanId" @dispatchDialog="dispatchDialog" />
    <edit-planning-train
      ref="editPlanningTrain"
      @dispatchDialog="dispatchDialog"
      @dispatchOperate="dispatchOperate"
      @refresh="refresh"
    />
    <!-- <edit-smooth-run-time ref="editSmoothRunTime" @dispatchDialog="dispatchDialog" /> -->
    <!-- <add-smooth-run-time ref="addSmoothRunTime" @dispatchDialog="dispatchDialog" /> -->
    <duplicate-train ref="duplicateTrain" @dispatchDialog="dispatchDialog" @dispatchOperate="dispatchOperate" @refresh="refresh" />
    <modifying-routing ref="modifyingRouting" @dispatchDialog="dispatchDialog" />
    <modifying-begin-time ref="modifyingBeginTime" />

    <systerm-out ref="systermOut" />
    <add-task ref="addTask" @dispatchOperate="dispatchOperate" @refresh="refresh" />
    <delete-task ref="deleteTask" @dispatchOperate="dispatchOperate" @refresh="refresh" />
    <modifying-task ref="modifyingTask" :load-run-plan-id="loadRunPlanId" @dispatchOperate="dispatchOperate" @refresh="refresh" />

    <edit-station-between-time ref="editStationBetweenTime" />

    <modifying-station-interval-time ref="modifyingStationIntervalTime" />
    <modifying-station-stop-time ref="modifyingStationStopTime" />
    <populating-generic-data ref="populatingGenericData" :load-run-plan-id="loadRunPlanId" />
    <create-empty-plan ref="createEmptyPlan" @refresh="refreshRunPlanList" />
    <edit-plan-name ref="editPlan" @renewal="refreshRunPlanName" />
    <gernarate-plan ref="gernaratePlanTrain" />
  </div>
</template>

<script>
import MenuBar from './menuBar';
import StatusBar from './statusBar';
import Schedule from './schedule';
import Parameter from './menus/parameter/index';
import OffLine from './menus/offLine';
import AddPlanningTrain from './menus/addPlanningTrain';
import EditPlanningTrain from './menus/editPlanningTrain';
import EditPlanName from './menus/editPlanName';
import DuplicateTrain from './menus/duplicateTrain';
import SystermOut from './menus/systermOut';
import AddTask from './menus/addTask';
import DeleteTask from './menus/deleteTask';
import ModifyingTask from './menus/modifyingTask';
import ModifyingRouting from './menus/modifyingRouting';
import ModifyingBeginTime from './menus/modifyingBeginTime';
import EditStationBetweenTime from './menus/editStationBetweenTime';
import GernaratePlan from './menus/gernaratePlanTrain';
// import AddSmoothRunTime from './menus/addSmoothRunTime';
// import EditSmoothRunTime from './menus/editSmoothRunTime';
import ModifyingStationIntervalTime from './menus/modifyingStationIntervalTime';
import ModifyingStationStopTime from './menus/modifyingStationStopTime';
import PopulatingGenericData from './menus/populatingGenericData';
import CreateEmptyPlan from './menus/createEmptyPlan';
import { deletePlanService } from '@/api/runplan';
import { checkLoginLine } from '@/api/login';
import { getRpListByMapId } from '@/api/runplan';
import { getPublishMapDetailById } from '@/api/jmap/map';

export default {
    name: 'Menus',
    components: {
        MenuBar,
        StatusBar,
        Schedule,
        PopulatingGenericData,
        Parameter,
        OffLine,
        AddPlanningTrain,
        EditPlanningTrain,
        DuplicateTrain,
        SystermOut,
        AddTask,
        DeleteTask,
        ModifyingTask,
        ModifyingRouting,
        ModifyingBeginTime,
        EditStationBetweenTime,
        // AddSmoothRunTime,
        // EditSmoothRunTime,
        ModifyingStationIntervalTime,
        ModifyingStationStopTime,
        CreateEmptyPlan,
        EditPlanName,
        GernaratePlan
    },
    data() {
        return {
            PlanConvert: {},
            timeDemon: null,
            runPlanList: [],
            loadRunPlanId: '',
            loadRunPlanName: ''
        };
    },
    computed: {
        lineCode() {
            return this.$route.query.lineCode || '02';
        }
    },
    created() {
        this.PlanConvert = this.$theme.loadPlanConvert(this.lineCode);
    },
    async mounted() {
        this.timeDemon = setInterval(() => {
            checkLoginLine();
        }, 5000 * 60);
        if (this.$route.query.mapId) {
            this.refreshRunPlanList(true);
            await this.loadMap();
        }
    },
    beforeDestroy() {
        if (this.timeDemon) {
            clearTimeout(this.timeDemon);
        }
    },
    methods: {
        dispatchDialog(dialogObj) {
            this.$nextTick(() => {
                if (this.$refs[dialogObj.name]) {
                    this.$refs[dialogObj.name].doShow(dialogObj.params);
                }
            });
        },
        dispatchOperate(operateObj) {
            this.$nextTick(() => {
                if (this.$refs[operateObj.dialogName]) {
                    this.$refs[operateObj.dialogName][operateObj.operate](operateObj.params);
                }
            });
        },
        async loadMap() {
            const res = await getPublishMapDetailById(this.$route.query.mapId);
            if (res && res.code == 200) {
                this.$store.dispatch('map/setMapData', res.data.graphDataNew);
            }
        },
        handleConfirm(params) {
            if (params.operate == 'AddPlanningTrain') {
                // 添加计划
                this.$refs.addPlanningTrain.handleConfirm();
            } else if (params.operate == 'DeletePlanningTrain') {
                // 删除计划
                const model = {
                    planId: this.$route.query.planId,
                    serviceNumber: params.serviceNumber
                };
                deletePlanService(model).then(resp => {
                    this.$message.success(this.$t('tip.deletePlanSuccessfully'));
                    this.$store.dispatch('runPlan/setSelected', {});
                    this.$store.dispatch('runPlan/refresh');
                    // this.$refs.openRunPlan.loadRunPlanData(Object.assign({refresh: true}, this.$route.query));
                }).catch(() => {
                    this.$messageBox(this.$t('tip.deletePlanFailed'));
                });
            }
        },
        showTrain() {
            if (this.$refs.schedule) {
                this.$refs.schedule.displayTrain();
            }
        },
        refresh() {
            this.$store.dispatch('runPlan/refresh');
        },
        refreshRunPlanList(firstLoad) {
            getRpListByMapId(this.$route.query.mapId).then(resp => {
                this.runPlanList = resp.data || [];
                if (firstLoad && this.runPlanList.length) {
                    this.loadRunPlanId = this.runPlanList[0].id;
                    this.loadRunPlanName = this.runPlanList[0].name;
                }
            }).catch(() => {
                this.$message.error('获取草稿运行图列表失败');
            });
        },
        doClose() {
            this.$emit('doClose');
        },
        loadingRunPlan(param) {
            this.loadRunPlanId = param.planId;
            this.loadRunPlanName = param.planName;
        },
        checkIsLoadRunPlan(planId) {
            this.refreshRunPlanList(planId === this.loadRunPlanId);
        },
        modifyRunPlanName() {
            if (this.loadRunPlanId && this.loadRunPlanName) {
                this.$refs.editPlan.doShow({id: this.loadRunPlanId, name: this.loadRunPlanName});
            } else {
                this.$message.info(this.$t('planMonitor.openRunPlan.pleaseSelectRunplan'));
            }
        },
        refreshRunPlanName(name) {
            this.$refs.schedule.refreshRunPlanName(name);
        }
    }
};
</script>

<style>
    .plan-tool {
        position: absolute;
    }

    .plan-tool .pop-menu {
        background: #F0F0F0;
    }

    .plan-tool .pop-menu span {
        color: #000;
    }

    .plan-tool .system-close {
        cursor: pointer;
        height: 25px;
        width: 25px;
        background: -webkit-linear-gradient(#CD98A0, #C27D6E, #B63022, #C68770);
        background: -o-linear-gradient(#CD98A0, #C27D6E, #B63022, #C68770);
        background: -moz-linear-gradient(#CD98A0, #C27D6E, #B63022, #C68770);
        background: linear-gradient(#CD98A0, #C27D6E, #B63022, #C68770);
        border: 1px solid white;
        border-radius: 4px;
    }

    .plan-tool .system-close::before {
        position: absolute;
        top: 0px;
        left: 0px;
        font-size: x-large;
    }

    .planEdit__tool {
        overflow: hidden !important;
    }
</style>
