<template>
  <el-tabs v-model="activeName" class="card">
    <el-tab-pane class="view-control" :label="$t('map.property')" name="first" :lazy="lazy">
      <operate-property
        ref="dataform"
        :form="form"
        :edit-model="editModel"
        :rules="rules"
        type="Arrow"
        @updateMapModel="updateMapModel"
        @clearDeviceSelect="clearDeviceSelect"
      />
    </el-tab-pane>
    <el-tab-pane class="view-control" :label="$t('map.newConstruction')" name="second" :lazy="lazy">
      <create-operate
        ref="createForm"
        :add-model="addModel"
        :create-form="form"
        :create-rules="rules"
        @create="create"
      />
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import { mapGetters } from 'vuex';
import { getUID } from '@/jmapNew/utils/Uid';
import OperateProperty from './components/operateProperty';
import CreateOperate from './components/createOperate';
import { deepAssign } from '@/utils/index';

export default {
    name: 'StationStandDraft',
    components: {
        OperateProperty,
        CreateOperate
    },
    props: {
        selected: {
            type: Object,
            default: function () {
                return null;
            }
        }
    },
    data() {
        return {
            activeName: 'first',
            lazy: true,
            editModel: {
                _type: 'Arrow',
                code: '',
                lineWidth: 2,
                length: 15,
                rotate: 0,
                color: 'rgba(255, 255, 255, 1)',
                triangleLength: 10,
                triangleHeight: 6,
                position: {x:0, y:0}
            },
            addModel: {
                _type: 'Arrow',
                code: '',
                lineWidth: 2,
                length: 15,
                rotate: 0,
                color: 'rgba(255, 255, 255, 1)',
                triangleLength: 10,
                triangleHeight: 6,
                position: {x:0, y:0}
            },
            rules: {
                code: [
                    { required: true, message: this.$t('rules.pleaseSelectLine'), trigger: 'blur' }
                ],
                'position.x': [
                    { required: true, message: this.$t('rules.trainPositionX'), trigger: 'blur' }
                ],
                'position.y': [
                    { required: true, message: this.$t('rules.trainPositionY'), trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        ...mapGetters('map', [
            'arrowList'
        ]),
        form() {
            const form = {
                labelWidth: '120px',
                items: {
                    draw: {
                        name: this.activeName == 'first' ? this.$t('map.drawData') : '',
                        item: [
                            { prop: 'code', label: '箭头编码', type: 'select', optionLabel: 'code', optionValue: 'code', options: this.arrowList, deviceChange: this.deviceChange, isHidden:this.activeName == 'second' },
                            { prop: 'position', label: '坐标:', type: 'coordinate', width: '120px', children: [
                                { prop: 'position.x', firstLevel: 'position', secondLevel: 'x', label: 'x:', type: 'number', labelWidth: '25px' },
                                { prop: 'position.y', firstLevel: 'position', secondLevel: 'y', label: 'y:', type: 'number', labelWidth: '25px' }
                            ] },
                            { prop: 'length', label: '长度:', type: 'number', min:1, placeholder: 'px'},
                            { prop: 'lineWidth', label: this.$t('map.lineWidth'), type: 'number', min: 1, placeholder: 'px' },
                            { prop: 'triangleLength', label: '三角长度:', type: 'number', min: 1, placeholder: 'px'},
                            { prop: 'triangleHeight', label: '三角高度:', type: 'number', min: 1, placeholder: 'px'},
                            { prop: 'rotate', label: '旋转角度', type: 'number', max: 360, placeholder: '度' },
                            { prop: 'color', label: '颜色:', type: 'color' }
                        ]
                    }
                }
            };
            return form;
        }
    },
    methods: {
        deviceChange(code) {
            this.$emit('setCenter', code);
            this.deviceSelect(this.$store.getters['map/getDeviceByCode'](code));
        },
        deviceSelect(selected) {
            this.$refs.createForm && this.$refs.createForm.resetFields();
            if (selected && selected._type.toUpperCase() === 'Arrow'.toUpperCase()) {
                this.activeName = 'first';
                this.$nextTick(()=>{
                    this.$refs.dataform && this.$refs.dataform.resetFields();
                    this.editModel = deepAssign(this.editModel, selected);
                });
            }
        },
        clear() {
            this.addModel = {
                _type: 'Arrow',
                code: '',
                lineWidth: 2,
                length: 15,
                color: 'rgba(255, 255, 255, 1)',
                triangleLength: 10,
                triangleHeight: 6,
                position: {x:0, y:0}
            };
            this.$refs.createForm && this.$refs.createForm.resetFields();
        },
        clearDeviceSelect() {
            this.$emit('deviceSelect', '');
        },
        create() {
            const model = deepAssign(this.addModel, {code: getUID('Arrow', this.arrowList)});
            this.$emit('updateMapModel', model);
            this.clear();
        },
        updateMapModel(data) {
            this.$emit('updateMapModel', data);
        },
        deleteObj() {
            this.$refs.dataform.deleteObj();
        },
        // 修改对象
        edit() {
            this.$refs.dataform.edit();
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
	@import "src/styles/mixin.scss";
	.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;
		}
	}

	.point-section {
		/*float: left;*/
		position: absolute;
		left: 120px;
		width: calc(100% - 120px);
	}

	.point-button {
		width: 28px;
		height: 28px;
		display: flex;
		align-items: center;
		justify-content: center;
		float: left;

		/deep/ {
			.el-icon-plus,
			.el-icon-minus {
				transform: translateY(-5px);
			}
		}
	}

	.el-input-number--mini {
		width: 110px;
	}
</style>
