<template>
  <div class="edit-dialog" v-loading="loading">
    <el-form :inline="true" :model="formInline" label-width="100px" :disabled="disabled" class="edit-dialog-formInline">
      <el-form-item label="设备名称:" required>
        <el-input v-model="formInline.deviceName" placeholder="请输入设备名称"></el-input>
      </el-form-item>
      <el-form-item label="设备类型:" required>
        <el-select v-model="formInline.deviceType" placeholder="请选择">
          <el-option :label="item.dictLabel" :value="item.dictValue" v-for="item in typeOptions" :key="item.dictCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号:">
        <el-input v-model="formInline.deviceCode" placeholder="设备编号"></el-input>
      </el-form-item>
      <el-form-item label="机组编号:">
        <el-input v-model="formInline.groupCode" placeholder="机组编号"></el-input>
      </el-form-item>
      <el-form-item label="设备地址:">
        <el-input v-model="formInline.deviceIp" placeholder="设备地址"></el-input>
      </el-form-item>
      <el-form-item label="启用状态:" required>
        <el-radio-group v-model="formInline.status">
          <el-radio :label="0">开启</el-radio>
          <el-radio :label="1">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getDeviceDetail, addDevice, updateDevice } from "@/api/decoct/device";
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    id: [Number, String]
  },
  data() {
    return {
      formInline: {
        deviceName: '',
        deviceType: '',
        deviceCode: '',
        groupCode: '',
        deviceIp: '',
        status: 0,
      },
      loading: false,
      typeOptions: []
    }
  },
  mounted() {
    this.getDicts("device_type").then((response) => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    openDialog() {
      this.formInline = {
        deviceName: '',
        deviceType: '',
        deviceCode: '',
        groupCode: '',
        deviceIp: '',
        status: 0,
      }

    },
    // 详情
    getDetail() {
      this.loading = true;
      getDeviceDetail(this.id).then((response) => {
        this.formInline = response.data
        this.loading = false;
      });
    },
    // 修改
    toUpdate() {
      this.loading = true;
      updateDevice(this.formInline).then((response) => {
        this.msgSuccess('修改成功')
        this.$emit('success')
      }).finally(res => {
        this.loading = false;
      })
    },
    // 新增
    toAdd() {
      this.loading = true;
      addDevice(this.formInline).then((response) => {
        this.msgSuccess('新增成功')
        this.$emit('success')
      }).finally(res => {
        this.loading = false;
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.edit-dialog {
  height: 400px;
  overflow: auto;
  &-formInline{
    width: 330px;
    margin: auto;
  }
  ::v-deep .el-form-item{
    display: flex;
    &__content{
      flex: 1;
    }
    .el-select{
      width: 100%;
    }
  }
}
</style>
