<?php
    include('session.php');
?>

<?php

    date_default_timezone_set('Asia/Singapore');

     if($_SERVER["REQUEST_METHOD"] == "POST")
    {
          if (isset($_POST['saveRecord']))
          {
              $stprefix = strtoupper($_POST['prefix']);
              $strcompany = strtoupper($_POST['company']);
              $stradd = strtoupper($_POST['add']);
              $strcontact = strtoupper($_POST['contact']);
              
              $sql = "INSERT INTO $tblcompany (Prefix, Company, Addresss, ContactNo) VALUES (?, ?, ?, ?)";
              
              $stmt = mysqli_prepare($conn, $sql);

              mysqli_stmt_bind_param($stmt, "ssss", $stprefix, $strcompany, $stradd, $strcontact);

              if (mysqli_stmt_execute($stmt)) {
                  $msg = "New Company Details Created!";
                  $_SESSION['status_message'] = $msg;
                  echo "<script>alert('" . $msg . "');</script>";
              } else {
                  $msg = "ERROR : Create CompanY Details Record!";
                  $_SESSION['status_message'] = $msg;
                  echo "<script>alert('" . $msg . "');</script>";
              }

              mysqli_stmt_close($stmt);

            }
            else if (isset($_POST['updateRekod']))
              {
                  $strcodeid = strtoupper($_POST['recordid']);
                  $stprefix = strtoupper($_POST['prefix']);
                  $strcompany = strtoupper($_POST['company']);
                  $stradd = strtoupper($_POST['add']);
                  $strcontact = strtoupper($_POST['contact']);

                  $tmpuserid = $login_index;

                  $sql = "SELECT * FROM $tblcompany WHERE AutoIndex = '$strcodeid'";

                  $result = mysqli_query($conn, $sql);
                  $num_rows = mysqli_num_rows($result);

                  if ($num_rows <= 0)
                  {
                      $msg = "ERROR : Use Account Record ID : $strcodeid NOT EXIST!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  }
                  else
                  {
                      
                  $tmpuserid = $login_session;
                      
                  $sql = "UPDATE $tblcompany SET Prefix = '$stprefix', Company = '$strcompany', Addresss = '$stradd', ContactNo = '$strcontact' WHERE AutoIndex LIKE '$strcodeid'";
                      
                  if ($conn->query($sql) === TRUE) {
                      
                      $msg = "Company Details Updated!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  } else {
                      $msg = "ERROR : Company Details Updated!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  }
                  }

              }
            else if (isset($_POST['deleteRekod']))
              {
                  $strcodeid = strtoupper($_POST['recordid']);
                  
                  $sql=mysqli_query($conn,"SELECT * FROM $tblcompany WHERE AutoIndex LIKE '$strcodeid'");

                  $num_rows = mysqli_num_rows($sql);

                  if ($num_rows <= 0)
                  {
                      $msg = "ERROR : Account Index : $strcodeid NOT EXIST!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  }
                  else
                  {
                      
                      $tmpuserid = $login_session;
                      
                      $sql = "DELETE FROM $tblcompany WHERE AutoIndex LIKE '$strcodeid'";
                      
                      if ($conn->query($sql) === TRUE) {
                          $msg = "Company Details Deleted!";
                          $_SESSION['status_message'] = $msg;
                          echo "<script>alert('" . $msg . "');</script>";
                      } else {
                          $msg = "ERROR : Delete Company Details Record!";
                          $_SESSION['status_message'] = $msg;
                          echo "<script>alert('" . $msg . "');</script>";
                      }
                  }
              }
        //}
            
     }

else
{
    $month = date('m');
    $day = date('d');
    $strmonth = date('m');
    $year = date('Y');

    $todaydate =  date('Y') . '-' . date('m') . '-' . date('d');
    $todaydatedisplay =  date('d') . '-' . date('m') . '-' . date('Y');
    
    $d = cal_days_in_month(CAL_GREGORIAN,date('m'),date('Y'));

    $begindate = date('Y') . '-' . date('m') . '-' .'01';
    $begindate = date('Y') . '-' . "01" . '-' .'01';
    $enddate = date('Y') . '-' . date('m') . '-' .$d;

    $displaybegindate = "01-". date('m') . '-' . date('Y');
    $displayenddate = $d."-". date('m') . '-' . date('Y') ;
}
?>


<!DOCTYPE html>
<html lang="en" class="h-100">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, shrink-to-fit=9">
        <meta name="description" content="<?php echo $sitetitle ?>">
        <meta name="author" content="<?php echo $sitetitle ?>">
        <title><?php echo $sitetitle ?></title>
		
		<!-- Favicon Icon -->
        <link rel="icon" type="image/png" href="images/logo.png">
		
        <!-- Stylesheets -->
        <?php include 'plugincss.php' ?>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css">


	</head>

<body class="d-flex flex-column h-100">

        <!-- Create Coupon Model Start-->
        <div class="modal fade" id="AddRecords" tabindex="-1" aria-labelledby="AddRecordsLabel" aria-hidden="false">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="AddRecordsLabel">Company Details</h5>
                        <button type="button" class="close-model-btn" data-bs-dismiss="modal" aria-label="Close"><i class="uil uil-multiply"></i></button>
                    </div>
                    <form class="form" method="post" action="">
                        <div class="modal-body">
                            <div class="model-content main-form">
                                <div class="row">
                                        <div class="model-content main-form">
                                            <div class="row">
                                                <div class="col-lg-3 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Prefix</label>
                                                        <input class="form-control h_40" id="prefix" name="prefix" type="text">
                                                    </div>
                                                </div>
                                                <div class="col-lg-9 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Company</label>
                                                        <input class="form-control h_40" id="company" name="company" type="text">
                                                    </div>
                                                </div>
                                                <div class="col-lg-12 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Address</label>
                                                        <textarea class="form-control h_40" id="add" name="add" rows="4" style="height:100px;"></textarea>
                                                    </div>
                                                </div>
                                                <div class="col-lg-3 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Contact No</label>
                                                        <input class="form-control h_40" id="contact" name="contact" type="text">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <input type="hidden" name="action" id="action" value="saveRecord" />
                                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-dismiss="modal"><i class="fa-solid fa-cancel me-3"></i>Close</button>
                                    <button type="submit" class="btn btn-success" id = "saveRecord" name = "saveRecord"><i class="fa-solid fa-save me-3"></i>Save</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    </div>
                    </div>



	<!-- Add Organisation Model End-->
	<!-- Header Start-->
	<header class="header">
        <?php include 'topmenu.php' ?>
	</header>
	<!-- Header End-->
	<!-- Left Sidebar Start -->
	<nav class="vertical_nav">
        <?php include 'sidemenu.php' ?>
	</nav>



	<!-- Left Sidebar End -->
	<!-- Body Start -->
	<div class="wrapper wrapper-body">
		<div class="dashboard-body">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
						<div class="d-main-title">
							<h3>Company Details</h3>
						</div>
					</div>

                        <div class="col-md-12">
                            <div class="d-md-flex flex-wrap align-items-center">
                                <div class="rs ms-auto mt_r4">
                                    <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#AddRecords">New</button>
                                </div>

                            </div>
                        </div>

                        
                        <div class="event-list">
                            <div class="tab-content">
                                <div class="tab-pane fade show active" id="orders-tab" role="tabpanel">
                                    <div class="table-card mt-4">
                                        <div class="main-table">
                                            <div class="table-responsive">
                                                <table id="dataTable" class="table">
                                                    <thead class="thead-dark">
                                                        <tr>
                                                            <th scope="col" style="width: 5%;">#</th>
                                                            <th scope="col" style="width: 10%;">Prefix</th>
                                                            <th scope="col" style="width: 20%;">Company</th>
                                                            <th scope="col" style="width: 25%;">Address</th>
                                                            <th scope="col" style="width: 20%;">No Tel</th>
                                                            <th scope="col" style="width: 10%;">Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                                $i=0;

                                                                    $sql = "SELECT * FROM $tblcompany ORDER BY Company ASC";

                                                                   if($resultrec=mysqli_query($conn,$sql)){
                                                                        while ($rowrec=mysqli_fetch_array($resultrec)){
                                                                            $i=$i+1;
                                                                        
                                                                    ?>
                                                                        <tr>
                                                                            <td align = "left"><?php echo $i?></td>
                                                                            <td align = "left"><?php echo $rowrec['Prefix'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['Company'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['Addresss'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['ContactNo'] ;?></td>
                                                                           
                                                                            <td align ="left">
                                                                            <button type="button" id = "btnUpdate" name = "btnUpdate" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editRecord<?php echo $rowrec['AutoIndex']?>"><i class="fa-regular fa-pen"></i></button>
                                                                            
                                                                                <!-- Create Coupon Model Start-->
                                                                                <div class="modal fade" id="editRecord<?php echo $rowrec['AutoIndex']?>" tabindex="-1" aria-labelledby="editRecordLabel<?php echo $rowrec['AutoIndex']?>" aria-hidden="false">
                                                                                    <div class="modal-dialog modal-lg">
                                                                                        <div class="modal-content">
                                                                                            <div class="modal-header">
                                                                                                <h5 class="modal-title" id="editRecordLabel<?php echo $rowrec['AutoIndex']?>">Company Details</h5>
                                                                                                <button type="button" class="close-model-btn" data-bs-dismiss="modal" aria-label="Close"><i class="uil uil-multiply"></i></button>
                                                                                            </div>
                                                                                                    <form class="form" method="post" action="">
                                                                                                        <div class="modal-body">
                                                                                                            <div class="model-content main-form">
                                                                                                                <div class="row">
                                                                                                                        <div class="model-content main-form">
                                                                                                                            <div class="row">
                                                                                                                                <div class="form-group mt-4">
                                                                                                                                    <input class="form-control h_40" id="recordid" name="recordid" type="hidden" value="<?php echo $rowrec['AutoIndex'];?>">
                                                                                                                                </div>
                                                                                                                                    <div class="col-lg-3 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Prefix</label>
                                                                                                                                            <input class="form-control h_40" id="prefix" name="prefix" type="text" value="<?php echo $rowrec['Prefix'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-9 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Company</label>
                                                                                                                                            <input class="form-control h_40" id="company" name="company" type="text" value="<?php echo $rowrec['Company'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-12 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Address</label>
                                                                                                                                            <input class="form-control h_40" id="add" name="add" type="text" value="<?php echo $rowrec['Addresss'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-3 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Contact</label>
                                                                                                                                            <input class="form-control h_40" id="contact" name="contact" type="text" value="<?php echo $rowrec['ContactNo'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    
                                                                                                                            </div>
                                                                                                                        </div>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="modal-footer">
                                                                                                                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-dismiss="modal"><i class="fa-solid fa-cancel me-3"></i>Close</button>
                                                                                                                    <button type="submit" class="btn btn-danger" id = "deleteRekod" name = "deleteRekod"><i class="fa-solid fa-trash me-3"></i>Delete</button>
                                                                                                                    <button type="submit" class="btn btn-success" id = "updateRekod" name = "updateRekod"><i class="fa-solid fa-save me-3"></i>Save</button>
                                                                                                                </div>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                    </form>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                
                                                                                
                                                                                
                                                                                </td>
                                                                            </tr>
                                                                    <?php
                                                                    }
                                                                       }

                                                                    ?>

                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th scope="col" style="width: 5%;">#</th>
                                                            <th scope="col" style="width: 10%;">Prefix</th>
                                                            <th scope="col" style="width: 20%;">Company</th>
                                                            <th scope="col" style="width: 25%;">Address</th>
                                                            <th scope="col" style="width: 20%;">No Tel</th>
                                                            <th scope="col" style="width: 10%;">Action</th>
                                                        </tr>
                                                    </tfoot>

                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                </div>
            </div>
        </div>
    </div>
	
    <div class="copyright-footer">
        <?php echo $copyrights ?>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Include DataTable JS -->
    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <script>
        // Initialize DataTable
        $(document).ready(function() {
          $('#dataTable').DataTable({
                  "ordering": false,
                  "searching": false,
                  "lengthChange": false,
                  "pageLength": 20
              });
        });
    </script>

	<!-- Body End -->
    <?php include 'pluginjs.php' ?>

</body>
</html>
