<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item width="100" prop="name" label="设备名称">
        <el-input
          v-model="form.name"
          size="small"
          placeholder="请输入涂鸦平台设备名称"
        ></el-input>
      </el-form-item>
      <el-form-item width="100" prop="serial" label="序列号">
        <el-input
          v-model="form.serial"
          size="small"
          placeholder="请输入运维平台站点序列号"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { register } from '@/api/tuya';
export default {
  name: 'BindDevice',
  data() {
    return {
      form: {
        name: '',
        serial: '',
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入设备名称' }],
        serial: [{ required: true, trigger: 'blur', message: '请输入序列号' }],
      },
      projectId: '',
      deviceData: [],
      title: '',
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    handleCountryChange(value) {
      if (value) {
        const len = value.length - 1;
        this.form.parentId = value[len];
      } else {
        this.form.parentId = 0;
      }
    },

    showDetail(accNumber) {
      this.title = '注册设备';
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          const { msg } = await register(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.select {
  width: 100%;
}
</style>
