<template>
  <div name="data-view" class="template-preview-container">
    <span v-if="html" class="template-content">
      <div
        v-if="templateInfo.name && showTitle === 1"
        class="ht-data-lists-title"
      >
        {{ templateInfo.name }}
      </div>
      <div v-if="showDateView" class="data_view">
        <div v-if="isControl && (isMobile || mobileMode)" class="header">
          <!-- <span class="title">{{ templateInfo.name }}</span>
          <span v-show="isShowMore" class="more" @click="goToDetail">
            更多
            <i class="el-icon-arrow-right"></i>
          </span> -->
        </div>
        <!-- 改用动态组件渲染 -->
        <component
          :is="dynamicComponent"
          ref="dataViewTemplate"
          :key="refreshTime"
          :template-key="templateKey"
          :search-form="searchForm"
          :template-info="templateInfo"
          :single="single"
          :task-type="taskType"
          :is-join-flow="isJoinFlow"
          :def-key="defKey"
          :data-view="dataView"
          :current-table-height="tableHeight"
          :current-table-max-height="tableMaxHeight"
          :is-manage="isManage"
          :is-show-header="isShowHeader"
          :is-page-size="isPageSize"
          class="data-preview-wrap"
          :parameterq-querys="parameterqQuerys"
          :is-public="isPublic"
          :is-control="isControl"
          :mobile-mode="mobileMode"
          :is-show-more="isShowMore"
          :from-module="fromModule"
          :is-show-summary="isShowSummary"
          :autofocus="autofocus"
          :join-query-config="joinQueryConfig"
          :custom-query="customQuery"
          @table-total="getTableTotal"
        />
      </div>
    </span>
    <div v-if="loadedFail" class="loaded-fail__div">报表不存在...</div>
  </div>
