package com.madeu.view.web;

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

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.servlet.view.AbstractView;

import com.madeu.util.ConvertStringUtil;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WebPaymentView extends AbstractView{

    private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // 출력 데이터
        Map<String, Object> map = (Map<String, Object>)model.get("map");
        String filename = (String)map.get("name");
        List<Map<String, Object>> selectList = (List<Map<String, Object>>)map.get("selectListPayment");

        int rowCount = 0;   // row
        int colCount = 0;   // col

        //1차로 workbook을 생성
        XSSFWorkbook workbook = new XSSFWorkbook();

        setContentType(CONTENT_TYPE_XLSX);

        //2차는 sheet생성
        Sheet sheet = workbook.createSheet("수납 내역");
        XSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        Row header = sheet.createRow(rowCount++);


        createHeaderCell(0, header, headerStyle, "번호");
        createHeaderCell(1, header, headerStyle, "진료일");
        createHeaderCell(2, header, headerStyle, "결제일");
        createHeaderCell(3, header, headerStyle, "결제구분");
        createHeaderCell(4, header, headerStyle, "고객이름");
        createHeaderCell(5, header, headerStyle, "진료유형");
        createHeaderCell(6, header, headerStyle, "시술(상품)명");
        createHeaderCell(7, header, headerStyle, "담당의사");
        createHeaderCell(8, header, headerStyle, "담당자");
        createHeaderCell(9, header, headerStyle, "결제메모");
        createHeaderCell(10, header, headerStyle, "결제총액");
        createHeaderCell(11, header, headerStyle, "할인");
        createHeaderCell(12, header, headerStyle, "멤버십");
        createHeaderCell(13, header, headerStyle, "미수");
        createHeaderCell(14, header, headerStyle, "결제금액");
        createHeaderCell(15, header, headerStyle, "공급가");
        createHeaderCell(16, header, headerStyle, "VAT");
        createHeaderCell(17, header, headerStyle, "취소/환불");
        createHeaderCell(18, header, headerStyle, "결제상태");

        // 엑셀에 데이터 입력
        for(Map<String, Object> e:selectList){
            Row row = sheet.createRow(rowCount++);
            row.setHeight((short)350);
            row.createCell(0).setCellValue(ConvertStringUtil.ifNullToString(rowCount-1));
            row.createCell(1).setCellValue(ConvertStringUtil.ifNullToString(e.get("reserveDate")));
            row.createCell(2).setCellValue(ConvertStringUtil.ifNullToString(e.get("paymentDate")));
            row.createCell(3).setCellValue(ConvertStringUtil.ifNullToString(e.get("paymentCategoryItemName")));
            row.createCell(4).setCellValue(ConvertStringUtil.ifNullToString(e.get("userName")));
            row.createCell(5).setCellValue(ConvertStringUtil.ifNullToString(e.get("treatmentName")));
            row.createCell(6).setCellValue(ConvertStringUtil.ifNullToString(e.get("treatmentProcedureNm")));
            row.createCell(7).setCellValue(ConvertStringUtil.ifNullToString(e.get("reserveMemberName")));
            row.createCell(8).setCellValue(ConvertStringUtil.ifNullToString(e.get("muMemberName")));
            row.createCell(9).setCellValue(ConvertStringUtil.ifNullToString(e.get("etc")));
            row.createCell(10).setCellValue(ConvertStringUtil.ifNullToString(e.get("totalAmount")));
            row.createCell(11).setCellValue(ConvertStringUtil.ifNullToString(e.get("discountAmount")));
            row.createCell(12).setCellValue(ConvertStringUtil.ifNullToString(e.get("membershipPrice")));
            row.createCell(13).setCellValue(ConvertStringUtil.ifNullToString(e.get("nonpaymentAmount")));
            row.createCell(14).setCellValue(ConvertStringUtil.ifNullToString(e.get("paymentAmount")));
            row.createCell(15).setCellValue(ConvertStringUtil.ifNullToString(e.get("amount")));
            row.createCell(16).setCellValue(ConvertStringUtil.ifNullToString(e.get("vat")));
            row.createCell(17).setCellValue(ConvertStringUtil.ifNullToString(e.get("refundAmount")));
            row.createCell(18).setCellValue(ConvertStringUtil.ifNullToString(e.get("status1")));

        }


        //response.setContentType("Application/Msexcel");
        response.setContentType(getContentType());
        Date toDay = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDateTime = dateFormat.format(toDay);
        log.debug("1. 현재날짜                                : "+ nowDateTime);
        response.setHeader("Content-Disposition", "ATTachment; Filename=".concat(new String(filename.getBytes("euc-kr"),"8859_1")).concat("_").concat(nowDateTime).concat(".xlsx"));

        ServletOutputStream out = response.getOutputStream();
        out.flush();
        workbook.write(out);
        out.flush();

    }

    private void createHeaderCell(int col, Row row, CellStyle cellStyle, String value){
        Cell cell = row.createCell(col);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }
}