<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="110px" :model="addModel" size="mini" :rules="makeRules">
            <el-form-item :label="$t('map.textContent')" prop="content">
              <el-input v-model="addModel.content" :placeholder="$t('map.pleaseEnter')" class="input-with-select">
                <el-select slot="prepend" v-model="addModel.prepend" :placeholder="$t('map.pleaseSelect')">
                  <el-option :label="$t('map.horizontal')" value="H" />
                  <el-option :label="$t('map.vertical')" value="V" />
                </el-select>
              </el-input>
            </el-form-item>
            <div class="coordinate">
              <span class="title" style="width: 110px">{{ $t('map.textPoints') }}</span>
              <el-form-item
                label="x:"
                prop="position.x"
                style="display: table; float: left; margin-right: 20px;"
                label-width="25px"
              >
                <el-input-number v-model="addModel.position.x" label="x:" />
              </el-form-item>
              <el-form-item
                label="y:"
                prop="position.y"
                style="display: table; float: left;"
                label-width="25px"
              >
                <el-input-number v-model="addModel.position.y" label="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: 'StationStandDraft',
    components: {
        ConfigList
    },
    props: {
        selected: {
            type: Object,
            default: function () {
                return null;
            }
        }
    },
    data() {
        return {
            activeName: 'first',
            skins: [],
            editModel: {
                code: '',
                prepend: 'H',
                content: '',
                font: '',
                fontColor: '',
                position: {
                    x: 0,
                    y: 0
                }
            },
            addModel: {
                prepend: 'H',
                content: '',
                position: {
                    x: 0,
                    y: 0
                }
            },
            rules: {
                code: [
                    { required: true, message: this.$t('rules.selectText'), trigger: 'blur' }
                ],
                content: [
                    { required: true, message: this.$t('rules.pleaseEnterContent'), trigger: 'blur' }
                ],
                font: [
                    { required: true, message: this.$t('rules.textFont'), trigger: 'blur' }
                ],
                fontColor: [
                    { required: true, message: this.$t('rules.textFontColor'), trigger: 'blur' }
                ]
            },
            makeRules: {
                code: [
                    { required: true, message: this.$t('rules.selectText'), trigger: 'blur' }
                ],
                content: [
                    { required: true, message: this.$t('rules.pleaseEnterContent'), trigger: 'blur' }
                ],
                'position.x': [
                    { required: true, message: this.$t('rules.pleaseEnterXCoordinate'), trigger: 'blur' }
                ],
                'position.y': [
                    { required: true, message: this.$t('rules.pleaseEnterYCoordinate'), trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        ...mapGetters('map', [
            'textList',
            'stationList',
            'lineCode'
        ]),
        form() {
            const form = {
                labelWidth: '130px',
                items: {
                    code: {
                        name: '',
                        item: []
                    },
                    draw: {
                        name: this.$t('map.drawData'),
                        item: [
                            { prop: 'code', label: this.$t('map.textCode'), type: 'select', optionLabel: 'code', optionValue: 'code', options: this.textList, change: true, deviceChange: this.deviceChange },
                            { prop: 'content', label: this.$t('map.textContent'), type: 'fontContent', content: 'content', prepend: 'prepend', placeholder: this.$t('map.pleaseEnter') },
                            { prop: 'font', label: this.$t('map.textFont'), type: 'font', placeholder: this.$t('map.font') },
                            { prop: 'fontColor', label: this.$t('map.textFontColor'), type: 'color' },
                            { prop: 'position', label: this.$t('map.textPoints'), type: 'coordinate', width: '120px', children: [
                                { prop: 'position.x', firstLevel: 'position', secondLevel: 'x', label: 'x:', type: 'number', labelWidth: '25px', disabled: false },
                                { prop: 'position.y', firstLevel: 'position', secondLevel: 'y', label: 'y:', type: 'number', labelWidth: '25px', disabled: false }
                            ] }
                        ]
                    },
                    map: {
                        name: this.$t('map.mapData'),
                        item: [
                        ]
                    }
                }
            };
            return form;
        }
    },
    watch: {
        selected: function (val, oldVal) {
            this.deviceSelect(val);
        }
    },
    methods: {
        deviceChange(code) {
            this.$emit('setCenter', code);
            this.deviceSelect(this.$store.getters['map/getDeviceByCode'](code));
        },
        deviceSelect(selected) {
            this.$refs.form.resetFields();
            this.$refs.make.resetFields();
            if (selected && selected._type.toUpperCase() === 'Text'.toUpperCase()) {
                this.activeName = 'first';
                this.editModel = deepAssign(this.editModel, selected);
                [this.editModel.prepend, this.editModel.content] = selected.content.split('::');
            }
        },
        create() {
            this.$refs['make'].validate((valid) => {
                if (valid) {
                    const model = {
                        _type: 'Text',
                        code: getUID('Text'),
                        font: '14px consolas',
                        fontColor: '#FFFFFF',
                        content: `${this.addModel.prepend}::${this.addModel.content}`,
                        position: {
                            x: this.addModel.position.x,
                            y: this.addModel.position.y
                        }
                    };
                    this.$emit('updateMapModel', model);
                }
            });
        },
        // 修改对象
        edit() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    const data = Object.assign({_type: 'Text'}, this.editModel);
                    data.content = `${this.editModel.prepend}::${this.editModel.content}`;
                    this.$emit('updateMapModel', data);
                }
            });
        },
        // 删除对象
        deleteObj() {
            const selected = this.$store.getters['map/getDeviceByCode'](this.editModel.code);
            if (selected && selected._type.toUpperCase() === 'Text'.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('updateMapModel', {...selected, _dispose: true});
                    _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: 120px;
            font-weight: bold;
            display: block;
            float: left;
            margin-right: 7px;
        }
    }
    /deep/ {
        .el-select .el-input {
            width: 130px;
        }

        .input-with-select .el-input-group__prepend {
            background-color: #fff;
        }
    }
</style>
