<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="rules" />
        </el-scrollbar>
      </div>
      <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>
    </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">
          <config-list ref="make" :form="makeForm" :form-model="addModel" :rules="rules" />
          <!-- <el-form ref="make" label-width="120px" :model="addModel" size="mini" :rules="makeRules">
            <el-form-item :label="$t('map.lineType')" prop="type">
              <el-select v-model="addModel.type" filterable>
                <el-option
                  v-for="item in LineTypeList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('map.lineWidth')" prop="width">
              <el-input-number v-model="addModel.width" :min="1" />px
            </el-form-item>
            <div class="coordinate">
              <span class="title">{{ $t('map.linePoint') }}</span>
              <div class="point-section">
                <template v-for="(point, index) in addModel.points">
                  <div :key="index" style="overflow: hidden;">
                    <el-form-item
                      label=""
                      :prop="'points[' + index + '].x'"
                      style="display: table; float: left;"
                      label-width="0px"
                    >
                      <el-input-number v-model="point.x" />
                    </el-form-item>
                    <span style="display: table; margin-left: 8px; float: left; line-height: 28px;">
                      , </span>
                    <el-form-item
                      label=""
                      :prop="'points[' + index + '].y'"
                      style="display: table; float: left; margin-right: 5px;"
                      label-width="10px"
                    >
                      <el-input-number v-model="point.y" />
                    </el-form-item>
                    <el-button
                      icon="el-icon-plus"
                      circle
                      class="point-button"
                      @click="addPoint(addModel.points, index)"
                    />
                    <el-button
                      icon="el-icon-minus"
                      :disabled="index == 0 || index == addModel.points.length - 1"
                      circle
                      class="point-button"
                      @click="delPoint(addModel.points, index)"
                    />
                  </div>
                </template>
              </div>
            </div>
          </el-form> -->
        </el-scrollbar>
      </div>
      <el-button-group class="map-draft-group">
        <el-button type="primary" size="small" @click="create">{{ $t('map.create') }}</el-button>
      </el-button-group>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import ConstConfig from '@/scripts/ConstConfig';
