<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="Visible"
      width="50%"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="设备名称" prop="deviceName">
              <el-input
                v-model="form.deviceName"
                placeholder="请输入设备名称"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="设备类型">
              <el-input disabled value="数字样表"></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="mac地址" prop="deviceMac">
              <el-input
                v-model="form.deviceMac"
                placeholder="请输入mac地址"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="设备厂商" prop="deviceFirmId">
              <el-select
                v-model="form.deviceFirmId"
                placeholder="请选择设备厂商"
              >
                <el-option
                  v-for="(v, key) in dict.deviceFirmId"
                  :key="key"
                  :label="v"
                  :value="Number(key)"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="分辨率" prop="name">
              <el-select v-model="form.value" placeholder="请选择分辨率">
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.value"
                  :value="item.id"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="设备IP" prop="ip">
              <el-input
                v-model="form.ip"
                placeholder="请输入设备IP"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="端口号" prop="port">
              <el-input
                v-model="form.port"
                placeholder="请输入端口号"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="负责人" prop="leadingOfficial">
              <el-input
                v-model="form.leadingOfficial"
                placeholder="请输入责任人姓名"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="手机号" prop="leadingOfficialTelephone">
              <el-input
                v-model="form.leadingOfficialTelephone"
                placeholder="请输入责任人手机号"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="设备经度" prop="lon">
              <el-input
                v-model="form.lon"
                placeholder="请输入设备经度"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="设备纬度" prop="lati">
              <el-input
                v-model="form.lati"
                placeholder="请输入设备纬度"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="12"
            ><el-form-item label="所在楼栋" prop="deviceInBuilding">
              <el-input-number
                v-model="form.deviceInBuilding"
                controls-position="right"
                :min="1"
                :max="100"
                placeholder="请输入楼栋"
              ></el-input-number> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12"
            ><el-form-item label="所在楼层" prop="deviceInFloor">
              <el-input-number
                v-model="form.deviceInFloor"
                controls-position="right"
                :min="1"
                :max="100"
                placeholder="请输入楼层"
              ></el-input-number> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="所属机构" prop="institution">
              <el-input
                v-model="form.institution"
                placeholder="请输入所属机构"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24"
            ><el-form-item label="备注" prop="deviceRemark">
              <el-input
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
                clearable
                placeholder="请输入备注"
                v-model="form.deviceRemark"
                resize="none"
              >
              </el-input> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="handleOk"
          >确 定</el-button
        >
        <el-button size="small" @click="handleReset">重 置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveDevice } from "@/api/device";
export default {
  props: {
    title: {
      type: String,
      required: true,
      default: "",
    },
    dialogVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
    dict: {
      type: Object,
      required: true,
      default: () => {},
    },
  },
  data() {
    return {
      form: {
        deviceId: "", // 设备id
        deviceName: "", // 设备名称
        deviceCode: "", // 设备编码
        deviceMac: "", // 设备的MAC地址
        siteId: "", // 站点Id
        siteCode: "", // 站点编号
        siteName: "", // 站点名称
        productCode: "", // 产品编码
        productName: "", // 产品名称
        deviceSrc: "", // 设备来源(0.子设备，1.网关设备，2.直连设备)
        lon: "", // 经度
        lati: "", // 纬度
        leadingOfficial: "", // 负责人
        leadingOfficialTelephone: "", // 联系电话
        // deviceStatus: "", // 设备状态 (0.未激活，1.离线,2.在线)
        deviceRemark: "", // 备注
        source: "", // 设备来源(0.旧设备，1.新设备)
        deviceFirmId: "", // 设备生产厂商ID
        deviceFirmname: "", // 设备生产厂商名称
        ip: "", // 设备访问ip
        port: "", // 端口
        // enabled: "", // 启用状态 (0.停止，1.启用)
        deviceInBuilding: "", // 所在楼宇
        deviceInFloor: "", // 所在楼层
        institution: "",
      },
      rules: {
        deviceName: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        deviceMac: [
          { required: true, message: "请输入设备mac地址", trigger: "blur" },
        ],
        leadingOfficial: [
          { required: true, message: "请输入负责人姓名", trigger: "blur" },
        ],
        leadingOfficialTelephone: [
          { required: true, message: "请输入负责人手机号码", trigger: "blur" },
        ],
      },
      options: [
        {
          id: 1,
          value: "1920*1080",
        },
        {
          id: 2,
          value: "1080*1920",
        },
      ],
    };
  },
  computed: {
    Visible: {
      get() {
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", val);
      },
    },
  },
  methods: {
    handleReset() {
      this.$refs.form.resetFields();
    },
    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await saveDevice(this.form);
          let { code, msg } = res.data;
          if (code == 1) {
            this.$message.success(msg);
            this.$emit("addSuccess");
            this.handleClose();
          }
        }
      });
    },
    // 新增
    onAdd() {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
    },
    // 编辑
    onEdit(row) {
      this.form = { ...row };
    },
    handleClose() {
      this.Visible = false;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input-number {
  width: 100%;
  input {
    text-align: left !important;
  }
}
/deep/.el-select {
  width: 100%;
}
</style>