<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="120px" :model="addModel" :rules="createRules" 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-item :label="$t('map.counterType')" prop="type">
              <el-select v-model="addModel.type" filterable :placeholder="$t('map.pleaseSelect')">
                <el-option
                  v-for="item in typeList"
                  :key="item.code"
                  :label="item.name"
                  :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 '@/jmapNew/utils/Uid';
import ConfigList from './config/list';
import { deepAssign } from '@/utils/index';

export default {
    name: 'CounterDraft',
    components: {
        ConfigList
    },
    props: {
        selected: {
            type: Object,
            default: function () {
                return null;
            }
        }
    },
    data() {
        return {
            activeName: 'first',
            typeList: [
                { code: '01', name: this.$t('map.areaSolution') },
                { code: '02', name: this.$t('map.alwaysSolution') }
            ],
            editModel: {
                code: '',
                name: '',
                type: '',
                max: 0,
                val: 0,
                stationCode: '',
                position: {
                    x: 0,
                    y: 0
                }
            },
            addModel: {
                type: '',
                stationCode: ''
            }
        };
    },
    computed: {
        ...mapGetters('map', [
            'counterList',
            'stationList',
            'lineCode'
        ]),
        createRules: function () {
            return {
                type: [
                    { required: true, message: this.$t('map.selectCounterType'), trigger: 'change' }
                ],
                stationCode: [
                    { required: true, message: this.$t('map.selectStation'), trigger: 'change' }
                ]
            };
        },
        rules: function () {
            return {
                code: [
                    { required: true, message: this.$t('map.pleaseSelectDevice'), trigger: 'change' }
                ],
                stationCode: [
                    { required: true, message: this.$t('map.selectStation'), trigger: 'change' }
                ],
                name: [
                    { required: true, message: this.$t('map.pleaseSelectCountName'), trigger: 'change' }
                ],
                type: [
                    { required: true, message: this.$t('map.selectCounterType'), trigger: 'change' }
                ],
                max: [
                    { required: true, message: this.$t('map.pleaseCounterValue'), 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.counterCoding'), type: 'select', optionLabel: 'code', optionValue: 'code', options: this.counterList, change: true, deviceChange: this.deviceChange },
                            { prop: 'name', label: this.$t('map.counterName'), type: 'input' },
                            { prop: 'type', label: this.$t('map.counterType'), type: 'select', optionLabel: 'name', optionValue: 'code', options: this.typeList },
                            { prop: 'max', label: this.$t('map.countMax'), type: 'number', min: 0, placeholder: this.$t('map.piece') },
                            { prop: 'position', label: this.$t('map.counterPosition'), 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.$refs.form.resetFields();
            this.$refs.make.resetFields();
            if (selected && selected._type.toUpperCase() === 'Counter'.toUpperCase()) {
                this.activeName = 'first';
                this.editModel = deepAssign(this.editModel, selected);
            } else if (selected && selected._type.toUpperCase() === 'Station'.toUpperCase()) {
                this.addModel.stationCode = selected.code;
            }
        },
        create() {
            this.$refs.make.validate((valid) => {
                if (valid) {
                    const uid = getUID('Counter', this.counterList);
                    const model = {
                        _type: 'Counter',
                        code: uid,
                        type: this.addModel.type,
                        max: 0,
                        val: 0
                    };
                    this.typeList.forEach(elem => {
                        if (elem.code === this.addModel.type) {
                            model.name = elem.name;
                        }
                    });
                    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: 'Counter'}, this.editModel);
                    this.$emit('updateMapModel', data);
                }
            });
        },
        // 删除对象
        deleteObj() {
            const selected = this.$store.getters['map/getDeviceByCode'](this.editModel.code);
            if (selected && selected._type.toUpperCase() === 'Counter'.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>
