require '../inc/classes/Helpers.php';
// page vars
$page_title = "";
$id = $_REQUEST['id'];
// id required
if ($id == "") {
    header("Location:mainpage.php");
    exit;
}
// if form was submitted
if ($_POST['commit'] == "Cancel") {
    header("Location:expertisesolution_list.php");
    exit;
}
if ($_POST['commit'] == "Delete Expertise Solution") {
    $objExpertiseSolution = new ExpertiseSolution();
    $objExpertiseSolution->Delete($id);
    header("Location:expertisesolution_list.php");
    exit;
}
$objExpertiseSolution = new ExpertiseSolution($id);
include "includes/pagetemplate.php";
function PageContent()
{
    global $objExpertiseSolution;
    global $id;
    ?>

            <div class="layout laymidwidth">

                <?php 
    header("Location:expertisesolution_list.php");
    exit;
}
if ($_POST['commit'] == "Save Expertise Solution") {
    if ($id == 0) {
        // add the listing
        $objExpertiseSolution = new ExpertiseSolution();
        $objExpertiseSolution->SolutionName = $_REQUEST["solution_name"];
        $objExpertiseSolution->SolutionSubheading = $_REQUEST["solution_subheading"];
        $objExpertiseSolution->SolutionText = $_REQUEST["solution_text"];
        $objExpertiseSolution->Create();
        // redirect to listing list
        header("Location:expertisesolution_list.php");
        exit;
    } else {
        $objExpertiseSolution = new ExpertiseSolution($_REQUEST["id"]);
        $objExpertiseSolution->SolutionName = $_REQUEST["solution_name"];
        $objExpertiseSolution->SolutionSubheading = $_REQUEST["solution_subheading"];
        $objExpertiseSolution->SolutionText = $_REQUEST["solution_text"];
        $objExpertiseSolution->Update();
        // redirect to listing list
        header("Location:expertisesolution_list.php");
        exit;
    }
} else {
    if ($_REQUEST['mode'] == 'e') {
        //listing
        $objExpertiseSolution = new ExpertiseSolution($id);
        $solution_name = $objExpertiseSolution->SolutionName;
        $solution_subheading = $objExpertiseSolution->SolutionSubheading;
        $solution_text = $objExpertiseSolution->SolutionText;
function PageContent()
{
    ?>
    
        <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                // fix to preserve width of cells
                var fixHelper = function(e, ui) {
                    ui.children().each(function() {
                        $(this).width($(this).width());
                    });
                    return ui;
                };
                var saveIndex = function(e, ui) {
                    //alert("New position: " + ui.item.index());
                    //alert("Image Id: " + ui.item.attr("id"));
                    id = ui.item.attr("id").replace("img_", "");
                    $.ajax({
                        url: 'expertisesolution_list.php',
                        data: {
                            dragsort: 1,
                            idx: ui.item.index(),
                            id: id
                        },
                        type: 'POST',
                        dataType: 'html',
                        success: function (data) {
                            //alert("done");
                        },
                        error: function (xhr, status) {
                            alert('Sorry, there was a problem!');
                        }
                    });
                }; // end saveIndex
    
                $("#list_table tbody").sortable({
                    helper: fixHelper,
                    stop: saveIndex
                }).disableSelection();
    
            }); // end document.ready
        </script>
        <style>
            .icon-resize-vertical:hover {
                cursor:grab;
            }
        </style>        
    
            <div class="layout center-flex">

                <?php 
    $aLabels = array();
    $aLinks = array();
    $aLabels[0] = 'Home';
    $aLinks[0] = 'mainpage.php';
    $aLabels[1] = 'Expertise Solutions';
    $aLinks[1] = '';
    echo Helpers::CreateBreadCrumbs($aLabels, $aLinks);
    ?>

                <div class="bigbotspace flex-container space-between">
                    <p class="larger auto heading">Expertise Solutions</p>
                    <button class=" "><a href="expertisesolution_admin.php" class="button_link">Add New Expertise Solution</a></button>
                </div>
            </div>
    
            <div class="layout">
    
                <table class="tablestyle" id="list_table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Subheading</th>
                            <th class="mid">Order</th>
                            <th class="mid">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
    $objExpertiseSolution = new ExpertiseSolution();
    $oExpertiseSolution = $objExpertiseSolution->GetAllExpertiseSolution('sort_order');
    foreach ($oExpertiseSolution as $expertisesolution) {
        echo '<tr id="img_' . $expertisesolution->Id . '">' . PHP_EOL;
        echo '<td>' . $expertisesolution->Id . '</td>' . PHP_EOL;
        echo '<td>' . $expertisesolution->SolutionName . '</td>' . PHP_EOL;
        echo '<td>' . $expertisesolution->SolutionSubheading . '</td>' . PHP_EOL;
        echo '<td class="mid"><img src="img/arrow-up-down.png" /></td>' . PHP_EOL;
        echo '<td class="mid"><a href="expertisesolution_admin.php?id=' . $expertisesolution->Id . '"><img src="img/edit-icon.png" /></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="expertisesolution_delete.php?id=' . $expertisesolution->Id . '"><img src="img/delete-icon.png" /></a></td>' . PHP_EOL;
        echo '</tr>' . PHP_EOL;
    }
    ?>
                    </tbody>
                </table>
    
            </div> <!-- layout -->
    
<?php 
}