<!-- excel在线编辑 -->
<script setup lang="ts" name="ExcelEditDialog">
import '@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css'
import '@grapecity-software/spread-sheets-designer/styles/gc.spread.sheets.designer.min.css'
import GC from '@grapecity-software/spread-sheets'
import '@grapecity-software/spread-sheets-shapes'
import '@grapecity-software/spread-sheets-charts'
import '@grapecity-software/spread-sheets-slicers'
import '@grapecity-software/spread-sheets-print'
import '@grapecity-software/spread-sheets-barcode'
import '@grapecity-software/spread-sheets-pdf'
import '@grapecity-software/spread-sheets-pivot-addon'
import '@grapecity-software/spread-sheets-tablesheet'
import '@grapecity-software/spread-sheets-ganttsheet'
import '@grapecity-software/spread-sheets-formula-panel'
import '@grapecity-software/spread-sheets-reportsheet-addon'
import '@grapecity-software/spread-sheets-io'
import '@grapecity-software/spread-excelio'
import '@grapecity-software/spread-sheets-resources-zh'
// 注意：spread-sheets-designer-resources-cn 与 spread-sheets-designer 的先后顺序
import '@grapecity-software/spread-sheets-designer-resources-cn'
import '@grapecity-software/spread-sheets-designer'
import { importSjsFile } from './index'
import { importFile, exportFile } from './methods/file'
const emits = defineEmits(['initComplete'])
GC.Spread.Common.CultureManager.culture('zh-cn')
import bindForm from './bindForm'
// const config = GC.Spread.Sheets.Designer.DefaultConfig
let designer: any = null
let spread: any = null
let sheet: any = null
const designerContainerRef = ref(null) // 引用Designer的宿主Dom元素
// ----------------------------------------初始化相关-----------------------------------------------
// designer初始化完成
async function initComplete() {
  // const res = await fetch('http://111.198.10.15:21408/test/压力表-新_1741080567308.xlsx')
  // const file = await res.blob()
  // spread.import(
  //   file,
  //   () => {
  //     console.log('spread：导入文件成功')
  //   },
  //   (e) => {
  //     console.log('spread：上传文件发生错误', e)
  //   },
  //   {
  //     fileType: GC.Spread.Sheets.FileType.excel,
  //   },
  // )
  // importSjsFile(spread, new File(a))

  // handleBindForm()
  emits('initComplete', spread)
  // const res = await fetch('http://111.198.10.15:21408/test/校准证书_1742974211244.sjs')
  // const file = await res.blob()
  // importSjsFile(spread, file).then(() => {
  //   handleBindForm()
  // })
}

// -----------------------------------导入导出-------------------------------------------------
// 执行导入文件
const handleImportFile = (file: any, data: any) => {
  importFile(spread, file).then(() => {
    // handleBindForm(data)
    bindForm(spread, data)
  })
}
// 执行导出文件
const handleExportFile = async () => {
  const blob = await exportFile(spread, 'xlsx', true)
  console.log('获取到文件', blob)
}

// -----------------------------------数据绑定-------------------------------------------------
// 表格绑定
function handleBindTable() {
  console.log('执行表格绑定')
  const datasource = {
    dw: '我是单位',
    dz: '我是地址',
  }
  // var datasource = [
  //   { name: 'Alice', age: 27, birthday: '1985/08/31', position: 'PM' },
  // ]
  // var colInfos = [
  //   { name: 'position', displayName: '姓名', size: 50, visible: false },
  //   { name: 'name', displayName: 'Display Name', size: 70 },
  //   { name: 'birthday', displayName: 'Birthday', formatter: 'd/M/yy', size: 120 },
  //   { name: 'age', displayName: 'Age', size: 40, resizable: false },
  // ]
  const sheet = spread.getSheet(1) // 获取第几个sheet页
  // sheet.autoGenerateColumns = false
  sheet.setDataSource(datasource)
  // sheet.bindColumns(colInfos)
}

// 表单绑定
function handleBindForm(formData: any) {
  console.log('执行表单绑定')
  const data = {
    certificateNo: '12345678987',
    dz: '我是地址',
  }
  const source = new GC.Spread.Sheets.Bindings.CellBindingSource(data)
  const sheet = spread.getSheet(2) // 获取第几个sheet页
  sheet.setDataSource(source)
}

// ----------------------------------弹窗基本操作-----------------------------------------------
const dialogVisible = ref(false) // 弹窗显示
const loading = ref(false)
// 初始化弹窗
const initDialog = (id: string, sampleid: string, orderid: string) => {
  dialogVisible.value = true
  nextTick(() => {
    designer = new GC.Spread.Sheets.Designer.Designer(designerContainerRef.value)
    spread = designer.getWorkbook()
    sheet = spread.getActiveSheet()
    initComplete()
  })
}
const closeDialog = () => {
  dialogVisible.value = false
}

// -------------------------------------------------------------------------------------------
defineExpose({ initDialog, handleImportFile })
</script>

<template>
  <el-dialog v-model="dialogVisible" v-loding="loading" :z-index="1200" append-to-body class="excel-edit-dialog" :fullscreen="true" title="excel在线编辑" width="700" @close="closeDialog">
    <div id="designer-container" ref="designerContainerRef" />
    <template #footer>
      <el-button type="info" @click="closeDialog">
        关闭
      </el-button>
      <!-- <el-button type="info" @click="handleBindForm">
        绑定
      </el-button> -->
      <el-button type="info" @click="handleExportFile">
        导出
      </el-button>
    </template>
  </el-dialog>
</template>

<style lang="scss">
#designer-container {
  width: 100%;
  height: 86vh;
}
</style>
