<template>
  <div>
    <el-form ref="form" :rule="rules" :model="addModel" label-width="100px">
      <el-form-item v-if="addModel.code" label="按钮编号" prop="code">
        <el-input v-model="addModel.code" />
      </el-form-item>
      <el-form-item label="图形宽度" prop="width">
        <el-input-number v-model="addModel.width" />
      </el-form-item>
      <el-form-item label="X轴坐标" prop="x">
        <el-input-number v-model="addModel.x" />
      </el-form-item>
      <el-form-item label="Y轴坐标" prop="y">
        <el-input-number v-model="addModel.y" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">{{ buttonText }}</el-button>
        <el-button v-show="showDeleteButton" type="danger" @click="deleteDevice">{{ $t('global.delete') }}</el-button>
        <el-button v-show="showDeleteButton" @click="initPage">{{ $t('global.cancel') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
    name: 'BrakeMachine',
    data() {
        return {
            addModel:{
                code: '',
                width: 25,
                x: 10,
                y: 10
            },
            rules: {
                width:[{ required: true, message:'请输入设备图形宽度', trigger: 'blur' }],
                x: [{ required: true, message: '请输入设备图形的X轴坐标', trigger: 'blur' }],
                y: [{ required: true, message: '请输入设备图形的Y轴坐标', trigger: 'blur' }]
            },
            showDeleteButton: false,
            buttonText: '立即创建'
        };
    },
    methods: {
        onSubmit(form) {
            if (!this.addModel.code) {
                this.generateCode();
            }
            this.$refs[form].validate((valid) => {
                if (valid) {
                    const maButtonModel = {
                        point: {
                            x: this.addModel.x,
                            y: this.addModel.y
                        },
                        code: this.addModel.code,
                        _type: 'brakeMachine',
                        width: this.addModel.width
                    };
                    this.$emit('createDataModel', maButtonModel);
                    this.initPage();
                } else {
                    return false;
                }
            });
        },
        deleteDevice() {
            const maButtonModel = {
                point: {
                    x: this.addModel.x,
                    y: this.addModel.y
                },
                code: this.addModel.code,
                _type: 'brakeMachine',
                width: this.addModel.width
            };
            this.$emit('deleteDataModel', maButtonModel);
            this.initPage();
        },
        initPage() {
            this.isUpdate = false;
            this.buttonText = '立即创建';
            this.showDeleteButton = false;
            this.addModel = {
                code: '',
                width: 25,
                x: 10,
                y: 10
            };
        },
        generateCode() {
            const mydate = new Date();
            this.addModel.code = 'brakeMachine_' + mydate.getDay() + mydate.getHours() + mydate.getMinutes() + mydate.getSeconds() + mydate.getMilliseconds() + Math.round(Math.random() * 10000);
        }
    }

};
</script>

<style scoped>

</style>
