<template>
  <div v-show="dialogVisible">
    <el-dialog v-dialogDrag :title="title" :visible.sync="dialogVisible" fullscreen :before-close="handleClose" center :close-on-click-modal="false" :z-index="2000">
      <div style="overflow-y: scroll;" :style="{height: height+ 'px'}">
        <el-card>
          <div slot="header" style="font-weight: bold;text-align: center;">
            <span>通用配置项</span>
          </div>
          <el-table :data="generalData" style="width: 100%;">
            <el-table-column prop="configKey" label="key" />
            <el-table-column prop="configValue" label="value">
              <template slot-scope="scope">
                <div v-if="scope.row.type === 'input'">
                  <div v-if="!scope.row.focus" style="width: 100%;cursor: pointer;" @click="changeFocus(scope.row, '1')">{{ scope.row.configValue }}</div>
                  <el-input v-if="scope.row.focus" v-model="scope.row.configValue" size="mini" style="width: 100%" @blur="changeFocus(scope.row, '0')" />
                </div>
                <div v-else-if="scope.row.type === 'number'">
                  <div v-if="!scope.row.focus" style="width: 100%;cursor: pointer" @click="changeFocus(scope.row, '1')">{{ scope.row.configValue }}</div>
                  <el-input-number v-if="scope.row.focus" v-model="scope.row.configValue" size="mini" style="width: 100px;" :min="0" controls-position="right" @blur="changeFocus(scope.row, '0')" />
                </div>
                <div v-else-if="scope.row.type === 'boolean'">
                  <el-radio v-model="scope.row.configValue" :label="true">是</el-radio>
                  <el-radio v-model="scope.row.configValue" :label="false">否</el-radio>
                </div>
                <div v-else-if="scope.row.type === 'select'">
                  <el-select v-model="scope.row.configValue" size="mini" style="width:120px;">
                    <el-option
                      v-for="item in scope.row.options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </div>
                <div v-else-if="scope.row.type === 'range'">
                  <el-input v-model="scope.row.rangeStart" size="mini" style="display: inline-block;width: 80px;" @change="rangeChange(scope.row)" />-
                  <el-input v-model="scope.row.rangeEnd" size="mini" style="display: inline-block;width: 80px;" @change="rangeChange(scope.row)" />
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="描述" />
          </el-table>
        </el-card>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">{{ $t('global.confirm') }}</el-button>
        <el-button @click="dialogVisible = false">{{ $t('global.cancel') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRealLineConfigList, putRealLineConfig } from '@/api/management/mapline';
export default {
    name: 'Config',
    components: {
        // EditConfig
    },
    props: {
        type: {
            type: String,
            default() {
                return '';
            }
        }
    },
    data() {
        return {
            dialogVisible: false,
            index: 0,
            title:'',
            id: '',
            generalData: [],
            height: 800,
            roadData: [],
            focus: false,
            booleanList: ['lockFirst', 'switchSingleHandle', 'switchNRTurnChain', 'switchSingleLockChain', 'switchLossChain', 'signalForceCancelRoute', 'initSingleLockSwitch', 'ctcOverlapOnlyTurnBackStationLock'],
            selectList: ['upDirection', 'runMode'],
            generalConfig: ['lockFirst', 'switchSingleHandle', 'upDirection', 'switchNRTurnChain', 'switchSingleLockChain', 'switchLossChain', 'signalForceCancelRoute', 'runMode', 'initSingleLockSwitch', 'ctcOverlapOnlyTurnBackStationLock', 'noParkingSM', 'parkingSM'],
            rangeList: ['noParkingSM', 'parkingSM'],
            numberList: [],
            optionsMap: {
                upDirection: [{label: 'right', value: 'right'}, {label: 'left', value: 'left'}],
                runMode:[{label: 'CBTC级别', value: 'CBTC'}, {label: '点式通信', value: 'ITC'}, {label: '联锁级', value: 'IL'}]
            },
            remarkMap: {
                lockFirst: '是否先锁闭——办理过程直接先锁闭区段',
                upDirection: '上行方向',
                switchSingleHandle: '道岔区段状态改变按单个道岔处理',
                switchNRTurnChain:'道岔正/反操是否联动',
                switchSingleLockChain:'道岔单解/锁是否联动',
                signalForceCancelRoute:'是否强制取消进路/在接近区段占用时是否依旧强制执行取消进路',
                runMode:'列车控制模式/级别',
                initSingleLockSwitch: '初始加载设备时是否默认单锁正线道岔',
                ctcOverlapOnlyTurnBackStationLock: 'CTC列车进路延续保护仅折返站处锁闭',
                noParkingSM: '不停站头码车服务号取值范围',
                parkingSM: '停站头码车服务号取值范围',
                switchLossChain: '道岔故障失表是否联动'
            }
        };
    },
    computed: {
    },
    methods: {
        async show(row) {
            this.title = row.name + ' 配置项';
            this.dialogVisible = true;
            this.height = document.documentElement.clientHeight - 180;
            if (row && row.id) {
                this.id = row.id;
                this.getList();
            }
        },
        changeFocus(row, flag) {
            if (flag === '0') {
                this.$set(row, 'focus', false);
            } else {
                this.$set(row, 'focus', true);
            }
        },
        rangeChange(row) {
            row.configValue = row.rangeStart + '-' + row.rangeEnd;
        },
        async getList() {
            try {
                const res = await getRealLineConfigList(this.id);
                if (res.data) {
                    const keys = Object.keys(res.data);
                    this.generalData = [];
                    this.roadData = [];
                    keys.forEach(key => {
                        // let value = '';
                        let type = 'input';
                        let rangeStart = 0;
                        let rangeEnd = 0;
                        let options = [];
                        if (this.booleanList.indexOf(key) >= 0) {
                            type = 'boolean';
                        } else if (this.selectList.indexOf(key) >= 0) {
                            type = 'select';
                            options = this.optionsMap[key];
                        } else if (this.numberList.indexOf(key) >= 0) {
                            type = 'number';
                        } else if (this.rangeList.indexOf(key) >= 0) {
                            type = 'range';
                            if (res.data[key]) {
                                const ranges = res.data[key].split('-');
                                rangeStart = ranges[0];
                                rangeEnd = ranges[1];
                            }
                        } else {
                            type = 'input';
                        }
                        const param = {
                            configKey: key,
                            configValue: res.data[key],
                            rangeStart: rangeStart,
                            rangeEnd: rangeEnd,
                            type: type,
                            options: options,
                            remark: this.remarkMap[key]
                        };
                        if (this.generalConfig.includes(key)) {
                            this.generalData.push(param);
                        } else {
                            this.roadData.push(param);
                        }
                    });
                } else {
                    this.generalData = [];
                    this.roadData = [];
                }
            } catch (error) {
                console.log(error);
            }
        },
        handleClose(done) {
            if (done) {
                done();
            } else {
                this.dialogVisible = false;
            }
        },
        addModel() {
            this.$refs.addConfig.show();
        },
        editModel(item, index) {
            this.$refs.editConfig.show(item);
            this.index = index;
        },
        save() {
            const param = {};
            this.generalData.forEach(item => {
                param[item.configKey] = item.configValue;
                // if (item.boolean) {
                //     const value = item.configValue == 'true';
                //     param[item.configKey] = value;
                // }
            });
            this.roadData.forEach(item => {
                param[item.configKey] = item.configValue;
            });
            putRealLineConfig(this.id, param).then(res => {
                this.$message.success(`保存配置项成功!`);
                this.dialogVisible = false;
            }).catch(() => {
                this.$messageBox(`保存配置项失败.`);
            });

        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .icon_font{
        font-size: 18px;
        margin-left: 15px;
        cursor: pointer;
    }
    .flex_box{
        display: flex;
        justify-content: flex-start;
        align-items: center;
    }

</style>

