<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    class="beijing-01__schedule reload-today-plan"
    :visible.sync="dialogShow"
    width="80%"
    :before-close="doClose"
    :modal="false"
    :close-on-click-modal="false"
  >
    <QueryListPage ref="pageRules" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
    <el-row type="flex" justify="center" class="button-group">
      <el-button type="primary" :loading="loading" @click="handleConfirm">加 载</el-button>
      <el-button @click="dialogShow = false">取 消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { runPlanTemplateList } from '@/api/runplan';
import { getStationList } from '@/api/runplan';
import { getEveryDayRunPlanData, generateDayRunPlan } from '@/api/simulation';
import { getPublishMapInfo } from '@/api/jmap/map';

export default {
    name: 'ReloadTodayPlan',
    data() {
        return {
            dialogShow: false,
            loading: false,
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            queryForm: {
                reset: true,
                labelWidth: '100px',
                queryObject: {
                    name: {
                        type: 'text',
                        label: '运行图名称'
                    }
                }
            },
            queryList: {
                query: this.queryFunction,
                selectCheckShow: false,
                radioShow: true,
                columns: [
                    {
                        title: '运行图名称',
                        prop: 'name'
                    }
                ]
            },

            currentModel: {}
        };
    },
    computed: {
        title() {
            return '加载当天计划';
        }
    },
    methods: {
        doShow() {
            this.loading = false;
            this.dialogShow = true;
        },
        doClose() {
            this.dialogShow = false;
        },
        queryFunction(params) {
            if (this.$store.state.map && this.$store.state.map.map) {
                // params['lineCode'] = this.$store.getters['map/lineCode'];
                params['mapId'] = this.$route.query.mapId;
            }
            return runPlanTemplateList(params);
        },
        // 生成每日运行图
        handleConfirm() {
            if (this.$refs && this.$refs.pageRules) {
                const choose = this.$refs.pageRules.currentChoose();
                if (choose && choose.id) {
                    this.loading = true;
                    generateDayRunPlan(choose.id, this.$route.query.group).then(response => {
                        this.loading = false;
                        this.reloadTable();
                        this.loadRunData();
                        this.doClose();
                        this.$message.success(`生成用户每日运行图成功`);
                    }).catch((error) => {
                        console.error(error);
                        this.loading = false;
                        this.reloadTable();
                        this.$messageBox(`生成用户每日运行图失败`);
                    });
                } else {
                    this.$messageBox(`请选择需要加载的运行图`);
                }
            }
        },
        loadRunData() {
            // const skinCode = this.$route.query.skinCode;
            const mapId = this.$route.query.mapId;
            this.$store.dispatch('runPlan/clear');
            if (mapId) {
                getStationList(mapId).then(response => {
                    const stations = response.data;
                    getPublishMapInfo(this.$route.query.mapId).then(res=>{
                        this.PlanConvert = this.$theme.loadPlanConvert(res.data.lineCode);
                        this.$store.dispatch('runPlan/setStations', stations).then(() => {
                            // getEveryDayRunPlanData(this.$route.query.group).then(resp => {
                            //     this.$store.dispatch('runPlan/setPlanData', resp.data);
                            // }).catch(() => {
                            //     this.$store.dispatch('runPlan/setPlanData', []);
                            //     this.$messageBox(`获取运行图数据失败`);
                            // });
                        });
                    });
                }).catch(() => {
                    this.$messageBox(`获取车站列表失败`);
                });
            }
        },
        reloadTable() {
            this.queryList.reload();
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/mixin.scss";
</style>
