<!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
          <div class="container-fluid">
              <div class="row mb-2">
                  <div class="col-sm-6">
                      <h1>404 Error Page</h1>
                  </div>
                  <div class="col-sm-6">
                      <ol class="breadcrumb float-sm-right">
                          <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>">Dashboard</a></li>
                          <li class="breadcrumb-item active">404 Error Page</li>
                      </ol>
                  </div>
              </div>
          </div><!-- /.container-fluid -->
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="error-page">
              <h2 class="headline text-warning"> 404</h2>

              <div class="error-content">
                  <h3><i class="fas fa-exclamation-triangle text-warning"></i> Oops! Page not found.</h3>

                  <p>
                      We could not find the page you were looking for.
                      Meanwhile, you may <a href="<?php echo base_url(); ?>">return to dashboard</a> or try using the search form.
                  </p>

                 

              </div>
              <!-- /.error-content -->
          </div><?php
                if (isset($_GET['curd'])) {
                    
                    if ($_GET['curd'] == "yes") {
                        if (isset($_GET['table'])) {
                            $tableName = $_GET['table'];
                            echo ' <h2 class="headline text-warning"> GENERATED CURD</h2>';
                            echo 'Create Code<textarea rows=10 class="form-control col-12">' . htmlentities(createCODE($tableName)) . '</textarea>';

                            echo 'Listing Code<textarea rows=10 class="form-control col-12">' . htmlentities(listCODE($tableName)) . '</textarea>';
                            echo 'Edit Form Code<textarea rows=10 class="form-control col-12">' . htmlentities(editCODE($tableName)) . '</textarea>';

                            echo 'Controller Code<textarea rows=10 class="form-control col-12">' . htmlentities(controllerCode($tableName)) . '</textarea>';
                        } else
                            echo "Table not defined as table=%tablename% in querystring";
                    } else
                        echo "CURD option 'yes' is needed in querystring";
                }

                ?>
          <!-- /.error-page -->
      </section>

      <!-- /.content -->
  </div>


  <?php
    function listCODE($tableName)
    {
        $ci = &get_instance();
        $commandText = "
    <div class=\"content-wrapper\">\n    <div class=\"content-header\">\n    <div class=\"container-fluid\">\n    <div class=\"row mb-2\">\n    <div class=\"col-sm-6\">\n
    <h1 class=\"m-0\"><?php echo \$pageTitle; ?></h1>\n    </div>\n    <div class=\"col-sm-6\">\n    <ol class=\"breadcrumb float-sm-right\">\n
    <li class=\"breadcrumb-item\"><a href=\"<?php echo base_url();?>\">Dashboard</a></li>\n    <li class=\"breadcrumb-item active\"><?php echo \$pageTitle; ?></li>\n
    </ol>\n    </div>\n    </div>\n    </div>\n    </div>\n    <div class=\"content\">\n    <div class=\"container-fluid\">\n    <div class=\"row\">\n    <div class=\"col\">\n
    <div class=\"card card-primary card-outline\">\n    <div class=\"card-header\">\n
    <h5 class=\"m-0\"><?php echo \$pageTitle; ?> <a href=\"<?php echo site_url(\$this->uri->segment(1).\"/\".\$this->uri->segment(2).\"/add\"); ?>\" class=\"btn btn-sm btn-primary float-right\">Create New <?php echo \$pageTitle; ?></a></h5>\n
    </div>\n
    <div class=\"card-body\">\n
  ";
        $commandText .= "<?php \$TableData=\$this->db->query(\"select * from $tableName where status=1\")->result(); ?>\n";
        $commandText .= "<table class=\"table table-bordered table-striped dataTable\">\n";
        $commandText .= "<thead>\n";
        $commandText .= "<tr>\n";
        $tableFileds = $ci->db->field_data($tableName);
        foreach ($tableFileds as $tableFiled) {
            $fieldnamelabel = ucwords(str_replace("_", " ", $tableFiled->name));
            $commandText .= "<th><?php myLang('" . $fieldnamelabel . "');?></th>\n";
        }
        $commandText .= "<th class=\"table-col col-2\">Action</th>\n";
        $commandText .= "</tr>\n";
        $commandText .= "</thead>\n";
        $commandText .= "<tbody>\n";
        $commandText .= "<?php foreach(\$TableData as \$TableRow): ?>\n";
        $commandText .= "<?php foreach(\$TableRow as \$cols): \$id=\$cols; break; endforeach; ?>";
        $commandText .= "<tr>\n";
        foreach ($tableFileds as $tableFiled) {
            $commandText .= "<td><?php echo \$TableRow->" . $tableFiled->name . "; ?></td>\n";
        }
        $commandText .= "<td><a href=\"<?php echo site_url(\$this->uri->segment(1).\"/\".\$this->uri->segment(2).\"/edit/\$id\"); ?>\" class=\"btn btn-success btn-xs\">Edit</a>
  <a onClick=\"javascript:doDelete(<?php echo \$id; ?>);\" class=\"btn btn-success btn-xs\">Delete</a></td>\n";
        $commandText .= "</tr>\n";
        $commandText .= "<?php endforeach; ?>\n";
        $commandText .= "<tbody>\n";
        $commandText .= "</table>\n";
        $commandText .= "<script>function doDelete(id){if(confirm('Are you sure to delete?')){window.location='<?php echo site_url(\$this->uri->segment(1).\"/\".\$this->uri->segment(2).\"/delete/\"); ?>'+id;}}</script>";

        $commandText .= "</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n";
        return $commandText;
    }
    function createCODE($tableName)
    {
        $ci = &get_instance();
        $commandText = "
    <div class=\"content-wrapper\">\n
    <div class=\"content-header\">\n
    <div class=\"container-fluid\">\n

    <div class=\"row mb-2\">\n

    <div class=\"col-sm-6\">\n

    <h1 class=\"m-0\"><?php echo \$pageTitle; ?></h1>\n
    </div>\n
    <div class=\"col-sm-6\">\n

    <ol class=\"breadcrumb float-sm-right\">\n

    <li class=\"breadcrumb-item\"><a href=\"<?php echo base_url();?>\">Dashboard</a></li>\n

    <li class=\"breadcrumb-item active\"><?php echo \$pageTitle; ?></li>\n
    </ol>\n
    </div>\n
    </div>\n
    </div>\n
    </div>\n
    <div class=\"content\">\n

    <div class=\"container-fluid\">\n

    <div class=\"row\">\n

    <div class=\"col\">\n

    <div class=\"card card-primary card-outline\">\n
    <div class=\"card-header\">\n
    <h5 class=\"m-0\">Create <?php echo \$pageTitle; ?> <a href=\"<?php echo site_url(\$this->uri->segment(1).\"/\".\$this->uri->segment(2).\"/list\"); ?>\" class=\"btn btn-sm btn-primary float-right\">List <?php echo \$pageTitle; ?></a></h5>\n
    </div>\n
    <div class=\"card-body\">\n
  ";
        $commandText .= "<form method=POST action=\"\" enctype=\"multipart/form-data\" name=\"$tableName\">\n\t";
        $tableFileds = $ci->db->field_data($tableName);
        unset($tableFileds[0]);
        foreach ($tableFileds as $tableFiled) {
            if ($tableFiled->name != "created_on" && $tableFiled->name != "created_by" && $tableFiled->name != "status") {
                $commandText .= "<div class=\"row\">\n\t<!--COL START-->\n\t<div class=\"col\">
        ";
                if (strpos($tableFiled->name, 'desc') !== false) {
                    $commandText .= curd_createTextArea($tableFiled->name) . "\n\t";
                } else if (strpos($tableFiled->name, 'alias') !== false) {
                    //$commandText.=curd_createTextArea($tableFiled->name);
                } else if (strpos($tableFiled->name, 'remarks') !== false) {
                    $commandText .= curd_createTextArea($tableFiled->name) . "\n\t";
                } else {
                    $commandText .= curd_createTextField($tableFiled->name) . "\n\t";
                }

                $commandText .= "\n\t</div>\n\t<!--COL END-->\n\t</div>\n\t";
            }
        }
        $commandText .= "<button type=\"reset\" class=\"btn btn-default\">Reset</button> <button class=\"btn btn-primary\" type=\"submit\" name=\"submit\">Save</button>";
        $commandText .= "\n\t</form>";
        $commandText .= "</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n";
        return $commandText;
    }
    function createSaveCode($tableName)
    {
        $t = explode("_", $tableName);
        $t = $t[1];
        $ci = &get_instance();
        $commandText = "if(isset(\$_POST['submit'])){\n";
        $commandText .= "\$TableData=array(\n";
        $tableFileds = $ci->db->field_data($tableName);

        unset($tableFileds['name=>created_on']);
        unset($tableFileds['created_by']);
        unset($tableFileds['status']);
        unset($tableFileds[0]);
        $title = '';
        $alias = '';
        foreach ($tableFileds as $tableFiled) {
            if ($title == '') $title = (strpos($tableFiled->name, '_name') != 0) ? $tableFiled->name : '';
            if ($alias == '') $alias = (strpos($tableFiled->name, '_alias') != 0) ? $tableFiled->name : '';
            if ($tableFiled->name == 'created_by') $commandText .= "'" . $tableFiled->name . "'=>'admin',\n";
            elseif ($tableFiled->name == 'created_on') $commandText .= "'" . $tableFiled->name . "'=>date('Y-m-d H:i:s'),\n";
            elseif ($tableFiled->name == 'status') $commandText .= "'" . $tableFiled->name . "'=>1,\n";
            elseif ($tableFiled->name == $alias) $commandText .= "'" . $tableFiled->name . "'=>getalias(\$_POST['$title'], '$tableFiled->name', '$tableName'),\n";
            else $commandText .= "'" . $tableFiled->name . "'=>filter_var(\$_POST['$tableFiled->name']),\n";
        }
        $commandText .= "); \n";
        $commandText .= "\$this->db->insert('$tableName',\$TableData);\n";
        $commandText .= "redirect(\"master/$t\");\n";
        $commandText .= "}";
        return $commandText;
    }
    function editCODE($tableName)
    {
        $ci = &get_instance();
        $commandText = "
    <div class=\"content-wrapper\">\n    <div class=\"content-header\">\n    <div class=\"container-fluid\">\n    <div class=\"row mb-2\">\n    <div class=\"col-sm-6\">\n
    <h1 class=\"m-0\"><?php echo \$pageTitle; ?></h1>\n    </div>\n    <div class=\"col-sm-6\">\n    <ol class=\"breadcrumb float-sm-right\">\n
    <li class=\"breadcrumb-item\"><a href=\"<?php echo base_url();?>\">Dashboard</a></li>\n    <li class=\"breadcrumb-item active\"><?php echo \$pageTitle; ?></li>\n
    </ol>\n     </div>\n    </div>\n    </div>\n    </div>\n    <div class=\"content\">\n    <div class=\"container-fluid\">\n    <div class=\"row\">\n    <div class=\"col\">\n
    <div class=\"card card-primary card-outline\">\n    <div class=\"card-header\">\n
    <h5 class=\"m-0\">Create <?php echo \$pageTitle; ?> <a href=\"<?php echo site_url(\$this->uri->segment(1).\"/\".\$this->uri->segment(2).\"/list\"); ?>\" class=\"btn btn-sm btn-primary float-right\">List <?php echo \$pageTitle; ?></a></h5>\n
    </div>\n    <div class=\"card-body\">\n
  ";
        $tableFileds = $ci->db->field_data($tableName);
        $firstField = $tableFileds[0]->name;
        $commandText .= "<?php \$id=\$this->uri->segment(4); \$TableData=\$this->db->query(\"select * from $tableName where $firstField =\\\"\$id\\\" \")->row(); ?>\n";
        $commandText .= "<form method=POST action=\"\" enctype=\"multipart/form-data\" name=\"$tableName\">\n\t";

        unset($tableFileds[0]);
        foreach ($tableFileds as $tableFiled) {
            if ($tableFiled->name != "created_on" && $tableFiled->name != "created_by" && $tableFiled->name != "status") {
                $commandText .= "<div class=\"row\">\n\t<!--COL START-->\n\t<div class=\"col\">\n\t
        ";
                if (strpos($tableFiled->name, 'desc') !== false) {
                    $commandText .= curd_createTextArea($tableFiled->name, "<?php echo \$TableData->$tableFiled->name; ?>") . "\n\t";
                } else if (strpos($tableFiled->name, 'alias') !== false) {
                    //$commandText.=curd_createTextArea($tableFiled->name);
                } else if (strpos($tableFiled->name, 'remarks') !== false) {
                    $commandText .= curd_createTextArea($tableFiled->name, "<?php echo \$TableData->$tableFiled->name; ?>") . "\n\t";
                } else {
                    $commandText .= curd_createTextField($tableFiled->name, "<?php echo \$TableData->$tableFiled->name; ?>") . "\n\t";
                }

                $commandText .= "\n\t</div>\n\t<!--COL END-->\n\t</div>\n\t";
            }
        }
        $commandText .= "<button type=\"reset\" class=\"btn btn-default\">Reset</button> <button class=\"btn btn-primary\" type=\"submit\" name=\"submit\">Save</button>";
        $commandText .= "\n\t</form>";
        $commandText .= "</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n";
        return $commandText;
    }
    function editSaveCode($tableName)
    {
        $ci = &get_instance();
        $t = explode("_", $tableName);
        $t = $t[1];
        $commandText = "if(isset(\$_POST['submit'])){\n";
        $commandText .= "\$id=\$this->uri->segment(4);\n";
        $commandText .= "\$TableData=array(\n";
        $tableFileds = $ci->db->field_data($tableName);
        $index_id = $tableFileds[0]->name;
        unset($tableFileds['name=>created_on']);
        unset($tableFileds['created_by']);
        unset($tableFileds['status']);
        unset($tableFileds[0]);
        $title = '';
        $alias = '';
        foreach ($tableFileds as $tableFiled) {
            if ($title == '') $title = (strpos($tableFiled->name, '_name') != 0) ? $tableFiled->name : '';
            if ($alias == '') $alias = (strpos($tableFiled->name, '_alias') != 0) ? $tableFiled->name : '';
            if ($tableFiled->name == 'created_by') $commandText .= "'" . $tableFiled->name . "'=>'admin',\n";
            elseif ($tableFiled->name == 'created_on') $commandText .= "'" . $tableFiled->name . "'=>date('Y-m-d H:i:s'),\n";
            elseif ($tableFiled->name == 'status') $commandText .= "'" . $tableFiled->name . "'=>1,\n";
            elseif ($tableFiled->name == $alias) $commandText .= "'" . $tableFiled->name . "'=>getalias(\$_POST['$title'], '$tableFiled->name', '$tableName'),\n";
            else $commandText .= "'" . $tableFiled->name . "'=>filter_var(\$_POST['$tableFiled->name']),\n";
        }
        $commandText .= "); \n";
        $commandText .= "\$this->db->where('$index_id',\$id);\n";
        $commandText .= "\$this->db->update('$tableName',\$TableData);\n";
        $commandText .= "redirect(\"master/$t\");\n";
        $commandText .= "}";
        return $commandText;
    }
    function deleteSaveCode($tableName)
    {
        $t = explode("_", $tableName);
        $t = $t[1];
        $commandText = "";
        $ci = &get_instance();
        $tableFileds = $ci->db->field_data($tableName);
        $index_id = $tableFileds[0]->name;
        $commandText .= "\$id=\$this->uri->segment(4);\n";
        $commandText .= "\$this->db->where('$index_id',\$id);\n";
        $commandText .= "\$this->db->delete('$tableName');\n";
        $commandText .= "redirect(\"master/$t\");\n";
        return $commandText;
    }
    function curd_createTextField($fieldname, $defaultValue = "")
    {
        $fieldnamelabel = ucwords(str_replace("_", " ", $fieldname));
        $t = "
  <div class=\"form-group\">
    <label for=\"$fieldname\"><?php myLang('" . $fieldnamelabel . "');?></label>";
        if (strpos($fieldname, 'email') !== false) {
            $type = "email";
        } else if (strpos($fieldname, 'phone') !== false) {
            $type = "tel";
        } else if (strpos($fieldname, 'mobile') !== false) {
            $type = "tel";
        } else {
            $type = "text";
        }
        $t .= "<input type=\"$type\" class=\"form-control\" id=\"$fieldname\" value=\"$defaultValue\" name=\"$fieldname\">
  </div>";
        return $t;
    }
    function curd_createTextArea($fieldname, $defaultValue = "")
    {
        $fieldnamelabel = ucwords(str_replace("_", " ", $fieldname));
        $t = "
  <div class=\"form-group\">\n\t
    <label for=\"$fieldname\"><?php myLang('" . $fieldnamelabel . "');?></label>\n\t";
        $t .= "<textarea class=\"form-control\" id=\"$fieldname\" name=\"$fieldname\">$defaultValue</textarea>\n\t
  </div>";
        return $t;
    }

    function controllerCode($tableName)
    {
        $t = explode("_", $tableName);
        $t = $t[1];
        $ClassName = ucfirst($t);
        $commandText = "<?php\ndefined('BASEPATH') or exit('No direct script access allowed');\nclass $ClassName extends CI_Controller\n{\n function __construct() {\n parent::__construct();\n checkLogin();\n }\npublic function _remap(\$alias=\"\",\$params=array())\n	{\n
		\$data['dataValue']=\$this->session;\n
		\$data['pageTitle']=\"$tableName - CHANGE THIS TO APPROPRIATE TITLE\";\n
		switch(\$alias)
		{\n";
        $commandText .= "case 'add':\n";
        $commandText .= createSaveCode($tableName);
        $commandText .= "\t\tloadView(\"$t/add\",\$data);\n";
        $commandText .= "\t break;\n";
        $commandText .= "case 'edit':\n";
        $commandText .= editSaveCode($tableName);
        $commandText .= "\t\tloadView(\"$t/edit\",\$data);\n";
        $commandText .= "\t break;\n";
        $commandText .= "case 'delete':\n";
        $commandText .= deleteSaveCode($tableName);
        $commandText .= "\t break;\n";
        $commandText .= "default:\n";
        $commandText .= "loadView(\"$t/list\",\$data);\n";
        $commandText .= "}\n}\n}\n?>";
        return $commandText;
    }
    ?>