<?php
if (!isset($_SESSION['FirstName'])) {
	session_start();
} 
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
	exit;
}
include ('includes/db1config.php');
include ('../includes/mysql_connect.php');

echo "</div><h4>Marketing Materials</h4>";
echo '<div id="dialog" title="Update Material Details" style="display:none;">
    <iframe style="width:100%;height:400px;" src="updatethumbnail.php" id="iframe-add-thumbnail" class="menu-item-iframe"></iframe>
</div>';

?>
    
    <?php include("admin_globals.php"); ?>
    

    <style>
        .toPopDiv{
            height:400px !important;
        }
        .popup.add-menu-item:hover{
            cursor: pointer;
        }
    </style>
<?php //print_r($_SESSION);
if($_SESSION['masterAdmin'] == 1){ 
?>
    <form action="uploadMaterial.php" method="post" accept-charset="utf-8" id="formm" enctype="multipart/form-data">
        <label>Upload Image</label>&nbsp;&nbsp;&nbsp;
        <input style="border: 1px solid grey;border-radius: 7px;" type="file" name="image" value="" id="my_file_field" placeholder="Upload Image" required>&nbsp;&nbsp;
        <input id="save-button" type="submit" value="Upload">                    
    </form>
    
    <?php 
    $res = mysql_query('select count(*) from marketing_materials');
    $row = mysql_fetch_row($res);

    if( $row[0] > 0 ){
        ?>
    <table cellspacing="5" cellpadding="5" width="100%" style="text-align:center;margin-top:10px;" >
        <thead>
            <tr>
                <th>Name</th>
                <th>Thumbnail</th>
                <th>Detail</th>
                <th>Description</th>
                <th>Action(s)</th>
            </tr>
        </thead>
        <tbody>
        <?php
            $query = "select * from marketing_materials";
            $rt    = mysql_query($query);   

            $res = mysql_query('select count(*) from marketing_materials');
            $row = mysql_fetch_row($res);

            if( $row[0] > 0 ){
                while($nt=mysql_fetch_array($rt)){

                    if( $nt['thumbnail'] != '' ){
                        $path = '<img height="40" width="40" src="/custom/marketing/'.$nt["thumbnail"].'" alt="Thumbnail Image">';
                    } else {
                        $path = '-';
                    }

                    echo"<tr>
                        <td width='20%' style='padding:5px;' >".$nt['file_name']."</td>
                        <td width='20%' style='padding:5px;' >".$path."</td>
                        <td width='20%' style='padding:5px;' >".$nt['detail']."</td>
                        <td width='20%' style='padding:5px;' >".$nt['description']."</td>
                        <td><a href='javascript:void(0);' title='' id='save-button' class='editThumbnail' data-id='".$nt['recordkey']."' >Edit</a></td>
                    </tr>";
                }
            }
        ?>
        </tbody>
    </table>
<?php
    } else {
        echo "<h2 style='text-align:center;'>Nothing to show</h2>";
    }
} else { 
    $res = mysql_query('select count(*) from marketing_materials');
    $row = mysql_fetch_row($res);

    if( $row[0] > 0 ){

?>

    <table cellspacing="5" cellpadding="5" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Thumbnail</th>
                <th>Detail</th>
                <th>Description</th>
            </tr>
        </thead>
        <tbody>
        <?php
            $query = "select * from marketing_materials";
            $rt    = mysql_query($query);   

            while($nt=mysql_fetch_array($rt)){
                if( $nt['thumbnail'] != '' ){
                    $path = '<img height="40" width="40" src="/custom/marketing/'.$nt["thumbnail"].'" alt="Thumbnail Image">';
                } else {
                    $path = '-';
                }
                echo"<tr>
                    <td width='20%' style='padding:5px;' >".$nt['file_name']."</td>
                    <td width='20%' style='padding:5px;' >".$path."</td>
                    <td width='20%' style='padding:5px;' >".$nt['detail']."</td>
                    <td width='20%' style='padding:5px;' >".$nt['description']."</td>
                </tr>";
            }
        ?>
        </tbody>
    </table>
<?php 
    } else {
        echo "<h2 style='text-align:center;'>Nothing to show</h2>";
    }
}
?>
<script>
    $(document).ready(function() {
        $('.editThumbnail').click(function(event) {
            $( "#dialog" ).dialog({
              close: function( event, ui ) {
                    
                }
            });
            $('#iframe-add-thumbnail').attr('src','updatethumbnail.php?id='+$(this).data('id')+'');
            $(".ui-dialog").css({"width":"50%",'left':'25%'});
        });

        $("#formm").submit(function() {
            var ext = $('#my_file_field').val().split('.').pop().toLowerCase();
            if($.inArray(ext, ['gif','png','jpg','jpeg','bmp','pdf','']) == -1) {
                alert('File type not allowed');
                return false;
            }
        });


    });

</script>


<div style="clear: both;">&nbsp;</div>
			  <div style="text-align:center;"><?php echo $bottom_links; ?></div>