
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align:left;"
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="companyId">
              <el-select
                v-model="formModel.companyId"
                filterable
                placeholder="请选择"
                style="width:90%"
              >
                <el-option
                  v-for="company in companyResult"
                  :key="company.id"
                  :label="company.name"
                  :value="company.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="ip地址" prop="ipAddress">
              <el-input
                v-model="formModel.ipAddress"
                @blur="queryDeviceNo"
                placeholder="请输入ip地址"
                style="width:80%"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口" prop="port">
              <el-input
                v-model="formModel.port"
                @blur="queryDeviceNo"
                placeholder="请输入端口"
                style="width:80%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="deviceNo">
              <el-input v-model="formModel.deviceNo" placeholder="请输入设备编号" style="width:80%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备别名" prop="aliasName">
              <el-input v-model="formModel.aliasName" placeholder="请输入设备别名" style="width:80%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="formModel.type" filterable placeholder="请选择" style="width:90%">
                <el-option
                  v-for="result in typeData"
                  :key="result.id"
                  :label="result.name"
                  :value="result.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开锁密码" prop="unlockPassword">
              <el-input
                v-model="formModel.unlockPassword"
                placeholder="请输入开锁密码"
                style="width:80%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="二维码地址" prop="codeAddress">
              <el-input v-model="formModel.codeAddress" placeholder="请输入二维码地址" style="width:80%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import deviceInfoApi from "@/api/base/deviceInfo";

export default {
  props: ["businessKey", "title", "typeData", "companyResult"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        aliasName: [
          { required: true, message: "设备别名不能为空", trigger: "blur" }
        ],
        isOnline: [
          {
            required: true,
            message: "运行状态不能为空",
            trigger: "blur"
          }
        ],
        // unlockPassword: [
        //   { required: true, message: "开锁密码不能为空", trigger: "blur" }
        // ],
        // codeAddress: [
        //   { required: true, message: "二维码地址不能为空", trigger: "blur" }
        // ],
        ipAddress: [
          { required: true, message: "ip地址不能为空", trigger: "blur" }
        ],
        port: [{ required: true, message: "端口不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }]
      },
      showDialog: true,
      loading: false,
      submitting: false,
      canQuery: true
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate(valid => {
        if (valid) {
          (function() {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return deviceInfoApi.add(self.formModel);
            } else {
              return deviceInfoApi.update(self.formModel);
            }
          })().then(function(response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success"
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning"
              });

              //self.$emit("close", false);
            }
          });
        }
      });
    },
    queryDeviceNo() {
      var self = this;
      if (self.canQuery) {
        self.canQuery = false;
        var formData = new FormData();
        formData.append("ipAddress", self.formModel.ipAddress);
        formData.append("port", self.formModel.port);
        deviceInfoApi.getByIpAddressAndPort(formData).then(function(response) {
          var jsonData = response.data;
          setTimeout(() => {
            self.canQuery = true;
            //延迟1秒
          }, 1000);
          if (jsonData.result) {
            if (jsonData.data != null) {
              let deviceNo = jsonData.data.deviceNo;
              self.formModel.deviceNo = deviceNo;
            }
          }
        });
      }
    }
  },
  mounted: function() {
    var self = this;
    (function() {
      if (self.businessKey.length == 0) {
        return deviceInfoApi.create();
      } else {
        return deviceInfoApi.edit(self.businessKey);
      }
    })()
      .then(response => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch(error => {
        self.$message.error(error + "");
      });
  }
};
</script>