<template>
  <div class="form-container" id="flow-form-container">
    <div class="preview-header">
      <ht-h3 inline>表单预览</ht-h3>
      <div class="preview-header__left">
        <ht-switch
          v-model="validate"
          active-text="校验表单"
          inactive-text="不校验表单"
        />
        <el-button type="primary" @click="handleShowData">表单数据</el-button>
        <el-button
          plain
          class="scan-code__btn"
          v-if="isMobile"
          @click="handleScanPreview"
          >扫码预览</el-button
        >
      </div>
      <div class="qrCode-wrap" v-show="isShowCode">
        <div class="code-header">
          <span class="code-title">手机扫码预览</span>
          <i class="el-icon-close close-icon" @click="handleClose"></i>
        </div>
        <div class="mobile-qrCode-container">
          <div class="qr-image__wrap">
            <vue-qr
              :correct-level="3"
              :auto-color="false"
              color-dark="#000000"
              background-color="#ffffff"
              :text="codeUrl"
              :size="238"
              :margin="16"
              :logo-margin="10"
              :logo-corner-radius="0"
              :logo-scale="0.12"
              class="qr-code"
            ></vue-qr>
          </div>
          <span class="code-tip">请扫码在手机查看表单详情</span>
        </div>
      </div>
    </div>
    <ht-divider />
    <div v-if="isMobile" class="mobile-form__container">
      <ht-online-form
        ref="flowOnlineForm"
        :html="innerHtml"
        :data="data"
        :permission="permission"
        :mobile-mode="isMobile"
        :extend-prop="extendProp"
        :def-id="defId"
        isPreview
      />
    </div>
    <div class="preview-form-container" v-else ref="previewForm">
      <ht-online-form
        ref="flowOnlineForm"
        :html="innerHtml"
        :data="data"
        :permission="permission"
        :extend-prop="extendProp"
        :def-id="defId"
      />
    </div>
    <el-dialog
      class="data-show__dialog"
      :visible.sync="dataShow"
      title="表单数据"
    >
      <codemirror v-model="dataStr" :options="dataStyle"></codemirror>
      <el-footer>
        <el-button size="small" @click="dataShow = false">关闭</el-button>
        <el-button type="primary" size="small" @click="handleCopyData"
          >复制</el-button
        >
      </el-footer>
    </el-dialog>
  </div>
