<?php session_start();

require_once __DIR__."/includes/headerincludes.php";
require_once __DIR__."/includes/dbconfig.php";
require_once __DIR__."/functions/allfunctions.php";


if(isset($_SESSION["proditemarrayinventory"]))
{
    $itemarrayprod = $_SESSION["proditemarrayinventory"];
}
else
{
 $itemarrayprod = array();
}

if(isset($_SESSION["proditemqty"]))
{
  $itemarrayquantity = $_SESSION["proditemqty"];
}
else
{
  $itemarrayquantity = array();
}

if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]))
{
    $itemarray = array();
}
else
{
  $memberid = $_SESSION['MEMBER_ID'];
  $type = $_SESSION['TYPE'];
}

  $username = $_SESSION['USERNAME'];

if(empty($_GET['id']))
{


}
else
{
	$sales_inquiry_id = $_GET['id'];

}


if(empty($_GET['userid']))
{


}
else
{
	$userid = $_GET['userid'];

}

if(empty( $_GET['action']))
{


}
else
{
	$action =  $_GET['action'];

}

$sales_inquiry_id = isset($sales_inquiry_id)?$sales_inquiry_id:'';
$action = isset($action)?$action:'';






//before we store information of our member, we need to start first the session


	//create a new function to check if the session variable member_id is on set
	function logged_in() {
		return isset($_SESSION['MEMBER_ID']);

	}
	//this function if session member is not set then it will be redirected to index.php
	function confirm_logged_in() {
		if (!logged_in()) {

?>
			<script type="text/javascript">
				window.location = "pages/login.php";
			</script>
<?php
		}
	}
	confirm_logged_in();

?>
<!DOCTYPE html>
<head>
    <title>GBG Enterprise - Dashboard</title>
</head>
<script>
$(document).ready(function () {
             var country = $('.countryvalue').val();
             var region = $('.regionvalue').val();
             var city = $('.cityvalue').val();
             var refID = $('.refIDvalue').val();
            //$('.country option:selected').val(country);
            //$('.voidtype option:selected').val();
            //$( ".country" ).change(function() {
              $(".country").val(country);

              $.ajax({
               type: "GET",
               url: "functions/getregion.php",
               data: {country : country },
               success: function (data) {
                   //alert(data);
                   //$(".customeradd").addClass('hidden');
                   $(".region").html(data);
                   $('.region option:selected').val(region);
                   //alert(data);


               }
             });

               $.ajax({
               type: "GET",
               url: "functions/getcity.php",
               data: {city : region },
               success: function (data) {
                   //alert(data);
                   //$(".customeradd").addClass('hidden');
                   $(".city").html(data);
                   $('.city option:selected').val(city);
                   //alert(data);


               }
             });


             $('.submiteditagent').on('click', function(){


        			var firstname = $('.firstname').val();
        			var middlename = $('.middlename').val();
        			var lastname = $('.lastname').val();
        			var bday = $('.birthday').val();
              var address = $('.address').val();
              var country = $('.country').val();
              var region = $('.region').val();
              var city = $('.city').val();
              var mobile = $('.mobile').val();
              var gender = $('.gender').val();
              var email = $('.email').val();
              var refID = $('.refID').val();
              var other = $('.other').val();
              var agentID = $('.agentID').val();
              var username = $('.username').val();
              var password = $('.password').val();
              var userid = $('.userid').val();
              var checker = 0;

        			var datastring = "";
        			datastring = datastring + "firstname="+firstname;
        			datastring = datastring + "&middlename="+middlename;
        			datastring = datastring + "&lastname="+lastname;
        			datastring = datastring + "&birthday="+bday;
        			datastring = datastring + "&address="+address;
              datastring = datastring + "&country="+country;
              datastring = datastring + "&region="+region;
              datastring = datastring + "&city="+city;
              datastring = datastring + "&mobile="+mobile;
              datastring = datastring + "&gender="+gender;
              datastring = datastring + "&email="+email;
              datastring = datastring + "&refID="+refID;
              datastring = datastring + "&other="+other;
              datastring = datastring + "&agentID="+agentID;
              datastring = datastring + "&userid="+userid;
              datastring = datastring + "&username="+username;
              datastring = datastring + "&password="+password;
              //alert(datastring);

        			if(firstname=="")
        			{

        				alert("Please input first name");
                checker = 2;
        			}
        			else if(middlename=="")
        			{

        				alert("Please input middle name");
                checker = 2;
        			}
              else if(lastname=="")
        			{

        				alert("Please input last name");
                checker = 2;
        			}
              else if(bday=="")
        			{

        				alert("Please input birthday");
                checker = 2;
        			}
              else if(address=="")
        			{

        				alert("Please input address");
                checker = 2;
        			}
              else if(country=="")
        			{

        				alert("Please input country");
                checker = 2;
        			}
              else if(region=="")
        			{

        				alert("Please input region");
                checker = 2;
        			}
              else if(city=="")
        			{

        				alert("Please input city");
                checker = 2;
        			}
              else if(mobile=="")
        			{

        				alert("Please input mobile");
                checker = 2;
        			}
              else if(gender=="none")
        			{

        				alert("Please input gender");
                checker = 2;
        			}
              else if(email=="")
        			{

        				alert("Please input email");
                checker = 2;
        			}
              else if(refID=="")
        			{

        				alert("Reference ID error");
                checker = 2;
        			}
              else if(username=="")
        			{

        				alert("Username error");
                checker = 2;
        			}
              else if(password=="")
        			{

        				alert("Password error");
                checker = 2;
        			}

        			if(checker!=2)
        			{
        					//alert(datastring);
        					$.ajax({
        												  type: "POST",
        												  url: "functions/custacedit.php",
        												  data: datastring,
        												  cache: false,
        												  success: function(result)
        												  {
        														//alert(result);
        														if(result == 1)
        														{

        															alert("Agent Updated");
        															window.location = "userlist.php";

        														}
        														else if(result == 0)
        														{
        															alert("Failed to add Agent or Agent already exist");
        															window.location = "userlist.php";

        														}
        												  }
        					 });

        			}

               });


         });
