<?php include('session.php'); ?>
<!DOCTYPE html>
<html lang="en">
  <head>
	<title>ECIMS ADMIN | ADD TRADESMAN</title>
    <?php include('includes/head.php'); ?>
  </head>
  <body>
    <div class="container-scroller">
      <div class="horizontal-menu">
        <?php include('includes/header.php'); ?>
      </div>
      <div class="container-fluid page-body-wrapper">
        <div class="main-panel">
          <div class="content-wrapper">
            <div class="page-header">
              <h3 class="page-title"> Add Tradesman </h3>
            </div>
            <div class="row">
              <div class="col-12 grid-margin stretch-card">
                <div class="card">
                  <div class="card-body">
                      <?php $err = $msg = '';
							if(isset($_POST['submit'])) {
								$title = mysqli_real_escape_string($conn, $_POST['title']);
								$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
								$dept_id = mysqli_real_escape_string($conn, $_POST['dept_id']);
								$designation = mysqli_real_escape_string($conn, $_POST['designation']);
							$insert = $conn->query("INSERT INTO engineers_list (name,status,mobile,dept_id,designation) VALUES('".$title."','1','".$mobile."','".$dept_id."','".$designation."')");
							if($insert){
								$msg = 'Data Added Successfully.';
							} else {
								$err = $conn->error;
							}
						}
						if(!empty($err)){
						  echo "
							<div class='alert alert-danger alert-dismissible'>
							  <h4><i class='fa fa-warning'></i> Error!</h4>
							  ".$err."
							</div>
						  ";
						}
						if(!empty($msg)){
						  echo "
							<div class='alert alert-success alert-dismissible'>
							  <h4><i class='fa fa-check'></i> Success!</h4>
							  ".$msg."
							</div>
						  ";
						}
						?>
                    <form class="forms-sample" method="POST">
                      <div class="form-group">
                        <label for="title">Name</label>
                        <input type="text" class="form-control" required name="title" id="title" placeholder="Name">
                      </div>
                      <div class="form-group">
                        <label for="mobile">Mobile</label>
                        <input type="text" class="form-control" required name="mobile" id="mobile" placeholder="Mobile">
                      </div>
                      <div class="form-group">
                        <label for="designation">Designation</label>
                        <input type="text" class="form-control" required name="designation" id="designation" placeholder="Designation">
                      </div>
                      <div class="form-group">
                        <label for="dept_id">Department</label>
                        <select class="form-control" name="dept_id" required id="dept_id">
						  <?php if($_SESSION['admintype'] == 'dept'){
						      $dept = $conn->query("SELECT * FROM admin WHERE type='dept' AND status='1' AND id='".$_SESSION['admin']."'"); 
						  } else {
						      $dept = $conn->query("SELECT * FROM admin WHERE type='dept' AND status='1'"); 
						  }
						  while($fetchdept = $dept->fetch_assoc()) { ?>
                          <option value="<?php echo $fetchdept['id']; ?>"><?php echo $fetchdept['name']; ?>(<?php echo $fetchdept['designation']; ?>)</option>
						  <?php } ?>
                        </select>
                      </div>
                      <button type="submit" name="submit" class="btn btn-primary me-2">Submit</button>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
		  <?php include('includes/footer.php'); ?>
        </div>
      </div>
    </div>
    <?php include('includes/foot.php'); ?>
  </body>
</html>