<template>
  <el-tabs v-model="activeName" class="card">
    <el-tab-pane class="view-control" :label="$t('map.property')" name="first" :lazy="lazy">
      <div style="height: calc(100% - 46px);">
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <config-list ref="form" :form="form" :form-model="editModel" :rules="editRules" />
        </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" :lazy="lazy">
      <div style="height: calc(100% - 46px);">
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <el-form ref="make" label-width="120px" :rules="createRules" :model="addModel" size="mini">
            <el-form-item :label="$t('map.stationName')" prop="stationCode">
              <el-select v-model="addModel.stationCode" filterable>
                <el-option
                  v-for="item in stationList"
                  :key="item.code"
                  :label="item.name + ' (' + item.code+ ')'"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </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: 'DelayUnlockDraft',
    components: {
        ConfigList
    },
    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
                }
            },
            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, change: true, 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;
        }
    },
    watch: {
        selected: function (val, oldVal) {
            this.deviceSelect(val);
        }
    },
    mounted() {
    },
    methods: {
        deviceChange(code) {
            this.deviceSelect(this.$store.getters['map/getDeviceByCode'](code));
        },
        deviceSelect(selected) {
            this.$nextTick(() => {
                this.$refs.form.resetFields();
                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() {
            this.$refs.make.validate((valid) => {
                if (valid) {
                    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);
                }
            });
        },
        // 修改对象
        edit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    const data = Object.assign({_type: 'DelayUnlock', status: '00'}, this.editModel);
                    this.$emit('updateMapModel', data);
                }
            });
        },
        // 删除对象
        deleteObj() {
            const selected = this.$store.getters['map/getDeviceByCode'](this.editModel.code);
            if (selected && selected._type.toUpperCase() === 'DelayUnlock'.toUpperCase()) {
                const _that = this;
                this.$confirm(this.$t('tip.confirmDeletion'), this.$t('tip.hint'), {
                    confirmButtonText: this.$t('map.confirm'),
                    cancelButtonText: this.$t('map.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%;
	}
</style>
