<template>
  <el-dialog
    v-if="dialogVisible"
    :title="title"
    :visible.sync="dialogVisible"
    :width="dialogWidth"
    :style="{ '--dialogHeight': dialogHeight }"
    custom-class="template-iframe-dialog"
    top="5vh"
  >
    <iframe
      id="template-iframe-dialog_iframe"
      width="100%"
      class="template-iframe-dialog_iframe"
      frameborder="0"
      hspace="0"
      :src="url"
    ></iframe>
  </el-dialog>
</template>

<script>
  export default {
    name: 'HtTemplateIframeDialog',
    data() {
      return {
        dialogVisible: false,
        dialogWidth: '70%',
        dialogHeight: '740px',
        url: '',
        title: '',
      }
    },
    created() {
      window.addEventListener('message', (e) => {
        if (e.data.type == 'closeDialog') {
          this.dialogVisible = false
        }
      })
    },
    beforeDestroy() {
      window.removeEventListener('message', (e) => {
        if (e.data.type == 'closeDialog') {
          this.dialogVisible = false
        }
      })
    },
    methods: {
      showDialog(param) {
        if (param.title && param.title != '') {
          this.title = param.title
        }
        this.url = param.url + '&isDialog=true'

        this.dialogWidth =
          param.dialogWidth &&
          (String(param.dialogWidth).includes('px') ||
            String(param.dialogWidth).includes('%'))
            ? param.dialogWidth
            : param.dialogWidth
            ? param.dialogWidth + 'px'
            : '70%'
        this.dialogHeight =
          param.dialogHeight &&
          (String(param.dialogHeight).includes('px') ||
            String(param.dialogHeight).includes('%'))
            ? param.dialogHeight
            : param.dialogHeight
            ? param.dialogHeight + 'px'
            : '740px'
        this.dialogVisible = true
      },
    },
  }
</script>

<style lang="scss" scoped>
  ::v-deep {
    .template-iframe-dialog {
      height: var(--dialogHeight);
      box-sizing: border-box;
      .el-dialog__body {
        height: calc(100% - 84px);
        overflow: auto;
        box-sizing: border-box;
        .el-button {
          margin-right: 10px;
        }
        .template-iframe-dialog_iframe {
          height: calc(100% - 4px);
        }
      }
    }
  }
</style>
