<?php	
require_once '../functions/dataaccess.php';
session_start();
extract($_POST);

$table1 = TB_PRE . "admin";
$table2 = TB_PRE . "config";
$table3 = TB_PRE . "pages";
$table4 = TB_PRE . "structure";
$table5 = TB_PRE . "notification";
$table6 = TB_PRE . "groups";

$check1 = dbquery("SHOW TABLES LIKE '".$table1."'")->num_rows;
$check2 =  dbquery("SHOW TABLES LIKE '".$table2."'")->num_rows;
$check3 =  dbquery("SHOW TABLES LIKE '".$table3."'")->num_rows;
$check4 =  dbquery("SHOW TABLES LIKE '".$table4."'")->num_rows;
$check5 =  dbquery("SHOW TABLES LIKE '".$table5."'")->num_rows;
$check6 =  dbquery("SHOW TABLES LIKE '".$table6."'")->num_rows;

if($check1 < 1 && $check2 < 1 && $check3 < 1 && $check4 < 1 && $check5 < 1  && $check6 < 1){
    session_destroy();
    header("Location:../setup");
}

else if (!isset($_SESSION['phpsolid_id'])&&$_SESSION['phpsolid_id']==""){
   header("Location: ../login/");     
}
    
$id = 1;

$getconfig = dbquery("SELECT * FROM ".$table2." WHERE id = '".dbescape($id)."'")->rows;
$projectname = $getconfig[0]['projectname'];
$icon = $getconfig[0]['icon'];
$background = $getconfig[0]['background'];
$copyright = $getconfig[0]['copyright'];

$psid = $_SESSION['phpsolid_id'];
$getadmin = dbquery("SELECT * FROM ".$table1." WHERE id = '".dbescape($psid)."'")->rows;

$image = $getadmin[0]['image'];
$fullname = $getadmin[0]['fullname'];
$email = $getadmin[0]['email'];

$getnotification = dbquery("SELECT * FROM ".$table5." WHERE recipient = '".dbescape("All")."' OR recipient = '".dbescape($email)."' ORDER BY id DESC LIMIT 10")->rows;
$countnotification = dbquery("SELECT * FROM ".$table5." WHERE recipient = '".dbescape("All")."' OR recipient = '".dbescape($email)."' ORDER BY id DESC LIMIT 10")->num_rows;


$pageid = $_GET['pid'];
$getpage = dbquery("SELECT * FROM ".$table3." WHERE id = '".dbescape($pageid)."'")->rows;
$pgname = $getpage[0]['pagename'];

$pgename = str_replace(' ', '_', $pgname);
$pgename = strtolower($pgename);

$table7 = TB_PRE . $pgename."_usergen";


$getupid = dbquery("SELECT * FROM ".$table7." ORDER BY id DESC LIMIT 1")->rows;
$upid = $getupid[0]['id'];

$currentdate = date("Y-m-d H:i:s");

$getfilestrucname = dbquery("SELECT * FROM ".$table4." WHERE structype = 'Select File' AND pageid = '$pageid' ORDER BY id DESC LIMIT 1")->rows;
$filestrucname = $getfilestrucname[0]['strucname'];
$filestrucno = $getfilestrucname[0]['strucnumber'];
$filestrucextra = $getfilestrucname[0]['strucextra'];
$filestructype = $getfilestrucname[0]['structype'];
$filefieldname = dbescape($filestrucname);
$filefieldname = strtolower($filefieldname);
$filefieldname = str_replace(' ', '_', $filefieldname);

$columns = "";
    
$getcol = dbquery("SELECT * FROM ".$table4." WHERE pageid = '$pageid' AND structype != 'PHP Code' ORDER BY strucnumber ASC")->rows;

foreach($getcol as $col){
    
    $strucname = $col['strucname'];
    $dbfieldname = dbescape($strucname);
    $dbfieldname = strtolower($dbfieldname);
    $dbfieldname = str_replace(' ', '_', $dbfieldname);
    
    if($columns != ""){
        
        $columns = $columns . ",".$dbfieldname;
        
    } else{
        $columns = $dbfieldname;
    }
    
}

$getfirst = dbquery("SELECT * FROM ".$table7." ORDER BY id DESC LIMIT 1")->num_rows;

if($getfirst < 1){
    $columns = $columns. ",date_created";
}
$columns = $columns. ",pageres";


$valuesadd = "";

foreach($_POST as $eachvalue) {


    $eachvalue = dbescape($eachvalue);
    $eachvalue = str_replace(',', ' ', $eachvalue);
    
    $eachvalue = "'". $eachvalue . "'";
    
    if($valuesadd != ""){
        
        $valuesadd = $valuesadd . ",".$eachvalue;
        
    } else{
        $valuesadd = $eachvalue;
    }
}

$valuesadd = str_replace("''", "", $valuesadd);


if($getfirst < 1){
    $valuesadd = $valuesadd . ",'{$currentdate}'";
}

$valuesadd = $valuesadd . ",'0'";

$arr = array();
$defarr = array();
$count = 1;

