<?php
$cate_id = "REG_INSP_QC"; //현재 메뉴 ID 필수!!!
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _SVC_INC_PATH_ . "/auth_check.php";
include_once _HOME_INC_PATH_ . "/ajax_init.php";
$_tmp_dir = _UP_PATH_."/temp";

$_insp_dir = _UP_PATH_."/insp";

if (!_notAuth("R")) {/************ 권한체크*/
  switch ($_REQUEST['mode']) {

    case "getOrderList":

      $squl = "USP_Q_REG_INSP_QC_S10";

      $params = array(
        "P_WO_NO" => $_REQUEST["WO_NO"] ?: "",
        "P_ST_DATE" => $_REQUEST["date_st"],
        "P_IS_ALL" => $_REQUEST["isAll"],
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if ($rst) {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }

      break;


    // 오더별 검사항목 가져오기
    case "getInspList":

      $squl = "USP_Q_REG_INSP_QC_S30";

      $params = array(
        "P_WO_NO" => $_REQUEST["WO_NO"] ?: "",
        "P_OP_CD" => "",
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if ($rst) {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }

      break;
    // 공정별 불량실적 가져오기
    case "getDefList":

      $squl = "USP_Q_REG_INSP_QC_S20";

      $params = array(
        "P_WO_NO" => $_REQUEST["WO_NO"] ?: "",
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if ($rst) {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }

      break;


    case "putInsp":
      if (_notAuth("W"))
        break; /************ 권한체크*/

      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata'],true);

      $rst2 = true;
      $_REQUEST['RS_MSG'] = "";
      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['wholeData'] = json_decode($_REQUEST['wholeData'],true);


      foreach ($_REQUEST['wholeData'] as $whole) {
        $_REQUEST['RS_MSG'] = "";
        $_REQUEST['RS_CODE'] = "";
        if (empty($whole['INSP_RSLT_NO'])) {
          $sql = 'USP_Q_REG_INSP_QC_I20';
          $_REQUEST['INSP_RSLT_NO'] = "";
          $params = array(
            "WO_NO"       => $whole['WO_NO']
          , "WO_SEQ"      => $whole['WO_SEQ']
          , "OKNG_FLG"    => $whole['OKNG_FLG']  // OKNG FLG 추가
          , "INSP_SEQ"    => $whole['INSP_SEQ']
          , "INSP_ITEM"   => $whole['INSP_ITEM']
          , "INSP_TOOL"   => $whole['INSP_TOOL']
          , "INSP_EMP_NO" => $whole['INSP_EMP_NO'] ?? ""
          , "REMARK"      => $whole['REMARK']
          , "T1"          => trim($whole['T1'] ) === "" ? null :  $whole['T1']
          , "T2"          => trim($whole['T2'] ) === "" ? null :  $whole['T2']
          , "T3"          => trim($whole['T3'] ) === "" ? null :  $whole['T3']
          , "T4"          => trim($whole['T4'] ) === "" ? null :  $whole['T4']
          , "T5"          => trim($whole['T5'] ) === "" ? null :  $whole['T5']
          , "T6"          => trim($whole['T6'] ) === "" ? null :  $whole['T6']
          , "T7"          => trim($whole['T7'] ) === "" ? null :  $whole['T7']
          , "T8"          => trim($whole['T8'] ) === "" ? null :  $whole['T8']
          , "T9"          => trim($whole['T9'] ) === "" ? null :  $whole['T9']
          , "T10"         => trim($whole['T10']) === "" ? null : $whole['T10']
          , "T11"         => trim($whole['T11']) === "" ? null : $whole['T11']
          , "T12"         => trim($whole['T12']) === "" ? null : $whole['T12']
          , "T13"         => trim($whole['T13']) === "" ? null : $whole['T13']
          , "T14"         => trim($whole['T14']) === "" ? null : $whole['T14']
          , "T15"         => trim($whole['T15']) === "" ? null : $whole['T15']
          , "T16"         => trim($whole['T16']) === "" ? null : $whole['T16']
          , "T17"         => trim($whole['T17']) === "" ? null : $whole['T17']
          , "T18"         => trim($whole['T18']) === "" ? null : $whole['T18']
          , "T19"         => trim($whole['T19']) === "" ? null : $whole['T19']
          , "T20"         => trim($whole['T20'])  === "" ? null:  $whole['T20']
          , "MAKE_USER_ID"=> $GLOBALS['_Umem']['member_id']
          , "INSP_RSLT_NO"=> array(&$_REQUEST['INSP_RSLT_NO'], SQLSRV_PARAM_OUT)
          , "RS_CODE" => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT)
          , "RS_MSG" => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          );
          $rst = $baseObj->DB->exec($sql, $params);

        }else{
          $sql = 'USP_Q_REG_INSP_QC_U20';

          $params = array(
              "INSP_RSLT_NO"=> $whole['INSP_RSLT_NO']
            , "WO_NO"       => $whole['WO_NO']
            , "WO_SEQ"      => $whole['WO_SEQ']
            , "OKNG_FLG"    => $whole['OKNG_FLG']  // OKNG FLG 추가
            , "INSP_SEQ"    => $whole['INSP_SEQ']
            , "INSP_ITEM"   => $whole['INSP_ITEM']
            , "INSP_TOOL"   => $whole['INSP_TOOL']
            , "INSP_EMP_NO" => $whole['INSP_EMP_NO'] ?? ""
            , "REMARK"      => $whole['REMARK']
            , "T1"          => $whole['T1']  === "" ? null :  $whole['T1']
            , "T2"          => $whole['T2']  === "" ? null :  $whole['T2']
            , "T3"          => $whole['T3']  === "" ? null :  $whole['T3']
            , "T4"          => $whole['T4']  === "" ? null :  $whole['T4']
            , "T5"          => $whole['T5']  === "" ? null :  $whole['T5']
            , "T6"          => $whole['T6']  === "" ? null :  $whole['T6']
            , "T7"          => $whole['T7']  === "" ? null :  $whole['T7']
            , "T8"          => $whole['T8']  === "" ? null :  $whole['T8']
            , "T9"          => $whole['T9']  === "" ? null :  $whole['T9']
            , "T10"         => $whole['T10'] === "" ? null : $whole['T10']
            , "T11"         => $whole['T11'] === "" ? null : $whole['T11']
            , "T12"         => $whole['T12'] === "" ? null : $whole['T12']
            , "T13"         => $whole['T13'] === "" ? null : $whole['T13']
            , "T14"         => $whole['T14'] === "" ? null : $whole['T14']
            , "T15"         => $whole['T15'] === "" ? null : $whole['T15']
            , "T16"         => $whole['T16'] === "" ? null : $whole['T16']
            , "T17"         => $whole['T17'] === "" ? null : $whole['T17']
            , "T18"         => $whole['T18'] === "" ? null : $whole['T18']
            , "T19"         => $whole['T19'] === "" ? null : $whole['T19']
            , "T20"         => $whole['T20']  === "" ? null:  $whole['T20']
            , "MAKE_USER_ID"=> $GLOBALS['_Umem']['member_id']
            , "RS_CODE" => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT)
            , "RS_MSG" => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          );
          $rst = $baseObj->DB->exec($sql, $params);



        }
      }
      if ($rst && !$_REQUEST['RS_MSG'] ) {
        $retval['code'] = true;
      } else {
        $retval['code'] = false;
        $retval['mesg'] = $_REQUEST['RS_MSG'];
      }
      break;

    case "setSeq":
      if (_notAuth("W"))
        break; /************ 권한체크*/

      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata']);
      $rst = true;
      $ok_cd = array();
      // foreach ($_REQUEST['reqdata'] as $data) {
      // $data = (array) $data;
      $sql = "USP_Q_REG_INSP_QC_I10";
      $_REQUEST['INSP_RSLT_NO'] = '';
      $params = array(
        "WO_NO" => $_REQUEST["WO_NO"],
        "WO_SEQ" => $_REQUEST["WO_SEQ"],
        "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'],
        "INSP_RSLT_NO" => array(&$_REQUEST['INSP_RSLT_NO'], SQLSRV_PARAM_OUT)
      );

      $rst = $baseObj->DB->exec($sql, $params);

    case "regPrd":
      if (_notAuth("W"))
        break; /************ 권한체크*/

       $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata']);
       $rst = true;

       $_REQUEST['RS_CODE'] = '';
       foreach ($_REQUEST['reqdata'] as $data) {
         $data = (array)$data;
         if ($data['_STATUS_'] == 'U') {
          $sql = "USP_Q_REG_INSP_QC_I10";

          $_REQUEST['RS_CODE'] = '';
          $params = array(
            "WO_NO"       => $data["WO_NO"],
            "WO_SEQ"      => $data["WO_SEQ"],
            "AFT_QTY"     => intval($data["PRD_QTY"]),
            "OP_STATUS"   => $data["OP_STATUS"],
            "WRK_EMP_NO"  => $GLOBALS['_Umem']['member_id'],
            "MAKE_USER_ID"=> $GLOBALS['_Umem']['member_id'],
            "RS_CODE"=> array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),

          );

          $rst = $baseObj->DB->exec($sql, $params);
        }
         if ($rst) {
           $retval['code'] = true;
           
         } else {
           $retval['code'] = false;
         }
       }
      break;

    case "getInspFile":
      $sql = "USP_P_INSP_FILE_S10";

      $params = array(
        "WO_NO"     => $_REQUEST["WO_NO"] ?: "",
        "WO_SEQ"    => $_REQUEST["WO_SEQ"]?: "",
        "INSP_ITEM" => $_REQUEST["INSP_ITEM"]?: "",
      );
      $rst = $baseObj->DB->exec($sql, $params);

      $rows = array();
      if ($rst) {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }

      break;
    case "putTempFile":

      if( isset($_FILES["upfile"]) ){
        $fdata['file_name']   = $_FILES['upfile']['name']?:"";
        $fdata['file_size']   = $_FILES['upfile']['size']?:0;
        $fdata['file_type']   = $_FILES['upfile']['type']?:"";
        $fdata['file_width']  = $_FILES['upfile']['width']?:"";
        $fdata['file_height'] = $_FILES['upfile']['height']?:"";

        $fdata['tmp_name'] = \Frody\Util\MultiFileUpload::swfFormSaveFile($_FILES["upfile"], $_tmp_dir, $_REQUEST['temp_fid']);

        $retval['code'] = 1;
        $retval['data'] = $fdata;

        \Frody\Util\MultiFileUpload::swfCleanDir($_tmp_dir);
      }
      break;

    case "putInspFile":


      $_REQUEST['RS_CODE']="";
      $_REQUEST['RS_MSG']="";
      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata'], 1);

      $rst = 1;
      $upfiles = array();
      foreach( $_REQUEST['reqdata'] as $data ){
        if(file_exists($_tmp_dir."/{$data['TMP_NAME']}") ){
          $upfiles = \Frody\Util\MultiFileUpload::swfFormFileInfo($data['TMP_NAME'], $_tmp_dir);
        }

        if( $rst ){

            $_REQUEST['RS_CODE']="";
            $_REQUEST['RS_MSG']="";

            $sid = uniqid();


            switch( $data['_STATUS_'] ){
              case "I":
                if( $upfiles ){

                  $insp_sql = "USP_P_INSP_FILE_I10";

                  $insp_params = Array(
                    "WO_NO"          => $data['WO_NO'] ?:"",
                    "WO_SEQ"         => $data['WO_SEQ'] ?:"",
                    "FILE_ID"        => $sid?:"",
                    "FILE_NM"        => $data['FILE_NM'] ?:"",
                    "OP_CD"          => $data['OP_CD'] ?:"",
                    "INSP_ITEM"      => $data['INSP_ITEM'] ?:"",
                    "INSP_NAME"      => $data['INSP_NAME'] ?:"",
                    "REMARK"         => $data['REMARK'] ?:"",
                    "MAKE_USER_ID"   => $GLOBALS['_Umem']['member_id']?:"",
                  );

                  $fileId = $baseObj->DB->result($baseObj->DB->exec($insp_sql, $insp_params),0,0);

                  if($fileId){

                    \Frody\Util\MultiFileUpload::swfMoveUploadedFile($upfiles[0]['tmp_name'], "{$_insp_dir}/{$data['WO_NO']}_{$data['WO_SEQ']}_{$fileId}.dat");

                  }


                  if( $rst ){
                    $retval['code'] = $_REQUEST['RS_CODE'];
                  }

                }
                break;

              case "U":
                $insp_sql = "USP_P_INSP_FILE_U10";

                $insp_params = Array(
                  "FILE_ID"        => $data['FILE_ID']?:"",
                  "REMARK"         => $data['REMARK']?:"",

                );

                $rst =  $baseObj->DB->exec($insp_sql, $insp_params);
                break;

              case "D":
                $insp_sql = "USP_P_INSP_FILE_D10";

                $insp_params = Array(
                  "FILE_ID"        => $data['FILE_ID']?:"",
                );
                $rst =  $baseObj->DB->exec($insp_sql, $insp_params);

                if( $rst ){
                  @unlink("{$_insp_dir}/{$data['WO_NO']}_{$data['WO_SEQ']}_{$data['FILE_ID']}.dat");
                }
                break;
          }
        }
      }

      $retval['code'] = true;
      break;

  }
}

if (!$_REQUEST['no_echo']) {
  echo json_encode($retval);
}