</template>
<script>
import form from '@/api/form.js'
import req from '@/utils/req'
import VueQr from 'vue-qr'
import {mapState} from 'vuex'
export default {
  name: 'FormPreview',
  components: {
    VueQr
  },
  props: {
    formId: String
  },
  data() {
    return {
      innerHtml: null,
      data: null,
      permission: null,
      validate: false,
      dataShow: false,
      dataStyle: {
        value: '',
        mode: 'javascript',
        readOnly: true,
        smartIndent: true,
        tabSize: 2,
        theme: 'base16-light',
        lineNumbers: true,
        line: true
      },
      flag: 0,
      extendProp: {
        initFillData: true // 子表默认预览时初始化
      },
      formCss: '',
      defId: '',
      isShowCode: false
    }
  },
  computed: {
    ...mapState('login', ['currentUser']),
    codeUrl() {
      return `${window.context.mobile}/formDetail/${this.formId}?token=${this.currentUser.token}`
    },
    dataStr: {
      get() {
        return JSON.stringify(this.data, null, '\t')
      },
      set(newVal) {
        this.data = typeof newVal === 'string' ? JSON.parse(newVal) : newVal
        return JSON.stringify(this.data, null, '\t')
      }
    },
    isMobile() {
      return (
        this.$route.query.mobile === true || this.$route.query.mobile === 'true'
      )
    }
  },
  mounted() {
    this.$http = req
    this.getFormById()
    this.$nextTick(() => {
      this.$refs.previewForm &&
        this.$refs.previewForm.addEventListener(
          'scroll',
          this.setScrollTop,
          true
        )
    })
  },
  methods: {
    handleScanPreview() {
      this.isShowCode = true
    },
    handleClose() {
      this.isShowCode = false
    },
    setScrollTop() {
      if (this.$refs.previewForm.scrollTop && this.flag < 1) {
        this.$refs.previewForm.scrollTop = 0
      }
      this.flag += 1
    },
    getFormById() {
      if (!this.formId) {
        return
      }
      form[
        this.formId.indexOf('_') > -1 ? 'previewDesignTemp' : 'previewDesignVue'
      ](this.formId).then(resp => {
        const {bpmForm, data, permission} = resp.data
        this.innerHtml = bpmForm.formHtml
        this.formCss = bpmForm.formCss
        this.data = data
        this.permission = permission
        this.defId = bpmForm.defId
        bpmForm.diyJs && this.handlerJs(bpmForm.diyJs)
        this.$nextTick(() => {
          if (this.formCss) {
            let style = document.createElement('style')
            style.id = 'online-form-addStyle'
            style.type = 'text/css'
            style.innerHTML = `div[name='online-form'] ${this.formCss}`
            document.head.appendChild(style)
          }
        })
      })
    },
    handlerJs(diyJs) {
      this.$nextTick(() => {
        const currentUser = this.$store.state.login.currentUser
        let data = this.data
        let _this = this
        const $ = require('jquery')
        eval(diyJs)
      })
    },
    handleShowData() {
      this.$refs.flowOnlineForm.getData(this.validate).then(resp => {
        resp && (this.dataShow = true)
      })
    },
    handleCopyData() {
      this.$copyText(this.dataStr).then(() => {
        this.$message.success('复制成功')
        this.dataShow = false
      })
    }
  },
  beforeDestroy() {
    document
      .getElementsByTagName('head')
      .item(0)
      .removeChild(document.getElementById('online-form-addStyle'))
  }
}
</script>
<style lang="scss" scoped>
//表单中子表孙表顶部信息背景颜色
$base-form-table-header-bg-color: #eef2fb;
//表单中子表孙表标题颜色
$base-form-table-header-title-color: #333;
.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  h3 {
    margin: 0;
  }
  .preview-header__left {
    display: flex;
    align-items: center;
    .scan-code__btn {
      border-color: #409eff;
      color: #409eff;
    }
  }
  .qrCode-wrap {
    position: absolute;
    top: 40px;
    right: 0;
    width: 460px;
    height: 380px;
    background: #ffffff;
    box-shadow: 0px 0px 8px 1px rgba(97, 97, 97, 0.16);
    border-radius: 8px 8px;
    z-index: 9;
    .code-header {
      height: 56px;
      padding: 0 24px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .code-title {
        color: #262626;
        font-size: 16px;
        font-weight: 500;
      }
      .close-icon {
        font-size: 16px;
        color: #8c8c8c;
        cursor: pointer;
      }
    }
    .mobile-qrCode-container {
      display: flex;
      flex-direction: column;
      justify-content: center;
      text-align: center;
      padding-top: 25px;
      .code-tip {
        color: #262626;
        font-size: 14px;
      }
      .qr-image__wrap {
        padding-bottom: 17px;
      }
      .qr-code {
        width: 238px;
        height: 238px;
        border: 1px solid #f5f5f5;
      }
    }
  }
}
.form-container {
  background: #fff;
  padding: 20px;
  height: calc(100% - 40px);
  overflow: auto;

  .mobile-form__container {
    background: url('../../assets/img/mobile-android.png') no-repeat center
      center;
    background-size: calc(100% + 36px) calc(100% + 60px);
    width: 360px;
    height: 720px;
    padding: 16px;
    overflow: auto;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -180px;
    margin-top: -400px;
    border: 20px solid transparent;
    border-width: 30px 20px 36px 20px;

    scrollbar-width: 0;
    -ms-overflow-style: none;
    &::-webkit-scrollbar {
      width: 0;
    }
    ::v-deep {
      .sub-div-mobile {
        .formT_box {
          .el-collapse-item__content {
            .form-table {
              .el-row {
                // width: calc(100% - 120px);
              }
            }
          }
        }
      }
      .sub-div-title-mobile {
        overflow: hidden;
        width: 200px;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .sun-record-title-mobile {
        span {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          width: 100px;
          display: block;
        }
      }
      .mobile-ht-form-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0;
        border-bottom: 1px solid #ebeef5;

        .el-form-item__label {
          width: 120px;
          position: unset !important;
          transform: unset !important;
          display: flex;
          align-items: center;
        }
        .el-form-item__content {
          flex: 1;
          margin-left: 0 !important;
          .mobile-number-location {
            align-items: center;
          }

          .ht-mobile-relevant__flow {
            .header-btn {
              top: 0px;
            }
          }
          .mobile-field-tail__wrap .el-form-item__error {
            bottom: -3px;
          }
        }
        .mobile-upload {
          top: -84px;
        }
        .has-value-input {
          .mobile-upload {
            top: -60px;
          }
        }
        .mobile-number-wrap {
          .mobile-number-input {
            display: flex;
            align-items: center;
          }
        }
        .ht-mobile-signature-wrap .btn_wrap {
          top: -48px;
        }
      }
      .ht-form-item-top-label {
        flex-direction: column;
      }
      .mobile-map-form-item,
      .mobile-relevant-flow__form-item,
      .mobile-qrcode-form-item,
      .mobile-opinion-form-item,
      .mobile-textarea-form-item {
        display: block;
      }
      .mobile-map-form-item,
      .mobile-relevant-flow__form-item {
        .el-form-item__content {
          margin-left: 120px !important;
        }
      }
      .mobile-step {
        .el-step__head .el-step__line {
          top: 7px;
        }
      }
    }
  }
  ::v-deep {
    .el-row--flex.is-align-top {
      align-items: unset;
    }
    .clear-all-btn,
    .copy-btn,
    .delete-btn,
    .custom-dialog-btn,
    .add-btn {
      font-size: 14px;
      i {
        font-size: 16px;
      }
    }
    .custom-dialog-btn {
      padding: 0 10px;
    }
   
    .form-table__container {
      .sun-table__title {
        height: 40px !important;
        line-height: 40px !important;
        background: $base-form-table-header-bg-color !important;
        font-size: 16px;
        margin: 0 12px;
        padding: 0 16px;
        border: 1px solid #ebebeb;
        color: $base-form-table-header-title-color;
        font-weight: bold;
        text-align: left;
      }
      .sun-table_main {
        padding: 0 12px;
      }
      .form-table-header {
        height: 40px;
        line-height: 40px;
        // background: #f9f9f9;
        font-size: 16px;
        // margin: 0 12px;
        // padding: 0 16px;

        // border: 1px solid #dcdee0;
        .sub-table__title {
          color: $base-form-table-header-title-color;
          font-weight: bold;
        }
      }
      .el-table__footer-wrapper tbody td.el-table__cell{
        background: #f9f9f9;
      }

      .el-table {
        &::before {
          height: 0;
        }
        .el-table__fixed,
        .el-table__fixed-right {
          &::before {
            height: 0;
          }
        }
        .el-table__header-wrapper,
        .el-table__fixed-header-wrapper {
          th {
            background: var(--headerBgColor);
            padding: 12px 0;
            .cell {
              color: #333;
              font-size: 14px;
              font-weight: bold;
            }
          }
        }
        .el-table__empty-block {
          display: none;
        }
      }
      .table-footer {
        height: 56px;
        line-height: 56px;
        border: 1px solid #ebebeb;
        border-top: none;
        padding: 0 20px;
        .add-btn {
          font-weight: 500;
        }
      }
      .pagination-wrap {
        display: flex;
        justify-content: flex-end;
        padding: 16px 36px;
      }
      // 孙表表格样式跟子表统一
      .form-table thead .sub-table-header th {
        background: #f4f4f4;
        padding: 12px 0;
        color: #333;
        font-size: 14px;
        font-weight: bold;
        .table-header__cell,
        .table-header__cell span {
          color: #333;
          font-size: 14px;
          font-weight: bold;
        }
      }
      .sun-div-container {
        .el-header {
          text-align: left;
        }
        .sun-collapse-item-title {
          display: flex;
          padding-right: 16px;
          width: 100%;
          justify-content: space-between;
        }
      }
      .field-tail__wrap {
        .el-form-item__error {
          position: static;
        }
      }
    }
    //子表附件位置
    .el-table {
      .file-list__wrap .file-item {
        width: 100%;
        .file__name {
          text-align: left;
        }
      }
    }
    .el-row {
      .el-col {
        &:last-child {
          margin-right: 0 !important;
        }
      }
    }
  }
  ::v-deep.w-e-toolbar {
    z-index: 2 !important;
  }
}
.data-show__dialog ::v-deep {
  .el-dialog__body {
    padding: 20px;

    .el-footer {
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.preview-form-container{
   ::v-deep {
      .table_layout_wrap {
        .main-table {
          margin-bottom: 0;
          .main-title {
            padding: 0 8px !important;
          }
        }
      }
    }
}
</style>
<style lang="scss">
.van-popup {
  height: 60vh !important;
  .mobile-sunpop-content {
    height: calc(60vh - 120px);
  }
}
</style>