$getfilestrucname = dbquery("SELECT * FROM ".$table4." WHERE structype = 'Select File' AND pageid = '$pageid' ORDER BY id DESC")->rows;
foreach($getfilestrucname as $efilestrucname){

    $filestrucname = $efilestrucname['strucname'];
    $filestrucno = $efilestrucname['strucnumber'];
    $filestrucextra = $efilestrucname['strucextra'];
    $filestructype = $efilestrucname['structype'];
    $filefieldname = dbescape($filestrucname);
    $filefieldname = strtolower($filefieldname);
    $filefieldname = str_replace(' ', '_', $filefieldname);

    $columns = str_replace("$filefieldname,", "", $columns);
    $columns = str_replace(",,", ",", $columns);

    $defarr[] = array("url" => $filestrucextra, "fieldname" => $filefieldname);

    if (isset($_FILES[$filefieldname])){
        $url = $filestrucextra;
        // get details of the uploaded file
        $fileTmpPath = $_FILES[$filefieldname]['tmp_name'];
        $fileName = $_FILES[$filefieldname]['name'];
        $fileSize = $_FILES[$filefieldname]['size'];
        $fileType = $_FILES[$filefieldname]['type'];
        $fileNameCmps = explode(".", $fileName);
        $fileExtension = strtolower(end($fileNameCmps));
    
        // sanitize file-name
        $newFileName = md5(time() . $fileName) . '.' . $fileExtension;
    
        // check if file has one of the following extensions
        $allowedfileExtensions = array('jpg', 'jpeg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc', 'docx', 'pdf');
    
        if (in_array($fileExtension, $allowedfileExtensions))
        {
          // directory in which the uploaded file will be moved
          $uploadFileDir = '../gallery/';
          $dest_path = $uploadFileDir . $newFileName;
          
          $pt = $_SERVER['REQUEST_URI']; 
    
          $pt = explode('/', $pt);
          
          $dpt = $pt[1];
           
          $uploadFileDi =  "./$dpt/gallery/";
          $dest_pat = $uploadFileDi . $newFileName;
          
          
          
            $repl = substr($dest_pat, 1);
            
            $basename = "https://" . $_SERVER['SERVER_NAME'];
            
       
            $url = $basename.$repl;
    
          if(move_uploaded_file($fileTmpPath, $dest_path)) 
          {
            $arr[] = array("url" => $url, "fieldname" => $filefieldname);
            $message ='File is successfully uploaded.';
            $urlactive = "1";
          }
          else 
          {
            $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
          }
        }
        else
        {
          $message = 'Upload failed. Allowed file types: ' . implode(',', $allowedfileExtensions);
        }
    } else {
        $message = 'There is some error in the file upload. Please check the following error.<br>';
        $message .= 'Error:' . $_FILES['uploadedFile']['error'];
    }

}


$valuesadd = str_replace(",,", ",", $valuesadd);

if(isset($dataprocess)){
    
    if($getfirst > 0){
        
        //print_r($columns);
        //echo "<br>";
        //print_r($valuesadd);

        $columns = explode (",", $columns); 
        $valuesadd = explode (",", $valuesadd); 

        foreach (array_combine($columns, $valuesadd) as $updatecol => $updateval) {
            $runadd1 = dbquery("UPDATE ". $table7 . " SET $updatecol = $updateval WHERE id = '".dbescape($upid)."'");
        }
        
        if($arr != null){
            foreach($getfilestrucname as $eachfilestrucname){
                $strucName = $eachfilestrucname['strucname'];
                $strucName = strtolower($strucName);
                $getDelImage = dbquery("SELECT * FROM " .$table7. " WHERE id ='".dbescape($upid)."'")->rows;
                $getImageUrl = $getDelImage[0][$strucName];
                $getImageUrl = explode ("/admin/", $getImageUrl); 
                $getImagePath = "../".$getImageUrl[1];
                if(unlink($getImagePath)){
                } else {
                }
            }
            
        }

        foreach($arr as $eacharr){
            $url = $eacharr['url'];
            $filefieldname = $eacharr['fieldname'];
            $runadd1 = dbquery("UPDATE ". $table7 . " SET $filefieldname = '".dbescape($url)."' WHERE id = '".dbescape($upid)."'");
        }

        notify($fullname. " edited data in page: " .$pgname, "All");
        header('Location: '.$_SERVER['REQUEST_URI']);
        
    } else{
        
        // print_r($columns);
        // echo "<br>";
        // print_r($valuesadd);

        $valuesadd = $valuesadd . "'{$currentdate}'";
        
        $runadd1 = dbquery("INSERT INTO " .$table7. " (".$columns.") VALUES (".$valuesadd.")");
        if($runadd1 > 0){
            if($getfilestrucname > 0 && $filestructype == "Select File"){
                $getlastinsert = dbquery("SELECT * FROM ".$table7." ORDER BY id DESC LIMIT 1")->rows;
                $lastinsertid = $getlastinsert[0]['id'];
                if($arr != null){
                    foreach($arr as $eacharr){
                        $url = $eacharr['url'];
                        $filefieldname = $eacharr['fieldname'];
                        $runadd1 = dbquery("UPDATE ". $table7 . " SET $filefieldname = '".dbescape($url)."' WHERE id = '".dbescape($lastinsertid)."'");
                    }
                }else{
                    foreach($defarr as $eacharr){
                        $url = $eacharr['url'];
                        $filefieldname = $eacharr['fieldname'];
                        $runadd1 = dbquery("UPDATE ". $table7 . " SET $filefieldname = '".dbescape($url)."' WHERE id = '".dbescape($lastinsertid)."'");
                    }
                }
            }
            notify($fullname. " added a new data to page: " .$pgname, "All");
            // echo '<script language="javascript">';
            // echo 'alert("New Data Added Into '.$pgname.' Page!")';
            // echo '</script>';
            header('Location: '.$_SERVER['REQUEST_URI']);
        }
       
    }
    
}
    
