<?php
  session_start();
  include '../_connect.php';
  $ct_no = $_REQUEST["ct_no"];
  $sql = "SELECT
  	a.ct_no,
  	a.ps_id,
  	a.ps_name,
  	a.serial_no,
  	a.pserial_no,
  	a.product_name,
  	b.REG_EXPIRE,
  	b.PLC_EXPIRE
  FROM EHONGDB.dbo.hps_ctmain a
  left outer join (select
  	REG_EXPIRE,
  	PLC_EXPIRE,
  	SERIAL_NO
  from EHONGDB.dbo.PRODUCT_PRODUCTSERIAL) b on (a.serial_no=b.SERIAL_NO)
  WHERE ct_no = '$ct_no' ";
  // echo $sql;
  $select=$conn->prepare($sql);
  $select->execute();
  $fetch=$select->fetch();
    $ct_no = $fetch['ct_no'];
    $ps_id = $fetch['ps_id'];
    $ps_name = $fetch['ps_name'];
    $serial_no = $fetch['serial_no'];
    $product_name = $fetch['product_name'];
    $img = "http://192.168.10.52/photos/docregistbookimgs/".$ct_no."/".$ct_no."_img_1.jpg";
    $pserial_no = $fetch['pserial_no'];

    if ($fetch['REG_EXPIRE'] != '') {
      $REG_EXPIRE1 = substr($fetch['REG_EXPIRE'],0,4)+543; $REG_EXPIRE2 = substr($fetch['REG_EXPIRE'],5,2); $REG_EXPIRE3 = substr($fetch['REG_EXPIRE'],8,2);
      $REG_EXPIRE = $REG_EXPIRE3."/".$REG_EXPIRE2."/".$REG_EXPIRE1;
    } else {
      $REG_EXPIRE = "";
    }
    if ($fetch['PLC_EXPIRE'] != '') {
      $PLC_EXPIRE1 = substr($fetch['PLC_EXPIRE'],0,4)+543; $PLC_EXPIRE2 = substr($fetch['PLC_EXPIRE'],5,2); $PLC_EXPIRE3 = substr($fetch['PLC_EXPIRE'],8,2);
      $PLC_EXPIRE = $PLC_EXPIRE3."/".$PLC_EXPIRE2."/".$PLC_EXPIRE1;
    } else {
      $PLC_EXPIRE = "";
    }
?>

<div class="box-shadowx" align="left">

  <div class="" align="right">
    <a href="https://lin.ee/w99bHgu" style="text-decoration: none;">
      <input type="submit" name="" class="button_1 button1_1" value="ติดต่อเจ้าหน้าที่" style="background-color:green;">
    </a>
      <input type="submit" name="" class="button_1 button1_1" id="myBtn" value="เช็คเอกสาร" style="background-color:;">
  </div>

  <b style="color:brown;">ผู้ซื้อ :</b>  <?php echo $ps_name; ?> <br>
  <b style="color:brown;">สินค้า :</b> <?php echo $product_name; ?> <br>
  <b style="color:brown;">เลขเครื่อง :</b> <?php echo $serial_no; ?> <br>
  <b style="color:brown;">เลขตัวถัง :</b> <?php echo $pserial_no; ?> <br>
  <b style="color:brown;">วันหมดอายุ ทะเบียน :</b> <?php echo $REG_EXPIRE; ?> <br>
  <b style="color:brown;">วันหมดอายุ พ.ร.บ. :</b> <?php echo $PLC_EXPIRE; ?>
  <br> <hr>
  <h3>สำเนาหน้าเล่ม</h3>
  <img src="<?php echo $img; ?>" id="myImg" alt="<?php echo $ct_no." ".$ps_name; ?>" onclick="scriptimg();" width="100%">
</div>

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

