<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="设备编号:" required>
        <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="关联煎药区" prop="decoctionAreaCode">
        <el-select v-model="formInline.decoctionAreaCode" placeholder="请选择备用煎药区" filterable>
          <el-option v-for="item in stationOptions" :key="item.id"
                     :label="item.areaName" :value="item.areaCode" :disabled="item.state !== 0"
          />
        </el-select>
      </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";
import { getDecoctStationOptions } from '@/api/decoct';
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    id: [Number, String]
  },
  data() {
    return {
      formInline: {
        decoctionAreaCode: void 0,
        deviceName: '',
        deviceType: '',
        deviceCode: '',
        groupCode: '',
        deviceIp: '',
        status: 0,
      },
      loading: false,
      // 煎药区
      stationOptions: [],
      typeOptions: []
    }
  },
  mounted() {
    this.getDicts("device_type").then((response) => {
      this.typeOptions = response.data;
    });
    getDecoctStationOptions().then((response) => {this.stationOptions = 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;
      this.formInline.decoctionAreaName = this.formInline.decoctionAreaCode == null ? '' : (this.stationOptions.find(option => option.areaCode === this.formInline.decoctionAreaCode) || {}).areaName || '';
      updateDevice(this.formInline).then((response) => {
        this.msgSuccess('修改成功')
        this.$emit('success')
      }).finally(res => {
        this.loading = false;
      })
    },
    // 新增
    toAdd() {
      this.loading = true;
      this.formInline.decoctionAreaName = this.formInline.decoctionAreaCode == null ? '' : (this.stationOptions.find(option => option.areaCode === this.formInline.decoctionAreaCode) || {}).areaName || '';
      addDevice(this.formInline).then((response) => {
        this.msgSuccess('新增成功')
        this.$emit('success')
      }).finally(res => {
        this.loading = false;
      })
    }
  },
}
</script>

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