"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _comp = require("../../../ui/src/comp");
var _ui = require("../../../ui");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _utils = require("../../../ui/src/utils");
var _log = require("../../../ui/src/log");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const {
  errLog
} = (0, _log.createComponentLog)('table');
const {
  getI18n,
  getIcon,
  globalMixins,
  renderEmptyElement
} = _ui.VxeUI;
var _default = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeTableImportPanel',
  mixins: [globalMixins.sizeMixin],
  props: {
    defaultOptions: {
      type: Object,
      default: () => ({})
    },
    storeData: {
      type: Object,
      default: () => ({})
    }
  },
  inject: {
    $xeTable: {
      default: null
    }
  },
  data() {
    const reactData = {
      loading: false
    };
    return {
      reactData
    };
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeSelectName() {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const {
        storeData
      } = props;
      return `${storeData.filename}.${storeData.type}`;
    },
    computeHasFile() {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const {
        storeData
      } = props;
      return storeData.file && storeData.type;
    },
    computeParseTypeLabel() {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const {
        storeData
      } = props;
      const {
        type,
        typeList
      } = storeData;
      if (type) {
        const selectItem = _xeUtils.default.find(typeList, item => type === item.value);
        return `${selectItem ? selectItem.label : '*.*'}`;
      }
      return `*.${typeList.map(item => item.value).join(', *.')}`;
    }
  }),
  created() {
    const $xeTableImportPanel = this;
    const VxeUIModalComponent = _ui.VxeUI.getComponent('VxeModal');
    const VxeUIButtonComponent = _ui.VxeUI.getComponent('VxeButton');
    const VxeUISelectComponent = _ui.VxeUI.getComponent('VxeSelect');
    $xeTableImportPanel.$nextTick(() => {
      if (!VxeUIModalComponent) {
        errLog('vxe.error.reqComp', ['vxe-modal']);
      }
      if (!VxeUIButtonComponent) {
        errLog('vxe.error.reqComp', ['vxe-button']);
      }
      if (!VxeUISelectComponent) {
        errLog('vxe.error.reqComp', ['vxe-select']);
      }
    });
  },
  methods: {
    clearFileEvent() {
      Object.assign(this.storeData, {
        filename: '',
        sheetName: '',
        type: ''
      });
    },
    selectFileEvent() {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const $xeTable = $xeImportPanel.$xeTable;
      const {
        storeData,
        defaultOptions
      } = props;
      $xeTable.readFile(defaultOptions).then(params => {
        const {
          file
        } = params;
        Object.assign(storeData, (0, _utils.parseFile)(file), {
          file
        });
      }).catch(() => {});
    },
    showEvent() {
      const $xeImportPanel = this;
      $xeImportPanel.$nextTick(() => {
        const targetElem = $xeImportPanel.$refs.fileBtn;
        if (targetElem) {
          targetElem.focus();
        }
      });
    },
    cancelEvent() {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const {
        storeData
      } = props;
      storeData.visible = false;
    },
    importEvent() {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const reactData = $xeImportPanel.reactData;
      const $xeTable = $xeImportPanel.$xeTable;
      const {
        storeData,
        defaultOptions
      } = props;
      const importOpts = $xeTable.computeImportOpts;
      reactData.loading = true;
      $xeTable.importByFile(storeData.file, Object.assign({}, importOpts, defaultOptions)).then(() => {
        reactData.loading = false;
        storeData.visible = false;
      }).catch(() => {
        reactData.loading = false;
      });
    },
    renderVN(h) {
      const $xeImportPanel = this;
      const props = $xeImportPanel;
      const reactData = $xeImportPanel.reactData;
      const $xeTable = $xeImportPanel.$xeTable;
      const $xeGrid = $xeTable.$xeGrid;
      const $xeGantt = $xeTable.$xeGantt;
      const {
        defaultOptions,
        storeData
      } = props;
      const selectName = $xeImportPanel.computeSelectName;
      const hasFile = $xeImportPanel.computeHasFile;
      const parseTypeLabel = $xeImportPanel.computeParseTypeLabel;
      const slots = defaultOptions.slots || {};
      const topSlot = slots.top;
      const bottomSlot = slots.bottom;
      const defaultSlot = slots.default;
      const footerSlot = slots.footer;
      return h('vxe-modal', {
        ref: 'modal',
        props: {
          id: 'VXE_IMPORT_MODAL',
          value: storeData.visible,
          title: getI18n('vxe.import.impTitle'),
          width: 540,
          minWidth: 360,
          minHeight: 240,
          mask: true,
          lockView: true,
          showFooter: true,
          escClosable: true,
          maskClosable: true,
          showMaximize: true,
          resize: true,
          loading: reactData.loading
        },
        on: {
          input(value) {
            storeData.visible = value;
          },
          show: $xeImportPanel.showEvent
        },
        scopedSlots: {
          default: () => {
            const params = {
              $table: $xeTable,
              $grid: $xeGrid,
              $gantt: $xeGantt,
              options: defaultOptions,
              params: defaultOptions.params
            };
            return h('div', {
              class: 'vxe-table-export--panel'
            }, [topSlot ? h('div', {
              class: 'vxe-table-export--panel-top'
            }, $xeTable.callSlot(topSlot, params, h)) : renderEmptyElement($xeTable), h('div', {
              class: 'vxe-table-export--panel-body'
            }, defaultSlot ? $xeTable.callSlot(defaultSlot, params, h) : [h('table', {
              class: 'vxe-table-export--panel-table',
              attrs: {
                cellspacing: 0,
                cellpadding: 0,
                border: 0
              }
            }, [h('tbody', [h('tr', [h('td', getI18n('vxe.import.impFile')), h('td', [hasFile ? h('div', {
              class: 'vxe-table-export--selected--file',
              attrs: {
                title: selectName
              }
            }, [h('span', selectName), h('i', {
              class: getIcon().INPUT_CLEAR,
              on: {
                click: $xeImportPanel.clearFileEvent
              }
            })]) : h('button', {
              ref: 'fileBtn',
              class: 'vxe-table-export--select--file',
              attrs: {
                type: 'button'
              },
              on: {
                click: $xeImportPanel.selectFileEvent
              }
            }, getI18n('vxe.import.impSelect'))])]), h('tr', [h('td', getI18n('vxe.import.impType')), h('td', parseTypeLabel)]), h('tr', [h('td', getI18n('vxe.import.impMode')), h('td', [h('vxe-select', {
              props: {
                value: defaultOptions.mode,
                options: storeData.modeList
              },
              on: {
                modelValue(value) {
                  defaultOptions.mode = value;
                }
              }
            })])])])])]), bottomSlot ? h('div', {
              class: 'vxe-table-export--panel-bottom'
            }, $xeTable.callSlot(bottomSlot, params, h)) : renderEmptyElement($xeTable)]);
          },
          footer: () => {
            const params = {
              $table: $xeTable,
              $grid: $xeGrid,
              $gantt: $xeGantt,
              options: defaultOptions,
              params: defaultOptions.params
            };
            return h('div', {
              class: 'vxe-table-export--panel-footer'
            }, footerSlot ? $xeTable.callSlot(footerSlot, params, h) : [h('div', {
              class: 'vxe-table-export--panel-btns'
            }, [h('vxe-button', {
              on: {
                click: $xeImportPanel.cancelEvent
              }
            }, getI18n('vxe.import.impCancel')), h('vxe-button', {
              props: {
                status: 'primary',
                disabled: !hasFile || reactData.loading
              },
              on: {
                click: $xeImportPanel.importEvent
              }
            }, getI18n('vxe.import.impConfirm'))])]);
          }
        }
      });
    }
  },
  render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */