<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.filter.output');
JHtml::_('bootstrap.framework');
$doc = JFactory::getDocument();
$mainframe = JFactory::getApplication();
$doc->addStyleSheet(
    JURI::root().
    'components/com_marketplace/css/csspin-meter.css'
);
$doc->addStyleSheet(
    JURI::root().
    'components/com_marketplace/css/asktoadmin.css'
);
$adminemail = $mainframe->getCfg('mailfrom');
$param  = JComponentHelper::getParams('com_marketplace');
$bgcolor=trim($param->get('head_bg_color'));
$textcolor=trim($param->get('head_txt_color'));
$menuid=plgSystemMarketplace::getMenuId('index.php?option=com_marketplace&view=asktoadmin');
$user = JFactory::getUser();
$model = $this->getModel();
$ask_data = $model->getSellerMessage($user->id);
?>
<div class="mp_ask_view">
    <div class="ask_head">
        <span class="detail_design"></span>
        <span class="detail_text"><?php echo JText::_('ADMIN_QUERIES')?></span>
    </div>
    <div class="ask-body" id="horizontalTab" >
        <div class="button-block">
            <a href="javascript:void(0);" class="ask_button btn btn-warning">
                <?php echo JText::_('ASK_TO_ADMIN');?>
            </a>
        </div>
        <div class="conver-block">
        <?php
        if (!count($ask_data)) {
        ?>
            <p style="text-align:center;">
                <?php echo JText::_('NO_CONVER_TO_ADMIN');?>
            </p>
        <?php
        } else {
            $html_block = '';
            foreach ($ask_data as $key => $value) {
                $mess="";
                if (strlen($value->message)>250) {
                    $mess = "";
                    $str1 = substr($value->message, 0, 250);
                    $str2 = substr(
                        $value->message, 250, strlen($value->message)
                    );
                    $hidden_str = '<span class="hidden-more">'.$str2.
                        '<a class="wk_show_link"'.
                        'href="javascript:void(0);" onclick="showless'.
                        '(jQuery(this));"> '.JText::_('SHOW_LESS').
                        '</a>'.'</span>';
                    $show_more=$str1.
                        '<a class="wk_show_link show-less" href="javascript:void(0);" '.
                        'onclick="showmore(jQuery(this));"> '.JText::_(
                            'SHOW_MORE'
                        ).'</a>';
                    $mess = $show_more.$hidden_str;
                } else {
                    $mess =$value->message;
                }

                $html_block .= '<div class="message-block"><div class="message-subject">'.
                    $value->subject.'</div><div class="message-date">'.
                    $value->time
                    .'</div><div class="message-body">'.$mess.
                    '</div></div>';
            }
            echo $html_block;
        }
        ?>
        </div>
    </div>
</div>
<div id="light" class="white_content">
    <div class="head_bar">
        <span class="hd"><?php echo JText::_('ASK_TO_ADMIN');?></span>
        <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">
            <span class="cls">x</span>
        </a>
    </div>
    <form action="#" method="post" id="ask-form">
        <div class="wkoverlay_loader">
            <div class="adminask-loader">
                <div class="cp-spinner cp-meter"></div>
            </div>
        </div>
        <div class="field">
            <div class="wk_asksubarea">
                <label><?php echo JText::_('SUBJECT');?><span style="color:red;font-size:20px">*</span></label>
                <input required pattern="[a-zA-Z0-9]+[a-zA-Z0-9 ]+" class="wtd ask_sub" type="text" name="subject" value="" placeholder="<?php echo JText::_("ASK_TO_ADMIN_SUB")?>"
                />
            </div>
            <div class="wk_askmsgarea">
                <label><?php echo JText::_('QUESTION');?><span style="color:red;font-size:20px">*</span></label>
                <textarea rows="4" required pattern="[a-zA-Z0-9]+[a-zA-Z0-9 ]+" class="wtd ask_msg" name="ask" placeholder="<?php echo JText::_("ASK_TO_ADMIN_QUE")?>"></textarea>
            </div>

            <div class="wk_buttonarea">
                <input id="askbtn" class="btn " type="button" name="submit_ask" value="<?php echo JText::_("ASK_SUBMIT") ?>">
                <input id="resetbtn" class="btn" type="reset" name="reset_ask" value="<?php echo JText::_("ASK_RESET") ?>">
            </div>
        </div>
    </form>
