"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _comp = require("../../ui/src/comp");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _ui = require("../../ui");
var _util = require("./util");
var _vn = require("../../ui/src/vn");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxePrint',
  props: {
    align: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().print.align;
      }
    },
    title: String,
    headerAlign: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().print.headerAlign;
      }
    },
    footerAlign: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().print.footerAlign;
      }
    },
    showPageNumber: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().print.showPageNumber;
      }
    },
    customLayout: Boolean,
    pageBreaks: Array,
    content: String,
    html: String,
    headerHtml: String,
    footerHtml: String,
    leftHtml: String,
    rightHtml: String,
    showAllPageTitle: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().print.showAllPageTitle;
      }
    },
    pageStyle: {
      type: Object,
      default: function _default() {
        return (0, _ui.getConfig)().print.pageStyle;
      }
    },
    customStyle: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().print.customStyle;
      }
    },
    styleUrls: {
      type: Array,
      default: function _default() {
        return (0, _ui.getConfig)().print.styleUrls;
      }
    },
    beforeMethod: Function
  },
  provide: function provide() {
    var $xePrint = this;
    return {
      $xePrint: $xePrint
    };
  },
  data: function data() {
    var reactData = {
      staticPageBreaks: []
    };
    return {
      xID: _xeUtils.default.uniqueId(),
      reactData: reactData
    };
  },
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xePrint = this;
      $xePrint.$emit(type, (0, _ui.createEvent)(evnt, {
        $print: $xePrint
      }, params));
    },
    print: function print() {
      var $xePrint = this;
      var props = $xePrint;
      var reactData = $xePrint.reactData;
      var align = props.align,
        title = props.title,
        headerAlign = props.headerAlign,
        footerAlign = props.footerAlign,
        showPageNumber = props.showPageNumber,
        customLayout = props.customLayout,
        pageBreaks = props.pageBreaks,
        content = props.content,
        headerHtml = props.headerHtml,
        footerHtml = props.footerHtml,
        leftHtml = props.leftHtml,
        rightHtml = props.rightHtml,
        showAllPageTitle = props.showAllPageTitle,
        pageStyle = props.pageStyle,
        customStyle = props.customStyle,
        styleUrls = props.styleUrls,
        beforeMethod = props.beforeMethod;
      var elem = $xePrint.$refs.refElem;
      return (0, _util.printHtml)(Object.assign({}, {
        align: align,
        title: title,
        headerAlign: headerAlign,
        footerAlign: footerAlign,
        showPageNumber: showPageNumber,
        customLayout: customLayout,
        pageBreaks: pageBreaks,
        content: content,
        headerHtml: headerHtml,
        footerHtml: footerHtml,
        leftHtml: leftHtml,
        rightHtml: rightHtml,
        showAllPageTitle: showAllPageTitle,
        pageStyle: pageStyle,
        customStyle: customStyle,
        styleUrls: styleUrls,
        beforeMethod: beforeMethod
      }, {
        _pageBreaks: !!reactData.staticPageBreaks.length,
        html: (0, _util.trimHtml)(elem ? elem.outerHTML : '') || props.html || props.content || ''
      }));
    },
    //
    // Render
    //
    renderPageConfigLayouts: function renderPageConfigLayouts(h) {
      var $xePrint = this;
      var props = $xePrint;
      var title = props.title,
        showPageNumber = props.showPageNumber,
        showAllPageTitle = props.showAllPageTitle,
        align = props.align,
        headerAlign = props.headerAlign,
        footerAlign = props.footerAlign;
      var pageBreaks = props.pageBreaks || [];
      var pageCount = pageBreaks.length;
      return pageBreaks.map(function (item, index) {
        var bodyHtml = item.bodyHtml;
        var headerHtml = item.headerHtml || props.headerHtml;
        var footerHtml = item.footerHtml || props.footerHtml;
        var leftHtml = item.leftHtml || props.leftHtml;
        var rightHtml = item.rightHtml || props.rightHtml;
        var currentPage = index + 1;
        var params = {
          currentPage: currentPage,
          pageCount: pageCount
        };
        return h('div', {
          class: ['vxe-print-page-break', align ? "align--".concat(align) : '']
        }, [h('div', {
          class: ['vxe-print-page-break--header', headerAlign ? "align--".concat(headerAlign) : '']
        }, headerHtml ? "".concat(_xeUtils.default.isFunction(headerHtml) ? headerHtml(params) : headerHtml || '') : [title && (showAllPageTitle || !index) ? h('div', {
          class: 'vxe-print-page-break--header-title'
        }, "".concat(title || '')) : (0, _ui.renderEmptyElement)($xePrint)]), h('div', {
          class: 'vxe-print-page-break--body'
        }, [h('div', {
          class: 'vxe-print-page-break--left'
        }, "".concat(_xeUtils.default.isFunction(leftHtml) ? leftHtml(params) : leftHtml || '')), h('div', {
          class: 'vxe-print-page-break--content'
        }, "".concat(_xeUtils.default.isFunction(bodyHtml) ? bodyHtml(params) : bodyHtml || '')), h('div', {
          class: 'vxe-print-page-break--right'
        }, "".concat(_xeUtils.default.isFunction(rightHtml) ? rightHtml(params) : rightHtml || ''))]), h('div', {
          class: ['vxe-print-page-break--footer', footerAlign ? "align--".concat(footerAlign) : '']
        }, footerHtml ? "".concat(_xeUtils.default.isFunction(footerHtml) ? footerHtml(params) : footerHtml || '') : [showPageNumber ? h('div', {
          class: 'vxe-print-page-break--footer-page-number'
        }, "".concat(currentPage, "/").concat(pageCount)) : (0, _ui.renderEmptyElement)($xePrint)])]);
      });
    },
    renderPageStaticLayouts: function renderPageStaticLayouts(h) {
      var $xePrint = this;
      var props = $xePrint;
      var slots = $xePrint.$scopedSlots;
      var reactData = $xePrint.reactData;
      var title = props.title,
        showPageNumber = props.showPageNumber,
        showAllPageTitle = props.showAllPageTitle,
        align = props.align,
        headerAlign = props.headerAlign,
        footerAlign = props.footerAlign;
      var staticPageBreaks = reactData.staticPageBreaks;
      var pageCount = staticPageBreaks.length;
      return staticPageBreaks.map(function (item, index) {
        var itemSlots = item.slots || {};
        var currentPage = index + 1;
        var defaultSlot = itemSlots.default;
        var headerSlot = itemSlots.header || slots.header;
        var footerSlot = itemSlots.footer || slots.footer;
        var leftSlot = itemSlots.left || slots.left;
        var rightSlot = itemSlots.right || slots.right;
        var params = {
          currentPage: currentPage,
          pageCount: pageCount
        };
        return h('div', {
          class: ['vxe-print-page-break', align ? "align--".concat(align) : '']
        }, [h('div', {
          class: ['vxe-print-page-break--header', headerAlign ? "align--".concat(headerAlign) : '']
        }, headerSlot ? (0, _vn.getSlotVNs)(headerSlot(params)) : [title && (showAllPageTitle || !index) ? h('div', {
          class: 'vxe-print-page-break--header-title'
        }, "".concat(title || '')) : (0, _ui.renderEmptyElement)($xePrint)]), h('div', {
          class: 'vxe-print-page-break--body'
        }, [h('div', {
          class: 'vxe-print-page-break--left'
        }, leftSlot ? (0, _vn.getSlotVNs)(leftSlot(params)) : []), h('div', {
          class: 'vxe-print-page-break--content'
        }, defaultSlot ? (0, _vn.getSlotVNs)(defaultSlot(params)) : []), h('div', {
          class: 'vxe-print-page-break--right'
        }, rightSlot ? (0, _vn.getSlotVNs)(rightSlot(params)) : [])]), h('div', {
          class: ['vxe-print-page-break--footer', footerAlign ? "align--".concat(footerAlign) : '']
        }, footerSlot ? (0, _vn.getSlotVNs)(footerSlot(params)) : [showPageNumber ? h('div', {
          class: 'vxe-print-page-break--footer-page-number'
        }, "".concat(currentPage, "/").concat(pageCount)) : (0, _ui.renderEmptyElement)($xePrint)])]);
      });
    },
    renderVN: function renderVN(h) {
      var $xePrint = this;
      var props = $xePrint;
      var slots = $xePrint.$scopedSlots;
      var reactData = $xePrint.reactData;
      var customLayout = props.customLayout;
      var staticPageBreaks = reactData.staticPageBreaks;
      var defaultSlot = slots.default;
      return h('div', {
        ref: 'refElem',
        class: ['vxe-print']
      }, customLayout ? defaultSlot ? (0, _vn.getSlotVNs)(defaultSlot({})) : [] : [h('div', {
        key: 'slot',
        class: 'vxe-print-slots'
      }, defaultSlot ? (0, _vn.getSlotVNs)(defaultSlot({})) : [])].concat(staticPageBreaks.length ? $xePrint.renderPageStaticLayouts(h) : $xePrint.renderPageConfigLayouts(h)));
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */