<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item :label="$t('maintenance.sb')">
        <el-select
          v-model="form.deviceCode"
          filterable
          placeholder="请选择设备"
        >
          <el-option
            v-for="item in deviceData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          ></el-option>
        </el-select>
      </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 { simBindDevice, ldDeviceByProjectId } from '@/api/flow';
export default {
  name: 'BindDevice',
  data() {
    return {
      form: {
        accessNum: '',
        deviceCode: '',
      },
      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;
      }
    },
    async getDevice() {
      const { data } = await ldDeviceByProjectId({ projectId: this.projectId });
      this.deviceData = data;
    },
    showDetail(accNumber) {
      this.title = '绑定设备';
      this.form.accessNum = accNumber;
      this.projectId = sessionStorage.getItem('projectId');
      this.dialogFormVisible = true;
      this.getDevice();
    },
    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 simBindDevice(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>
