<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <div class="manage-container">
      <el-form
        ref="form"
        :model="form"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="选择站点">
          <el-select v-model="form.code" placeholder="请选择站点">
            <el-option
              v-for="item in deviceData"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <vab-query-form>
          <el-button type="primary" @click="save">
            {{ $t('defalult.tjtn') }}
          </el-button>
        </vab-query-form>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { ysBindDevice, ldDeviceByProjectId } from '@/api/fluorite';
export default {
  data() {
    return {
      form: {
        ysSerial: '',
        code: '',
        account: '',
      },
      deviceData: [],
      dialogFormVisible: false,
      title: '',
    };
  },
  methods: {
    bindDevice(row) {
      this.form.ysSerial = row.deviceSerial;
      this.form.account = row.account;
      this.dialogFormVisible = true;
      this.title = '绑定设备';
      this.getDeviceData();
    },
    async getDeviceData() {
      const { data } = await ldDeviceByProjectId({
        projectId: sessionStorage.getItem('projectId'),
      });
      this.deviceData = data;
    },
    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 ysBindDevice(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