</template>
<script>
  import Vue from 'vue'
  import mobileFormList from '@/mixins/mobileFormList'
  import templatePreviewMixin from '@/mixins/templatePreview.js'
  import utils from 'hotent-ui/src/utils.js'
  // const { Base64 } = require('js-base64')
  export default {
    name: 'HtTemplatePreview',
    props: {
      templateKey: { type: String, required: true },
      parameterqQuerys: String,
      single: String,
      taskType: {
        type: String,
        default: '',
      },
      defKey: String,
      dataView: Object,
      isJoinFlow: {
        type: Boolean,
        default: false,
      },
      tableHeight: {
        type: [Number, Function, String],
      },
      tableMaxHeight: {
        type: [Number, Function, String],
      },
      isManage: {
        type: Boolean,
        default: false,
      },
      isShowHeader: {
        type: Boolean,
        default: true,
      },
      isPageSize: {
        type: [Number, String],
        default: '',
      },
      isPublic: {},
      fromModule: {
        type: Boolean,
        default: false,
      },
      mobileMode: {
        type: Boolean,
        default: false,
      },
      isControl: {
        type: Boolean,
        default: false,
      },
      isShowSummary: {
        type: Boolean,
        default: true,
      },
      autofocus: {
        type: Boolean,
        default: true,
      },
      showTitle: {
        type: Number,
        default: 0,
      },
    },
    data() {
      return {
        html: '',
        templateInfo: {},
        loadedFail: false,
        refreshTime: new Date().getTime(),
        showDateView: true,
        isMobile: utils.isMobile(),
        isShowMore: false,
        customQuery: null,
        joinQueryConfig: null,
        dynamicComponent: null,
      }
    },
    watch: {
      templateKey: {
        handler(newVal) {
          if (newVal) {
            this.init()
          }
        },
        immediate: true,
      },
    },
    beforeDestroy() {
      // 组件销毁前清理引用，避免内存泄漏
      this.dynamicComponent = null
    },
    methods: {
      // goToDetail() {
      //   this.$requestConfig.goToFormList({
      //     ...this.templateInfo,
      //     dataView: Base64.encode(JSON.stringify(this.dataView)),
      //   })
      // },
      // 根据当前formKey初始化
      init() {
        let _me = this
        this.$requestConfig
          .getBpmDataTemplateInfo({
            templateKey: this.templateKey,
          })
          .then((result) => {
            if (result.state) {
              _me.templateInfo = result.value
              _me.$emit('loaded', result.value)
              _me.templateInfo.displayFieldArray = JSON.parse(
                _me.templateInfo.displayField
              )
              if (_me.templateInfo.joinQueryConfig) {
                _me.joinQueryConfig = JSON.parse(
                  _me.templateInfo.joinQueryConfig
                )
                if (_me.joinQueryConfig.alias) {
                  this.$requestConfig
                    .getCustomQuery(_me.joinQueryConfig.alias)
                    .then((res) => {
                      _me.customQuery = res
                      _me.getTemplateDataListForm()
                    })
                } else {
                  _me.getTemplateDataListForm()
                }
              } else {
                _me.getTemplateDataListForm()
              }
            } else {
              this.$message.error(result.message)
            }
          })
      },
      getTemplateDataListForm() {
        this.$requestConfig
          .getTemplateDataListForm(
            this.templateKey,
            this.isPublic || this.mobileMode ? 1 : 0
          )
          .then((result) => {
            if (result.state) {
              this.html = result.value
              this.customInit()
            } else {
              this.loadedFail = true
              this.$message.error(result.message || '获取表单列表模板html失败')
            }
          })
      },
      getTableTotal(total) {
        this.isShowMore = total > 5 ? true : false
      },
      customInit() {
        // 每次创建新的组件构造函数（避免全局注册覆盖问题）
        this.dynamicComponent = Vue.extend({
          mixins: [mobileFormList, templatePreviewMixin],
          props: {
            templateInfo: {
              type: Object,
            },
            single: String,
            currentTableHeight: {
              type: [Number, Function, String],
            },
            currentTableMaxHeight: {
              type: [Number, Function, String],
            },
            isManage: {
              type: Boolean,
              default: false,
            },
            isShowHeader: {
              type: Boolean,
              default: true,
            },
            isPageSize: {
              type: [Number, String],
              default: '',
            },
            isPublic: {},
            fromModule: {
              type: Boolean,
              default: false,
            },
            isControl: {
              type: Boolean,
              default: false,
            },
            isShowMore: {
              type: Boolean,
              default: false,
            },
            mobileMode: {
              type: Boolean,
              default: false,
            },
            isShowSummary: {
              type: Boolean,
              default: true,
            },
            autofocus: {
              type: Boolean,
              default: true,
            },
            joinQueryConfig: {
              type: Object,
              default: null,
            },
            customQuery: {
              type: Object,
              default: null,
            },
          },
          data() {
            return {}
          },
          template: this.html,
        })
        this.refreshTime = new Date().getTime()
      },
    },
  }
