<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="editRules"
        type="DelayUnlock"
        @updateMapModel="updateMapModel"
      />
    </el-tab-pane>
    <el-tab-pane class="view-control" :label="$t('map.newConstruction')" name="second" :lazy="lazy">
      <create-operate
        ref="createForm"
        :create-form="formMake"
        :add-model="addModel"
        :create-rules="createRules"
        @create="create"
      />
    </el-tab-pane>
  </el-tabs>
</template>

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

export default {
    name: 'DelayUnlockDraft',
    components: {
        OperateProperty,
        CreateOperate
    },
    props: {
        selected: {
            type: Object,
            default: function () {
                return null;
            }
        }
    },
    data() {
        return {
            activeName: 'first',
            lazy: true,
            skins: [],
            editModel: {
                code: '',
                unlockTime: 0,
                stationCode: '',
                fontColor: '#FFFFFF',
                font: '',
                position: {
                    x: 0,
                    y: 0
                },
                status:'00'
            },
            addModel: {
                stationCode: ''
            }
        };
    },
    computed: {
        ...mapGetters('map', [
            'delayShowList',
            'stationList',
            'lineCode'
        ]),
        createRules: function () {
            return {
                stationCode: [
                    { required: true, message: this.$t('map.selectStation'), trigger: 'change' }
                ]
            };
        },
        editRules: function () {
            return {
                code: [
                    { required: true, message: this.$t('map.pleaseSelectDevice'), trigger: 'change' }
                ],
                stationCode: [
                    { required: true, message: this.$t('map.selectStation'), trigger: 'change' }
                ],
                unlockTime: [
                    { required: true, message: this.$t('map.pleaseEnterDelayTime'), trigger: 'change' }
                ],
                fontColor: [
                    { required: true, message: this.$t('map.pleaseEnterFontColor'), trigger: 'change' }
                ],
                font: [
                    { required: true, message: this.$t('map.pleaseEnterFontFormat'), trigger: 'change' }
                ],
                'position.x': [
                    { required: true, message: this.$t('map.pleaseEnterXCoordinate'), trigger: 'change' }
                ],
                'position.y': [
                    { required: true, message: this.$t('map.pleaseEnterYCoordinate'), trigger: 'change' }
                ]
            };
        },
        form() {
            const form = {
                labelWidth: '120px',
                items: {
                    code: {
                        name: '',
                        item: []
                    },
                    draw: {
                        name: this.$t('map.drawData'),
                        item: [
                            { prop: 'code', label: this.$t('map.delayUnlockingCode'), type: 'select', optionLabel: 'code', optionValue: 'code', options: this.delayShowList, deviceChange: this.deviceChange },
                            { prop: 'unlockTime', label: this.$t('map.delayTime'), type: 'number', min: 0, max: 1000, placeholder: 's' },
                            { prop: 'font', label: this.$t('map.font'), type: 'font' },
                            { prop: 'fontColor', label: this.$t('map.color'), type: 'color' },
                            { prop: 'position', label: this.$t('map.delayUnlockPosition'), type: 'coordinate', width: '110px', 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' }
                            ] }
                        ]
                    },
                    map: {
                        name: this.$t('map.mapData'),
                        item: [
                            { prop: 'stationCode', label: this.$t('map.belongsStation'), type: 'select', optionLabel: 'name&&code', optionValue: 'code', options: this.stationList }
                        ]
                    }
                }
            };
            return form;
        },
        formMake() {
            const form = {
                labelWidth: '120px',
                items:{
                    all:{
                        name:'',
                        item: [
                            { prop: 'stationCode', label: this.$t('map.stationName'), type: 'select', optionLabel: 'name&&code', optionValue: 'code', options: this.stationList }
                        ]
                    }
                }
            };
            return form;
        }
    },
    mounted() {
    },
    methods: {
        deviceChange(code) {
            this.deviceSelect(this.$store.getters['map/getDeviceByCode'](code));
        },
        deviceSelect(selected) {
            this.$nextTick(() => {
                this.$refs.form && this.$refs.form.resetFields();
                this.$refs.make && this.$refs.make.resetFields();
                if (selected && selected._type.toUpperCase() === 'DelayUnlock'.toUpperCase()) {
                    this.activeName = 'first';
                    this.editModel = deepAssign(this.editModel, selected);
                } else if (selected && selected._type.toUpperCase() === 'Station'.toUpperCase()) {
                    this.addModel.stationCode = selected.code;
                }

                this.$refs.form.clearValidate();
            });
        },
        create() {
            const uid = getUID('DelayUnlock');
            const model = {
                _type: 'DelayUnlock',
                font: '12px consolas',
                fontColor: '#FFFFFF',
                unlockTime: 10,
                code: uid,
                delayName: 'Test',
                status: '00'
            };
            this.stationList.forEach(elem => {
                if (elem.code === this.addModel.stationCode) {
                    model.position = { x: elem.position.x, y: elem.position.y };
                    model.stationCode = elem.code;
                }
            });
            this.$emit('updateMapModel', model);
        },
        updateMapModel(data) {
            this.$emit('updateMapModel', data);
        },
        deleteObj() {
            this.$refs.dataform.deleteObj();
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/mixin.scss";
	.card {
		height: 100%;
	}
</style>
