<?php
  error_reporting(0);
  header('Access-Control-Allow-Origin: *');
  include '_connect.php';
  $BILL_NO = $_REQUEST['BILL_NO'];
  $CT_NO = $_REQUEST['CT_NO'];
  $PS_ID = $_REQUEST['PS_ID'];
  $IMG_NO = $_REQUEST['IMG_NO'];
  $IMAGES = $_REQUEST['IMAGES'];
  $IMG_DATE = date('Y-m-d');

  echo $IMAGES;

  // $file_tmp = $IMAGES1;
  // // เปลี่ยนรูปภาพ Base64
  // $bin_string = file_get_contents($_FILES["IMAGES"]["name"]);
  // $hex_string = base64_encode($bin_string);
  // // $type = pathinfo($file_tmp, PATHINFO_EXTENSION);
  // // $data = file_get_contents($file_tmp);
  // $IMAGES = 'data:image/'.$type.';base64,'.base64_encode($hex_string);
  // echo $IMAGES;

  // $IMG_NO = $_POST['IMG_NO'];
  // $errors=array();
  // $allowed_ext = array('jpg','jpeg','png','gif');
  // $file_name = $_FILES['IMAGES']['tmp_name'];
  // // $file_name = $_FILES['image']['tmp_name'];
  // $file_ext = @end(explode('.', $file_name));
  //
  // $file_size = $_FILES['IMAGES']['size'];
  // $file_tmp = $_FILES['IMAGES']['tmp_name'];
  // // echo $file_tmp;
  //
  // $type = pathinfo($file_tmp, PATHINFO_EXTENSION);
  // $data = file_get_contents($file_tmp);
  // $IMAGES = 'data:image/'.$type.';base64,'.base64_encode($data);
  // echo $IMAGES."<br>";



  // รูปภาพ ลูกค้า
  if ($PS_ID != "") {
    $sql = "SELECT * from CONFIG.dbo.IMG64_APPROVE
    WHERE PS_ID = '$PS_ID' AND IMG_NO = '$IMG_NO' ";
    $select=$conn->prepare($sql);
    $select->execute();
    $fetch=$select->fetch();
    if ($fetch['PS_ID'] == $PS_ID) {
      // echo "ข้อมูลตรง ทำการแก้ไข";
      $ID_NO = $fetch['ID_NO'];
      $sql_ib64 = "UPDATE CONFIG.dbo.IMG64_APPROVE SET
      IMG_DATE = '$IMG_DATE',
      IMAGES = '$IMAGES'
      WHERE ID_NO = '$ID_NO'";
      // echo $sql_ib64;
      $result = $conn->query($sql_ib64);
      if ($result) {
        echo "บันทึกข้อมูลสำเร็จ";
      } else {
        echo "Error : " . $sql . "<br>" . $conn->error;
      }
    }
    else {
      // echo "ไม่มีข้อมูล เพิ่มข้อมูล";
      $sql_ib64 = "INSERT INTO CONFIG.dbo.IMG64_APPROVE
      (PS_ID,IMG_NO,IMG_DATE,IMAGES)
      VALUES ('$PS_ID','$IMG_NO','$IMG_DATE','$IMAGES')";
      // echo $sql_ib64;
      $result = $conn->query($sql_ib64);
      if ($result) {
        echo "บันทึกข้อมูลสำเร็จ";
      }
      else {
        echo "Error : " . $sql . "<br>" . $conn->error;
      }
    }
  }

  // รูปภาพ บันทึกประกอบสัญญา
  if ($CT_NO != "") {
    $sql = "SELECT * from CONFIG.dbo.IMG64_APPROVE
    WHERE CT_NO = '$CT_NO' AND IMG_NO = '$IMG_NO' ";
    $select=$conn->prepare($sql);
    $select->execute();
    $fetch=$select->fetch();
    if ($fetch['CT_NO'] == $CT_NO) {
      // echo "ข้อมูลตรง ทำการแก้ไข";
      $ID_NO = $fetch['ID_NO'];
      $sql_ib64 = "UPDATE CONFIG.dbo.IMG64_APPROVE SET
      IMG_DATE = '$IMG_DATE',
      IMAGES = '$IMAGES'
      WHERE ID_NO = '$ID_NO'";
      // echo $sql_ib64;
      $result = $conn->query($sql_ib64);
      if ($result) {
        echo "บันทึกข้อมูลสำเร็จ";
      } else {
        echo "Error : " . $sql . "<br>" . $conn->error;
      }
    }
    else {
      // echo "ไม่มีข้อมูล เพิ่มข้อมูล";
      $sql_ib64 = "INSERT INTO CONFIG.dbo.IMG64_APPROVE
      (CT_NO,IMG_NO,IMG_DATE,IMAGES)
      VALUES ('$CT_NO','$IMG_NO','$IMG_DATE','$IMAGES')";
      // echo $sql_ib64;
      $result = $conn->query($sql_ib64);
      if ($result) {
        echo "บันทึกข้อมูลสำเร็จ";
      }
      else {
        echo "Error : " . $sql . "<br>" . $conn->error;
      }
    }
  }

  // ภาพขออนุมัติขาย
  if ($BILL_NO != "") {
    $sql = "SELECT * from CONFIG.dbo.IMG64_APPROVE
    WHERE BILL_NO = '$BILL_NO' AND IMG_NO = '$IMG_NO' ";
    $select=$conn->prepare($sql);
    $select->execute();
    $fetch=$select->fetch();
    if ($fetch['BILL_NO'] == $BILL_NO) {
      // echo "ข้อมูลตรง ทำการแก้ไข";
      $ID_NO = $fetch['ID_NO'];
      $sql_ib64 = "UPDATE CONFIG.dbo.IMG64_APPROVE SET
      IMG_DATE = '$IMG_DATE',
      IMAGES = '$IMAGES'
      WHERE ID_NO = '$ID_NO'";
      // echo $sql_ib64;
      $result = $conn->query($sql_ib64);
      if ($result) {
        echo "บันทึกข้อมูลสำเร็จ";
      } else {
        echo "Error : " . $sql . "<br>" . $conn->error;
      }
    }
    else {
      // echo "ไม่มีข้อมูล เพิ่มข้อมูล";
      $sql_ib64 = "INSERT INTO CONFIG.dbo.IMG64_APPROVE
      (BILL_NO,IMG_NO,IMG_DATE,IMAGES)
      VALUES ('$BILL_NO','$IMG_NO','$IMG_DATE','$IMAGES')";
      // echo $sql_ib64;
      $result = $conn->query($sql_ib64);
      if ($result) {
        echo "บันทึกข้อมูลสำเร็จ";
      }
      else {
        echo "Error : " . $sql . "<br>" . $conn->error;
      }
    }
  }







  // $sql_1 = "SELECT * from CONFIG.dbo.IMG64_REGISTER2
  // WHERE CT_NO = '$CT_NO' AND IMG_NO = '$IMG_NO' ";
  // $select_1=$conn->prepare($sql_1);
  // $select_1->execute();
  // $fetch_1=$select_1->fetch();
  // if ($fetch_1['CT_NO'] == $CT_NO) {
  //   // UPDATE
  //   // $ID_NO = $fetch_1['ID_NO'];
  //   // $sql_ib64 = "UPDATE CONFIG.dbo.IMG64_REGISTER2 SET
  //   // IMG_UPDATE = '$date',
  //   // IMAGES = '$base64'
  //   // WHERE ID_NO = '$ID_NO'";
  //   // // echo $sql_ib64;
  //   // $result = $conn->query($sql_ib64);
  //   // if ($result) {
  //   //   echo "บันทึกข้อมูลสำเร็จ";
  //   // } else {
  //   //   echo "Error : " . $sql . "<br>" . $conn->error;
  //   // }
  // }
  // else {
  //   // insert
  //   // $sql_ib64 = "INSERT INTO CONFIG.dbo.IMG64_REGISTER2
  //   // (CT_NO,IMG_NO,IMG_DATE,IMG_UPDATE,IMAGES)
  //   // VALUES ('$CT_NO','$IMG_NO','$date','$date','$base64')";
  //   // // echo $sql_ib64;
  //   // $result = $conn->query($sql_ib64);
  //   // if ($result) {
  //   //   echo "บันทึกข้อมูลสำเร็จ";
  //   // } else {
  //   //   echo "Error : " . $sql . "<br>" . $conn->error;
  //   // }
  // }

?>
