<html>

<head>
  <meta charset="UTF-8">
  <title>Form Add Customer</title>
  <style>
    * {
      font: 12px tahoma;
    }

    #head {
      margin: auto;
      font: 20px tahoma;
      color: #009;
      text-align: center;
      width: 840px;
      height: 30px;
      background: #ddf;
    }

    body {
      background: url(images/bg.jpg);
      min-width: 700px;
    }

    #all {
      margin: auto;
      width: 830px;
      padding: 5px;
      background: #ddf;
    }

    select {
      background: #eee none repeat scroll 0 0;
      border: 1px solid gray;
    }

    #bg {
      margin-top: 10px;
      width: 830px;
      padding: 5px;
      color: crimson;
      background: #ddf;
      border: 2px solid rgba(186, 186, 186, 1.00);
    }

    #bg p {
      font-size: 16px;
    }

    /*input{background: #eee none repeat scroll 0 0;}*/
    span {
      font-size: 18px;
      color: blanchedalmond;
    }

    .textHighlight p {
      width: 300px;
      color: wheat;
    }
  </style>
</head>

<body>

  <script type="text/javascript">
    function nextbox(e, id) {
      // อ่าน keycode (cross browser)
      var keycode = e.which || e.keyCode;
      // ตรวจสอบ keycode (13 คือ กด enter)
      if (keycode == 13) {
        // ย้ายโฟกัสไปยัง input ที่ id
        document.getElementById(id).focus();
        // return false เพื่อยกเลิกการ submit form
        return false;
      }
    }
  </script>

  <?php
  error_reporting(0);

  include "functions/dblink.php";

  //ถ้าเป็นการ Postback เพื่อส่งข้อมูลกลับเข้ามา
  if (isset($_POST['C_NO'])) { //นำข้อมูลจากตัวแปร $_POST ทีเหลือมาเรียงต่อเป็นสตริงเดียวกันโดยขั้นด้วย ' , '
    error_reporting(0);

    $name=$_POST['Name'];
    $date=$_POST['Date'];
    $kind=$_POST['Kind'];
    $sur=$_POST['Surname'];
    $name=$_POST['Name'];
    $adrs=$_POST['Adrs'];
    $dstrt=$_POST['Dstrt'];
    $prvn=$_POST['Prvn'];
    $zip=$_POST['Zip'];
    $telc1=$_POST['Telc1'];
    $tel1=$_POST['Tel1'];
    $telc2=$_POST['Telc2'];
    $tel2=$_POST['Tel2'];
    $contrct=$_POST['Contrct'];
    $tax_no=$_POST['Tax_no'];
    $smsur=$_POST['Smsur'];
    $fname=$_POST['Smfname'];
    $lname=$_POST['Smlname'];
    $disc=$_POST['Disc'];
    $note=$_POST['Note'];

    $sqlName="SELECT * FROM cust001 WHERE C_NAME = '$name' LIMIT 1";
    $resultName=mysqli_query($con,$sqlName);
    $customer=mysqli_fetch_assoc($resultName);

    if($_POST['Name']){
      
      if($customer['C_NAME'] === $name){
        echo "<script>alert('Username already exists');</script>";
      }
    }
    else{  
      /*$values = implode("','", $_POST);  //ลักษณะผลลัพธ์เช่น  a',  'b',  'c', 'd
      //ปิดหัว ท้าย ด้วย ' เพื่อให้ครบคู่ ลักษณะผลลัพธ์จะเป็น 	'a',  'b',  'c', 'd'
      $values = "'" . $values . "'";*/

      //นำข้อมูลนั้นมาสร้างเป็น  SQL ในแบบคำสั่ง REPLACE
      $sql = "INSERT INTO cust001(C_NAME,C_DATE,C_KIND,C_NAME,C_ADRS,C_DSTRT,C_PRVN,C_ZIP,
                                  C_TELC1,C_TEL1,C_TELC2,C_TEL2,C_CONTRCT,C_TAX_NO,C_SMSUR,
                                  C_SMFNAME,C_SMLNAME,C_DISC,NOTE) 
                            VALUE($name,$date,$kind,$name,$adrs,$dstrt,$prvn,$zip,
                                  $telc1,$tel1,$telc2,$tel2,$contrct,$tax_no,$smsur,
                                  $fname,$lname,$disc,$note);";
      $replace = mysqli_query($con, $sql);

      if (!$replace) {
        //echo mysqli_error($con);
        echo "<script>alert('Username already exists');</script>";
        back();
      } else { ?>
        <div id="bg" align="center">
          <p>--- ข้อมูลบันทึกแล้ว ---</p> <br>
        <?php back();
      } ?>
        </div>
        <?php }
  }  

    //--------------	
    if (isset($_GET['action'])) {
      $action = $_GET['action'];
      //ถ้าเป็นเพิ่มข้อมูล ก็ไม่ต้องทำอะไร เพื่อให้ฟอร์มนั้นว่างเปล่าสำหรับรับข้อมูลใหม่
      if ($action == "seller/addSellerAction") {
      } //$h="เพิ่มข้อมูล";
      else if ($action == "seller/deleteSellerAction") { //ถ้าเป็นการลบ ก็นำค่า id ไปกำหนดเป็นเงื่อนใขการลบ
        $id = $_GET['C_NO'];
        $select = "SELECT * FROM cust001 WHERE C_NO=$id";
        $search = mysqli_query($con, $select);
        while ($data = mysqli_fetch_array($search)) {
          $name = $data['C_NAME'];
        } ?>
        <div id="bg" align="center">
          <form method="post" id="conf" action="#">
            <div class="textHighlight" align="center" style="border: double; font-size: 18px; background: #1836B1">
              <br>
              <p>ต้องการลบข้อมูลลูกค้า</p><br>
              <span style="color: red"> ชื่อ/บริษัท : </span>
              <span><?php echo $name; ?></span>
              <p></p><br>
            </div>
            <br>
            <p><span style="font-size: 20px; color: red;">ใช่</span>
              (Yes - 'Y' ; 'y') <span style="color: black; font-size: 18px;">หรือ</span>
              <span style="font-size: 20px; color: red;">ไม่</span>
              (No - 'N' ; 'n') ?
            </p>
            <!--<p> (ใส่ได้เฉพาะ 'Y' , 'y' => ใช่  OR 'N' , 'n' => ไม่ใช่)</p>-->
            <p>=>
              <input type="text" name="conf" size="2" pattern="[nyNY]" style="border: double;" required>
            </p>
            <input type="submit" value="Confirm" style="color: red;">
            <?php
            if ($_POST['conf'] == 'Y' or $_POST['conf'] == 'y') {
              $sql = "DELETE FROM cust001 WHERE C_NO=$id";
              $delete = mysqli_query($con, $sql);
            }
            if (!$delete) {
              echo  mysqli_error($con);
            } else { ?>
              <div class="textHighlight" align="center">
                <p style="border: double; font-size: 18px; background: #1836B1">
                  ข้อมูลลูกค้าถูกลบแล้ว
                </p>
              </div>
            <?php }

            if ($_POST['conf'] == 'N' or $_POST['conf'] == 'n') { ?>
              <div class="textHighlight" align="center">
                <p style="border: double; font-size: 18px; background: #1836B1;">
                  ยกเลิกการลบข้อมูลลูกค้า
                </p>
              </div>
            <?php }
            back(); ?>
          </form>
        </div>
    <?php }
    }
    

    function back()
    {
      global $con;
      mysqli_close($con);
      exit("<p><a href='actionController.php?action=seller/retrieveSellerAction'>ย้อนกลับ</a>
		        </p></body></html>");
    }
    mysqli_close($con);
    //echo $h; 
    ?>

    <div id="head">แบบฟอร์มเพิ่มข้อมูลลูกค้า</div>
    <div id="all" align="center">

      <form name="addform" method="post">
        <!-- <form method="post"> -->
        <table width="921">
          <tbody>
            <tr>
              <td colspan="6">ข้อมูลลูกค้า</td>
            </tr>
            <input type="hidden" name="C_NO" value=0>
            <tr>
              <td width="90">
                <p align="right">วันที่ : </p>
              </td>
              <td width="819">
                <input type="hidden" name="Date" id="Date" value="<?php echo date('Y-m-d'); ?>">
                <input type="date" name="Date" id="Date" size="19" readonly disabled value="<?php echo date('Y-m-d'); ?>" onkeydown="return nextbox(event, 'C_KIND');">
              </td>
            </tr>
          </tbody>
        </table>
        <table width="921">
          <tr>
            <td width="90">
              <p align="right">ประเภทลูกค้า : </p>
            </td>
            <td width="134">
              <input type="text" name="Kind" id="Kind" size="20" 
                                 value="<?php if (!isset($data['C_KIND'])) { $data['C_KIND'] = ''; }
                                 echo $data['C_KIND']; ?>" 
                                 onkeydown="return nextbox(event, 'C_SURNAME');">
            </td>
            <td width="678">
          </tr>
        </table>
        <table width="921">
          <tr>
            <td width="92">
              <p align="right">ชื่อ : </p>
            </td>
            <td width="50">
              <input type="text" name="Surname" id="Surname" size="8" 
                                 value="<?php if (!isset($data['C_SURNAME'])) { $data['C_SURNAME'] = ''; }
                                 echo $data['C_SURNAME']; ?>" 
                                 onkeydown="return nextbox(event, 'C_NAME');">
            </td>
            <td width="764">
              <input type="text" name="Name" id="Name" size="50" 
                                 value="<?php if (!isset($data['C_NAME'])) { $data['C_NAME'] = ''; }
                                 echo $data['C_NAME']; ?>" 
                                 onkeydown="return nextbox(event, 'C_ADRS');">
            </td>
          </tr>
        </table>
        <table width="921">
          <tbody>
            <tr>
              <td width="91">
                <p align="right">ที่อยู่ : </p>
              </td>
              <td width="822"><input type="text" name="Adrs" id="Adrs" size="100" 
                                                 value="<?php if (!isset($data['C_ADRS'])) { $data['C_ADRS'] = ''; }
                                                 echo $data['C_ADRS']; ?>" 
                                                 onkeydown="return nextbox(event, 'C_DSTRT');"></td>
            </tr>
          </tbody>
        </table>
        <table width="921">
          <td width="138">
            <p align="right">อำเภอ : </p>
          </td>
          <td>
            <input type="text" name="Dstrt" id="Dstrt" size="20" value="<?php if (!isset($data['C_DSTRT'])) {
                                                                              $data['C_DSTRT'] = '';
                                                                            }
                                                                            echo $data['C_DSTRT']; ?>" onkeydown="return nextbox(event, 'C_PRVN');">
          </td>
          <td width="80" align="right">&nbsp;</td>
          <td width="531">&nbsp;</td>
          <tr>
            <td width="138">
              <p align="right">จังหวัด : </p>
            </td>
            <td><input type="text" name="Prvn" id="Prvn" size="25" 
                                   value="<?php if (!isset($data['C_PRVN'])) { $data['C_PRVN'] = ''; }
                                   echo $data['C_PRVN']; ?>" 
                                   onkeydown="return nextbox(event, 'C_ZIP');"></td>
            <td width="25">
              <p align="right">รหัสไปรษณีย์ : </p>
            </td>
            <td><input type="text" name="Zip" id="Zip" size="7" 
                                   value="<?php if (!isset($data['C_ZIP'])) { $data['C_ZIP'] = ''; }
                                   echo $data['C_ZIP']; ?>" 
                                   onkeydown="return nextbox(event, 'C_TELC1');"></td>
          </tr>
        </table>

        <table width="921">
          <tbody>
            <tr>
              <td width="199">
                <p align="right">โทร. : </p>
              </td>
              <td>
                <input type="text" name="Telc1" id="Telc1" size="3" 
                                   value="<?php if (!isset($data['C_TELC1'])) { $data['C_TELC1'] = ''; }
                                   echo $data['C_TELC1']; ?>" 
                                   onkeydown="return nextbox(event, 'C_TEL1');">
                <input type="text" name="Tel1" id="Tel1" size="11" 
                                   value="<?php if (!isset($data['C_TEL1'])) { $data['C_TEL1'] = ''; }
                                   echo $data['C_TEL1']; ?>" 
                                   onkeydown="return nextbox(event, 'C_TELC2');">
              </td>
            </tr>
          </tbody>
          <tbody>
            <tr>
              <td>&nbsp;</td>
              <td>
                <input type="text" name="Telc2" id="Telc2" size="3" 
                                   value="<?php if (!isset($data['C_TELC2'])) { $data['C_TELC2'] = ''; }
                                   echo $data['C_TELC2']; ?>" 
                                   onkeydown="return nextbox(event, 'C_TEL2');">
                <input type="text" name="Tel2" id="Tel2" size="11" 
                                   value="<?php if (!isset($data['C_TEL2'])) { $data['C_TEL2'] = ''; }
                                   echo $data['C_TEL2']; ?>" 
                                   onkeydown="return nextbox(event, 'C_CONTRCT');">
              </td>
            </tr>
          </tbody>
        </table>

        <table width="921">
          <tr>
            <td width="87">
              <p align="right">ชื่อผู้ติดต่อ : </p>
            </td>
            <td><input type="text" name="Contrct" id="Contrct" size="35" 
                                   value="<?php if (!isset($data['C_CONTRCT'])) { $data['C_CONTRCT'] = ''; }
                                   echo $data['C_CONTRCT']; ?>" 
                                   onkeydown="return nextbox(event, 'C_TAX_NO');"></td>
          </tr>
          <tr>
            <td width="87">
              <p align="right">Tax No. : </p>
            </td>
            <td><input type="text" name="Tax_no" id="Tax_no" size="13" 
                                   value="<?php if (!isset($data['C_TAX_NO'])) { $data['C_TAX_NO'] = ''; }
                                   echo $data['C_TAX_NO']; ?>" 
                                   onkeydown="return nextbox(event, 'C_SMSUR');"></td>
          </tr>
        </table>
        <table width="921">
          <tr>
            <td width="90">
              <p align="right">ชื่อผู้ขาย : </p>
            </td>
            <td width="53"><input type="text" name="Smsur" id="Smsur" size="8" 
                                              value="<?php if (!isset($data['C_SMSUR'])) { $data['C_SMSUR'] = ''; }
                                              echo $data['C_SMSUR']; ?>" 
                                              onkeydown="return nextbox(event, 'C_SMFNAME');"></td>
            <td width="122"><input type="text" name="Smfname" id="Smfname" size="20" 
                                               value="<?php if (!isset($data['C_SMFNAME'])) { $data['C_SMFNAME'] = ''; }
                                               echo $data['C_SMFNAME']; ?>" 
                                               onkeydown="return nextbox(event, 'C_SMLNAME');"></td>
            <td width="197"><input type="text" name="Smlname" id="Smlname" size="30" 
                                               value="<?php if (!isset($data['C_SMLNAME'])) { $data['C_SMLNAME'] = ''; }
                                               echo $data['C_SMLNAME']; ?>" 
                                               onkeydown="return nextbox(event, 'C_DISC');"></td>
            <td width="438">ส่วนลด : <input type="text" name="Disc" id="Disc" size="8" 
                                                       value="<?php if (!isset($data['C_DISC'])) { $data['C_DISC'] = 0; }
                                                       echo $data['C_DISC']; ?>" 
                                                       onkeydown="return nextbox(event, 'NOTE');"> %
            </td>
          </tr>
        </table>
        <table width="921">
          <tr>
            <td width="65">
              <p align="right">Note : </p>
            </td>
            <td width="620">
              <input type="text" name="Note" id="Note" size="100" 
                                 value="<?php if (!isset($data['NOTE'])) { $data['NOTE'] = ''; }
                                 echo $data['NOTE']; ?>">
            </td>
            <!-- <td width="197"><input type="submit" name="Submit" value="ส่งข้อมูล"></td> -->
          </tr>
        </table>
        <table width="921">
          <tr>
            <br>
            <td align="center"><button>บันทึกข้อมูล(Save)</button> &nbsp;&nbsp;
              <a href='actionController.php?action=seller/retrieveSellerAction'>ย้อนกลับ</a>
            </td>
          </tr>
        </table>
      </form>

    </div>
</body>

</html>