class Tb_insp_detail_class extends R_TbClass{
  constructor(container){
    super(container);
    this.getOption = {
      url: "./mng_insp_oper.ajax.php",
      data: {
        mode: "getInsp"
      }
    }

    this.fields = [
      { field:"OKNG_FLG", formatter: "rownum", title: "NO", width: 40 },
      { field: "WO_NO",       title: "생산오더", width: 70 },
      { field: "ITEM_CD",     title: "품목", width: 70 },
      { field: "ITEM_NM",     title: "품명", width: 100, minWidth: 40 },
      { field: "OP_SEQ",      title: "공순", width: 40 },
      { field: "OP_CD",       title: "공정", width: 40 },
      { field: "INSP_EMP_NO", title: "검사자", width: 70 },
      { field: "ORD_DT",      title: "지시 일자", width: 70,  formatter:"datetime", formatterParams:{
          inputFormat:"yy-MM-dd",
          outputFormat:"yy.MM.dd",
        }
      },
      { field: "INSP_DT",   title: "검사일자", width: 100 },
      { field: "INSP_ITEM",   title: "검사항목", width: 100, visible:false },
      { field: "INSP_NAME",   title: "검사항목명", width: 70 },
      { field: "INSP_METH",   title: "검사방법", width: 80 ,formatter: tb_common_funtions.formatterSelect, formatterParams: {values: varsINSP_METH,}},
      { field: "INSP_SEQ",    title: "검사회차", width: 70, visible: false },
      { field: "SPEC_VAL",    title: "기준값<br>(mm)", width: 70, hozAlign:"right",
        formatter: "money",
        formatterParams: {
          decimal: ".",
          precision: "2",
        },
      },
      { field: "TOL_UPPER", title: "상한치<br>(㎛)", width: 80, hozAlign:"right",
        formatter: "money",
        formatterParams: {
          decimal: ".",
          precision: "0",
        },
      },
      { field: "TOL_LOWER", title: "하한치<br>(㎛)", width: 80, hozAlign:"right",
        formatter: "money",
        formatterParams: {
          decimal: ".",
          precision: "0",
        },
      },
      { field: "INSP_TOOL", title: "측정기", width: 80,
        formatter: tb_common_funtions.formatterSelect, 
        formatterParams: {values: varsINSP_TOOL,},
      },
      {
        field: "FILE_MANAGE",
        title: "파일관리",
        width: 70,
        formatter: this.popIcon,
        cellClick:(e,cell)=>{
          this.openModal(cell);
        }
      },
      {
        field: "INSP_TOOL",
        title: "측정치(㎛)",
        cssClass: "width-800",
        columns: this.getInspValue(),
      },
    ];

    this.tbOption.columns = this.fields;

    this.init();
    this.setEvent();
  }

  // 조회시에 필요한 데이터 셀렉
  preprocess(){
    let arr = ['WO_NO','date_st','date_ed','ITEM_CD','INSP_FLG'];
    let data = new Object();

    for(let i=0; i < arr.length; i++){
      let temp = arr[i];
      if(document.getElementById(arr[i]).type == "checkbox"){
        data[temp] = document.getElementById(temp).checked?"Y":"N";
      }else{
        data[temp] = document.getElementById(temp).value
      }
    }
    // this.getOption['data'] = {...this.getOption['data'], ...data};
    return data;
  }

  //팝업 아이콘
  popIcon = function(cell, formatterParams, onRendered) { //plain text value
    return "<button class='reverse-btn fa-solid fa-paperclip padding-1-12'></button>";
  };

  // 파일 다운로드 모달
  openModal(cell) {

    let clickData = cell.getData();

    vnLoad("", "../reg_insp_qc/reg_insp_qc_pop.inc.php", "", (loadHtml) => {
      this.putPartnerModal = kuls_modal(loadHtml);


      const popFileUpload = new Tb_file_upload_class("#popup_file_upload",clickData);
      popFileUpload.init_insp();

    });
  }

  // 측정치 20개 생성
  getInspValue() {
    const datefields = [];
    for (let i = 1; i <= 20; i++) {
      // 데이터 형태에 따라서 field값 및 title값 변경가능
      datefields.push({
        field: `T${i}`,
        title: i,
        width: 50,
        editor:"input",
        editable: (cell)=>{
          return i<=this.qty
        },
        formatter:tb_custom_formatter.checkInspValue,
        formatterParams:{ checkPoint : 'OKNG_FLG' },
        hozAlign:"right"
      });
    }
    return datefields;
  }
}

const Tb_insp_detail = new Tb_insp_detail_class("#sub_tab_2");
Tb_insp_detail.init();