</script>
<style lang="scss" scoped>
  .template-preview-container {
    height: 100%;
    .template-content {
      display: block;
      height: 100%;
      width: 100%;
      .header {
        display: flex;
        justify-content: space-between;
        padding: 16px 16px 12px;
        align-items: center;
        .title {
          font-size: 16px;
          font-weight: 500;
          color: #262626;
        }
        .more {
          font-size: 12px;
          color: #8c8c8c;
        }
      }
    }
    .data_view,
    .data-preview-wrap {
      height: 100%;
      position: relative;
    }
    >>> .navbar-collapse {
      position: absolute;
      top: 45%;
      cursor: pointer;
      width: 0px;
      z-index: 99;
    }

    >>> .navbar-collapse.navbar-collapse-right {
      top: 45%;
    }

    >>> .navbar-collapse:hover {
      transform: scale(1);
    }

    >>> .navbar-collapse:hover .navbar-collapse-arrow {
      color: #333333;
    }

    >>> .navbar-collapse-arrow {
      position: relative;
      top: 25%;
      right: 15px;
      font-size: 18px;
      color: #a8a8a8;
    }

    >>> .navbar-collapse-bg {
      -webkit-transition: all 0.12s ease;
      height: 40px;
      border-bottom: 8px solid transparent;
      border-right: none;
      border-left: 12px solid #ebebeb;
      border-top: 8px solid transparent;
      opacity: 0.9;
    }
    .ht-data-lists-title {
      font-size: 16px;
      font-weight: 550;
      color: #262626;
      margin: 16px auto 8px auto;
    }
  }
  >>> th.todo-header-row {
    background-color: #fafafa;
    font-size: 13px;
  }

  >>> tr.todo-row {
    font-size: 13px;
  }

  .el-container {
    background-color: #fff;
  }

  .el-row {
    margin-bottom: 20px;
  }

  .el-header {
    border-bottom: 1.5px solid #ededed;
  }
  .sun-div-container > .el-header,
  .sub-div-pc > .el-header {
    border-bottom: 1.5px solid var(--borderColor);
  }

  .i {
    margin-right: 4px;
  }

  .todo-subject {
    cursor: pointer;
  }

  >>> .el-dialog__body {
    padding: 15px 10px;
  }

  >>> .el-checkbox {
    // margin-left: 15px;
  }

  >>> .el-radio {
    margin-left: 15px;
  }

  >>> .el-button {
    margin-left: 0px;
  }
  .loaded-fail__div {
    text-align: center;
    font-size: 14px;
    font-weight: bold;
    color: #f56c6c;
  }
  .data-preview-wrap
    ::v-deep
    .dropdown-upload__btn
    > .el-button-group
    > button:first-child {
    margin: 0;
    padding: 0;
  }
  .start-process {
    border: 0;
  }
  >>> .template-view_change > .el-tabs__header {
    margin-bottom: 0;
    margin-left: 24px;
    width: calc(100% - 48px);
  }

  >>> .template-view_change > .el-tabs__content {
    height: calc(100% - 80px);
    overflow-y: auto;
    .printData {
      height: 100%;
    }
    .el-tab-pane {
      height: 100%;
      & > span {
        display: block;
        height: 100%;
      }
    }
    .table-main {
      padding: 0 0 5px !important;
    }
    .el-main {
      padding: 0 0 20px;
      // height: calc(100% - 25px);
      & > span {
        display: block;
        height: 100%;
      }
    }
    .ht-table-panel {
      height: 100%;
      padding: 0;
    }
    .summary-table {
      margin: 10px 0 24px;
    }
    .el-footer {
      padding-right: 0;
    }
  }
  ::v-deep {
    .toolbar-container {
      margin-right: 0 !important;
    }
    .el-dropdown__caret-button:hover::before,
    .el-dropdown__caret-button:focus::before,
    .el-dropdown__caret-button:active::before {
      top: 0;
      bottom: 0;
      border-left: 1px solid var(--themeColor);
    }
    .is-show-operation-dropdown {
      .el-icon-arrow-down {
        transform: rotate(180deg);
      }
      // .el-dropdown__caret-button::before {
      //   top: 0;
      //   bottom: 0;
      //   border-left: 1px solid;
      // }
    }
  }
