<?php
ob_start();

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


$formname = 'Slider';
$maindb = 'slider';
$printfile = "slider_print_fr.php";
$file = 'slider.php';
$trandbexist = 0;
$keyfld = "code";
$focusfield = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
//mysqli_select_db($conn,$zdb);
include("../includes/vouchno_generate.php");
include("../includes/prev&next.php");
if (isset($_POST["submit"])) {
    $a = 1;

    $header = $_POST["header"];
    $para = $_POST["para"];
    $image = $_POST["image"];

    $mvouch = $qvouch;

    if ($a == 1) {

        $path = "MyImages/slider/";

        #####################IMAGE#####################
        $filename1 = $mvouch . ".jpg";
        $img_file1 = $path . ($filename1);
        $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);

//        $size = $_FILES["image"]["size"];

        $uploadOk = 1;
        $a = 1;

        if ($uploadOk == 1) {
            move_uploaded_file($_FILES["image"]["tmp_name"], $img_file1);
        } else {
            browse("Sorry, there was an error uploading your file.(Check Size)");
            header("refresh: 1; $file?mode=last");
        }

        ############################## MAIN  ######################################################
        $getimage = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`='$mvouch'")or die(mysqli_error($conn));
        $image = mysqli_fetch_array($getimage);
        ############################## MAIN  ######################################################
        if ($image == "" && $a == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(code,`header`,`para`, `image`)
                                      VALUES($qvouch, '$header', '$para' ,'$img_file1')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1
            mysqli_query($conn, "UPDATE `$maindb` SET `header`='$header', `para`='$para', `image`='$img_file1' WHERE code = '$mvouch'") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

$senable = "";
$cenable = "";


if (isset($_GET["delete"])) {
    $imgfile = "MyImages/slider/".$_GET[$keyfld].".jpg";
    unlink($imgfile);
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last", $conn);
}

include("../includes/linkshead.php");
?>
<script src="MyScript/slider.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-10">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <!--                        <div class="row">
                                                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                                        <label class="label-control marg">Header</label>
                                                    </div>
                                                    <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5 pad">
                                                        <input type="text"  name="header" class="form-control form header" value="<? Echo ($addflag != 1) ? $get[header] : ""; ?>">
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                                        <label class="label-control marg">Paragraph</label>
                                                    </div>
                                                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                                        <input type="text"  name="para" class="form-control form para" value="<? Echo ($addflag != 1) ? $get[para] : ""; ?>">
                                                    </div>
                                                </div>-->
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Image (1789px X 676px)</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <input type="file" class="btn btn-primary form image" name="image" id="image">
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <img class="view" src="<?php Echo ($addflag != 1) ? $get['image'] . "?a=" . mt_rand() : "" ?>" style="padding: 10px; border: #E7B315 solid 1px;" alt="Image 1" />
                            </div>
                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("../includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="table table-responsive">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr style="background-color: #E7B315;">
                                    <th>Code</th>
                                    <th>Image</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT * FROM `$maindb`")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $src = $row['image'] . "?a=" . mt_rand();

                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td class='numeric'>$row[code]</td>";
                                        echo "<td><img src='$src' style='width: 50px;height:50px'></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("../includes/linksfoot.php"); ?>
</body>
</html>
