

const getExcel = async (filename, title, data,baseData) => {
  if (typeof ExcelJS === "undefined") return false;
  filename = filename || "noname.xlsx";
  title = title || "제목을 입력하지 않았습니다.";
  filename = filename.replace(/[\*\?\:\\\/\[\]]/, "");

  const workBook = new ExcelJS.Workbook();
  getSheet(workBook, title, data,baseData);

  const workSheet = workBook.getWorksheet(title);

   const logoImgBase64 = "[encoded data, 35904 characters]";

   const logoImg = workBook.addImage({
    base64: logoImgBase64, // 이미지 데이터 base64를 여기 넣습니다.
    extension: "png",
  });

  
   workSheet.addImage(logoImg, {
    tl: { col: 0, row: 0 },
    ext: { width: 150, height: 60 }
  });
  
 





  workBook.xlsx.writeBuffer().then((buffer) => {
    let blob = new Blob([buffer], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    });
    let url = window.URL.createObjectURL(blob);
    let anchor = document.createElement("a");
    anchor.href = url;
    anchor.download = filename;
    anchor.click();
    window.URL.revokeObjectURL(url);
   });
  };

  // 각 행의 셀들을 배열로 반환하는 함수
  const getApplyRow = (row) => {
    let aCellArr = [];
    for (let i = 0; i < 9; i++) {
      aCellArr.push(row.getCell(i + 1));
    }
    return aCellArr;
  };


 const getSheet = (wbook, sheetname, sheetdata,baseData) => {
  const workSheet = wbook.addWorksheet(sheetname, {
    pageSetup: {
      paperSize: 9,
      fitToPage: true,
      horizontalCentered: true,
      margins: {
        left: 0.28,
        right: 0.28,
        top: 0.88,
        bottom: 0.64,
        header: 0.48,
        footer: 0.52,
      },
      orientation: "landscape",
    },
    properties: { defaultRowHeight: 18 },
  });


  const all_border = {
    top: { style: "thin", color: { argb: "FF000000" } },
    left: { style: "thin", color: { argb: "FF000000" } },
    bottom: { style: "thin", color: { argb: "FF000000" } },
    right: { style: "thin", color: { argb: "FF000000" } },
  };
  const field_css = {
    yellow: {
      border: all_border,
      font: { color: { argb: "FF9C5700" }, bold: true },
      alignment: { vertical: "middle", horizontal: "center" },
      fill: {
        type: "pattern",
        pattern: "solid",
        fgColor: { argb: "FFFFEB9C" },
      },
    },
    green: {
      border: all_border,
      font: { color: { argb: "FF006100" }, bold: true },
      alignment: { vertical: "middle", horizontal: "center" },
      fill: {
        type: "pattern",
        pattern: "solid",
        fgColor: { argb: "FFC6EFCE" },
      },
    },
    red: {
      border: all_border,
      font: { color: { argb: "FF9C0006" }, bold: true },
      alignment: { vertical: "middle", horizontal: "center" },
      fill: {
        type: "pattern",
        pattern: "solid",
        fgColor: { argb: "FFFFC7CE" },
      },

    },
    blue: {
      border: all_border,
      font: { font: { size: 11 }, color: { argb: "0E3979" }, bold: true },
      alignment: { vertical: "middle", horizontal: "center" },
      fill: {
        type: "pattern",
        pattern: "solid",
        fgColor: { argb: "EAEFF3" },

      }
    },
    title: {
      font: { size: 30, bold: true },
      alignment: { vertical: "middle", horizontal: "center" },
    },
    box: {
      border: all_border,
      alignment: { vertical: "middle", horizontal: "center" },
      font: { size: 11 },

    },
    name: {
      border: all_border,
      alignment: { vertical: "middle", horizontal: "right" },
      font: { size: 13 },

    },


  };


  
  // 두 번째 행 설정
  let ws_row2 = workSheet.getRow(2);
  ws_row2.height = 60;
  ws_row2.getCell(1).style = field_css.title;
  ws_row2.getCell(22).border = all_border;
  ws_row2.getCell(23).border = all_border;
  ws_row2.getCell(24).border = all_border;
  // 병합된 셀에 텍스트 설정
  workSheet.mergeCells('A1:Q2');
  let mergedCell = workSheet.getCell('A1');
  mergedCell.value = "검사기준/성적서";
  mergedCell.style = field_css.title;
  
  // 결재 셀 병합 및 설정
  // workSheet.mergeCells('K1:K2');

  // ws_row2.getCell(11).border = all_border;
  // ws_row2.getCell(12).border = all_border;
  // ws_row2.getCell(13).border = all_border;
  // ws_row2.getCell(14).border = all_border;
  
  
  let ws_row1_2 = workSheet.getRow(1);
  ws_row1_2.getCell(18).value = "결재";
  ws_row1_2.getCell(18).style = field_css.blue;

  workSheet.mergeCells('R1:S4');
  // 첫 번째 행 설정
  let ws_row1 = workSheet.getRow(1);
  ws_row1.height = 20;
  ws_row1.getCell(20).value = "작성";
  ws_row1.getCell(20).style = field_css.blue;

  ws_row1.getCell(22).value = "검토";
  ws_row1.getCell(22).style = field_css.blue;
  ws_row1.getCell(24).value = "승인";
  ws_row1.getCell(24).style = field_css.blue;
  
  // 스타일 및 테두리 설정
  [12, 13, 14].forEach(cellNum => {
      ws_row1_2.getCell(cellNum).style = field_css.blue;
      ws_row1_2.getCell(cellNum).border = all_border;
      ws_row1.getCell(cellNum).style = field_css.blue;
      ws_row1.getCell(cellNum).border = all_border;
  });

  let ws_row3 = workSheet.getRow(3);
  ws_row3.height = 40;
  ws_row3.getCell(1).value = "품번:";
  ws_row3.getCell(1).style = field_css.name;
  
  ws_row3.getCell(2).value = baseData.ITEM_CD;
  ws_row3.getCell(2).style = field_css.name;

  workSheet.mergeCells('B3:E3');
  for (let col = 2; col <= 4; col++) {
    ws_row3.getCell(col).border = all_border;
  }
  
  ws_row3.getCell(6).value = "생산오더:";
  ws_row3.getCell(6).style = field_css.name;
  
  ws_row3.getCell(8).value = baseData.WO_NO;
  ws_row3.getCell(8).style = field_css.name;

  workSheet.mergeCells('F3:G3');
  workSheet.mergeCells('H3:K3');

  for (let col = 6; col <= 7; col++) {
    ws_row3.getCell(col).border = all_border;
  }
  
  ws_row3.getCell(12).value = "검사일자:";
  ws_row3.getCell(12).style = field_css.name;

  let dt = new Date();

  ws_row3.getCell(14).value = dt.getFullYear() +"-" +dt.getMonth()+"-"+dt.getDay()
  ws_row3.getCell(14).style = field_css.name;
  workSheet.mergeCells('L3:M3');
  workSheet.mergeCells('N3:Q3');
  for (let col = 9; col <= 11; col++) {
    ws_row3.getCell(col).border = all_border;
  }
  
  ws_row3.getCell(20).value = "검사Lot수";
  ws_row3.getCell(20).style = field_css.blue;

  workSheet.mergeCells('T1:U1');
  workSheet.mergeCells('T2:U2');
  workSheet.mergeCells('T3:U3');
  workSheet.mergeCells('T4:U4');

  ws_row3.getCell(22).value = "합격수";
  ws_row3.getCell(22).style = field_css.blue;

  workSheet.mergeCells('V1:W1');
  workSheet.mergeCells('V2:W2');
  workSheet.mergeCells('V3:W3');
  workSheet.mergeCells('V4:W4');

  ws_row3.getCell(24).value = "불합격수";
  ws_row3.getCell(24).style = field_css.blue;

  workSheet.mergeCells('X1:Y1');
  workSheet.mergeCells('X2:Y2');
  workSheet.mergeCells('X3:Y3');
  workSheet.mergeCells('X4:Y4');

  let ws_row4 = workSheet.getRow(4);
  ws_row4.height = 40;
  ws_row4.getCell(1).value = "품명:";
  ws_row4.getCell(1).style = field_css.name;
  
  ws_row4.getCell(2).value = baseData.ITEM_NM;
  ws_row4.getCell(2).style = field_css.name;
  ws_row4.getCell(22).border = all_border;
  ws_row4.getCell(23).border = all_border;
  ws_row4.getCell(24).border = all_border;
  workSheet.mergeCells('B4:E4');
  for (let col = 2; col <= 4; col++) {
    ws_row4.getCell(col).border = all_border;
  }
  
  ws_row4.getCell(6).style = field_css.name;
  ws_row4.getCell(6).value = "고객사명:";

  ws_row4.getCell(8).value = baseData.BP_NM;
  ws_row4.getCell(8).style = field_css.name;

  workSheet.mergeCells('F4:G4');
  workSheet.mergeCells('H4:K4');
  for (let col = 6; col <= 7; col++) {
    ws_row4.getCell(col).border = all_border;
  }
  
  ws_row4.getCell(12).value = "검사자:";
  ws_row4.getCell(12).style = field_css.name;
  
  ws_row4.getCell(14).value = baseData.EMP_NM;

  ws_row4.getCell(14).style = field_css.name;
  workSheet.mergeCells('L4:M4');
  workSheet.mergeCells('N4:Q4');
  for (let col = 9; col <= 11; col++) {
    ws_row4.getCell(col).border = all_border;
  }
  
  ws_row4.getCell(12).border = all_border;
  ws_row4.getCell(13).border = all_border;
  ws_row4.getCell(14).border = all_border;
  
  let ws_row5 = workSheet.getRow(5);
  ws_row5.height = 25;
  ws_row5.getCell(1).style = field_css.blue;
  ws_row5.getCell(1).value = "공정순서";


  ws_row5.getCell(2).style = field_css.blue;
  ws_row5.getCell(2).value = "측정기";
  // workSheet.mergeCells('B5:B6');


  workSheet.mergeCells('B5:B6');
  ws_row5.getCell(3).style = field_css.blue;
  ws_row5.getCell(3).value = "기준치";
  // ws_row5.getCell(3).style = field_css.name;
  workSheet.mergeCells('C5:C6');
  ws_row5.getCell(4).style = field_css.blue;
  ws_row5.getCell(4).value = "상한치";


  ws_row5.getCell(5).style = field_css.blue;
  ws_row5.getCell(5).value = "시료수";

  workSheet.mergeCells('E5:E6');
  
  let ws_row6 = workSheet.getRow(6);
  ws_row6.height = 25;
  ws_row6.getCell(1).style = field_css.blue;
  ws_row6.getCell(1).value = "검사항목명";
  ws_row6.getCell(4).style = field_css.blue;
  ws_row6.getCell(4).value = "하한치";
  
  // 측정치 셀 병합 및 스타일 설정
  workSheet.mergeCells('F5:Y5');
  ws_row5.getCell(16).value = "측정치";
  
  // 병합된 셀 스타일 설정 (H2:Q2)
  for (let i = 6; i <= 25; i++) {
    ws_row5.getCell(i).style = field_css.blue;
    ws_row5.getCell(i).border = all_border;
  }
  
  // 세 번째 행: 컬럼명 설정
  ws_row6.height = 25;
  const thirdRowColumns = ["시료1", "시료2", "시료3", "시료4", "시료5", "시료6", "시료7", "시료8", "시료9","시료10","시료11","시료12","시료13","시료14","시료15","시료16","시료17","시료18","시료19","시료20",];
  thirdRowColumns.forEach((col, index) => {
    let cell = ws_row6.getCell(index + 6);
    cell.value = col;
    cell.style = field_css.blue;
    cell.border = all_border;
  });
  
  workSheet.columns = [
    { width: 12 },
    { width: 12 },
    { width: 12 },
    { width: 12 },
    { width: 12 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },
    { width: 8 },

  ];
  
  if (sheetdata) {
    sheetdata.forEach((data, i) => {
      let headerRow = workSheet.getRow(i * 2 + 7);
      let dataRow = workSheet.getRow(i * 2 + 8);
  
      headerRow.height = 22;
      dataRow.height = 22;
  
      // 첫 번째 줄 데이터 (이름)
      headerRow.getCell(1).value = data["OP_SEQ"] || '';
      headerRow.getCell(2).value = data["INSP_TOOL"] || '';
      headerRow.getCell(3).value = parseFloat(data["SPEC_VAL"]).toFixed(2) || '';
      headerRow.getCell(4).value = data["TOL_UPPER"] || '';
      headerRow.getCell(5).value = data[""] || ''; // 시료수
      headerRow.getCell(6).value = data["T1"] || '';
      headerRow.getCell(7).value = data["T2"] || '';
      headerRow.getCell(8).value = data["T3"] || '';
      headerRow.getCell(9).value = data["T4"] || '';
      headerRow.getCell(10).value = data["T5"] || '';
      headerRow.getCell(11).value = data["T6"] || '';
      headerRow.getCell(12).value = data["T7"] || '';
      headerRow.getCell(13).value = data["T8"] || '';
      headerRow.getCell(14).value = data["T9"] || '';
      headerRow.getCell(15).value = data["T10"] || '';
      headerRow.getCell(16).value = data["T11"] || '';
      headerRow.getCell(17).value = data["T12"] || '';
      headerRow.getCell(18).value = data["T13"] || '';
      headerRow.getCell(19).value = data["T14"] || '';
      headerRow.getCell(20).value = data["T15"] || '';
      headerRow.getCell(21).value = data["T16"] || '';
      headerRow.getCell(22).value = data["T17"] || '';
      headerRow.getCell(23).value = data["T18"] || '';
      headerRow.getCell(24).value = data["T19"] || '';
      headerRow.getCell(25).value = data["T20"] || '';


  
      // 두 번째 줄 데이터 (값)
      dataRow.getCell(1).value = data["INSP_NAME"] || '';
      dataRow.getCell(2).value = data[""] || ''; // AQL
      dataRow.getCell(3).value = data[""] || '';  
      dataRow.getCell(4).value = data["TOL_LOWER"] || '';
  
      // 스타일 적용
      for (let j = 1; j <= 25; j++) {
        headerRow.getCell(j).style = field_css.box;
        dataRow.getCell(j).style = field_css.box;
      }
  
      const cellsToMerge = [
          'B','C', 'E', 'F', 'G', 'H',
          'I', 'J', 'K', 'L', 'M',
          'N', 'O', 'P', 'Q', 'R',
          'S', 'T', 'U', 'V', 'W',
          'X', 'Y'
      ];
  
      cellsToMerge.forEach(cell => {
        workSheet.mergeCells(`${cell}${i * 2 + 7}:${cell}${i * 2 + 8}`);
      });
  
      const cellsWithBorder = [3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
  
      cellsWithBorder.forEach(cell => {
        headerRow.getCell(cell).border = all_border;
        dataRow.getCell(cell).border = all_border;
      });
    });
  }
  

  //결재라인

  // workSheet.addImage(wbook.logoImg, {
  //   tl : { col: 1, row: 1 },
  //   ext: { width: 70, height: 63 }
  //  });

  // //로고
  // workSheet.addImage(workSheet.signImg, {
  //   tl : { col: 6.05, row: 2.15 },
  //   ext: { width: 70, height: 63 }
  //  });

  return workSheet;

  };