</script>
    <body id="page-top" onload="countrychange();" >

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

            <!-- Sidebar - Brand -->
            <a class="sidebar-brand d-flex align-items-center justify-content-center" href="index.php">
                <div class="sidebar-brand-icon">
                    <img src="img/gbg.jpg" alt="Company Logo" class="img-thumbnail">
                </div>
                <div class="sidebar-brand-text mx-3">GBG Enterprise</div>
            </a>

            <!-- Divider -->
            <hr class="sidebar-divider my-0">

            <!-- Nav Item - Dashboard -->
            <li class="nav-item active">
                <a class="nav-link" href="index.php">
                    <i class="fas fa-fw fa-tachometer-alt"></i>
                    <span>Dashboard</span></a>
            </li>

            <?php echo sidebar(); ?>



            <!-- Divider -->
       <!--     <hr class="sidebar-divider">-->



            <!-- Divider -->
            <hr class="sidebar-divider d-none d-md-block">



        </ul>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>


                    <!-- Topbar Navbar -->
                    <ul class="navbar-nav ml-auto">




                        <!-- Nav Item - User Information -->
                     <!--   <li class="nav-item dropdown no-arrow">
                            <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="mr-2 d-none d-lg-inline text-gray-600 small"><?php echo $type; ?></span>
                                <img class="img-profile rounded-circle"
                                    src="pic/undraw_profile.svg">
                            </a>

                            <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
                                aria-labelledby="userDropdown">
                                <a class="dropdown-item" href="#">
                                    <i class="fas fa-user fa-sm fa-fw mr-2 text-gray-400"></i>
                                    Profile
                                </a>
                                <a class="dropdown-item" href="#">
                                    <i class="fas fa-cogs fa-sm fa-fw mr-2 text-gray-400"></i>
                                    Settings
                                </a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">
                                    <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
                                    Logout
                                </a>
                            </div>
                        </li> -->
						<?php echo logindetails(); ?>


                    </ul>

                </nav>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                <div class="container-fluid">


                    <!-- Content Row -->

				<!-- Product and Image Content Row -->




					<form action="insert_quotation.php" method="post">
					<div class="row gy-5">
							<div class="col-lg-12">
								<div class="card border-left-info shadow h-100 py-2">
									<div class="card-body">
										<div class="row no-gutters align-items-center">
											<div class="col mr-2">
												<div class="text-sm font-weight-bold text-info text-uppercase mb-4">Agent Form</div>
												<div class="row  align-items-center">
													<div class="col-12 customerdata" id="quotationdata">
															 <div class="form-group col-12" >


														<input type="hidden"  class="agentID" name="agentID" id ="quotID" value="<?php echo $sales_inquiry_id; ?>">
                            <input type="hidden"  class="userID" name="userID" id ="userID" value="<?php echo $userid; ?>">




															</div>



														<div class="row gy-5"></div>
                            <?php $resultlist = getuserlistdata($sales_inquiry_id);

                                  while($row=mysqli_fetch_array($resultlist,MYSQLI_ASSOC)) { ?>


															<!--  BAG FORM  -->
															<div class="form-group row bagform1 ">
                                  <input type="hidden" class="form-control countryvalue" name="countryvalue" required value="<?Php echo $row['country']; ?>"  placeholder="Enter Address" aria-label="Product Construction" aria-describedby="basic-addon2">
                                  <input type="hidden" class="form-control regionvalue" name="regionvalue" required value="<?Php echo $row['region']; ?>"  placeholder="Enter Address" aria-label="Product Construction" aria-describedby="basic-addon2">
                                  <input type="hidden" class="form-control cityvalue" name="cityvalue" required value="<?Php echo $row['city']; ?>"  placeholder="Enter Address" aria-label="Product Construction" aria-describedby="basic-addon2">
                                  <input type="hidden" class="form-control refIDvalue" name="refIDvalue" required value="<?Php echo $row['refID']; ?>"  placeholder="Enter Address" aria-label="Product Construction" aria-describedby="basic-addon2">


																	<div class="col-sm-2">
																		<p>First Name:</p>
																	</div>
																	<div class="col-sm-2">
																		<p class="h5 custemail">
																		<input type="text" class="form-control firstname" name="firstname" required  placeholder="Enter First Name" aria-label="Product color" value="<?Php echo $row['firstname']; ?>" aria-describedby="basic-addon2" value="" >
																		</p>
																	</div>

                                  <div class="col-sm-2">
                                    <p>Middle Name:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custemail">
                                    <input type="text" class="form-control middlename" name="middlename" required  placeholder="Enter Middle Name" aria-label="Product color" value="<?Php echo $row['middlename']; ?>" aria-describedby="basic-addon2" value="" >
                                    </p>
                                  </div>

                                  <div class="col-sm-2">
                                    <p>Last Name:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custemail">
                                    <input type="text" class="form-control lastname" name="lastname" required  placeholder="Enter Last Name" aria-label="Product color" aria-describedby="basic-addon2" value="<?Php echo $row['lastname']; ?>" >
                                    </p>
                                  </div>


																	<div class="col-sm-2">
																		<p>Birthday</p>
																	</div>
																	<div class="col-sm-4">
																		<p class="h5 custcontact">
                                      <input type="text" class="form-control birthday" readonly="readonly" name="birthday" required  placeholder="Enter Birthday" aria-label="Purchase Order Date" aria-describedby="basic-addon2" value="<?Php echo $row['birthday']; ?>"  >
  																		</p>
																	</div>
																	<div class="col-sm-2">
																		<p>Gender:</p>
																	</div>
																	<div class="col-sm-4">
																		<p class="h5 custcontact">
                                      <select class="form-control form-select gender" name="gender" id = "gender" aria-label="Default select example">
                                         <option selected value="none">Select Gender</option>
                                         <option <?php if($row['gender']=="Male"){echo "selected"; }else {} ?> value="Male">Male</option>
                                         <option <?php if($row['gender']=="Female"){echo "selected"; }else {} ?>value="Female">Female</option>
                                      </select></p>
																	</div>

																	<div class="col-sm-2">
																		<p>Address:</p>
																	</div>
																	<div class="col-sm-10">
																		<p class="h5 custcontact">
																		<input type="text" class="form-control address" name="address" required value="<?Php echo $row['address']; ?>"  placeholder="Enter Address" aria-label="Product Construction" aria-describedby="basic-addon2">
																		</p>
																	</div>

																	<div class="col-sm-2">
																		<p>Country:</p>
																	</div>
																	<div class="col-sm-2">
																		<p class="h5 custcontact">
                                      <select class="form-control form-select country" name="country" id = "country" aria-label="Default select example" onchange="testtry()" >
                                         <option selected value="none">Select country</option>
                                          <?php

                                        	echo getDropdown('2_city',"status = 1 group by country",'country',"country");

                                        ?>
                                      </select>
																	</div>
																	<div class="col-sm-2">
																		<p>Region:</p>
																	</div>
																	<div class="col-sm-2">
																		<p class="h5 custcontact">
                                      <select class="form-control form-select region" name="region" id = "region" aria-label="Default select example" >
                                         <option  value="none">Select region</option>

                                      </select>	</p>
																	</div>
                                  <div class="col-sm-2">
                                    <p>City:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custcontact">
                                      <select class="form-control form-select city" name="city" id = "city" aria-label="Default select example">
                                         <option selected value="none">Select city</option>
                                      </select>	</p>
                                  </div>

																	<div class="col-sm-2">
																		<p>Mobile Number:</p>
																	</div>
																	<div class="col-sm-4">
																		<p class="h5 custcontact">
																		<input type="text" class="form-control mobile" name="mobile" required  placeholder="Enter Mobile Number" value="<?Php echo $row['mobile']; ?>" aria-label="Product Construction" aria-describedby="basic-addon2">
																		</p>
																	</div>
                                  <div class="col-sm-2">
                                    <p>Email Address:</p>
                                  </div>
                                  <div class="col-sm-4">
                                    <p class="h5 custcontact">
                                    <input type="email" class="form-control email" name="email" required  placeholder="Enter Email Address" value="<?Php echo $row['email']; ?>" aria-label="Product Construction" aria-describedby="basic-addon2">
                                    </p>
                                  </div>

                                  <div class="col-sm-2">
                                    <p>Referral ID:</p>
                                  </div>
                                  <div class="col-sm-4">
                                    <p class="h5 custcontact">
                                  <!--  <input type="email" class="form-control refID" name="refID" value="<?php echo $_SESSION['USERNAME']; ?>" disabled required   aria-label="Product Construction" aria-describedby="basic-addon2" > -->
                                  <select class="form-control form-select refID" name="refID" id = "refID" aria-label="Default select example" >
                                     <option  value="none">Select Reference ID</option>
                                      <?php
                                              $refID = $row['refID'];
                                              $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                                              $result = "";
                                              $tbl = "users";
                                              $cond = "status = 'Active'";
                                              $query = mysqli_query($con,"select * FROM ".$tbl." WHERE ".$cond. " ") or die(mysqli_error($con));
                                              $selected = "";
                                              mysqli_close($con);

                                              while($rows = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {
                                                if($rows['id'] == $refID)
                                                {
                                                      echo  "<option selected value='".$rows['id']."'>".$rows['USERNAME']."(".$rows['FIRST_NAME']." ".$rows['LAST_NAME'].")</option>";
                                                }
                                                else
                                                {
                                                  echo  "<option value='".$rows['id']."'>".$rows['USERNAME']."(".$rows['FIRST_NAME']." ".$rows['LAST_NAME'].")</option>";
                                                }
                                              }

                                    ?>
                                  </select>
                                    </p>
                                  </div>

															</div>

                                <?php } ?>

															<div class="form-group row bagform2 ">
																	<div class="col-sm-6">
																		<p>Remarks:</p>
																	</div>
																	<div class="col-sm-8">
																		<textarea class="form-control other" name="other" required="" placeholder="Enter remarks" aria-label="Delivery Terms" aria-describedby="basic-addon2"></textarea>
																	</div>

															</div>

													<!-- END OF BAG FORM -->




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



          <div class="row gy-5">
              <div class="col-lg-12">
                <div class="card border-left-info shadow h-100 py-2">
                  <div class="card-body">
                    <div class="row no-gutters align-items-center">
                      <div class="col mr-2">
                        <div class="text-sm font-weight-bold text-info text-uppercase mb-4">System Data</div>
                        <div class="row  align-items-center">






                          <div class="col-12 customerdata" id="quotationdata">
                               <div class="form-group col-12" >








                              </div>



                            <div class="row gy-5"></div>
                            <?php $resultlist = getagentlistdata($userid);

                                  while($row=mysqli_fetch_array($resultlist,MYSQLI_ASSOC)) { ?>


                              <!--  BAG FORM  -->
                              <div class="form-group row bagform1 ">



                                  <div class="col-sm-2">
                                    <p>Username:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custemail">
                                    	<input type="text" class="form-control username"  name="username" value="<?php echo $row['USERNAME']; ?>" required  placeholder="Enter Password" aria-label="Purchase Order Date" aria-describedby="basic-addon2"  >
                                    </p>
                                  </div>

                                  <div class="col-sm-2 row">
																		<p>Password:</p>
																	</div>

																	<div class="col-sm-2 row">
																		<input type="password" class="form-control password"  name="password" value="<?php echo $row['PASSWORD']; ?>" required  placeholder="Enter Password" aria-label="Purchase Order Date" aria-describedby="basic-addon2"  >

																	</div>

                                  <div class="row">
                                  <div class="col-sm-2">
                                    <p>Department:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custemail">
                                    <?Php echo $row['DEPARTMENT']; ?>
                                    </p>
                                  </div>

                                  <div class="col-sm-2">
                                    <p>User Type:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custemail">
                                      <?Php echo $row['USER_TYPE']; ?>
                                    </p>
                                  </div>


                                  <div class="col-sm-2">
                                    <p>Access Type</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custcontact">
                                      <?Php echo $row['ACCESS_TYPE']; ?>
                                      </p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p>Date Created:</p>
                                  </div>
                                  <div class="col-sm-2">
                                    <p class="h5 custcontact">
                                      <?Php echo $row['DATECREATED']; ?></p>
                                  </div>

                                  </div>
                                <?php } ?>
                          <!-- END OF BAG FORM -->




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



					<div class="row gy-5 align-items-center">

						<div class="col-lg-3">

						</div>
						<div class="col-lg-6">
						<button type="button" class="btn btn-success btn-lg btn-block submiteditagent" >Submit</button>

						</div>
						</form>

                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

            <!-- Footer -->
            <footer class="sticky-footer bg-white">
                <div class="container my-auto ">
                    <div class="copyright text-center my-auto">
                        <span>Copyright &copy; GBG 2020</span>
                    </div>
                </div>
            </footer>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="login.html">Logout</a>
                </div>
            </div>
        </div>
    </div>







  </body>
</html
