<?php include("auth.php");
session_start();
$did=$_SESSION[v_code];
require("conn.php");
$action = "Add";
$id = null;


function getTotalPin()
{
  include "conn.php";
  $v_code=$_SESSION['v_code'];

  $total = 0;
  $query = "select pin as tot from tbl_vendor where v_code='$v_code'";
  $smt1 = mysqli_query($conn, $query);
  if ($result = mysqli_fetch_assoc($smt1)) {
    if ($result['tot'])
      $total = $result['tot'];
  }
  return ($total);
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">

    <title>Manage Pin </title>
    <meta content="" name="description">
    <meta content="" name="keywords">
    <link href="assets/img/favicon.png" rel="icon">
    <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
    <?php include("templates/inc.php"); ?>

    <script src="scripts/ManagePin.js" type="text/javascript" language="javascript"></script>


</head>

<body>

    <!-- ======= Header ======= -->
    <?php
    include("templates/header.php");
    ?>

    <!-- End Header -->

    <!-- ======= Sidebar ======= -->
    <?php
    include("templates/sidebar.php");

    ?>
    <!-- End Sidebar-->

    <main id="main" class="main">

        <div class="pagetitle">
            <h1>Manage Pin</h1>
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
                    <!--<li class="breadcrumb-item">Manage Pin </li>-->
                     <li class="breadcrumb-item">Total PIN: <b> <?php echo getTotalPin(); ?></b></li>
                </ol>
            </nav>
            <hr>
            
        </div><!-- End Page Title -->

        <section class="section">
            <div class="row">
                <div class="col-lg-12">

                    <div class="card">
                        <div class="card-body">
                            <div class="card-body">
                                <h5 class="card-title">Add Pin</h5>   

                                <!-- Custom Styled Validation -->
                                <form id="pin" action="manage-pin-action.php" class="row g-3 needs-validation" novalidate="">

                                  
                                       <div class="col-md-6">
                                        <label for="validationCustom01" class="form-label"><b>Salesman ID</b></label>
                                        <input type="text" class="form-control" id="did" name="did" required="">
                                        <div class="invalid-feedback">
                                        Please provide a  Salesman ID
                                        </div>
                                    </div>


                                     <div class="col-md-6">
                                   <label for="validationCustom01" class="form-label"><b>No Of Pins</b></label>
                                        <input type="text" class="form-control" id="nop" name="nop" required="">
                                        <div class="invalid-feedback">
                                            Please provide Pin.
                                        </div>
                                    </div>
                                     

                                    <div class="col-12">
                                        <input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
                                        <input type="hidden" name="action" id="action" value="<?php echo $action; ?>" />
                                        <?php if (isset($_GET['id'])) {
                                        ?>
                                            <button class="btn btn-lg btn-primary btn-lg" type="submit">Update</button>
                                        <?php
                                        } else {
                                        ?>

                                    <button class="btn btn-primary  btn-lg" id="btnAdd" type="submit">Submit</button>
                                        <?php
                                        }
                                        ?>
                                        <br>
                                        <h4 id="msg"></h4>
                                    </div>
                                </form><!-- End Custom Styled Validation -->

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

                </div>
            </div>

        </section>







      <section class="section">
        <div class="row">
          <div class="col-lg-12">

            <div class="card" style="overflow:scroll;">
              <div class="card-body" id="records">
              <h5 class="card-title">Pin report</h5>
                <table class="table datatable">
                  <thead>
                    <tr>
                      <th scope="col">Id</th>
                      <th scope="col">SID</th>
                      <th scope="col">Number Of Pin</th>
                      <th scope="col">Date</th>

                    </tr>
                  </thead>

                  <tbody>
                    <?php
                    require "conn.php";
                    
                    $sl = 1;
                    $sql = "select * from tbl_pin1 where did='$did' and deleted_at is NULL order by id desc";
                    $rs = mysqli_query($conn, $sql);
                    while ($row = mysqli_fetch_assoc($rs)) {
                      echo "<tr>";
                      echo '<td>' . $sl . '</td>';
                      echo '<td><span class="badge bg-success btn-lg"><i class="bi bi-star me-1"></i> '.$row['sid'] . '</span></td>';
                      echo '<td><span class="badge bg-danger btn-lg"><h5> '.$row['nop'] . '</h5></span></td>';
                      echo '<td>' . $row['created_at'] . '</td>';
                      echo "</tr>";
                      $sl++;
                    }
                    ?>

                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
        
      </section>









    </main><!-- End #main -->

    <!-- ======= Footer ======= -->
    <?php
    include("templates/footer.php");
    ?>
    <!-- End Footer -->


    <?php include("templates/vendors.php"); ?>

</body>

</html>