<!--
 * @Author: liangjy liangjy@jee-soft.cn
 * @Date: 2024-05-30 09:00:51
 * @LastEditors: liangjy liangjy@jee-soft.cn
 * @LastEditTime: 2024-07-03 14:51:01
 * @FilePath: \hotent-ui\packages\TemplateIframeDrawer\src\main.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-drawer
    v-if="dialogVisible"
    :title="title"
    :visible.sync="dialogVisible"
    :size="dialogWidth"
    custom-class="template-iframe-drawer ht-template-form-drawer"
    top="5vh"
    :append-to-body="appendToBody"
  >
    <iframe
      id="template-iframe-drawer_iframe"
      width="100%"
      height="99%"
      class="template-iframe-drawer_iframe"
      frameborder="0"
      hspace="0"
      :src="url"
    ></iframe>
  </el-drawer>
</template>

<script>
  export default {
    name: 'HtTemplateIframeDrawer',
    props: {
      appendToBody: {
        type: Boolean,
        default: true,
      },
    },
    data() {
      return {
        dialogVisible: false,
        dialogWidth: '50%',
        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.dialogVisible = true
      },
    },
  }
</script>

<style lang="scss" scoped>
  ::v-deep {
    .template-iframe-drawer {
      border-radius: 16px 0 0 16px;
      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-drawer_iframe {
          height: calc(100% - 4px);
        }
      }
    }
  }
</style>
