<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');

@extract($_REQUEST);
if (empty($orderID)) {
  $heading = "View Documents";
} else {
  $heading = "View Documents";
}
$res = getResult("admin_details", " where user_type='admin'");

$record_type = "View Documents";
$redirect_script_name = "manage-orders.php";
$table_name = "orders_tbl";
#################### Add Record ##################
if ($id == 'remove_image') {
  mysqli_query($dbconn, "UPDATE $table_name SET $field='' where id = '$catID'");
  $_SESSION['session_message'] = $record_type . ' has been updated successfully';
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
  exit;
}

if ($_REQUEST['action'] == "add_record" && $name != "") {
  $numCnt = getCount($table_name, " where name='$name'");
  $count_cat = mysqli_query($dbconn, "SELECT * FROM $table_name");
  if (mysqli_num_rows($count_cat) >= $res['category_count']) {
    $_SESSION['session_message'] = '<div class="btn btn-danger"> ' . $record_type . ' count exceeds the allowed limit ['. $res[category_count] .']  </div>';
  } else {
    if ($numCnt > 0) {
      $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    } else {
    
      if ($_FILES['certificateimg']['name']!=" ") {
        $file_extension = strtolower(getExtension($_FILES['certificateimg']['name']));
        if ($_FILES['certificateimg']['name'] != "") {
          $certificate = str_replace(" ", "-", $name);
          $img_name = $certificate;
          $img = upload_file($img_name, "certificateimg", "uploaded-files/certificate/images");
        }
      }

      $sql = mysqli_query($GLOBALS['dbconn'],"insert into `$table_name` set name='$name',caption='$caption',image='$img'");
      $_SESSION['session_message'] ='<div class="btn btn-success">' . $name . ' has been added successfully  </div> ';

      header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
      exit;
    }
  }
}
################ Update Record ##################

if ($_REQUEST['action'] == "update_record" && $name != "") {
  $numCnt = getCount($table_name, " where name='$name' and id!='$catID'");
  if ($numCnt > 0) {
    $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }

  if ($_FILES['certificateimg']['name'] != "") {
    $file_extension = strtolower(getExtension($_FILES['certificateimg']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
      $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
      exit;
    }
    if ($_FILES['certificateimg']['name'] != "") {
      $categoryRes = getResult($table_name, " where id='$catID'", "image");
      $filename = $categoryRes['image'];
      if ($filename != "") {
        $file = site_url . "/uploaded-files/certificate/images/" . $filename;
        if (file_exists($file)) {
          unlink($file);
        }

      $certificate = str_replace(" ", "-", $name);
      $img_name = $certificate;
      $img = upload_file($img_name, "certificateimg", "uploaded-files/certificate/images");
      updateTable($table_name, " image='$img' where id='$catID'");
    }
  }
}
  
  updateTable($table_name, " name='$name',caption='$caption' where id='$catID'");
  $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
   
  header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);
  exit;
}
########################################################
admin_header();
$res = getResult($table_name, " where order_id='$orderID'");
?>
           <div class="content-wrapper">
          <div class="page-header">
            <h3 class="page-title">
			<?=$heading;?>
            </h3>
            <nav aria-label="breadcrumb">
              <ol class="breadcrumb">
			  <li class="nav-item d-none d-lg-flex">
            <a class="nav-link" href="<?=$redirect_script_name?>">
              <span class="btn btn-primary">Go Back</span>
            </a>
          </li>
              </ol>
            </nav>
          </div>
          <div class="row">
          
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-12">
                            <div class="row">
                            <div class="col-2 text-center">
                            <a href="<?=site_url?>/uploaded-files/document/<?=$res['aadhar_file']?>" download>
                                <img src="<?=admin_url?>images/download.png" class="img-thumbnail" width="140" height="140" title="View">
                            </a>
                            <p>Aadhar Card</p>
                            </div>
                            <div class="col-2 text-center">
                            <a href="<?=site_url?>/uploaded-files/document/<?=$res['pan_file']?>" download>
                                <img src="<?=admin_url?>images/download.png" class="img-thumbnail" width="140" height="140" title="View">
                            </a>
                            <p>Pan Card</p>
                            </div>
                            </div>
                            
                        </div>
                    </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>

