/***********************************************************************************
 * @@ Program Name : DynaReportRegister.java Description : 동적 보고 양식 Author : 강원중 Create Date : 2004-11-03 History :
 * @@
 ***********************************************************************************/
package kr.co.kihyun.beans.totsys.report;

import javax.servlet.http.HttpServletRequest;
import kr.co.kihyun.beans.entity.TotDoc;

import kr.co.kihyun.lang.Encoder;
import kr.co.kihyun.lang.MInteger;
import kr.co.kihyun.moumi.report.MReportData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DynaReportRegister extends ReportRegistFormElement implements ReportRegister {

	private static Logger LOG = LoggerFactory.getLogger(DynaReportRegister.class);
	private StringBuilder addJSBuf;
	private StringBuilder inDataJSBuf;
	private StringBuilder hiddenDataBuf;
	private StringBuilder insertInputCellName;
	private String tableRows = "";

	public DynaReportRegister(HttpServletRequest req, TotDoc totDoc, int rowCount) {
		super(totDoc, rowCount);
		setInsertForm(req);
	}

	public DynaReportRegister(HttpServletRequest req, TotDoc totDoc, int rowCount, String tableRows) {
		super(totDoc, rowCount, tableRows);
		this.tableRows = tableRows;
		setInsertForm(req);
	}

	/**
	 * @param req
	 */
	private void setInsertForm(HttpServletRequest req) {
		int delRowNum = MInteger.parseInt(req.getParameter("delRowNum"), 1);
		String modifyMode = req.getParameter("modifyMode");
		String tables = req.getParameter("tables");
		int addRowNum = MInteger.parseInt(req.getParameter("addRowNum"), 0);

		/************ 보고서 보고 및 작성 보기 ****************/
		addJSBuf = new StringBuilder(1024);
		inDataJSBuf = new StringBuilder(1024);
		hiddenDataBuf = new StringBuilder(1024);
		insertInputCellName = new StringBuilder(1024);

		int nameIndex = 0;
		String[] inData = new String[this.getSize()];

		/* 라인 삭제 수정해야 함. */
		if (modifyMode != null && modifyMode.equals("delRow")) {
			delRowNum = delRowNum - 1; // 배열은 0부터 시작하니까 1삭제

			StringBuilder tmpRows = new StringBuilder(1024);
			String tmpRowsList[] = tableRows.split(",");

			tmpRowsList[(Integer.parseInt(tables) - 1)] = Integer.toString(Integer.parseInt(tmpRowsList[(Integer.parseInt(tables) - 1)]) + 1);

			for (int tmpR = 0; tmpR < tmpRowsList.length; tmpR++) {
				if (tmpR == 0) {
					tmpRows.append(tmpRowsList[tmpR]);
				} else {
					tmpRows.append(",");
					tmpRows.append(tmpRowsList[tmpR]);
				}
			}

			this.setForm(getTotDoc(), getRowCount(), tmpRows.toString());

			int idx = 0;
			int here = 0;
			for (int i = 0; i < getTableRowList().length; i++) {
				for (int stp = 0; stp < Integer.parseInt(getTableRowList()[i]); stp++) {
					for (int p = here; p < (here + getTableColNumList()[i]); p++) {
                                                
                                            //42.비어있는 if문 : Update by YOUNGJUN,CHO
                                            //if (stp == delRowNum && (Integer.parseInt(tables) - 1) == i) {
                                            //} else {
                                            //    inData[idx] = Encoder.toJava(req.getParameter(this.getInItemNameList()[nameIndex]));
                                            //    idx++;
                                            //}
                                            
                                            if (stp != delRowNum || (Integer.parseInt(tables) - 1) != i) {
                                                inData[idx] = Encoder.toJava(req.getParameter(this.getInItemNameList()[nameIndex]));
                                                idx++;
                                            }
                                            //================================================
                                            
                                            nameIndex++;
					}
				}
				here = here + getTableColNumList()[i];
			}

			this.setForm(getTotDoc(), getRowCount(), tableRows);

		} else if (modifyMode != null && modifyMode.equals("addRow")) {
			int idx = 0;
			int here = 0;
			for (int i = 0; i < getTableRowList().length; i++) {
				for (int stp = 0; stp < Integer.parseInt(getTableRowList()[i]); stp++) {
					for (int p = here; p < (here + getTableColNumList()[i]); p++) {
						if ((Integer.parseInt(tables) - 1) == i) {
							if (stp < (Integer.parseInt(getTableRowList()[i]) - addRowNum)) {
								inData[idx] = Encoder.toJava(req.getParameter(this.getInItemNameList()[nameIndex]));
							} else {
								inData[idx] = "";
							}
						} else {
							if ((Integer.parseInt(tables) - 1) < i) {
								String inx = this.getInItemNameList()[nameIndex];
								String inxA = "";
								int inxN = 0;
								int inxD = 0;
								for (int pi = 0; pi < inx.length(); pi++) {
									if (inx.charAt(pi) >= '0' && inx.charAt(pi) <= '9') {
										inxD = pi;
										break;
									}
								}
								inxA = inx.substring(0, inxD);
								inxN = Integer.parseInt(inx.substring(inxD)) - addRowNum;
								inx = inxA + Integer.toString(inxN);

								inData[idx] = Encoder.toJava(req.getParameter(inx));
							} else {
								inData[idx] = Encoder.toJava(req.getParameter(this.getInItemNameList()[nameIndex]));
							}
						}
						idx++;
						nameIndex++;
					}
				}
				here = here + getTableColNumList()[i];
			}
		} else {


			for (int r = 0; r < this.getSize(); r++) {
				inData[r] = Encoder.toJava(req.getParameter(this.getInItemNameList()[nameIndex]));
				nameIndex++;

			}
		}

		// 입력셀이 숫자이고 입력된 값이 없거나 숫자가 아닌경우 0으로 치환함
		inData = MReportData.getReportData(inData, this.getItemList().getTypeList(), this.getTableColNumList(), this.getTableRowList());

		int index = 0;
		for (int c = 0; c < this.getSize(); c++)
        {

			inData[c] = inData[c].replaceAll("'", "`");
			inData[c] = inData[c].replaceAll("\r\n", "\\\\n");
			inDataJSBuf.append("dform.").append(getInItemNameList()[index]).append(".value='").append(inData[c]).append("';\n");
			addJSBuf.append("modifyForm.").append(getInItemNameList()[index]).append(".value=dform.").append(getInItemNameList()[index]).append(".value;\n");
			hiddenDataBuf.append("<input type='hidden' name='").append(getInItemNameList()[index]).append("' value=''>\n");
			if (c == 0) {
				insertInputCellName.append(getInItemNameList()[index]);
			} else {
				insertInputCellName.append(",").append(getInItemNameList()[index]);
			}

			index++;
		}

	}

	public String getInsertForm() {
		//LOG.debug("inFormBuf: {}", getInFormBuf().toString());
		return getInFormBuf().toString();
	}

	public String getAddJavaScript() {
		return addJSBuf.toString();
	}

	public String getInDataJavaScript() {
		return inDataJSBuf.toString();
	}

	public String getHiddenData() {
		return hiddenDataBuf.toString();
	}

	public String getInsertInputCellName() {
		return insertInputCellName.toString();
	}

        public void initDes()
        {
          addJSBuf=inDataJSBuf=hiddenDataBuf=insertInputCellName=null;
        }
}
