<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "Repair Tables";
$action = "compare_table.php";
//$action = "staffza.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$zyear = right($zdb, 4);

$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');
//browse($zyear);

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);


if(isset($_POST["submit"])) {
    $fromdb = $_POST['from'];
    $todb = $_POST['to'];
    
    $zdb = $fromdb;
    $newdb = $todb;
    if($fromdb == $todb) {
        browse("From Database And To Database Cannot Be Same.");
//        header("Location : comparetab_screen.php");
    } else {

        mysqli_select_db($conn, $zdb); // current DB

        $sql = "SHOW TABLES FROM $newdb";
        $result = mysqli_query($conn, $sql);
        $num_tabs = mysqli_num_rows($result);
        $arraytab = 0;
        while ($row = mysqli_fetch_row($result)) {
            $tableNames[$arraytab] = $row[0];
            $arraytab++; //only do this to make sure it starts at index 0
        }
        browse($num_tabs);

        foreach ($tableNames as & $tname) {
            print "<b style='font-size: 25px'>" . $tname . "</b><br>";
            browse($zdb);

            $nsql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$zdb' AND TABLE_NAME = '$tname'";
            $nresult = mysqli_query($conn, $nsql);
            $narraytab = 0;
            $nrow = mysqli_num_rows($nresult);
            if ($nrow > 0) {
                mysqli_select_db($conn, $newdb);
                $colsql = "SHOW COLUMNS FROM $tname";
                $columns = mysqli_query($conn, $colsql);
                $num_col = mysqli_num_rows($columns);

                $arraycol = 0;
                while ($col = mysqli_fetch_row($columns)) {
                    $tablecol[$arraycol] = $col[0];
                    $arraycol++; //only do this to make sure it starts at index 0
                }

                $ncolsql = "SHOW COLUMNS FROM $zdb.$tname";
                $ncolumns = mysqli_query($conn, $ncolsql);
                $narraycol = 0;
                while ($ncol = mysqli_fetch_row($ncolumns)) {
                    $ntablecol[$narraycol] = $ncol[0];
                    $narraycol++; //only do this to make sure it starts at index 0
                }

                for ($i = 0; $i <= $num_col; $i++) {
                    if ($tablecol[$i] == $ntablecol[$i]) {
//                print $tablecol[$i] . " " . $ntablecol[$i];
                    } else {
                        $chkcol = mysqli_query($conn, "SELECT COLUMN_TYPE, EXTRA FROM information_schema.columns 
                                        WHERE  `table_schema` = '$zdb'  AND `table_name` = '$tname' AND  column_name = '$tablecol[$i]'")or die(mysqli_error($conn));
                        $calcol = mysqli_num_rows($chkcol);
                        if ($calcol > 0) {
                            browse("Got the Field.");
//                    print $tablecol[$i] . "/" . $ntablecol[$i];
                        } else {
                            $key = "";
                            $data = "";
                            $autoincre = "";
                            browse('Fetching colums Details.');
//                    browse($tablecol[$i]);
                            $ndetail = mysqli_query($conn, "SELECT COLUMN_KEY,COLUMN_TYPE, EXTRA FROM information_schema.columns 
                                            WHERE  `table_schema` = '$newdb'  AND `table_name` = '$tname' 
                                            AND column_name = '$tablecol[$i]'")or die(mysqli_error($conn));
                            $dtl = mysqli_fetch_array($ndetail);
                            $keytype = $dtl["COLUMN_KEY"];
                            $datatype = $dtl["COLUMN_TYPE"];
                            $extra = $dtl["EXTRA"];

                            if (!empty($keytype)) {
                                if ($keytype == 'PRI') {
                                    $key = 'PRIMARY KEY';
                                } elseif ($keytype == 'MUL') {
                                    $key = 'MULTIPLE KEY';
                                } elseif ($keytype == 'UNI') {
                                    $key = 'UNIQUE KEY';
                                }
                            }

                            if (!empty($datatype)) {
                                $data = $datatype;
                            }

                            if (!empty($extra)) {
                                $autoincre = $extra;
                            }

//                    browse($key);
//                    browse($data);
//                    browse($autoincre);
                            if (!empty($data)) {
                                mysqli_query($conn, "ALTER TABLE $zdb.$tname ADD $tablecol[$i] $data $key $autoincre")or die(mysqli_error($conn));
                            }
                        }
                    }
                }
            } else {
                mysqli_select_db($conn, $zdb);
                browse("Creating Table.");
                mysqli_query($conn, "create table $tname LIKE $newdb.$tname") or die(mysqli_error($conn));
            }
        }
    }
}

include("includes/linkshead.php");
?>
<!--<script src="MyScript/item.js" type="text/javascript"></script>-->
</head>
<body>
    <form method='POST' >
        <!-- Default box -->
        <div>
            <div class="col-lg-5 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control ">Your DB</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 pad">
                                <input type="text" name="from" class="form-control" value="<?= $zdb ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control ">Compare with</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 pad">
                                <input type="text" name="to" class="form-control" value="<?= $zdb ?>">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 text-right pad">
                            <input type="submit" name="submit" value="Proceed" class="btn btn-primary"> 
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
