

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

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

  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) => {
  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: 15, bold: true },
      alignment: { vertical: "middle", horizontal: "center" },
    },
    box: {
      border: all_border,
      alignment: { vertical: "middle", horizontal: "center" },
      font: { size: 11 },

    },


  };




  let ws_row1 = workSheet.getRow(1);
  ws_row1.height = 40;
  ws_row1.getCell(1).value = "검사 실적 조회";
  ws_row1.getCell(1).style = field_css.title
  workSheet.mergeCells('A1:V1');



  let ws_row2 = workSheet.getRow(2);
  ws_row2.height = 25;
  const secondRowColumns = ["생산오더", "품목", "품명", "공순", "공정","검사항목명","검사방법", "기준값","상한치" ,"하한치","측정기","단위"];
  secondRowColumns.forEach((col, index) => {
    let cell = ws_row2.getCell(index + 1);
    cell.value = col;
    cell.style = field_css.blue;
    cell.border = all_border;

    // 두 번째 행과 세 번째 행 병합(행,시작열,끝행,끝열)
    workSheet.mergeCells(2, index + 1, 3, index + 1);
  });

  // 측정치 셀 병합 및 스타일 설정
  workSheet.mergeCells('M2:V2');
  ws_row2.getCell(13).value = "측정치";

  // 병합된 셀 스타일 설정 (H2:Q2)
  for (let i = 8; i <= 22; i++) {
    ws_row2.getCell(i).style = field_css.blue;
    ws_row2.getCell(i).border = all_border;
  }


  // 세 번째 행: 컬럼명 설정
  let ws_row3 = workSheet.getRow(3);
  ws_row3.height = 25;
  const thirdRowColumns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
  thirdRowColumns.forEach((col, index) => {
    let cell = ws_row3.getCell(index + 13);
    cell.value = col;
    cell.style = field_css.blue;
    cell.border = all_border;
  });


  workSheet.columns = [
    { width: 15 },
    { width: 15 },
    { width: 39 },
    { width: 10 },
    { width: 10 },

    { width: 13 },
    { width: 10 },
    { width: 10 },
    { width: 10 },
    { width: 10 },

    { width: 10 },
    { width: 10 },
    { width: 10 },
    { width: 10 },
    { width: 10 },

    { width: 10 },
    { width: 10 },
    { width: 10 },
    { width: 10 },
    { width: 10 },

    { width: 10 },
    { width: 10 },


  ];


  // //첫번째 숫자, 옆에 박스부분 채워놓는 부분
  // for (let i = 0; i < data.length; i++) {
  //   let row = work_Sheet.getRow(i + 4);
  //   row.height = 22;


  //   let numCell = row.getCell(1);
  //   numCell.value = i + 1;
  //   numCell.style = field_css.blue;

  //   //박스
  //   for (let j = 2; j < 18; j++) {
  //     let cell = row.getCell(j);
  //     cell.style = field_css.box;
  //   }
  // }


  // 데이터가 있으면 박스 안에 데이터 들어가는 곳
  if (sheetdata) {
    sheetdata.forEach((data, i) => {
      let ws_row = workSheet.getRow(i + 4);
      ws_row.height = 22;

      ws_row.getCell(1).value = data["WO_NO"] || '';
      ws_row.getCell(2).value = data["ITEM_CD"] || '';
      ws_row.getCell(3).value = data["ITEM_NM"] || '';
      ws_row.getCell(4).value = data["OP_SEQ"] || '';
      ws_row.getCell(5).value = data["OP_CD"] || '';
      ws_row.getCell(6).value = data["INSP_NAME"] || '';
      ws_row.getCell(7).value = data["INSP_METH"] || '';
      ws_row.getCell(8).value = isNaN(parseFloat(data["SPEC_VAL"]).toFixed(2))? '' : parseFloat(data["SPEC_VAL"]).toFixed(2);
      ws_row.getCell(9).value =  isNaN(parseFloat(data["TOL_UPPER"] ).toFixed(0))? '' : parseFloat(data["TOL_UPPER"] ).toFixed(0);
      ws_row.getCell(10).value = isNaN(parseFloat(data["TOL_LOWER"] ).toFixed(0))? '' : parseFloat(data["TOL_LOWER"] ).toFixed(0);
      ws_row.getCell(11).value = data["INSP_TOOL"] || '';
      ws_row.getCell(12).value = data["SPEC_UNIT"] || '';
      ws_row.getCell(13).value = data["T1"] || '';
      ws_row.getCell(14).value = data["T2"] || '';
      ws_row.getCell(15).value = data["T3"] || '';
      ws_row.getCell(16).value = data["T4"] || '';
      ws_row.getCell(17).value = data["T5"] || '';
      ws_row.getCell(18).value = data["T6"] || '';
      ws_row.getCell(19).value = data["T7"] || '';
      ws_row.getCell(20).value = data["T8"] || '';
      ws_row.getCell(21).value = data["T9"] || '';
      ws_row.getCell(22).value = data["T10"] || '';


      for (let j = 1; j <= 22; j++) {
        ws_row.getCell(j).style = field_css.box;
      }
    });
  }


  return workSheet;

  };