package zciyon

import "strings"

func General_excel_xml(fields []map[string]string, datas [][]string, param map[string]any, total []map[string]any) string {
	styles := map[string]string{}
	styles["l"] = `<Alignment ss:Horizontal="Left"/><Font ss:Color="#000000"/><Borders>
	<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
	<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
	<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
	<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>`
	styles["c"] = `<Alignment ss:Horizontal="Center"/><Font ss:Color="#000000"/><Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>`
	styles["r"] = `<Alignment ss:Horizontal="Right"/><Font ss:Color="#000000"/><Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>`
	styles["dd"] = `<Alignment ss:Horizontal="Center"/><Font ss:Color="#000000"/><Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders><NumberFormat ss:Format="yyyy\-mm\-dd"/>`
	styles["dt"] = `<Alignment ss:Horizontal="Center"/><Font ss:Color="#000000"/><Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders><NumberFormat ss:Format="yyyy\-mm\-dd\ hh:mm"/>`
	styles["ts"] = `<Alignment ss:Horizontal="Center"/><Interior ss:Color="#bdd7ee" ss:Pattern="Solid"/><Borders>
<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
</Borders>`
	styles["cap"] = `<Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
<Font ss:Size="16" ss:Color="#1f4e78" ss:Bold="1"/>
<Interior ss:Color="#ffffff" ss:Pattern="Solid"/>`
	sheetname := "sheetCIY"
	if param["sheetname"] != nil {
		sheetname = Tostr(param["sheetname"])
	}
	DefaultColumnWidth := 60 //默认宽度
	DefaultRowHeight := 20   //默认高度
	dat := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
	<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
	<Author>CIY</Author>
	<Version>15.00</Version>
	</DocumentProperties>
	<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"><AllowPNG/></OfficeDocumentSettings>
	<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"><WindowTopX>0</WindowTopX><WindowTopY>0</WindowTopY>  <ProtectStructure>False</ProtectStructure><ProtectWindows>False</ProtectWindows></ExcelWorkbook>
	<Styles>`
	for id, style := range styles {
		dat += `<Style ss:ID="` + id + `">` + style + `</Style>`
	}
	dat += `</Styles><Worksheet ss:Name="` + sheetname + `"><Table ss:ExpandedColumnCount="` + Tostr(len(fields)+10) + `" ss:ExpandedRowCount="` + Tostr(len(datas)+20) + `" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="` + Tostr(DefaultColumnWidth) + `" ss:DefaultRowHeight="` + Tostr(DefaultRowHeight) + `">`
	for _, field := range fields {
		if v, ok := field["width"]; ok {
			dat += `<Column ss:Width="` + Tostr(v) + `"/>`
		} else {
			dat += `<Column ss:Width="` + Tostr(DefaultColumnWidth) + `"/>`
		}
	}
	if v, ok := param["toptitle"]; ok {
		dat += `<Row ss:Height="30"><Cell ss:StyleID="cap"><Data ss:Type="String"></Data></Cell><Cell ss:StyleID="cap"><Data ss:Type="String">` + Tostr(v) + `</Data></Cell></Row>`
		//dat += `<Row ss:Height="30"><Cell ss:MergeAcross="` + Tostr(len(fields)-1) + `" ss:StyleID="cap"><Data ss:Type="String">` + Tostr(v) + `</Data></Cell></Row>`
	}
	if v, ok := param["rowstop"]; ok {
		dat += Tostr(v)
	}
	if v, ok := param["titleheight"]; ok {
		dat += `<Row ss:Height="` + Tostr(v) + `">`
	} else {
		dat += `<Row>`
	}
	cellpre := `<Cell`
	if _, ok := styles["ts"]; ok {
		cellpre += ` ss:StyleID="ts"`
	}
	for _, field := range fields {
		dat += cellpre + `><Data ss:Type="String">` + field["name"] + `</Data></Cell>`
	}
	dat += `</Row>`
	fieldcnt := len(fields)
	for _, data := range datas {
		dat += `<Row>`

		for ind, d := range data {
			dat += `<Cell`
			typestr := "String"
			if ind < fieldcnt {
				if v, ok := fields[ind]["style"]; ok {
					dat += ` ss:StyleID="` + Tostr(v) + `"`
				}
				if v, ok := fields[ind]["type"]; ok {
					typestr := v
					if typestr == "Number" && !Is_numeric(d) {
						typestr = "String"
					}
				} else {
					if len(d) < 11 && Is_numeric(d) {
						typestr = "Number"
					} else {
						typestr = "String"
					}
				}
			}
			dat += `><Data ss:Type="` + typestr + `">` + Tostr(d) + `</Data></Cell>`
		}
		dat += `</Row>`
	}
	if len(total) > 0 {
		dat += `<Row>`
		for _, tt := range total {
			dat += `<Cell`
			if v, ok := tt["merge"]; ok {
				dat += ` ss:MergeAcross="` + Tostr(v) + `"`
			}
			if v, ok := tt["style"]; ok {
				dat += ` ss:StyleID="` + Tostr(v) + `"`
			}
			name := Tostr(tt["name"])
			if strings.HasPrefix(name, "=") {
				dat += ` ss:Formula="` + name + `"><Data ss:Type="Number"></Data></Cell>`
			} else {
				dat += `><Data ss:Type="String">` + name + `</Data></Cell>`
			}
		}
		dat += `</Row>`
	}
	if v, ok := param["rowsfooter"]; ok {
		dat += Tostr(v) //自定义表格尾
	}
	dat += `</Table>
	<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
	 <PageSetup>`
	if v, ok := param["pagesetup"]; ok {
		dat += Tostr(v)
	} else {
		if v, ok := param["landscape"]; ok {
			if Tobool(v) {
				dat += `<Layout x:Orientation="Landscape"/>` //横向打印
			}
		}
		dat += `<Header x:Margin="0.3"/><Footer x:Margin="0.3"/>
		<PageMargins x:Bottom="0.3" x:Left="0.3" x:Right="0.3" x:Top="0.3"/>`
	}
	dat += `</PageSetup>`
	if _, ok := param["fixtopage"]; ok {
		dat += `<FitToPage/>`
	}
	dat += `<Print>
	<ValidPrinterInfo/>
	<PaperSizeIndex>1</PaperSizeIndex>
	<HorizontalResolution>600</HorizontalResolution>
	<VerticalResolution>0</VerticalResolution>
   </Print>
   <Selected/>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>`
	return dat
}
