<?php
include 'header.php';
$page_nm = "Vinyl Flooring";
$pageUrl = "Vinyl_Flooring.php";
$imageUrl = "../upload/Vinyl_Flooring/";


if ($_GET['mode'] != '') {
    $id = (int) $_REQUEST['id'];
    if (isset($_POST['btn_submit'])) {

$status = $_POST['status'];
$image_path = $ai_core->aiUpload($_FILES['cover_image'], $imageUrl, 'image', $_POST['old_file_name']);

        if ($_POST['id'] == '' && $_POST['mode'] == 'add') {

            $add_qry = "INSERT INTO ".DB_PREFIX."vinyl_flooring SET 
				   image='".$image_path."',
                   status='".$status."'";
				   
           		 $ai_db->aiQuery($add_qry);
				$ai_core->aiGoPage($pageUrl . "?msg=1");
        }
		
		
		
		
        if ($_POST['id'] != '' && $_POST['mode'] == 'edit') {
            $editqry = "UPDATE ".DB_PREFIX."vinyl_flooring SET 
                   image='".$image_path."',
                   status='".$status."' WHERE id=" . $id;
				   
            $ai_db->aiQuery($editqry);
			
			$ai_core->aiGoPage($pageUrl . "?msg=2");
        }
    }
    if ($_GET['mode'] == 'delete' && $id != '') 
	{
		$image_name = $ai_core->aiGetValue("tbl_vinyl_flooring", 'image', 'id', $id);
        $ai_core->aiDelete($imageUrl.$image_name);
		
		$qry_del_su = "Delete from ".DB_PREFIX."vinyl_flooring WHERE id=".$id;
        $ai_db->aiQuery($qry_del_su);
		
		$ai_core->aiGoPage($pageUrl . "?msg=3");
    }
	
	if ($id != '') 
	{
        $qry = "SELECT * FROM ".DB_PREFIX."vinyl_flooring WHERE id=" . $id;
        $row = $ai_db->aiGetQueryObj($qry);
	}
} 
else
{
    $qry = "SELECT * FROM ".DB_PREFIX."vinyl_flooring";
    $result = $ai_db->aiGetQueryObj($qry);
}
?>
<div class="main-panel">
    <div class="main-content">
        <div class="content-wrapper"><!-- Basic form layout section start -->
            <section id="horizontal-form-layouts">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="content-header">Manage <?php echo $page_nm; ?></div>
                    </div>
                </div>
                <?php if ($_REQUEST['mode'] != '') { ?>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-body">
                                    <div class="px-3">

                                        <form name="frm" id="frm" class="form form-horizontal" method="POST" action="" enctype="multipart/form-data" onsubmit="return chkFrm();">
                                            <input type="hidden" name="mode" id="mode" value="<?php echo $_REQUEST['mode']; ?>" />
                                            <input type="hidden" name="id" id="id" value="<?php echo $_REQUEST['id']; ?>" />                                            <div class="form-body">
                                                <h4 class="form-section"><i class="ft-life-buoy"></i>Vinyl Flooring Info</h4>
                                                
												<div class="form-group row">
											<label class="col-md-3 label-control"></label>
											<div class="col-md-9"><b>Note : Image size should be same fore all producs</b></div>
											</div>
												<div class="form-group row">
                                                    <label class="col-md-3 label-control">Image: </label>
                                                    <div class="col-md-9">

                                                        <input type="file" name="cover_image" id="cover_image" />
                                                        <?php if ($row[0]->image != '') { ?>
                                                            <img src="<?php echo $imageUrl . $row[0]->image ?>" width="200" />
                                                        <?php } ?>
                                                        <input type="hidden" name="old_file_name" id="old_file_name" value="<?php echo $row[0]->image ?>" />
                                                    </div>
                                                </div>
												
												
												<div class="form-group row">
												<label class="col-md-3 label-control" for="projectinput9">Status: </label>
													<div class="col-md-9">
														<select name="status" id="status" class="form-control">
															<option value="">Select Status</option>
															
															<option value="active" <?php if($row[0]->status == 'active'){ ?> selected="selected" <?php } ?>>Active</option>
															<option value="deactive" <?php if($row[0]->status == 'deactive'){ ?> selected="selected" <?php } ?>>Deactive</option>
														</select>
													</div>
												</div>	
												
												
											
						
						
						
						</div>
                                            <div class="form-actions">
                                                <button type="submit" name="btn_submit" id="btn_submit" class="btn btn-raised btn-primary"><i class="fa fa-check-square-o"></i>Submit</button>
												<a href="<?php echo ADMIN_URL . "" . $pageUrl; ?>"> <button type="button" class="btn btn-raised btn-warning mr-1">
                                                        <i class="ft-x"></i> Cancel
                                                    </button></a>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } else { ?>

                    <div class="col-lg-12">
                        <a href="<?php echo ADMIN_URL . $pageUrl . '?mode=add' ?>">
                            <button type="button"  style="float: right" class="btn btn-raised btn-success square btn-min-width mr-1 mb-1" >Add <?php echo $page_nm; ?></button>
                        </a>
                    </div>
                    <div class="col-lg-12 card">
                        <?php if ($_GET['msg'] != '') { ?>
                            <div class="alert   <?php if ($_GET['msg'] == '1' || $_GET['msg'] == '2' || $_GET['msg'] == '4') { ?> alert-success <?php } else { ?> alert-danger <?php } ?> alert-dismissible mb-2" role="alert" style="margin-top: 15px;">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                                <strong>
                                    <?php
                                    if ($_GET['msg'] == '1') {
                                        echo INSERT_MSG;
                                    } else if ($_GET['msg'] == "2") {
                                        echo UPDATE_MSG;
                                    } else if ($_GET['msg'] == '3') {
                                        echo DELETE_MSG;
                                    } else if ($_GET['msg'] == '4') {
										echo ACTIVE_MSG;
									} else if ($_GET['msg'] == '5') {
										echo DEACTIVE_MSG;
									}
									
                                    ?>
                                </strong>
                            </div>
                        <?php } ?>

                        <div class="card-body collapse show">

                            <div class="card-block card-dashboard">

                                <table class="table table-striped table-bordered multi-ordering">
                                    <thead>
                                        <tr>
											<th>Image</th>
											<th>Status</th>
											<th>Last Update</th>
											<th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        if (COUNT($result) > 0) {
                                            foreach ($result as $row) {
                                                ?>
                                                <tr>
													<td><img src="<?php echo $imageUrl.$row->image; ?>" style="width:100px;" /></td>
													<td><?php echo ucfirst(strtolower($row->status));?></td>
													<td><?php echo $row->created_at;?></td>
													<td>
                                                        <a href="<?php echo $pageUrl . "?mode=edit&id=".$row->id.""; ?>"><img src="assets/img/ico/edit.png" alt="Edit Icon" data-toggle="tooltip" data-placement="right" title="edit this row"/></a>
                                                        <a href="<?php echo $pageUrl . "?mode=delete&&id=".$row->id.""; ?>"><img src="assets/img/ico/delete.png" alt="Delete Icon" data-toggle="tooltip" data-placement="right" title="delete this row" onclick="return confirm('Are you sure You Want To Delete This Product');"/></a>
														
														
                                                    </td>
													
                                                </tr>
                                                <?php
                                            }
                                        }
                                        ?>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                <?php } ?>

            </section>
            <!-- // Basic form layout section end -->
        </div>
    </div>
	
	
	
											
											
<?php include ('footer.php'); ?>
<script type="text/javascript">
function chkFrm()
{
	if(document.getElementById("cover_image").value == "" && document.getElementById("old_file_name").value == "")
	{
		document.getElementById("cover_image").focus();
		alert("Please select image.");
		return false;
	}
	if(document.getElementById("status").value == "")
	{
		document.getElementById("status").focus();
		alert("Please select status.");
		return false;
	}
return true;
}
</script>