<?php 
include("../includes/config.php");
include("../includes/function.php");
include("../includes/session.php");

$session = new Session();
$database = new Database();
if(!empty($_REQUEST['num'])) {
    $standId = $_REQUEST['num'];
} else {
    $standId = "0";
}
$stands = $database->getRecords("manage_stands", "parent_id='0'", "created_on ASC");
$standDetails = $database->getRecord("manage_stands", "num='".$standId."'");
if(!empty($standId)) {
    $documents = $database->getRecords("manage_stands", "parent_id='".$standId."'", "created_on ASC");
} else {
 $documents = $database->getRecords("manage_stands", "parent_id='0'", "created_on ASC");   
}

include("includes/header.php");
?>
    <body class="top-navbar-fixed">
        <div class="main-wrapper">

            <?php echo include("includes/menu.php");?>

            <!-- ========== WRAPPER FOR BOTH SIDEBARS & MAIN CONTENT ========== -->
            <div class="content-wrapper">
                <div class="content-container">
                    <?php include("includes/sidebar.php");?>

                    <div class="main-page">
                        <div class="container-fluid">
                            <div class="row page-title-div">
                                <div class="col-md-6">
                                    <h4 class="title">Stand Images</h4>
                                    <p class="sub-title">One stop solution for perfect admin dashboard!</p>
                                </div>
                                
                                <div class="col-md-6" style="text-align:right;">
                                    <ul class="nav navbar-nav navbar-right" data-dropdown-in="fadeIn" data-dropdown-out="fadeOut">
                                        <li>
                                    <a href="javascript:void(0);" class="dropdown-toggle bg-primary" data-toggle="modal" data-target="#modal2"><i class="fa fa-plus-circle"></i> Add New</a>
                                    </li>
                                    </div>
                                
                            </div>
                            <!-- /.row -->
                            <div class="row breadcrumb-div">
                                <div class="col-md-6">
                                    <ul class="breadcrumb">
            							<li><a href="<?php echo CMS_ROOT;?>dashboard.php"><i class="fa fa-home"></i> Home</a></li>
            							<li class="active">Stands</li>
            						</ul>
                                </div>
                                
                            </div>
                            <!-- /.row -->
                        </div>
                        <!-- /.container-fluid -->

                        <section class="section pt-10">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-md-12">
                                        
                                    <table id="example" class="display table table-striped table-bordered" cellspacing="0" width="100%">
                                                    <thead>
                                                        <tr>
                                                            <th width="300">Name</th>
                                                            <?php if(!empty($standId)) {?><th width="400">Image</th><?php } ?>
                                                            <th width="150">Created On</th>
                                                            <th width="150">Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach($documents as $record):
                                                        $exhibitors = $database->getRecord("manage_company_profile", "num='".$record['company_id']."'");?>
                                                        <tr>
                                                            <td><?php echo $record['title'];?></td>
                                                            <?php if(!empty($standId)) {?><td><a href="<?php echo $record['image'];?>" target="_blank"><img src="<?php echo $record['image'];?>" style="width:200px;"></a></td><?php }?>
                                                            <td><?php echo date("d-m-Y", strtotime($record['created_on']));?></td>
                                                            <td><button type="button" class="btn btn-success icon-only edit-record" data-title="<?php echo $record['title'];?>" data-count="<?php echo $record['count'];?>" data-url="<?php echo $record['image'];?>" data-id="<?php echo $record['num'];?>" data-toggle="modal" data-target="#modal2"><i class="fa fa-pencil"></i></button>
                                                            <?php if(empty($standId)) {?><button type="button"  onClick="location.href='<?php echo CMS_ROOT;?>stands.php?num=<?php echo $record['num'];?>'" class="btn bg-primary-500 icon-only edit-record"><i class="fa fa-eye"></i></button><?php } ?> <button type="button" data-id="delete-<?php echo $record['num'];?>" class="btn btn-danger del_url icon-only"><i class="fa fa-trash-o"></i></button></td>
                                                        </tr>
                                                        <?php endforeach;?>
                                                    </tbody>
                                                </table>
                                                </div>
                                </div>
                                <!-- /.row -->
                            </div>
                            <!-- /.container-fluid -->
                        </section>
                        <!-- /.section -->

                        
                    </div>
                    <!-- /.main-page -->

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

        </div>
        <!-- /.main-wrapper -->
        
        <!-- Modal -->
        <div class="modal fade draggable" id="modal2" tabindex="-1" role="dialog" aria-labelledby="modal2Label">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <form name="add_update_record" id="add_update_record" action=""  method="post">
                    <div class="modal-header draggable-handle">
                        <h4 class="modal-title" id="modal2Label"><i class="fa fa-arrows"></i> Add Stand <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button></h4>
                    </div>
                    <div class="modal-body">
                        
                            <input type="hidden" name="action" value="updateStand">
                            <input type="hidden" name="tablename" id="tablename" value="manage_stands">
                            <input type="hidden" name="record_id" id="record_id" value="">
                            <input type="hidden" name="main_stand" id="main_stand" value="<?php echo $standId;?>">
                            <input type="hidden" name="user_id" id="user_id" value="<?php echo $session->get('userAdminID');?>">
                            <div class="form-group">
                        		<label for="inputVideoTitle">Title</label>
                        		<input type="text" class="form-control" id="sTitle" name="sTitle" placeholder="Stand Title">
                        	</div>
                        	<div class="form-group" <?php if(!empty($standId)) {?> style="display:none;"<?php }?>>
                        		<label for="inputVideoCount">No. Hotspot</label>
                        		<input type="number" class="form-control" id="count" name="count" placeholder="No. Images" value="<?php echo $standDetails['count'];?>">
                        	</div>
                        	
                        	<?php 
                        	    foreach($stands as $record):
                        	    $imageP = $record['count'];?>
                        	    <div id="standId<?php echo $record['num'];?>"  class="hides" style="display:none;">
                        	    <?php for ($x=1;$x<=$imageP;$x++) {?>
                        	    <label for="inputVideoCount">Image Position <?php echo $x;?></label>
                        		<input type="text" class="form-control" name="imageP<?php echo $i;?>" placeholder="Image Position <?php echo $x;?>" value="<?php echo $i;?>">
                        	   <?php }?> </div><?php endforeach;?>
                        	
                        	<?php if(!empty($standId)) {?>
                        	<div class="form-group" id="imgUpload">
                        		<label for="inputVideoUrl">Image</label>
                        		<input type="file" class="form-control" id="asset_doc" name="asset_doc" placeholder="Video Url"><br><br>
                        		<img src="" id="docurl" style="width:200px;">
                        	</div>
                        	<?php }?>
                    </div>
                    <div class="modal-footer">
                        <div class="btn-group" role="group">
                            <button type="button" class="btn btn-gray btn-wide btn-rounded" id="close_popup" data-dismiss="modal"><i class="fa fa-times"></i>Close</button>
                            <button type="submit" class="btn bg-success btn-wide btn-rounded"><i class="fa fa-check"></i>Save</button>
                        </div>
                        <!-- /.btn-group -->
                    </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- ========== COMMON JS FILES ========== -->
        <script src="<?php echo CMS_ROOT;?>js/jquery/jquery-2.2.4.min.js"></script>
        <script src="<?php echo CMS_ROOT;?>js/bootstrap/bootstrap.min.js"></script>
        <script src="<?php echo CMS_ROOT;?>js/pace/pace.min.js"></script>
        <script src="<?php echo CMS_ROOT;?>js/lobipanel/lobipanel.min.js"></script>
        <script src="<?php echo CMS_ROOT;?>js/iscroll/iscroll.js"></script>

        <!-- ========== PAGE JS FILES ========== -->
        <script src="<?php echo CMS_ROOT;?>js/prism/prism.js"></script>
        <script src="<?php echo CMS_ROOT;?>js/iziModal/iziModal.min.js"></script>
        <script src="<?php echo CMS_ROOT;?>js/sweet-alert/sweetalert.min.js"></script>

        <script src="<?php echo CMS_ROOT;?>js/DataTables/datatables.min.js"></script>

        <!-- ========== THEME JS ========== -->
        <script src="<?php echo CMS_ROOT;?>js/main.js"></script>
        <script>
        $(document).ready(function() {
                $("#modal12").iziModal();
                $('.modal-12-trigger').on('click', function(){
                    $('#modal12').iziModal('open', this);
                });
                
                $("#close_popup").click(function(){
                   location.reload(); 
                });
                
                $("#add_update_recorda").submit(function() {
                    var dataString = $(this).serialize();
                   $.ajax({
                    type: "POST",
                    url: "/includes/process.php",
                    async: true,
        			contentType: false,
            	    cache: false,
        			processData:false,
                    data: new FormData(this),
                    success: function(data){
                        if(data=="1") {
                            $(".modal-body").append("<div style='color:#fff; background:#5ECFB1; padding:5px;'>Data updated successfully</div>");
                        }
                    }
                  }); 
                  return false;
                });
                
                $(".close").click(function(){
                   location.reload(); 
                });
                
                $("#add_update_record").submit(function() {
                    var dataString = $(this).serialize();
                   $.ajax({
                    type: "POST",
                    url: "/includes/process.php",
                    async: true,
        			contentType: false,
            	    cache: false,
        			processData:false,
                    data: new FormData(this),
                    success: function(data){
                        console.log(data);
                        if(data=="v") {
                            $(".modal-body").append("<div style='color:#fff; background:#5ECFB1; padding:5px;'>Data updated successfully</div>");
                        }
                    }
                  }); 
                  return false;
                });
                
                $(".edit-record").click(function() {
                    var inputVideoId = $(this).attr("data-id");
                    var inputVideoTitle = $(this).attr("data-title");
                    var inputVideoCount = $(this).attr("data-count");
                    var inputVideoUrl = $(this).attr("data-url");
                    $("#record_id").val(inputVideoId);
                    $("#sTitle").val(inputVideoTitle);
                    $("#count").val(inputVideoCount);
                    $("#docurl").attr("src", inputVideoUrl);
                    $(".hides").hide();
                    $("#standId"+inputVideoId).show();
                });
                
                $(".del_url").on("click", function() {
                    var videoId = $(this).attr("data-id");
                    var dataString = "recordId="+videoId+"&tablename=manage_stands&action=ajaxRecord";
                    if (confirm("OK to submit?")) {
                    $.ajax({
                        type: "POST",
                        url: "/includes/process.php",
                        data: dataString,
                        success: function(data){
                            location.reload();
                            $(this).parent().parent().hide();
                        }
                      }); 
                    }
                });
                
                
                $('#example').DataTable();
                $( ".draggable" ).draggable({ handle: ".draggable-handle" });
        });
        </script>

        <!-- ========== ADD custom.js FILE BELOW WITH YOUR CHANGES ========== -->
    </body>
</html>