import Cookies from 'js-cookie';
import { mapGetters } from 'vuex';
import { getUID } from '@/jmapNew/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',
            lazy: true,
            LineTypeList: [],
            editModel: {
                code: '',
                width: 1,
                height: 1,
                showConditions: '01',
                position: {
                    x: 0,
                    y: 0
                }
            },
            addModel: {
                code: '',
                width: 200,
                height: 100,
                showConditions: '01',
                position: {
                    x: 0,
                    y: 0
                }
            },
            rules: {
                code: [
                    { required: true, message: this.$t('rules.pleaseSelectLine'), trigger: 'blur' }
                ],
                type: [
                    { required: true, message: this.$t('rules.pleaseSelectLineType'), trigger: 'blur' }
                ],
                width: [
                    { required: true, message: this.$t('rules.pleaseSelectLineWidth'), trigger: 'blur' }
                ]
            },
            makeRules: {
                code: [
                    { required: true, message: this.$t('rules.pleaseSelectLine'), trigger: 'blur' }
                ],
                type: [
                    { required: true, message: this.$t('rules.pleaseSelectLineType'), trigger: 'blur' }
                ],
                width: [
                    { required: true, message: this.$t('rules.pleaseSelectLineWidth'), trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        ...mapGetters('map', [
            'outerFrameList'
        ]),
        showConditionsList() {
            const showConditionsList = ConstConfig.ConstSelect.showConditionsList;
            return Cookies.get('user_lang') == 'en'
                ? showConditionsList.map(elem => { return { value: elem.value, label: elem.enlabel }; })
                : showConditionsList.map(elem => { return { value: elem.value, label: elem.label }; });
        },
        form() {
            const form = {
                labelWidth: '120px',
                items: {
                    draw: {
                        name: this.$t('map.drawData'),
                        item: [
                            { prop: 'code', label: this.$t('map.lineCoding'), type: 'select', optionLabel: 'code', optionValue: 'code', options: this.outerFrameList, change: true, deviceChange: this.deviceChange },
                            { prop: 'width', label: '宽度:', type: 'number', min: 1, placeholder: 'px' },
                            { prop: 'height', label: '高度:', type: 'number', min: 1, placeholder: 'px' },
                            { prop: 'showConditions', label: this.$t('map.showConditions'), type: 'radio', optionLabel: 'label', optionValue:'value', radioList: this.showConditionsList},
                            { prop: 'position', label: '坐标:', type: 'coordinate', width: '140px', children: [
                                { prop: 'position.x', firstLevel: 'position', secondLevel: 'x', label: 'x:', type: 'number', labelWidth: '20px' },
                                { prop: 'position.y', firstLevel: 'position', secondLevel: 'y', label: 'y:', type: 'number', labelWidth: '20px' }
                            ] }
                        ]
                    }
                }
            };
            return form;
        },
        makeForm() {
            return {
                labelWidth: '120px',
                items: {
                    draw: {
                        name: this.$t('map.drawData'),
                        item: [
                            { prop: 'width', label: '宽度:', type: 'number', min: 1, placeholder: 'px' },
                            { prop: 'height', label: '高度:', type: 'number', min: 1, placeholder: 'px' },
                            { prop: 'showConditions', label: this.$t('map.showConditions'), type: 'radio', optionLabel: 'label', optionValue:'value', radioList: this.showConditionsList},
                            { prop: 'position', label: '坐标:', type: 'coordinate', width: '110px', children: [
                                { prop: 'position.x', firstLevel: 'position', secondLevel: 'x', label: 'x:', type: 'number', labelWidth: '20px' },
                                { prop: 'position.y', firstLevel: 'position', secondLevel: 'y', label: 'y:', type: 'number', labelWidth: '20px' }
                            ] }
                        ]
                    }
                }
            };
        },
        isPointsShow() {
            return this.editModel.points.length > 0;
        }
    },
    watch: {
        // selected(val, oldVal) {
        //     this.deviceSelect(val);
        // }
    },
    mounted() {
        this.$Dictionary.lineType().then(list => {
            this.LineTypeList = list;
        });
    },
    methods: {
        deviceChange(code) {
            this.$emit('setCenter', code);
            this.deviceSelect(this.$store.getters['map/getDeviceByCode'](code));
        },
        deviceSelect(selected) {
            this.$refs.form && this.$refs.form.resetFields();
            this.$refs.make && this.$refs.make.resetFields();
            if (selected && selected._type.toUpperCase() === 'OutFrame'.toUpperCase()) {
                this.activeName = 'first';
                this.editModel = deepAssign(this.editModel, selected);
            }
        },
        addPoint(index) {
            const data = { x: 0, y: 0 };
            this.editModel.points.splice(index + 1, 0, data);
        },
        delPoint(index) {
            this.editModel.points.splice(index + 1, 0);
        },
        create() {
            this.$refs['make'].validate((valid) => {
                if (valid) {
                    const model = {
                        _type: 'OutFrame',
                        code: getUID('OutFrame', this.outerFrameList),
                        width: this.addModel.width,
                        height: this.addModel.height,
                        position: this.addModel.position
                    };
                    this.$emit('updateMapModel', model);
                }
            });
        },
        // 修改对象
        edit() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    const data = Object.assign({_type: 'OutFrame'}, this.editModel);
                    this.$emit('updateMapModel', data);
                }
            });
        },
        // 删除对象
        deleteObj() {
            const selected = this.$store.getters['map/getDeviceByCode'](this.editModel.code);
            if (selected && selected._type.toUpperCase() === 'OutFrame'.toUpperCase()) {
                this.$confirm(this.$t('tip.confirmDeletion'), this.$t('tip.hint'), {
                    confirmButtonText: this.$t('tip.confirm'),
                    cancelButtonText: this.$t('tip.cancel'),
                    type: 'warning'
                }).then(() => {
                    this.$emit('updateMapModel', {...selected, _dispose: true});
                    this.$refs.form && this.$refs.form.resetFields();
                }).catch(() => {
                    this.$message.info(this.$t('tip.cancelledDelete'));
                });
            }
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/mixin.scss";
	.view-control{
		height: 100%;
		overflow-y: auto;
	}
	.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;
        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>
