/*******************************************************************************
 * Copyright 2Fi Business Solutions Ltd.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.reports.view;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ffii.core.utils.ExcelUtils;
import com.ffii.core.utils.NumberUtils;
import com.ffii.core.utils.Params;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.view.document.AbstractExcelView;
import com.ffii.tbms.order.Order;
import com.ibm.icu.text.SimpleDateFormat;

import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class CustomerReportExcelView extends AbstractExcelView {

	protected Date printDate;
	protected final String REPORT_NAME = "Customer Report";
	
	protected CellStyle normalStyle;
	protected CellStyle wrapTextStyle;

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

				SecurityUtils.getUser();
		printDate = new Date();
		String pattern = "yyyy-MM-dd";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		String todayStr = simpleDateFormat.format(printDate);
		response.setHeader("Content-Disposition",
				"attachment; filename=\"" + response.encodeURL(REPORT_NAME + "_"+todayStr) + ".xlsx\"");
		Sheet sheet = workbook.getSheetAt(0);
		// get Data
		List<Map<String, Object>> records = (List<Map<String, Object>>) model.get(Params.RECORDS);
		String reportHeader =  (String)model.get("reportHeader");
		Map<String, Object> map = (Map<String,Object>) model.get("map");

		sheet.getHeader().setCenter(HSSFHeader.font("Calibri", "bold")+(reportHeader==null?"":reportHeader)+"\n"+REPORT_NAME+" ("
			+simpleDateFormat.format(map.get("dateFrom"))+" - "
			+simpleDateFormat.format(map.get("dateTo"))+")");

		int rowIndex = 1;
		for (int i = 0; i < records.size(); i++) {
			Map<String, Object> record = records.get(i);
			int colIndex = 0;

			if(rowIndex>1){
				ExcelUtils.copyAndInsertRow(workbook, sheet, 1, rowIndex);
			}

			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, i+1);
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("created"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("customerName"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("nameCh"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("phone1"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("phone2"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("email"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("address"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("remarks"));
			ExcelUtils.setCellValue(sheet, rowIndex, colIndex++, record.get("orderAmount"));
	
			rowIndex++;
		}
	}

}
