<template>
  <div>
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
    <edit-subsystem ref="editSubsystem" @tableReload="reloadTable" />
    <el-dialog
      width="30%"
      :title="title"
      :before-close="doCloseGenerate"
      :visible.sync="visible"
      center
    >
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="系统：" prop="simTypes">
          <el-select v-model="form.simTypes" multiple placeholder="请选择生成仿真类型">
            <el-option label="地铁CBTC" value="METRO" />
            <el-option label="大铁CTC" value="RAILWAY" />
            <el-option label="应急调度" value="EMERGENCY" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="doCloseGenerate">取 消</el-button>
        <el-button type="primary" @click="generateCommit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryMapSystemPaged, deleteMapSystem, generateMapSystemNew } from '@/api/trainingPlatform';
import { getPublishMapListOnline } from '@/api/jmap/map';
import EditSubsystem from './editSubsystem';
const simTypeMap = {
    METRO: '地铁CBTC',
    RAILWAY: '大铁CTC',
    EMERGENCY: '应急调度'
};
const clientMap = {
    C_ATS: '中心ATS工作站',
    C_ATS_BS: '中心ATS大屏',
    C_PA: '中心PA系统',
    C_CCTV: '中心视频监控系统',
    L_ATS: '现地ATS工作站',
    LCW: '本地控制工作站',
    L_CCTV: '现地视频监控系统',
    L_PA: '现地PA系统',
    GPC: '调度台终端',
    IPC: '联锁工作站',
    STPC: '车务终端',
    DMP: '车务管理终端',
    ISCS: 'ISCS',
    IBP: 'IBP',
    PSL: 'PSL',
    RUN_PLAN_DESIGN: '运行图编制',
    DRIVE: '列车驾驶'
};
export default {
    name: 'PublishMap',
    components: {
        EditSubsystem
    },
    data() {
        return {
            dialogVisible: false,
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            title: '一键生成子系统',
            visible: false,
            rules: {
                type: [
                    { required: true, message: '请选择生成仿真系统类型', trigger: 'change' }
                ]
            },
            form: {
                simTypes: []
            },
            queryForm: {
                labelWidth: '80px',
                reset: true,
                leftSpan: 18,
                queryObject: {
                    name: {
                        type: 'text',
                        label: '名称'
                    }
                }

            },
            queryList: {
                query: this.queryFunction,
                selectCheckShow: false,
                indexShow: true,
                columns: [
                    {
                        title: '名称',
                        prop: 'name'
                    },
                    {
                        title: '地图',
                        prop: 'mapId',
                        type: 'tag',
                        columnValue: (row) => { return this.$convertField(row.mapId, this.mapList, ['id', 'name']); },
                        tagType: () => { return 'success'; }
                    },
                    {
                        title: '系统',
                        prop: 'simType',
                        type: 'tag',
                        columnValue: (row) => { return simTypeMap[row.simType]; },
                        tagType: () => { return 'success'; }
                    },
                    {
                        title: '客户端',
                        prop: 'client',
                        type: 'tag',
                        columnValue: (row) => { return clientMap[row.paramVO.client]; },
                        tagType: () => { return ''; }
                    },
                    {
                        title: '描述',
                        prop: 'desc'
                    },
                    {
                        type: 'button',
                        title: '操作',
                        width: '300',
                        buttons: [
                            {
                                name: '更新',
                                handleClick: this.updateRow
                            },
                            {
                                name: '删除',
                                handleClick: this.deleteRow
                            }
                        ]
                    }
                ],
                actions: [
                    { text: '一键生成', handler: this.generateMapSystem },
                    { text: '新建', handler: this.handleAddSubsystem },
                    { text: '返回', handler: this.goBack }
                ]
            }
        };
    },
    computed: {
        isShow() {
            return this.$store.getters['roles'].indexOf('05');
        }
    },
    created() {
        this.mapList = [];
        getPublishMapListOnline().then(resp => {
            this.mapList = resp.data;
        });
    },
    methods: {
        queryFunction(params) {
            params['mapId'] = this.$route.query.mapId;
            return queryMapSystemPaged(params);
        },
        deleteRow(index, row) {
            this.$confirm('删除子系统，是否继续？', '提 示', {
                confirmButtonText: '确 定',
                cancelButtonText: '取 消',
                type: 'warning'
            }).then(() => {
                deleteMapSystem(row.id).then(resp => {
                    this.reloadTable();
                }).catch(error => {
                    this.$message.error(`删除子系统: ${error.message}`);
                });
            }).catch( () => { });
        },
        generateMapSystem() {
            // this.$confirm('一键生成子系统，是否继续？', '提 示', {
            //     confirmButtonText: '确 定',
            //     cancelButtonText: '取 消',
            //     type: 'warning'
            // }).then(() => {
            //     generateMapSystemNew(this.$route.query.mapId).then(resp => {
            //         this.reloadTable();
            //     }).catch(error => {
            //         this.$message.error(`生成子系统: ${error.message}`);
            //     });
            // }).catch( () => { });
            this.visible = true;
        },
        doCloseGenerate() {
            this.form.simTypes = [];
            this.visible = false;
        },
        generateCommit() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    generateMapSystemNew(this.$route.query.mapId, this.form).then(resp => {
                        this.reloadTable();
                        this.doCloseGenerate();
                    }).catch(error => {
                        this.$message.error(`一键生成仿真系统失败: ${error.message}`);
                    });
                }
            });
        },
        updateRow(index, row) {
            this.$refs.editSubsystem.doShow(row);
        },
        handleAddSubsystem() {
            this.$refs.editSubsystem.doShow();
        },
        reloadTable() {
            this.queryList.reload();
        },
        goBack() {
            this.$router.go(-1);
        }
    }
};
</script>
<style lang="scss" scoped>
/deep/
.el-button+.el-button{
	margin-top: 5px;
	margin-left: 5px;
}
</style>
