<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="850px" @close="handleClose" id="messageDialog">
    <el-row>
      <formItems :type="'floor'" ref="formItems"></formItems>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible=false">取消</el-button>
      <el-button size="small" type="primary" @click="confirm">确认</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { manageCreateFloor, manageUpdateFloor } from "@/api/object/floor";
import formItems from "@/components/ready/buildfloor/formItems";
import tools from "@/utils/buildfloor/tools";

export default {
  props: {
    title: {
      default: '提示'
    },
    type: {
      default: 'Floor'
    },
    curBuildId: {
      default: ''
    },
    curFloorId: {
      default: ''
    }
  },
  components: {
    formItems
  },
  data() {
    return {
      dialogVisible: false, //弹窗显示与隐藏
      floorData: {},
    };
  },
  methods: {
    showDialog(data) {
      this.floorData = data || {};
      this.floorData = tools.desFormatData(this.floorData)
      this.floorData.outline = this.floorData.outline ? JSON.stringify(this.floorData.outline) : undefined;
      //===测试代码===
      // this.floorData.DefaultQRCode = 'Eq1101080004a93a45ce15e948dda155e9683803cf46QR'
      // this.floorData.QRCodePic = 'Eq1101080004a93a45ce15e948dda155e9683803cf46QR'
      //=====
      this.timeoutSetVal()
      this.dialogVisible = true;
    },
    timeoutSetVal() {
      setTimeout(() => {
        if (this.$refs.formItems) {
          this.$refs.formItems.form = this.floorData
        } else {
          this.timeoutSetVal()
        }
      }, 500)
    },
    handleClose() {
    },
    //创建
    confirm() {
      this.$refs.formItems.submitForm(this.save)
    },
    save() {
      let form = this.$refs.formItems.form
      if (form.outline) form.outline = JSON.parse(form.outline);
      let newform = tools.formatData(form)
      newform.buildingId = this.curBuildId
      let Param = {
        content: [newform]
      }
      if (newform.id) {
        manageUpdateFloor(Param, res => {
          this.$message.success('更新成功')
          this.$emit('refresh')
          this.dialogVisible = false;
        })
      } else {
        manageCreateFloor(Param, res => {
          this.$message.success('创建成功')
          this.$emit('refresh')
          this.dialogVisible = false;
        })
      }
    }
  },
  mounted() {
  },
  created() {
  }
};
</script>
<style lang="scss" scoped>
#messageDialog {
  /deep/ .el-dialog__body {
    height: 420px;
    overflow: auto;
  }

  .el-form-item {
    /deep/ label.el-form-item__label {
      font-size: 12px;
    }
  }

  /deep/ .FloorTypeSelect .el-form-item__content {
    width: 200px;
  }
}
</style>
