<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item :label="this.$t('ibp.alarmCode')" prop="code">
        <el-input v-model="form.code" :disabled="true">
          <el-button slot="append" :disabled="isUpdate" type="primary" @click="generateCode">{{ $t('ibp.generateCode') }}</el-button>
        </el-input>
      </el-form-item>
      <el-form-item :label="this.$t('ibp.alarmWidth')" prop="alarmWidth">
        <el-input-number v-model="form.alarmWidth" controls-position="right" :min="1" />
      </el-form-item>
      <el-form-item :label="this.$t('ibp.xCoordinate')">
        <el-input-number v-model="form.x" controls-position="right" :min="1" />
      </el-form-item>
      <el-form-item :label="this.$t('ibp.yCoordinate')">
        <el-input-number v-model="form.y" controls-position="right" :min="1" />
      </el-form-item>
		<el-form-item label="表示状态">
			<el-select v-model="form.mean" placeholder="请选择">
				<el-option
					v-for="item in showMeanList"
					:key="item.value"
					:label="item.label"
					:value="item.value"
				/>
			</el-select>
		</el-form-item>
      <el-form-item label="方向">
        <el-select v-model="form.direction" placeholder="请选择">
          <el-option
          v-for="(item, i) in directions"
          :key="i"
          :label="item.name"
          :value="item.value"
          ></el-option>
        </el-select>
      </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: 'ButtonDraft',
    components: {
    },
    data() {
        return {
            isUpdate: false,
            buttonText: this.$t('ibp.createNow'),
            showDeleteButton: false,
            form: {
                code: '',
                alarmWidth: '',
                x: 10,
                y: 10,
                direction: null,
				mean: ''
            },
            rules: {
                code: [
                    { required: true, message: this.$t('rules.enterTheAlarmCode'), trigger: 'blur' }
                ],
                alarmWidth: [
                    { required: true, message: this.$t('rules.enterTheAlarmWidth'), trigger: 'blur' }
                ]
            },
            directions: [
                { name: '全部', value: null},
                { name: '上行', value: true},
                { name: '下行', value: false},
            ],
			showMeanList: [
				{label: '信号-蜂鸣器', value: 'SIGNAL_ALARM'},
				{label: '屏蔽门-蜂鸣器', value: 'PSD_ALARM'}
			]
        };
    },
    computed: {

    },
    watch: {
        '$store.state.ibp.rightClickCount': function (val) {
            const model = this.$store.getters['ibp/updateDeviceData'];
            if (model._type === 'Alarm' ) {
                this.buttonText = this.$t('global.modify');
                this.showDeleteButton = true;
                this.isUpdate = true;
                this.form.code = model.code;
                this.form.alarmWidth = model.width;
                this.form.x = model.point.x;
                this.form.y = model.point.y;
                this.form.mean = model.mean;
                this.form.direction = model.direction
            }
        }
    },
    mounted() {
    },
    methods: {
        onSubmit(form) {
            this.$refs[form].validate((valid) => {
                if (valid) {
                    const alarmModel = {
                        point:{
                            x: this.form.x,
                            y: this.form.y
                        },
                        code: this.form.code,
						mean: this.form.mean,
                        _type: 'Alarm',
                        width: this.form.alarmWidth,
                        direction: this.form.direction
                    };
                    this.$emit('createData', alarmModel);
                    this.initPage();
                } else {
                    return false;
                }
            });
        },
        deleteDevice() {
            const alarmModel = {
                point:{
                    x: this.form.x,
                    y: this.form.y
                },
                code: this.form.code,
                _type: 'Alarm',
				mean: this.form.mean,
                width: this.form.alarmWidth,
                direction: this.form.direction
            };
            this.$emit('deleteDataModel', alarmModel );
            this.initPage();
        },
        initPage() {
            this.isUpdate = false;
            this.buttonText = this.$t('ibp.createNow');
            this.showDeleteButton = false;
            this.form = {
                code: '',
                alarmWidth: '',
                x: 10,
                y: 10,
				mean: '',
                direction: null
            };
        },
        generateCode() {
            const mydate = new Date();
            this.form.code = 'alarm_' + mydate.getDay() + mydate.getHours() + mydate.getMinutes() + mydate.getSeconds() + mydate.getMilliseconds() + Math.round(Math.random() * 10000);
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

</style>
