<?php
  include '_connect.php';

  // $sqlx = "SELECT * FROM EHONGDB.dbo.hps_ctmain
  // WHERE ct_mobileno IN ('0822535103')";
  // $selectx=$conn->prepare($sqlx);
  // $selectx->execute();
  // while ($fetchx=$selectx->fetch()) {
  //   // echo $fetchx['ps_id']."<br>";
  //   $ct_no = $fetchx['ct_no'];
  //   $ps_name = $fetchx['ps_name'];
  //   $ct_mobileno = $fetchx['ct_mobileno'];

  $ct_no = $_GET['ct_no'];

  $sqlx = "SELECT * FROM EHONGDB.dbo.hps_ctmain
  WHERE ct_no = '$ct_no'";
  $selectx=$conn->prepare($sqlx);
  $selectx->execute();
  $fetchx=$selectx->fetch();
    $ps_name = $fetchx['ps_name'];
    $ct_mobileno = $fetchx['ct_mobileno'];

  ?>

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title>รายการใบเสร็จ</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <script src='https://kit.fontawesome.com/a076d05399.js' crossorigin='anonymous'></script>
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

        <style media="screen">
          /* table{
            font-size: 30px;
          } */
          body{
            font-size: 25px;
            background-color: #fff;
            background-image: url("img/b1.png");
          }
        </style>
        <style>
          #customers {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
            height: 50%;
          }

          #customers td, #customers th {
            border: 1px solid #ddd;
            padding: 8px;
          }

          #customers tr:nth-child(even){background-color: #f2f2f2;}

          #customers tr:hover {background-color: #ddd;}

          #customers th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            /* background-color: red; */
            color: white;
          }
        </style>
        <style>
        .buttonx {
          /* background-color: yellow; /* Green */
          border: none;
          color: black;
          text-align: center;
          text-decoration: none;
          display: inline-block;
          font-size: 20px;
          margin: 4px 2px;
          cursor: pointer;
        }
        .button1 {padding: 10px 24px;}
        .button2 {padding: 12px 28px;}
        .button3 {padding: 14px 40px;}
        .button4 {padding: 32px 16px;}
        .button5 {padding: 10px;}
        </style>
      </head>
      <body>
        <br>
      <center>
      <table border="1" id="customers" style="width:97%;">
        <tr style="background-color:red; color:#fff;">
          <th style="width:80%;"> ตรวจสอบยอดค้างชำระ </th>
          <!-- <th> <img src="img/logo.png" alt="" style="width:50%;"> </th> -->
        </tr>
        <tr>
          <td>
            <?php
              echo "เลขที่สัญญา : ".$ct_no."<br>";
              echo "ชื่อ-นามสกุล : ".$ps_name;
            ?>
          </td>
        </tr>
      </table>
      <table border="0" style="width:97%; ">
        <tr>
          <td style="width:90%; text-align:right;">

            <a href="https://ehongmd.com/barcode_pay_md.php">
              <input type="submit" name="" class="buttonx button5" style="background-color: Gold;" value="ชำระเงินผ่าน Barcode">
            </a>
            <a href="https://ehongmd.com/qrcode_pay_md.php">
              <input type="submit" name="" class="buttonx button5" style="background-color: Gold;" value="ชำระเงินผ่าน QR Code">
            </a>
            
          </td>
        </tr>
        <div class="container">

        <tr>
          <td style="width:10%; text-align:right;">
              <input type="submit" name="" class="buttonx button5" style="background-color: LightSeaGreen;" data-toggle="collapse" data-target="#demo" value="ประวัติการชำระ">
          </td>
        </tr>
      </table>
      <hr>
        <!-- <button type="button" class="btn btn-info buttonx button5" style="background-color: LightSeaGreen;" >ประวัติการชำระ</button> -->
        <div id="demo" class="collapse">
          <table border="0" id="customers" class="" style="width:92%; background-color:;">
            <thead>
            <tr style="background-color:black; color:#fff;">
              <th style="text-align:center;">ครั้งที่</th>
              <th style="text-align:center;">วันที่</th>
              <th style="text-align:center;">จำนวนเงิน / บาท</th>
              <th style="text-align:center;">ใบเสร็จ</th>
            </tr>
          </thead>
          <tbody>
          <?php
            $sql = "SELECT * FROM EHONGDB.dbo.hps_tran WHERE ct_no = '$ct_no'
            ORDER BY tr_date";
            $select=$conn->prepare($sql);
            $select->execute();
            // echo $sql;
            $no = 0;
            $sum = 0;
            $sumx = "0";
            while ($fetch=$select->fetch()) {
              $no ++;
              $TR_NO = $fetch['TR_NO'];
              $tr_date =  $fetch['TR_DATE'];
              $tr_date1 = substr($tr_date,0,4)+543; $tr_date2 = substr($tr_date,5,2); $tr_date3 = substr($tr_date,8,2);
              $tr_datex = $tr_date3."/".$tr_date2."/".$tr_date1;
              $pay_amnt = number_format($fetch['PAY_AMNT'],2);
              $d='';
           		$e='';
              if($fetch['tr_status']<0){
                $d = "red";
                $e = "*ยกเลิก**";
                $bcolor = "red";
                $color = "yellow";
              }else{
                 $sum += $fetch['PAY_AMNT'];
                 $bcolor = "";
                 $color = "";
              }
            ?>
            <tr>
              <td style="text-align:; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;"> <?php echo number_format($fetch['TR_NO']); ?>.<br> </td>
              <td style="text-align:; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;"> <?php echo $tr_datex; ?> </td>
              <td style="text-align:right; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;">
                <!-- <a a href="http://192.168.10.53/xehong-linux/printslip.php?sid=hps&fid=printslip&ct_no=<?php echo $ct_no; ?>&recbranch=<?php echo $fetch['recbranch']; ?>&ref_no=<?php echo $fetch['REF_NO']; ?>&ref_seq=<?php echo $fetch['REF_SEQ']; ?>&flg=1" target="_blank"> -->
                  <?php echo $e." ".$pay_amnt; ?>
                <!-- </a> -->
              </td>
              <td style="text-align:center; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;">
                <a href="printslip.php?sid=hps&fid=printslip&ct_no=<?php echo $ct_no; ?>&recbranch=<?php echo $fetch['recbranch']; ?>&ref_no=<?php echo $fetch['REF_NO']; ?>&ref_seq=<?php echo $fetch['REF_SEQ']; ?>&flg=1" target="">
                  <!-- <button style='font-size:;' class="buttonx button5" style="background-color: LightSeaGreen;"> </button> -->
                  <i class='fa fa-search-plus'></i>
                </a>
              </td>
            </tr>
            <?php
              if($fetch['tr_status']>=0){
                $sqlc = "SELECT * FROM EHONGDB.dbo.hps_trandet WHERE ct_no = '$ct_no' and tr_no = '$TR_NO'
                ORDER BY tr_date";
                $selectc=$conn->prepare($sqlc);
                $selectc->execute();
                // echo $sqlc;
                $sum_c = "0";
                // $sumx = "0";
                while ($fetchc=$selectc->fetch()) {
                  if ($fetchc['td_type'] == "3") {
                    $sum_c -= $fetchc['pay_amnt'];
                    $pay_amnt_c = number_format($fetchc['pay_amnt'],2);
                    $sumx += $fetchc['pay_amnt'];
                    // echo $sumx;
            ?>
            <tr>
              <td></td>
              <td style="text-align:; background-color:; color:red;"> - <?php echo $fetchc['td_detail']; ?></td>
              <td style="text-align:right; background-color:; color:red;">(<?php echo $pay_amnt_c; ?>)</td>
              <td></td>
            </tr>
          </tbody>
          <?php
          }
          }
          }
          }
         ?>
         <!-- <tfoot>
         <tr>
           <th colspan="2" style="text-align:center; background-color:black; color:#fff;"> รวม </th>
           <th style="text-align:right; background-color:black; color:#fff;"><?php echo number_format($sum-$sumx,2);?></th>
           <th style="background-color:black; color:#fff;"></th>
         </tr>
       </tfoot> -->
       </table>
     <br><br>
       <?php
        // }
       ?>
     </center>
    </div>
  </div>
  </body>
</html>
<?php include 'datatable.php' ?>
