<template>
  <el-tabs v-model="activeName" class="card">
    <el-tab-pane class="view-control" :label="$t('map.property')" name="first">
      <div style="height: calc(100% - 46px);">
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <config-list ref="form" :form="form" :form-model="editModel" :rules="rules" />
        </el-scrollbar>
      </div>
      <div class="button_box">
        <el-button-group class="map-draft-group">
          <el-button type="primary" size="small" @click="edit">{{ $t('map.updateObj') }}</el-button>
          <el-button type="danger" size="small" @click="deleteObj">{{ $t('map.deleteObj') }}</el-button>
        </el-button-group>
      </div>
    </el-tab-pane>
    <el-tab-pane class="view-control" :label="$t('map.newConstruction')" name="second">
      <div style="height: calc(100% - 46px);">
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <el-form ref="make" label-width="150px" :rules="createRules" :model="addModel" size="mini">
            <el-form-item :label="$t('map.statusSignalName')" prop="name">
              <el-input v-model="addModel.name" />
            </el-form-item>
            <div class="coordinate">
              <span class="title">{{ $t('map.stateSignalsPlotCoordinates') }}</span>
              <el-form-item
                label="x:"
                prop="position.x"
                style="display: table; float: left; margin-right: 20px;"
                label-width="20px"
              >
                <el-input-number v-model="addModel.position.x" />
              </el-form-item>
              <el-form-item
                label="y:"
                prop="position.y"
                style="display: table; float: left;"
                label-width="20px"
              >
                <el-input-number v-model="addModel.position.y" />
              </el-form-item>
            </div>
          </el-form>
        </el-scrollbar>
      </div>
      <div class="button_box">
        <el-button-group class="map-draft-group">
          <el-button type="primary" size="small" @click="create">{{ $t('map.create') }}</el-button>
        </el-button-group>
      </div>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import { mapGetters } from 'vuex';
import { getUID } from '@/jmap/utils/Uid';
import ConfigList from './config/list';
import { deepAssign } from '@/utils/index';

export default {
    name: 'LcControlDraft',
    components: {
        ConfigList
    },
    props: {
        selected: {
            type: Object,
            default: function () {
                return null;
            }
        }
    },
    data() {
        return {
            activeName: 'first',
            mapData: null,
            editModel: {
                code: '',
                name: '',
                position: {
                    x: 0,
                    y: 0
                }
            },
            addModel: {
                code: '',
                name: '',
                position: {
                    x: 0,
                    y: 0
                }
            },
            rules: {
                code: [
                    { required: true, message: this.$t('rules.pleaseSelectEncoding'), trigger: 'change' }
                ],
                name: [
                    { required: true, message: this.$t('rules.pleaseEnterStatusSignal'), trigger: 'blur' }
                ],
                'position.x': [
                    { required: true, message: this.$t('map.pleaseEnterXCoordinate'), trigger: 'blur' }
                ],
                'position.y': [
                    { required: true, message: this.$t('map.pleaseEnterYCoordinate'), trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        ...mapGetters('map', [
            'sectionList',
            'trainModelList',
            'lcList',
            'skinCode'
        ]),
        createRules: function () {
            return {
                name: [
                    { required: true, message: this.$t('rules.pleaseEnterStatusSignal'), trigger: 'blur' }
                ],
                'position.x': [
                    { required: true, message: this.$t('map.pleaseEnterXCoordinate'), trigger: 'blur' }
                ],
                'position.y': [
                    { required: true, message: this.$t('map.pleaseEnterYCoordinate'), trigger: 'blur' }
                ]
            };
        },
        form() {
            const form = {
                labelWidth: '150px',
                items: {
                    code: {
                        name: '',
                        item: []
                    },
                    draw: {
                        name: this.$t('map.drawData'),
                        item: [
                            { prop: 'code', label: `${this.$t('map.lcControl')}${this.$t('map.code')}`, type: 'select', optionLabel: 'name&&code', optionValue: 'code', options: this.lcList, change: true, deviceChange: this.deviceChange },
                            { prop: 'name', label: this.$t('map.statusSignalName'), type: 'input' },
                            { prop: 'position', label: this.$t('map.stateSignalsPlotCoordinates'), type: 'coordinate', width: '150px', children: [
                                { prop: 'position.x', firstLevel: 'position', secondLevel: 'x', label: 'x:', type: 'number', labelWidth: '20px', disabled: false },
                                { prop: 'position.y', firstLevel: 'position', secondLevel: 'y', label: 'y:', type: 'number', labelWidth: '20px', disabled: false }
                            ] }
                        ]
                    },
                    map: {
                        name: this.$t('map.mapData'),
                        item: [
                        ]
                    }
                }
            };
            return form;
        }
    },
    watch: {
        selected: function (val, oldVal) {
            this.deviceSelect(val);
        }
    },
    mounted() {
    },
    methods: {
        deviceChange(code) {
            this.$emit('setCenter', code);
            this.deviceSelect(this.$store.getters['map/getDeviceByCode'](code));
        },
        deviceSelect(selected) {
            if (selected && selected._type.toUpperCase() === 'LcControl'.toUpperCase()) {
                this.$refs.form.resetFields();
                this.$refs.make.resetFields();
                this.activeName = 'first';
                this.editModel = deepAssign(this.editModel, selected);
            }
        },
        create() {
            this.$refs.make.validate((valid) => {
                if (valid) {
                    const uid = getUID('LcControl');
                    const model = {
                        _type: 'LcControl',
                        code: uid,
                        name: this.addModel.name,
                        position: {
                            x: this.addModel.position.x,
                            y: this.addModel.position.y
                        }
                    };
                    this.$emit('addOrUpdateMapModel', model);
                }
            });
        },
        // 修改对象
        edit() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    const data = Object.assign({_type: 'LcControl'}, this.editModel);
                    this.$emit('addOrUpdateMapModel', data);
                }
            });
        },
        // 删除对象
        deleteObj() {
            const selected = this.$store.getters['map/getDeviceByCode'](this.editModel.code);
            if (selected && selected._type.toUpperCase() === 'LcControl'.toUpperCase()) {
                const _that = this;
                this.$confirm(this.$t('tip.confirmDeletion'), this.$t('tip.hint'), {
                    confirmButtonText: this.$t('tip.confirm'),
                    cancelButtonText: this.$t('tip.cancel'),
                    type: 'warning'
                }).then(() => {
                    _that.$emit('delMapModel', selected);
                    _that.deviceSelect();
                }).catch(() => {
                    _that.$message.info(this.$t('tip.cancelledDelete'));
                });
            }
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/mixin.scss";
	.view-control{
		height: 100%;
	}

	.card {
		height: 100%;
	}
    .coordinate {
        overflow: hidden;

        .title {
            text-align: right;
            font-size: 14px;
            color: #606266;
            line-height: 40px;
            padding: 0 12px 0 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            line-height: 28px;
            width: 160px;
            font-weight: bold;
            display: block;
            float: left;
        }
    }

    .map-draft-group {
        color: #3E44BE;
    }
</style>
