// Package excel -----------------------------
// @file      : options.go
// @author    : JJXu
// @contact   : wavingBear@163.com
// @time      : 2022/12/19 12:41:40
// -------------------------------------------
package excel

import (
	"fmt"
	"path/filepath"
	"strings"
	"time"
)

type Option func(excel *Excel)

//	func AddSaveFileSuffixWithUnixTime(excel *Excel) {
//		excel.SaveName
//		fmt.Sprintf("%v", time.Now().Unix())
//	}
//
// 时间戳作为文件后缀
func OptionFileNameSuffixWithUnixTime(excel *Excel) {
	ext := filepath.Ext(excel.SaveName)
	name := strings.Split(excel.SaveName, ext)[0]
	excel.SaveName = fmt.Sprintf("%s_%v%s", name, time.Now().Unix(), ext)
}

// 为第一行添加表头
func AfterAddHeader1(sheet Sheet, text string) func(excel *Excel) {
	return func(excel *Excel) {
		err := excel.ex.InsertRows(sheet.SheetName(), 1, 1)
		if err != nil {
			panic(err)
		}
		err = excel.ex.SetCellValue(sheet.SheetName(), GetCellIndex(1, 1), text)
		if err != nil {
			panic(err)
		}
		headerStyleID, errs := excel.ex.NewStyle(NewHeaderOneStyle())
		if errs != nil {
			fmt.Println(errs)
			err = errs
			return
		}
		//表头设置为默认样式，边框加粗、字体加粗
		_ = excel.ex.SetCellStyle(sheet.SheetName(), "A1", GetCellIndex(1, len(sheet.SheetHeaders())), headerStyleID)
		_ = excel.ex.MergeCell(sheet.SheetName(), "A1", GetCellIndex(1, len(sheet.SheetHeaders())))

	}
}