<!-- The Modal -->
<div id="myModal_x" class="modal_x">
  <!-- Modal content -->
  <div class="modalx-content">
    <span class="close_x"></span>

    <table class="customers">
      <tr >
        <th style="background-color:black;">
          ติดตามเอกสาร
        </th>
      </tr>
      <?php
        // ct_no = '$ct_no'
        $sqql = "SELECT
        bill_no, ct_no, g_id, g_name, ps_name, br_id, brand_name, model_no, color_name, serial_no, pserial_no, chkstatus0, fname, chkhint0, chkreturndate0, chkreturnusr0 ,
        chkdate0
        FROM EHONGDB.dbo.REG_BILL a
        left outer join (SELECT g_id AS xg_id,g_name FROM EHONGDB.dbo.REG_GWORK) b on (a.g_id=b.xg_id)
        left outer join (SELECT emp_id AS emp_xid,fname FROM EHONGDB.dbo.EMPLOYEE) c on (a.chkusr0=c.emp_xid)
        WHERE (status<>'V')
        and ct_no = '$ct_no'
        order by bill_date";
        // echo $sqql;
        $select=$conn->prepare($sqql);
        $select->execute();
        while ($fetchq=$select->fetch()) {
          // echo $fetchq['g_name'];
          if ($fetchq['chkhint0'] == "") {
            $chkhint0 = "-";
          }
          else {
            $chkhint0 = $fetchq['chkhint0'];
          }
      ?>
      <!-- // * ค้างตรวจความถูกต้อง กำลังตรวจสอบ
      // and (chkdate0 is null) and (chkreturndate0 is not null) -->
      <?php
        if ( ($fetchq['chkdate0'] == NULL && $fetchq['chkreturndate0'] != NULL) || ($fetchq['chkdate0'] == NULL) )  {
      ?>
        <tr>
          <th style="background-color:red;"><?php echo $fetchq['g_name']; ?> - (กำลังตรวจสอบเอกสาร)</th>
        </tr>
        <tr>
          <td style="color:red;"><i class="fa fa-clock-o"></i> <?php echo $chkhint0; ?></td>
        </tr>
      <?php } ?>

      <!-- ตรวจสอบแล้วไม่ผ่าน ให้ไปตามงานใหม่
      and (chkdate0 is not null) and (chkstatus0='N') -->
      <?php
        if ( ($fetchq['chkdate0'] != NULL && $fetchq['chkstatus0'] == 'N') )  {
      ?>
        <tr>
          <th style="background-color:red; color:;"><?php echo $fetchq['g_name']; ?> - (เอกสารไม่ครบ)</th>
        </tr>
        <tr>
          <td style="color:red;"><i class="fa fa-close"></i> <?php echo $fetchq['chkhint0']; ?></td>
        </tr>
      <?php } ?>

      <!-- // ตรวจสอบแล้วผ่าน
      // and (chkdate0 is not null) and (chkstatus0='Y') -->
      <?php
        if ($fetchq['chkdate0'] != NULL && $fetchq['chkstatus0'] == 'Y') {
      ?>
      <tr>
        <th><?php echo $fetchq['g_name']; ?></th>
      </tr>
      <tr>
        <td style="color:green;"><i class="	fa fa-check"></i> ตรวจสอบเอกสารแล้วผ่าน</td>
      </tr>
      <?php } ?>

      <?php } ?>
    </table>

  </div>
</div>

<script>
  function scriptimg() {
    // Get the modal
    var modal = document.getElementById("myModal");
    // Get the image and insert it inside the modal - use its "alt" text as a caption
    var img = document.getElementById("myImg");
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    img.onclick = function(){
      modal.style.display = "block";
      modalImg.src = this.src;
      captionText.innerHTML = this.alt;
    }
    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];
    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
      modal.style.display = "none";
    }
  }



  // Get the modal
  var modal = document.getElementById("myModal_x");
  // Get the button that opens the modal
  var btn = document.getElementById("myBtn");
  // Get the <span> element that closes the modal
  var span = document.getElementsByClassName("close_x")[0];
  // When the user clicks the button, open the modal
  btn.onclick = function() {
    modal.style.display = "block";
  }
  // When the user clicks on <span> (x), close the modal
  span.onclick = function() {
    modal.style.display = "none";
  }
  // When the user clicks anywhere outside of the modal, close it
  window.onclick = function(event) {
    if (event.target == modal) {
      modal.style.display = "none";
    }
  }

</script>