</div>
<div id="fade" class="black_overlay"></div>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));
}
function showmore(element) {
    element.closest(".message-body").find(".hidden-more").show();
    element.hide();
}
function showless(element) {
    element.closest(".message-body").find(".show-less").show();
    element.closest(".hidden-more").hide();
}
jQuery(document).ready(function () {
    var high = jQuery(document).height();
    jQuery('.ajaximage').css('height', high + "px");
    jQuery(".ask_button").click(function(){
        jQuery('#light').css('display', 'block');
        jQuery('#fade').css('display', 'block');
    });
    jQuery('#askbtn').on('click', function (e) {
        if (jQuery('.ask_sub').val().trim() == '') {
            jQuery('.ask_sub').css('border', '1px solid red');
        } else if (jQuery('.ask_msg').val().trim() == '') {
            jQuery('.ask_msg').css('border', '1px solid red');
        } else {
            jQuery('.ask_sub').css('border', '1px solid green');
            jQuery('.ask_msg').css('border', '1px solid green');
            jQuery('#ask-data').addClass('mail-procss');
            var subject = jQuery('.ask_sub').val();
            var message = jQuery('.ask_msg').val();
            jQuery(".wkoverlay_loader").show();
            jQuery.ajax({
                url: '<?php echo JRoute::_("index.php?option=com_marketplace&view=asktoadmin&task=ask_to_admin", false)?>',
                beforeSend: function () {
                    jQuery('body').css({
                        'overflow': 'hidden'
                    });
                    jQuery(document).scrollTop(0);
                    jQuery('.ajaximage').show();
                },
                complete: function () {
                    jQuery('.ajaximage').hide();
                },
                data: {
                    subject: subject,
                    message: message
                },
                type: 'post',
                dataType: 'html',
                success: function (d) {
                    jQuery('#ask-data').removeClass('mail-procss');
                    if (d == '1') {
                        jQuery('.field').css("display", "none");
                        jQuery('.head_bar').css('display', 'none');
                        jQuery('#resetbtn').trigger('click');
                        jQuery('#light').css('display', 'none');
                        jQuery('#fade').css('display', 'none');
                        jQuery('.field').css("display", "block");
                        jQuery('.head_bar').css('display', 'block');
                        jQuery('#resetbtn').trigger('click');
                        swal({
                            title: "<?php echo JText::_('Success');?>",
                            text: '<?php echo JText::_(
                                'MAIL_SEND'
                            ); ?>',
                            timer: 5000,
                            type: "success",
                            showConfirmButton: false
                        });
                        jQuery('body').css({
                            'overflow': 'auto'
                        });
                        window.parent.location.href = "<?php echo JRoute::_('index.php?option=com_marketplace&view=asktoadmin&Itemid='.$menuid, false)?>";
                    } else {
                        jQuery('.field').css("display", "none");
                        jQuery('.head_bar').css('display', 'none');
                        jQuery('#resetbtn').trigger('click');
                        jQuery('#light').css('display', 'none');
                        jQuery('#fade').css('display', 'none');
                        jQuery('.field').css("display", "block");
                        jQuery('.head_bar').css('display', 'block');
                        jQuery('#resetbtn').trigger('click');
                        swal({
                            title: "<?php echo JText::_('Failed');?>",
                            text: d,
                            timer: 5000,
                            type: "error",
                            showConfirmButton: false
                        });
                        jQuery('body').css({
                            'overflow': 'auto'
                        });
                        window.parent.location.href = "<?php echo JRoute::_('index.php?option=com_marketplace&view=asktoadmin&Itemid='.$menuid, false)?>";
                    }
                }
            });
        }
    });
    jQuery('body').on('click', '.cls', function () {
        jQuery('#light').css('display', 'none');
        jQuery('#fade').css('display', 'none');
    });
    jQuery('#resetbtn').on('click', function () {
        jQuery('.wtd').css("border-color", "#ccc");
    });
});
</script>
<?php
$style = "
.ask_head {
    background-color: ".$bgcolor.";
    width: 99.5%;
    height: 40px;
    font-size: 18px;
    font-weight: bold;
    text-transform: uppercase;
    color: ".$textcolor.";
}
.detail_text {
    color: ".$textcolor.";
    display: block;
    float: left;
    margin: 12px 10px;
}
";
$doc->addStyleDeclaration($style);