</style>
<style lang="scss">
  #printData {
    .el-table td.el-table__cell {
      border-bottom: none !important;
    }
  }
  .template-dropdown {
    .el-button:first-child {
      padding: 0 !important;
    }
    .template-dropdown-item {
      span {
        display: inline-block;
        padding: 7px 15px;
      }
    }
  }
  .right_menu {
    .cell {
      display: flex;
      align-items: center;
      justify-content: center;
      column-gap: 10px;
      flex-wrap: wrap;
      .el-button {
        font-size: 12px !important;
        margin-top: 8px;
        margin-left: 0;
      }
    }
    .el-button-group {
      display: flex !important;
    }
    .template-dropdown {
      .el-button:first-child {
        padding: 0 !important;
      }
      .template-dropdown-item {
        span {
          display: inline-block;
          padding: 4px 12px;
          line-height: 18px;
          max-width: 84px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
      .el-dropdown__caret-button {
        padding: 5px 5px;
        & > span {
          display: inline-block;
        }
      }
    }
  }
  .right_menu_left {
    .cell {
      justify-content: flex-start;
    }
  }
  .right_menu_right {
    .cell {
      justify-content: flex-end;
    }
  }
  .form-list--header {
    .form-list--search {
      padding: 16px 16px 12px 16px;
      .van-search__content {
        padding-left: 8px;
        border-radius: 8px;
      }
    }
    .filter-config {
      display: flex;
      width: 100%;
      background: #fff;
      margin-bottom: 12px;
      .config-item {
        height: 44px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #969799;
        font-size: 14px;
        .btn-icon {
          font-size: 16px;
          padding-left: 4px;
        }
        .filter-icon {
          margin-top: 2px;
        }
        .van-dropdown-menu__bar {
          box-shadow: unset;
          height: 44px;
          .van-dropdown-menu__title {
            color: #969799;
            font-size: 14px;
            &::after {
              border: 4px solid;
              margin-top: -6px;
              right: -9px;
              border-color: transparent transparent #969799 #969799;
            }
          }
          .van-dropdown-menu__title--down {
            &::after {
              margin-top: -1px;
              border-color: transparent transparent #409eff #409eff;
            }
          }
        }
      }
      .has-sort {
        .van-dropdown-menu__bar {
          .van-dropdown-menu__title {
            color: #409eff;
            &::after {
              border-color: transparent transparent #409eff #409eff;
            }
          }
        }
      }

      .active-item {
        color: #409eff;
      }
      .sort-wrap {
        padding: 0 16px;
        .sort-item {
          height: 48px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          .item-left {
            color: #262626;
            font-size: 14px;
            display: flex;
            align-items: center;
            padding-right: 32px;
            .selected-icon {
              margin-top: 4px;
              padding-right: 8px;
              font-size: 18px;
              color: #409eff;
            }
          }
          .selected {
            color: #409eff;
          }
          .item-right {
            white-space: nowrap;
            .selected-icon {
              color: #409eff;
            }
          }
          .order-icon {
            font-size: 22px;
            color: #262626;
          }
          .asc-order {
            padding-right: 12px;
          }
        }
      }
    }
  }
  .form-list--content {
    height: calc(100vh - 104px);
    overflow-y: scroll;
    .summary-box {
      display: flex;
      margin: 0px 16px 16px;
      padding-top: 4px;
      overflow-x: auto;
      overflow-y: hidden;

      //隐藏滚动条
      &::-webkit-scrollbar {
        display: none;
      }
      /*火狐下隐藏滚动条*/
      scrollbar-width: none;
      .summary-item {
        background: #fff;
        border-radius: 8px;
        height: 46px;
        padding: 12px 16px 14px 12px;
        margin-right: 12px;
        &:last-child {
          margin-right: 0;
        }
        .summary-item--title {
          font-size: 14px;
          color: #262626;
          font-weight: bold;
          padding-bottom: 9px;
        }
        .summary-item--content {
          display: flex;
          justify-content: space-between;
          align-items: center;
          min-width: 120px;
          .summary-content-box {
            display: flex;
            align-items: center;
            &:first-child {
              padding-right: 36px;
            }
            .summary-content__label {
              font-size: 12px;
              color: #8c8c8c;
              padding-right: 8px;
              width: 52px;
              white-space: nowrap;
            }
            .summary-content-value {
              font-size: 14px;
              color: #262626;
              font-weight: 800;
            }
          }
        }
      }
    }
    .set-summary-height {
      .summary-item {
        height: unset;
        .summary-item--title {
          height: var(--summaryHeight);
        }
      }
    }
    .van-cell {
      padding: 16px 0 0 0;
      margin-bottom: 12px;
      .field-wrap {
        .field-item--title {
          padding: 0 16px 8px 16px;
          display: flex;
          width: calc(100% - 32px);
          justify-content: space-between;
          .top-box {
            width: calc(100% - 90px);
            flex: 1;
            display: flex;
            .title,
            .title-item__wrap {
              padding-right: 32px;
              font-size: 16px;
              color: #262626;
              display: inline-block;
              flex: 1;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              &:empty {
                display: none;
              }
            }
            .field-item__value {
              font-size: 16px;
              font-weight: bold;
            }
          }

          .status,
          .status-item__wrap {
            font-size: 14px;
            color: #409eff;
            display: inline-block;
            max-width: 90px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            &:empty {
              display: none;
            }
            .status-item__wrap {
              font-size: 14px;
            }
          }
        }
        .field-content {
          padding: 0 16px 16px 46px;
          .field-items {
            .field-item-value__wrap {
              line-height: 17px;
              font-size: 12px;
              width: calc(100% - 62px);
              display: block;
              span {
                display: block;
                width: 100%;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              }
            }
          }
        }
        .field-items {
          padding-top: 8px;
          width: 100%;
          display: flex;
          align-items: center;
          .field-item--label,
          .field-item--value {
            color: #8c8c8c;
            font-size: 12px;
            display: inline-block;
          }
          .field-item--label {
            max-width: 120px;
            line-height: 18px;
            margin-right: 8px;
            white-space: nowrap;
          }
          .field-item--value {
            flex: 1;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }
          .van-progress {
            flex: 1;
          }
        }
        .field-items-wrap {
          flex-direction: column;
          align-items: flex-start;
          .mobile-picture {
            padding-bottom: 0;
            padding-top: 12px;
          }
        }
        .footer-wrap {
          border-top: 1px solid #e6e6e6;
          display: flex;
          height: 44px;
          &:empty {
            display: none;
          }
          .footer-item {
            height: 44px;
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            color: #262626;
            &::after {
              position: absolute;
              content: '';
              width: 1px;
              height: 16px;
              top: 14px;
              right: 0;
              background: #e6e6e6;
            }
            .footer-btn--icon {
              font-size: 16px;
              padding-right: 4px;
            }
            &:last-child {
              &::after {
                width: 0;
              }
            }
          }
        }
      }
      .hide-checkbox {
        .field-item--title {
          .top-box {
            .title {
              padding-left: 0;
            }
          }
        }
        .field-content {
          padding-left: 16px;
        }
      }
    }
  }
  .form-list--footer {
    border-top: 1px solid #e6e6e6;
    display: flex;
    align-items: center;
    height: 64px;
    background: #fff;
    padding: 0 16px;
    .form-list__footer--btn {
      flex: 1;
      border-radius: 8px;
      border-color: #e6e6e6;
      color: #262626;
      &:last-child {
        background: #409eff;
        border-color: #409eff;
        color: #fff;
      }
    }
    .checked-all {
      padding-right: 14px;
      min-width: 70px;
    }
    .van-checkbox__label {
      font-size: 14px;
      color: #262626;
      margin-left: 12px;
    }
  }
  .form-list__footer--btn.van-button--disabled {
    background: #f9f9f9;
    color: #bfbfbf;
  }

  .has-bottom--btn {
    height: calc(100vh - 168px);
  }
  .has-merge-search {
    height: calc(100vh - 166px);
  }
  .has-search-bottomBtn {
    height: calc(100vh - 228px);
  }
  .filter-action {
    min-height: 40vh;
    .van-nav-bar__title {
      font-size: 17px;
      font-weight: bold;
      color: #262626;
    }
    .van-nav-bar__text {
      color: #8c8c8c;
    }
  }
  .filter-action__footer {
    display: flex;
    height: 64px;
    align-items: center;
    background: #fff;
    padding: 0 16px;
    border-top: 1px solid #e6e6e6;
    &--btn {
      flex: 1;
      border-radius: 8px;
      border-color: #e6e6e6;
      color: #262626;
    }
    .van-button--info {
      background: #409eff;
      border-color: #409eff;
      color: #fff;
      margin-left: 12px;
    }
  }
</style>
