<?php
    global $wpdb, $objCloudSlider;	
    $curPage = (!empty($_GET['paged']) && is_numeric($_GET['paged'])) ? (int) $_GET['paged'] : 1;
    $limit= '10';
    $objCloudSlider->fnDeleteUnsavedSliders();
    $maxItem=$objCloudSlider->fnGetAllSliderCount();        
    $maxPage = ceil($maxItem / $limit);
    $maxPage = $maxPage ? $maxPage : 1;
    $newlimit =  ($limit * $curPage - $limit).', '.$limit;
    $slider_result=$objCloudSlider->fnGetAllSliderData($newlimit);
    $addNewLink='admin.php?page=cloud-slider&action=edit&type=new';
    $helpUrl='http://webuddysoft.com/cslider/documentation-wp';
?>
<div class="cloud_slider_panel">
    <?php if (isset($message) && $message !='') {?>
        <div class="cs_msg_right"><i class="fa fa-check"></i><?php echo $message; ?></div>
        <?php }?>
    <div class="main_heading">
        <h2>
            Cloud Slider
            <a href="<?php echo $addNewLink;?>" class="btn-link">Add New</a>
            <a href="<?php echo $helpUrl;?>" class="btn-link" target="_new">Help</a>
        </h2>
    </div>
    <?php
        if(isset($_GET["message"]))
        {
            if($_GET["message"]=="error")
            {
            ?>
            <div class="error">Your account does not have the necessary permission you chose, and your settings have not been saved to prevent locking yourself out of the plugin.</div>
            <?php
            }
            else if($_GET["message"]=="success")
                {
                ?>
                <div class="updated">Permission changes has been updated.</div>
                <?php
                }
                else if($_GET["message"]=="error_import")
                    {
                    ?>
                    <div class="error">Only Zip file is allowed.</div>
                    <?php
                    }
        }
    ?>
    <div class="slider-list-container">
        <form name="frmslider_list" id="frmslider_list" method="post" action="admin.php?page=cloud-slider-list&action=multiple">
            <input type="hidden" id="hiddenaction" name="hiddenaction" value=""/>
            <table class="slide-table" cellpadding="0" cellspacing="0">
                <tr>
                    <th><input type="checkbox" name="checkall" id="checkall" class="checkall"/></th>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Shortcode</th>
                    <th>Slides</th>
                    <th>Created</th>
                    <th>Modified</th>
                    <th>&nbsp;</th>
                </tr>
                <?php
                    if(count($slider_result) > 0)
                    {
                        foreach($slider_result as $slider)
                        {
                            $c_dt ="";
                            $m_dt ="";
                            $slider_layers = base64_decode($slider->data);
                            $slider_layers = unserialize($slider_layers);
                            if($slider->created && $slider->created != '0000-00-00 00:00:00')
                                $c_dt = new DateTime($slider->created);
                            if($slider->modified)
                            {
                                //$m_dt = new DateTime($slider->modified);
                                $m_dt=strtotime($slider->modified);
                            }
                        ?>
                        <tr>
                            <td class="text-center"><input type="checkbox" name="sel[]" id="sel" value="<?php echo $slider->id?>" class="checkone" /></td>
                            <td class="text-center"><?php echo $slider->id?></td>
                            <td class="text-center"><a href="admin.php?page=cloud-slider&action=edit&id=<?php echo $slider->id?>"><?php echo stripcslashes($slider->name)?></a></td>
                            <td class="text-center"><input type="text" class="input-medium" value='[cloudslider id="<?php echo $slider->id?>"]' readonly='readonly'></td>
                            <td class="text-center"><?php echo count($slider_layers['layers']);?></td>
                            <td class="text-center"><?php echo $c_dt ? date_format($c_dt, 'Y-m-d') : ''; ?></td>
                            <td class="text-center"><?php echo $m_dt ? display_time($m_dt) : ''; ?></td>
                            <td class="text-center">
                                <a href="admin.php?page=cloud-slider&action=edit&id=<?php echo $slider->id?>" class="edit_slider action-icon" title="Edit Slider"><i class="fa fa-edit"></i></a>
                                <a href="admin.php?page=cloud-slider-list&action=duplicate&id=<?php echo $slider->id?>" class="duplicate_slider action-icon" title="Duplicate Slider"><i class="fa fa-copy"></i></a>
                                <a href="admin.php?page=cloud-slider-list&action=delete&id=<?php echo $slider->id?>" class="delete_slider action-icon" title="Remove Slider"><i class="fa fa-trash"></i></a>
                            </td>
                        </tr>
                        <?php
                        }
                    ?>
                    <tr>
                        <td colspan="2">
                            <input type="button" name="duplicateall" id="duplicate-slider" value="Duplicate" class="btn-small duplicate-slider">&nbsp;&nbsp;
                            <input type="button" name="deleteall" id="delete-slider" value="Delete" class="btn-small delete-slider">
                        </td>
                        <td colspan="7">
                            <?php
                                if($maxItem > $limit)
                                {
                                ?>
                                <div class="cs_pagination">
                                    <div class="cs_pages">
                                        <span class="total_slider"><?php echo $maxItem ?> items</span>
                                        <span class="pagination-links">
                                            <a class="first-page" title="Go to the first page" href="<?php echo ($curPage > 1) ? 'admin.php?page=cloud-slider-list' : 'javascript:void(0);' ?>">&#171;</a>
                                            <a class="prev-page" title="Go to the previous page" href="<?php echo ($curPage > 1) ? 'admin.php?page=cloud-slider-list&amp;paged='.($curPage-1) : 'javascript:void(0);'; ?>">&#139;</a>
                                            <input class="current-page" title="Current page" id="cs_cur_page" type="text" name="paged" value="<?php echo $curPage ?>" size="1"> of
                                            <span class="total-pages"><?php echo $maxPage ?></span>
                                            <a class="next-page" title="Go to the next page" href="<?php echo ($curPage < $maxPage) ? 'admin.php?page=cloud-slider-list&amp;paged='.($curPage+1) : 'javascript:void(0);' ?>">&#155;</a>
                                            <a class="last-page" title="Go to the last page" href="<?php echo ($curPage < $maxPage) ? 'admin.php?page=cloud-slider-list&amp;paged='.$maxPage : 'javascript:void(0);' ?>">&#187;</a>

                                        </span>
                                    </div>
                                </div>
                                <?php
                                }
                            ?>
                        </td>
                    </tr>
                    <?php
                    }
                    else
                    {
                    ?>
                    <tr><td colspan="7" align="center">No Sliders.</td></tr>
                    <?php
                    }
                ?>
            </table>
        </form>
    </div>


    <div class="half-wrapper" style="float:left;">
        <div class="import-export-list">
            <form method="post" action="admin.php?page=cloud-slider-list&action=import-export" name="frm_import_export" id="frm_import_export" enctype="multipart/form-data">
                <input type="hidden" id="hidden_action" name="hidden_action" />
                <table class="slide-table" cellpadding="0" cellspacing="0">
                    <tr>
                        <th colspan="3">Import & Export Sliders</th>
                    </tr>
                    <tr>
                        <td class="text-right">Import Sliders</td>
                        <td><input type="file" name="import_file" id="import_file" accept="application/zip"/></td>
                        <td><input type="button" name="import" value="Import" class="btn-small importfile"></td>
                    </tr>
                    <tr>
                        <td class="text-right" valign="top">Export Sliders</td>
                        <td>
                            <select name="export_sliders[]" multiple="multiple" class="element-cls select-small" id="export_sliders">
                                <option value="-1">All Sliders</option>
                                <?php

                                    $slider_result=$objCloudSlider->fnGetAllSliderData();
                                    foreach($slider_result as $slider)
                                    {
                                        $slider_layers = base64_decode($slider->data);
                                        $slider_layers = unserialize($slider_layers);
                                    ?>

                                    <option value="<?php echo $slider->id?>">#<?php echo $slider->id?> - <?php echo stripcslashes($slider->name); ?></option>
                                    <?php
                                    }
                                ?>
                            </select>
                            <br><br>
                            <input type="checkbox" name="export_with_image" />Export with images
                        </td>
                        <td valign="bottom"><input type="button" name="export" value="Export" class="btn-small exportslider"></td>
                    </tr>
                </table>
            </form>
        </div>

        <div class="googlefont-settings">
            <form method="post" action="admin.php?page=cloud-slider-list&action=cs-googlefonts" name="frm_cs_googlefont_update" id="frm_cs_googlefont_update">

                <?php
                    $googlefonts = get_option('cs_googlefonts', '');
                    $arr_googlefonts = explode("|", $googlefonts);
                ?>
                <table class="slide-table" cellpadding="0" cellspacing="0">
                    <tr>
                        <th colspan="2">Google Fonts</th>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <table width="100%" class="table-googlefonts">
                                <?php $flag=false;foreach ($arr_googlefonts as $googlefont) { if($googlefont!=''){?>
                                        <tr>
                                            <td width="95%"><input type="text" name="cs_googlefonts[]" class="input-long" value="<link href='http://fonts.googleapis.com/css?family=<?php echo $googlefont?>' rel='stylesheet' type='text/css'>"/></td>
                                            <td><a href="#" class="cs-googlefont-remove action-icon" title="Remove Google Font"><i class="fa fa-trash"></i></a></td>
                                        </tr>
                                        <?php }else{
                                            $flag=true;
                                    } } if($flag){?>
                                    <tr>
                                        <td width="100%">Not added any google fonts </td>
                                    </tr>
                                    <?php }?>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">							
                            <input type="submit" class="btn-small" name="cs-googlefont-update" value="Update" id="cs-googlefont-update" />
                            <input type="button" class="btn-small" name="cs-googlefont-add" value="Add" id="cs-googlefont-add" />
                            <div class="clear"></div>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>

    <div class="half-wrapper" style="float:right; margin-right: 20px;">
        <?php
            /* if user is admin then only he can change the role */
            if(current_user_can('manage_options'))
            {
            ?>
            <div class="access-permission">
                <?php
                    $cs_capability = $custom_role = get_option('cs_capability', 'manage_options');
                ?>
                <form method="post" action="admin.php?page=cloud-slider-list&action=cs-permission" name="frm_cs_permission" id="frm_cs_permission">
                    <table class="slide-table" cellpadding="0" cellspacing="0">
                        <tr>
                            <th colspan="3">Allow Cloud Slider access to users with...</th>
                        </tr>
                        <tr>
                            <td class="text-right">Role</td>
                            <td>
                                <select name="cs_role" class="element-cls cs-role">
                                    <?php if(is_multisite()) : ?>
                                        <option value="manage_network" <?php echo ($cs_capability == 'manage_network') ? 'selected="selected"' : '' ?>> <?php _e('Super Admin', 'CloudSlider') ?></option>
                                        <?php endif; ?>
                                    <option value="manage_options" <?php echo ($custom_role == 'manage_options') ? 'selected="selected"' : '' ?>> Admin</option>
                                    <option value="publish_pages" <?php echo ($custom_role == 'publish_pages') ? 'selected="selected"' : '' ?>> Editor, Admin</option>
                                    <option value="publish_posts" <?php echo ($custom_role == 'publish_posts') ? 'selected="selected"' : '' ?>> Author, Editor, Admin</option>
                                    <option value="edit_posts" <?php echo ($custom_role == 'edit_posts') ? 'selected="selected"' : '' ?>> Contributor, Author, Editor, Admin</option>
                                    <option value="custom" <?php echo ($custom_role == 'custom') ? 'selected="selected"' : '' ?>> Custom</option>
                                </select>
                            </td>						
                        </tr>
                        <tr>
                            <td class="text-right">Capability</td>
                            <td><input type="text" class="input-long" placeholder="Enter custom capability" value="<?php if($custom_role=="custom"){ $custom_role; } ?>" name="cs_capability" /></td>
                            <td></td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <input type="button" name="update-access" value="Update" id="update-access" class="btn-small">
                                <div class="clear"></div>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <?php
            }
        ?>
        <div class="update-cloudslider">
            <form method="post" action="admin.php?page=cloud-slider-list" name="frm_cs_update" id="frm_cs_update">
                <input type="hidden" id="hiddenaction" name="action" value="cs_update"/>
                <!--<input type="hidden" id="page" name="page" value="cloud-slider-list"/>-->
                <?php
                    $cscode=get_option('cs_code', '');
                    $username = get_option('cs_envato_username', '');
                    $apikey = get_option('cs_envato_api_key', '0');
                ?>
                <table class="slide-table" cellpadding="0" cellspacing="0">
                    <tr>
                        <th colspan="2">Auto-updates | Update notifications and one-click installation</th>
                    </tr>
                    <tr>
                        <td class="text-right">Envato Username</td>
                        <td><input type="text" name="cs_envato_username" id="cs_envato_username" class="input-long" placeholder='Envato Username' value="<?php echo $username; ?>"/></td>
                    </tr>
                    <tr>
                        <td class="text-right">Envato API Key</td>
                        <td><input type="password" name="cs_envato_api_key" id="cs_envato_api_key" class="input-long" placeholder='Envato API Key' value="<?php echo $apikey; ?>"/></td>
                    </tr>
                    <tr>
                        <td class="text-right">Purchase code</td>
                        <td><input type="text" name="cs-code" id="cs-code" class="input-long" placeholder='Purchase Code' value="<?php echo $cscode; ?>"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="button" class="btn-small" name="cs-update" value="Update" id="cs-update" />
                            <div class="clear"></div>
                        </td>
                    </tr>
                </table>
            </form>
        </div>

        <div class="troubleshoot-settings">
            <form name="frmcs_settings" id="frmcs_settings" method="post" action="admin.php?page=cloud-slider-list&action=save_settings">
                <table cellpadding="0" cellspacing="0" class="slide-table">
                    <tr>
                        <th colspan="3">Troubleshooting & Advanced Settings</th>
                    </tr>
                    <tr>
                        <td class="text-right" width="30%">Use Google CDN version of jQuery</td>
                        <td width="10%"><input type="checkbox" class="checkbox" name="cdn_jquery" <?php echo get_option('cs_cdn_jquery', false) ? 'checked="checked"' : '' ?>></td>
                        <td>This option will likely solve "Old jQuery" issues.</td>
                    </tr>
                    <tr>
                        <td class="text-right">Include scripts in the footer</td>
                        <td><input type="checkbox" class="checkbox" name="include_script_footer" <?php echo get_option('cs_include_script_footer', false) ? 'checked="checked"' : '' ?>></td>
                        <td>Including resources in the footer could decrease load times, and solve other type of issues, but your theme might not support this method.</td>
                    </tr>
                    <tr>
                        <td class="text-right">Conditional script loading</td>
                        <td><input type="checkbox" class="checkbox" name="script_load" <?php echo get_option('cs_script_load', true) ? 'checked="checked"' : '' ?>></td>
                        <td>Increase your site's performance by loading resources only when needed. Outdated themes might not support this method.</td>
                    </tr>
                    <tr>
                        <td class="text-right">Concatenate output</td>
                        <td><input type="checkbox" class="checkbox" name="concatenate_output" <?php echo get_option('cs_concatenate_output', true) ? 'checked="checked"' : '' ?> ></td>
                        <td>Concatenating the plugin's output could solve issues caused by custom filters your theme might use.</td>
                    </tr>
                    <tr>
                        <td class="text-right">Put JS includes to body</td>
                        <td><input type="checkbox" class="checkbox" name="include_js_to_body" <?php echo get_option('cs_include_js_to_body', true) ? 'checked="checked"' : '' ?>></td>
                        <td>This is the most common workaround for jQuery related issues, and is recommended when you experience problems with jQuery.</td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <input type="submit" name="save_settings" value="Update" class="btn-small" />
                            <div class="clear"></div>
                        </td>
                    </tr>
                </table>
            </form>
        </div>

        <div class="clear"></div>
    </div>
</div>
