// 添加/修改配置
<template>
  <el-dialog
    :title="isEdit ? '编辑设备配置' : '添加设备配置'"
    width="800px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="100px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备地点" prop="address">
            <el-input v-model="form.address" placeholder="请输入设备地点" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备sn" prop="sn">
            <el-input v-model="form.sn" placeholder="请输入设备sn" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标题" prop="handleProveId">
            <el-select
              v-model="form.handleProveId"
              class="input"
              placeholder="请选择标题"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) in option"
                :key="index"
                :label="item.handleProveTitle"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公章_透明底" prop="zhang">
            <el-upload
              class="avatar-uploader"
              action="https://tx.hz-hanghui.com:8088/yx-fyzd/upload/pictureLocal"
              accept="image/*"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.zhang" :src="form.zhang" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setTitleList } from "@/api/devicelib";
import { getSelectData } from "@/api/titlelib";
export default {
  data() {
    return {
      form: {
        address: "",
        sn: "",
        handleProveId: "",
        zhang: "",
      },
      option: [],
      rules: {
        address: [{ required: true, message: "不能为空!", trigger: "blur" }],
        sn: [{ required: true, message: "不能为空!", trigger: "blur" }],
        handleProveId: [
          { required: true, message: "不能为空!", trigger: "blur" },
        ],
      },
      visible: false,
      submitLoading: false,
      isEdit: false,
      imageUrl: "",
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.getSelect();
    },
    async getSelect() {
      const res = await getSelectData();
      // console.log(res.data);
      this.option = res.data;
    },

    open(obj) {
      this.visible = true;
      this.submitLoading = false;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        if (obj && Object.keys(obj).length > 0) {
          this.form = JSON.parse(JSON.stringify(obj));
          this.isEdit = true;
        } else {
          this.isEdit = false;
        }
      });
    },
    submit(e) {
      e.preventDefault();
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        if (this.submitLoading) {
          return;
        }
        this.submitLoading = true;
        const res = {
          address: this.form.address,
          sn: this.form.sn,
          handleProveId: this.form.handleProveId,
          zhang: this.form.zhang,
          id: this.isEdit ? this.form.id : undefined,
        };
        await setTitleList(res);
        console.log(res);
        this.visible = false;
        this.$emit("success");
        this.submitLoading = false;
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(res.data, file);
      this.form.zhang = res.data;
      this.form.zhang = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isLt2M;
    },
  },
};
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