?>
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8" />
        <title><?php echo $projectname ?> - <?php echo $pgname ?></title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta content="<?php echo $projectname ?> - Admin Dashboard" name="description" />
        <meta content="<?php echo $projectname ?>" name="author" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <!-- App favicon -->
        <link rel="shortcut icon" href="<?php echo $icon ?>">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
      
        <!-- third party css -->
        <link href="../assets/libs/dataTables.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/buttons.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/responsive.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/select.bootstrap4.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/select2.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/bootstrap-tagsinput.css" rel="stylesheet" />
        <link href="../assets/libs/switchery.min.css" rel="stylesheet" type="text/css" />
         <link href="../assets/libs/bootstrap-timepicker.min.css" rel="stylesheet" type="text/css" />
         <link href="../assets/libs/bootstrap-colorpicker.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/libs/bootstrap-datepicker.css" rel="stylesheet">
        <link href="../assets/libs/daterangepicker.css" rel="stylesheet">
        <link href="../assets/libs/bootstrap-clockpicker.min.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.20/summernote-bs4.min.css" integrity="sha512-ngQ4IGzHQ3s/Hh8kMyG4FC74wzitukRMIcTOoKT3EyzFZCILOPF0twiXOQn75eDINUfKBYmzYn2AA8DkAk8veQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
        <!-- CodeMirror -->
          <link rel="stylesheet" href="https://adminlte.io/themes/v3/plugins/codemirror/codemirror.css">
          <link rel="stylesheet" href="https://adminlte.io/themes/v3/plugins/codemirror/theme/monokai.css">
        <link href="../assets/css/icons.min.css" rel="stylesheet" type="text/css" />
        <link href="../assets/css/app.min.css" rel="stylesheet" type="text/css" id="app-stylesheet" />
        <link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" id="bootstrap-stylesheet" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/4.5.95/css/materialdesignicons.min.css" integrity="sha512-EhtFgx6fGa2B3UNje2rTcPBgryWKx7TVkcGuOsCkybAbAaWEGrWDjsMFPqJUwXf2u1qmz6BxocZvcXVmTfMG9g==" crossorigin="anonymous" />
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/themify-icons/0.1.2/css/themify-icons.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
        <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.min.css'><link rel="stylesheet" href="../assets/css/selstyle.css">
    </head>

    <body>

        <!-- Begin page -->
        <div id="wrapper">

            
            <?php include '../header/foronepager.php'; ?>

            <!-- ============================================================== -->
            <!-- Start Page Content here -->
            <!-- ============================================================== -->

            <div class="content-page">
                <div class="content">

                    <!-- Footer Start -->
                    <footer class="footer">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-md-12">
                                    <?php echo date('Y'); ?> &copy; <?php echo $projectname ?> | PHPSOLID CMS DASHBOARD by <a href="https://medaltechie.com">MedalTechie</a>
                                </div>
                            </div>
                        </div>
                    </footer>
                    <!-- end Footer -->

                </div>
                <!-- end content -->

            </div>
            <!-- END content-page -->

        </div>
        <!-- END wrapper -->
        
        <!-- sample modal content -->
        <div id="editModal" class="modal fade bs-example-modal-xl" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <form method="post" enctype="multipart/form-data" class="form-validation">
            <div class="modal-dialog modal-xl">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="myModalLabel"><?php echo $pgname ?> (OnePager)</h5>
                        <a href="<?php echo $prevurl ?>"><button type="button" class="close" onclick="history.go(-1);"  aria-hidden="true">×</button></a>
                    </div>
                    <div class="modal-body">
                        
                        <?php 
                                $getstructure = dbquery("SELECT * FROM ".$table4." WHERE pageid = '$pageid' AND strucactive = 'yes' ORDER BY strucnumber ASC")->rows;
                                
                                foreach($getstructure as $structure){
                                    $sid = $structure['id'];
                                    $strucname = $structure['strucname'];
                                    $lowerstrucname = strtolower($strucname);
                                    $lowerstrucname = str_replace(' ', '_', $lowerstrucname);
                                    
                                    $strucnumber = $structure['strucnumber'];
                                    $structype = $structure['structype'];
                                    $strucactive = $structure['strucactive'];
                                    $lowerstructype = strtolower($structype);
                                    $strucextra = $structure['strucextra'];
                                    $lowerstrucextra = strtolower($strucextra);
                                    $splt = explode(",",$lowerstrucextra);
                                    $listextra = explode(",",$strucextra);
                                    
                                    $typenme = $splt[0];
                                    $iconn = $splt[1]; 
                                    
                                    $firstextra = $listextra[0];
                                    $secondextra = $listextra[1];
                                    $thirdextra = $listextra[2]; 
                                    $fourthextra = $listextra[3];
                                    $fifthextra = $listextra[4];
                                    $sixthextra = $listextra[5]; 
                                    
                                    $date = $structure['date_created'];
                                    $ago = getdiff($date);
                            ?>
                            
                            <div class="row">

                                <div class="col-lg-12">
                                    <?php if($structype == "Text" || $structype == "Email" || $structype == "Number" || $structype == "Phone" || $structype == "Address" || $structype == "City" || $structype == "URL"){ ?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" required parsley-trigger="change" type="<?php echo $lowerstructype ?>" class="form-control <?php echo $lowerstrucname ?>" placeholder="<?php echo $strucname ?>">
                                    <?php } else if($structype == "Password"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input id="edit<?php echo $lowerstrucname ?>" required parsley-trigger="change" type="<?php echo $lowerstructype ?>" class="form-control password <?php echo $lowerstrucname ?>" placeholder="<?php echo $strucname ?>">
                                        <input type="hidden" name="<?php echo $lowerstrucname ?>" parsley-trigger="change" required  class="form-control" id="editencpassword">
                                    <?php } else if($structype == "Text Area"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <textarea name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>" rows="5" id="<?php echo $lowerstrucname ?>" placeholder="<?php echo $strucname ?>"></textarea>
                                    <?php } else if($structype == "Default Time Picker"){?>
                                        <div class="input-group">
                                            <input id="timepicker" name="<?php echo $lowerstrucname ?>" type="text" class="form-control <?php echo $lowerstrucname ?> timepicker">
                                            <div class="input-group-append">
                                                <span class="input-group-text"><i class="mdi mdi-clock"></i></span>
                                            </div>
                                        </div>
                                    <?php } else if($structype == "24Hrs Time Picker"){?>
                                        <div class="input-group">
                                            <input id="timepicker2" name="<?php echo $lowerstrucname ?>" type="time" class="form-control <?php echo $lowerstrucname ?> timepicker2">
                                            <div class="input-group-append">
                                                <span class="input-group-text"><i class="mdi mdi-clock"></i></span>
                                            </div>
                                        </div>
                                    <?php } else if($structype == "Color Picker"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input type="text" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?> default-colorpicker" id="default-colorpicker" value="#8fff00">
                                    <?php } else if($structype == "RGBA Color Picker"){?>
                                        <div id="component-colorpicker" class="input-group" title="Using format option">
                                            <input type="text" name="<?php echo $lowerstrucname ?>" class="form-control input-lg <?php echo $lowerstrucname ?>" value="#188ae2" />
                                            <span class="input-group-append">
                                                    <span class="input-group-text colorpicker-input-addon"><i></i></span>
                                            </span>
                                        </div>
                                        
                                    <?php } else if($structype == "Date Picker"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input type="date" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>" placeholder="mm/dd/yyyy">
                                    <?php } else if($structype == "Clock Picker"){?>
                                        <div class="input-group clockpicker mb-3">
                                            <input type="text" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>" value="09:30">
                                            <div class="input-group-append">
                                                <span class="input-group-text"><i class="mdi mdi-clock"></i></span>
                                            </div>
                                        </div>
                                    <?php } else if($structype == "Editor"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <textarea class="summernote edit<?php echo $lowerstrucname ?>" id="summernote" name="<?php echo $lowerstrucname ?>"></textarea>
                                        
                                    <?php } else if($structype == "Money"){?>
                                         <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                         <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" required parsley-trigger="change" type="number" class="form-control <?php echo $lowerstrucname ?>" placeholder="<?php echo $strucname ?>">
                                        
                                    <?php } else if($structype == "Select File"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>

                                        <?php if($getfirst > 0){?>
                                        <div class="row">
                                            <div class="col-sm-1">
                                                <img class="img<?php echo $lowerstrucname ?>" height="60px" width="60px" src="">
                                            </div>
                                            <div class="col-sm-11">
                                                <br>
                                                <input type="file" class="filestyle" name="<?php echo $lowerstrucname ?>" data-btnClass="btn-primary">
                                            </div>
                                        </div>
                                        <?php } else{?>

                                            <input type="file" class="filestyle" name="<?php echo $lowerstrucname ?>" data-btnClass="btn-primary">

                                        <?php } ?>

                                        <!--<input id="<?php echo $lowerstrucname ?>" name="ex<?php echo $lowerstrucname ?>" required parsley-trigger="change" type="hidden" class="form-control <?php echo $lowerstrucname ?>">-->
                                        
                                    <?php } else if($structype == "Tags Input"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input type="text" class="form-control <?php echo $lowerstrucname ?>"  name="<?php echo $lowerstrucname ?>" value="" data-role="tagsinput" placeholder="add <?php echo $lowerstrucname ?>" />
                                        
                                    <?php } else if($structype == "Icon Inputs"){?>
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text"><i class="<?php echo $iconn ?>"></i></span>
                                            </div>
                                            <input type="<?php echo $typenme ?>" id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>" placeholder="<?php echo $strucname ?>">
                                        </div>
                                        
                                    <?php } else if($structype == "Input Select"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <select id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>" >
                                            <?php for ($i = 0; $i < count($listextra); $i++){?>
                                                <option value="<?php echo $listextra[$i] ?>"><?php echo $listextra[$i] ?></option>
                                            <?php }?>
                                        </select>
                                        
                                    <?php } else if($structype == "Disabled"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" type="text" class="form-control <?php echo $lowerstrucname ?>"  readonly value="<?php echo ${$firstextra} ?>">
                                        
                                    <?php } else if($structype == "Enabled"){?>
                                        <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                        <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" type="text" class="form-control <?php echo $lowerstrucname ?>"  value="<?php echo ${$firstextra} ?>">
                                        
                                    <?php } else if($structype == "PHP Code"){?>
                                    
                                      <?php eval($strucextra); ?>
                                      
                                    <?php } else if($structype == "Hidden"){?>
                                        <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" type="hidden" class="form-control <?php echo $lowerstrucname ?>"  value="<?php echo ${$firstextra} ?>">
                                        
                                    <?php } else if($structype == "Toggle Button"){?>
                                        <div class="custom-control custom-switch">
                                            <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                            <input name="<?php echo $lowerstrucname ?>" type="checkbox" class="custom-control-input <?php echo $lowerstrucname ?>" id="<?php echo $lowerstrucname ?>">
                                            <label class="custom-control-label" for="<?php echo $lowerstrucname ?>"><strong><?php echo $strucname ?></strong></label>
                                        </div>
                                        
                                    <?php } else if($structype == "Inline Checkboxes"){?>
                                    
                                        <?php for ($i = 0; $i < count($listextra); $i++){?>
                                            <div class="checkbox form-check-inline">
                                                <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                                <input class="form-control <?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" type="checkbox" id="<?php echo $listextra[$i] ?>" value="<?php echo $listextra[$i] ?>">
                                                <label for="<?php echo $listextra[$i] ?>"> <?php echo $listextra[$i] ?> </label>
                                            </div>
                                        <?php }?>
                                        
                                    <?php } else if($structype == "Inline Radios"){?>
                                    
                                        <?php for ($i = 0; $i < count($listextra); $i++){?>
                                            <div class="radio radio-info form-check-inline">
                                                <h5 class="font-14"><?php echo $strucname ?><span class="text-danger">*</span></h5>
                                                <input class="form-control <?php echo $lowerstrucname ?>" type="radio" id="<?php echo $listextra[$i] ?>" value="<?php echo $listextra[$i] ?>" name="<?php echo $lowerstrucname ?>" checked>
                                                <label for="<?php echo $listextra[$i] ?>"> <?php echo $listextra[$i] ?> </label>
                                            </div>
                                        <?php }?>

                                    <?php } else if($structype == "Group User ID"){?>
                                        <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" type="hidden" class="form-control <?php echo $lowerstrucname ?>" value="<?php echo $role ?>">
                                   
                                    <?php } else if($structype == "Current User ID"){?>
                                        <input id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" type="hidden" class="form-control <?php echo $lowerstrucname ?>" value="<?php echo $psid ?>">

                                        
                                    <?php } else if($structype == "Relationship"){?>
                                    
                                        <?php if($fourthextra == "0" && $fifthextra == "no filter"){ ?>
                                        
                                            <select id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>">
                                                <?php $getrel = dbquery("SELECT * FROM ".$firstextra." ORDER BY id " .$thirdextra."")->rows;
                                                        foreach($getrel as $rel){
                                                        $values = $rel[$secondextra];
                                                        $rid = $rel['id'];
                                                ?>
                                                    <option value="<?php echo $rid ?>"><?php echo $values ?></option>
                                                <?php }?>
                                            </select>
                                            
                                        <?php } else if($fourthextra != "0" && $fifthextra == "no filter"){?>
                                        
                                            <select id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>">
                                                <?php $getrel = dbquery("SELECT * FROM ".$firstextra." ORDER BY id " .$thirdextra." LIMIT ".$fourthextra. "")->rows;
                                                        foreach($getrel as $rel){
                                                        $values = $rel[$secondextra];
                                                        $rid = $rel['id'];
                                                ?>
                                                    <option value="<?php echo $rid ?>"><?php echo $values ?></option>
                                                <?php }?>
                                            </select>

                                        <?php } else if($fourthextra == "0" && $fifthextra != "no filter"){?>
                                        
                                            <select id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>">
                                                <?php $getrel = dbquery("SELECT * FROM ".$firstextra." WHERE $fifthextra LIKE '%$sixthextra%' ORDER BY id " .$thirdextra."")->rows;
                                                        foreach($getrel as $rel){
                                                        $values = $rel[$secondextra];
                                                        $rid = $rel['id'];
                                                ?>
                                                    <option value="<?php echo $rid ?>"><?php echo $values ?></option>
                                                <?php }?>
                                            </select>

                                        <?php } else if($fourthextra != "0" && $fifthextra != "no filter"){?>
                                        
                                            <select id="<?php echo $lowerstrucname ?>" name="<?php echo $lowerstrucname ?>" class="form-control <?php echo $lowerstrucname ?>">
                                                <?php $getrel = dbquery("SELECT * FROM ".$firstextra." WHERE $fifthextra LIKE '%$sixthextra%' ORDER BY id " .$thirdextra." LIMIT ".$fourthextra. "")->rows;
                                                        foreach($getrel as $rel){
                                                        $values = $rel[$secondextra];
                                                        $rid = $rel['id'];
                                                ?>
                                                    <option value="<?php echo $rid ?>"><?php echo $values ?></option>
                                                <?php }?>
                                            </select>
                                        
                                        <?php } ?>
                                        
                                    <?php }?>
                                    
                                    <input type="hidden" name="dataprocess" parsley-trigger="change" required  class="form-control" id="">
                                </div>

                                
                            </div>
                            
                            <br>
                            <?php } ?>
                            <br>
                        
                            
                    </div>
                    <div class="modal-footer">
                        <a href="<?php echo $prevurl ?>"><button type="button" class="btn btn-secondary waves-effect">Close</button></a>
                        <button type="submit" class="btn btn-success waves-effect waves-light edit-btn">Update</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
            </form>
        </div>
        <!-- /.modal -->

        <script>
        
        
            $( document ).ready(function() {
                
                 var varrId = "<?php echo $upid; ?>";
                 var table7 = "<?php echo $table7; ?>";
                 var pageid = "<?php echo $pageid; ?>";

                 $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {varrId : varrId, table7 : table7, pageid : pageid, type : "edit"},
                  success: function(datanow){
                        
                      var len = datanow.length;
                      
                      
                      for(var i=0; i<len; i++){

                          var code = datanow[i].code;
                          
                          var content = datanow[i].content;
                          
                          var type = datanow[i].type;
                          var extra = datanow[i].extra;

                          if(type == "Editor"){
                              $('.edit'+ code).summernote("code", content);
                          } else if(type == "Text Area"){
                              $('.'+ code).text(content);
                          } else if(type == "Inline Checkboxes"){
                                $('.'+ code).each(function (){
                           			var checkit = $(this).is(':checked'); 
                                
                                    if(checkit == true){
                                    	$(this).prop('checked', false);
                                    } 
                                 });
                              $('#'+ content).prop('checked', true);

                          } else if(type == "Inline Radios"){
                              $('#'+ content).prop('checked', true);

                          } else if(type == "Select File"){
                                $('.img'+ code).attr("src",content);
                                $('.'+ code).val(content);
                              
                          } else if(type == "Tags Input"){
                            newcontent = content.replaceAll(" ", ",");
                            
                            $('.'+ code).tagsinput('removeAll');
                            
                            var conArray = newcontent.split(",");
                            
                            var tagAction = $('.edit'+ code);
                            
                            conArray.forEach(function (item,index) {
                                tagAction.tagsinput('add', item);
                              })
                          } else {
                              $('.'+ code).val(content);
                          }
                      
                      }
                      
                      if(type == "Relationship" || type == "Input Select"){
                         var selopts = document.getElementsByClassName(code)[0].options;
                         console.log(selopts);
                         for(var i = 0; i < selopts.length; i++) {
                            if(selopts[i].value == content) {
                               var indexopt = i;
                               document.getElementsByClassName(code)[0].selectedIndex = indexopt;
                               break;
                            }
                         }
                          
                      }
                         
                    }
                });
                
                 $('#editModal').modal('show');
             
            });
        
        
            
            $('.password').on('input',function(e){
                
               var password = $('.password').val(); 
               
               $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {password : password, type : "encpassword"},
                  success: function(datanow){
                        $('#encpassword').val(datanow);
                    }
                });
            });
            
            
             $('.password').on('input',function(e){
                
               var password = $('.password').val(); 
               
               $.ajax({
                  url: './processflow.php',
                  type: 'POST',
                  dataType: 'json',
                  data: {password : password, type : "encpassword"},
                  success: function(datanow){
                        $('#editencpassword').val(datanow);
                    }
                });
            });
            
        </script>

        <script src='https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.jquery.min.js'></script>
        <script src='https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.6.0/js-yaml.min.js'></script><script  src="../assets/js/selscript.js"></script>

        <!-- Vendor js -->
        <script src="../assets/js/vendor.min.js"></script>

        <!-- Required datatable js -->
        <script src="../assets/libs/jquery.dataTables.min.js"></script>
        <script src="../assets/libs/dataTables.bootstrap4.min.js"></script>

        <!-- Buttons examples -->
        <script src="../assets/libs/dataTables.buttons.min.js"></script>
        <script src="../assets/libs/buttons.bootstrap4.min.js"></script>
        <script src="../assets/libs/dataTables.keyTable.min.js"></script>
        <script src="../assets/libs/dataTables.select.min.js"></script>
        <script src="../assets/libs/select2.min.js"></script>
        <script src="../assets/libs/parsley.min.js"></script>
        <script src="../assets/libs/jszip.min.js"></script>
        <script src="../assets/libs/pdfmake.min.js"></script>
        <script src="../assets/libs/vfs_fonts.js"></script>
        <script src="../assets/libs/buttons.html5.min.js"></script>
        <script src="../assets/libs/buttons.print.min.js"></script>
        <script src="../assets/libs/bootstrap-tagsinput.min.js"></script>
        <script src="../assets/libs/switchery.min.js"></script>
        <script src="../assets/libs/moment.min.js"></script>
        <script src="../assets/libs/bootstrap-filestyle.min.js"></script>
        <script src="../assets/libs/bootstrap-timepicker.min.js"></script>
        <script src="../assets/libs/bootstrap-colorpicker.min.js"></script>
        <script src="../assets/libs/bootstrap-datepicker.min.js"></script>
        <script src="../assets/libs/daterangepicker.js"></script>
        <script src="../assets/libs/bootstrap-clockpicker.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.20/summernote-bs4.min.js" integrity="sha512-ZESy0bnJYbtgTNGlAD+C2hIZCt4jKGF41T5jZnIXy4oP8CQqcrBGWyxNP16z70z/5Xy6TS/nUZ026WmvOcjNIQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
        
          <script>
            $(".modal").on("shown.bs.modal", function () {
              $('.user-info').slideUp();
            });
            
            $(".modal").on("hidden.bs.modal", function () {
              $('.user-info').slideDown();
            });
            
            var tempL1 = `<div class="container">
                              <div class="row">
                                <div class="col-12">First Element</div>
                              </div>
                            </div><p>&nbsp;</p>`;
            
            var tempL2 = `<div class="container">
                              <div class="row">
                                <div class="col-6">First Element</div>
                                <div class="col-6">Second Element</div>
                              </div>
                            </div><p>&nbsp;</p>`;
            var tempL3 = `<div class="container">
                              <div class="row">
                                <div class="col-4">First Element</div>
                                <div class="col-4">Second Element</div>
                                <div class="col-4">Third Element</div>
                              </div>
                            </div><p>&nbsp;</p>`;
                            
                            
            var tempL4a = `<a class="btn btn-primary" href="#" role="button">Example Button</a>`;
            
            var tempL4b = `<br><a class="btn btn-primary" href="#" role="button">Example Button</a>`;
            
            var tempL5 = `<div class="row row-cols-1 row-cols-md-2">
                          <div class="col mb-4">
                            <div class="card">
                              <img src="..." class="card-img-top" alt="...">
                              <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                                <a class="btn btn-primary" href="#" role="button">Example Button</a>
                              </div>
                            </div>
                          </div>
                          <div class="col mb-4">
                            <div class="card">
                              <img src="..." class="card-img-top" alt="...">
                              <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                                <a class="btn btn-primary" href="#" role="button">Example Button</a>
                              </div>
                            </div>
                          </div>
                        </div><p>&nbsp;</p>`;
                        
            var tempL6 = `<div class="row row-cols-1 row-cols-md-3">
                          <div class="col mb-4">
                            <div class="card">
                              <img src="..." class="card-img-top" alt="...">
                              <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                                <a class="btn btn-primary" href="#" role="button">Example Button</a>
                              </div>
                            </div>
                          </div>
                          <div class="col mb-4">
                            <div class="card">
                              <img src="..." class="card-img-top" alt="...">
                              <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content.</p>
                                <a class="btn btn-primary" href="#" role="button">Example Button</a>
                              </div>
                            </div>
                          </div>
                          <div class="col mb-4">
                            <div class="card">
                              <img src="..." class="card-img-top" alt="...">
                              <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                                <a class="btn btn-primary" href="#" role="button">Example Button</a>
                              </div>
                            </div>
                          </div>
                        </div><p>&nbsp;</p>`;
                        
            function randomString(length, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
                var result = '';
                for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
                return result;
            }
            
            var tempL7 = ``;
            setInterval(function() {
                var random = randomString(6);
                tempL7 = `<div class="accordion" id="accordionExample">
                  <div class="card">
                    <div class="card-header" id="heading${random}">
                      <h2 class="mb-0">
                        <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapse${random}" aria-expanded="true" aria-controls="collapse${random}">
                          Collapsible Group Item #1
                        </button>
                      </h2>
                    </div>
                
                    <div id="collapse${random}" class="collapse show" aria-labelledby="heading${random}" data-parent="#accordionExample">
                      <div class="card-body">
                        Some placeholder content for the first accordion panel. This panel is shown by default, thanks to the <code>.show</code> class.
                      </div>
                    </div>
                  </div>
                </div>`; 
            }, 1000);
            
            var tempL8 = `<br>&nbsp;`;
            
            
            var AddBreak = function (context) {
              var ui = $.summernote.ui;
              
              // create temp2
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add Break</button>',
                tooltip: 'Add Break',
                click: function () {
                  context.invoke('pasteHTML', tempL8);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            
            
            var AddGrid = function (context) {
              var ui = $.summernote.ui;
              
              // create temp2
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add Grid</button>',
                tooltip: 'Add Grid',
                click: function () {
                  context.invoke('pasteHTML', tempL1);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var Add2Grid = function (context) {
              var ui = $.summernote.ui;
              
              // create temp2
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add 2 Grid</button>',
                tooltip: 'Add 2 Grid',
                click: function () {
                  context.invoke('pasteHTML', tempL2);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var Add3Grid = function (context) {
              var ui = $.summernote.ui;
              
              // create temp2
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add 3 Grid</button>',
                tooltip: 'Add 3 Grid',
                click: function () {
                  context.invoke('pasteHTML', tempL3);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var AddButton = function (context) {
              var ui = $.summernote.ui;
            
              // create temp1
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add Button</button>',
                tooltip: 'Button',
                click: function () {
                  context.invoke('pasteHTML', tempL4a);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var AddButtonBreak = function (context) {
              var ui = $.summernote.ui;
            
              // create temp1
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add Button With Break</button>',
                tooltip: 'Button With Break',
                click: function () {
                  context.invoke('pasteHTML', tempL4b);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var Add2Card = function (context) {
              var ui = $.summernote.ui;
            
              // create temp1
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add 2 Cards</button>',
                tooltip: 'Add 2 Cards',
                click: function () {
                  context.invoke('pasteHTML', tempL5);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var Add3Card = function (context) {
              var ui = $.summernote.ui;
            
              // create temp1
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add 3 Cards</button>',
                tooltip: 'Add 3 Cards',
                click: function () {
                  context.invoke('pasteHTML', tempL6);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            var AddAccordion = function (context) {
              var ui = $.summernote.ui;
            
              // create temp1
              var button = ui.button({
                contents: '<button type="button" class="btn btn-dark">Add Accordion</button>',
                tooltip: 'Add Accordion',
                click: function () {
                  random = randomString(6);
                  context.invoke('pasteHTML', tempL7);
                }
              });
            
              return button.render();   // return button as jquery object
            }
            
            $('.summernote').summernote({
                toolbar: [
                	// [groupName, [list of button]]
                	['style', ['style']],
                	['font', ['bold', 'italic', 'underline', 'clear']],
                	['font', ['fontname', 'fontsize', 'fontsizeunit', 'strikethrough', 'superscript', 'subscript']],
                    ['color', ['color']],
                    ['para', ['ul', 'ol', 'paragraph']],
                    ['height', ['height']],
                    ['table', ['table']],
                    ['insert', ['link', 'picture', 'hr','video']],
                    ['view', ['fullscreen', 'codeview', 'undo', 'redo']],
                    ['help', ['help']],
                    ['mybutton', ['addbreak', 'addgrid', 'add2grid' , 'add3grid', 'addbutton', 'addbuttonbreak', 'add2card', 'add3card', 'addaccordion']]
                ],
                styleTags: [
                    'p',
                        { title: 'Blockquote', tag: 'blockquote', className: 'blockquote', value: 'blockquote' },
                        'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
            	],
                buttons: {
                    addbreak: AddBreak,
                    addgrid: AddGrid,
                    add2grid: Add2Grid,
                    add3grid: Add3Grid,
                    addbutton: AddButton,
                    addbuttonbreak: AddButtonBreak,
                    add2card: Add2Card,
                    add3card: Add3Card,
                    addaccordion: AddAccordion
                },
                codemirror: { "theme": "ambiance" },
                height: 2,
                spellCheck: true,
                dialogsInBody: true,
                focus: true,
                disableResizeEditor: true,
                hintMode:'word',
                hintSelect:'after',
                hintDirection:'bottom',
                fontSizes: ['8', '9', '10', '11', '12', '14', '16', '18', '20', '24', '36', '48' , '64', '82', '150'],
                fontSizeUnits: ['px','pt'],
                callbacks: {
                    onImageUpload: function(files, editor, welEditable) {
                        sendFile(this, files[0], editor, welEditable);
                    },
                    onMediaDelete : function(target) { 
                        deleteFile(target[0].src);
                    }
                }
            });
            
            
            // const rng = $('.summernote').summernote('editor.getLastRange');
            
            // rng.pasteHTML(tempL1);
            
            function sendFile(selection, file, editor, welEditable) {
                var summerid = selection.id;
                data = new FormData();
                data.append("file", file);
                data.append("action", 'upload_file');
                data.append("upload_directory", 'gallery');
                $.ajax({
                    data: data,
                    type: "POST",
                    url: 'summerload.php',
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function(url) {
                        var image = $('<img>').attr('src', url);
                        $('#'+summerid).summernote("insertNode", image[0]);
                    }
                });
            }
            
            function deleteFile(src) {
                data = new FormData();
                data.append("img_src", src);
                data.append("action", 'remove_file');
                data.append("delete_directory", 'gallery');
                $.ajax({
                    data: data,
                    type: "POST",
                    url: 'summerload.php',
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function(resp) {
                        console.log(resp);
                    }
                });
            }
        </script>
        
        <!-- Responsive examples -->
        <script src="../assets/libs/dataTables.responsive.min.js"></script>
        <script src="../assets/libs/responsive.bootstrap4.min.js"></script>

        <!-- Datatables init -->
        <script src="../assets/js/datatables.init.js"></script>
        <script src="../assets/libs/form-advanced.init.js"></script>
        
        <!-- CodeMirror -->
        <script src="https://adminlte.io/themes/v3/plugins/codemirror/codemirror.js"></script>
        <script src="https://adminlte.io/themes/v3/plugins/codemirror/mode/css/css.js"></script>
        <script src="https://adminlte.io/themes/v3/plugins/codemirror/mode/xml/xml.js"></script>
        <script src="https://adminlte.io/themes/v3/plugins/codemirror/mode/htmlmixed/htmlmixed.js"></script>
        
        <!-- App js -->
        <script src="../assets/js/app.min.js"></script>

    </body>
</html>