<?php
global $current_user;
$wp_create_nonce = wp_create_nonce('caoclick-' . $current_user->ID);

if (empty($_GET['post_id'])) {
    echo "<h1>出错: ID为空</h1>";
    return ;
}

$post_id = $_GET['post_id'];

$post_get = $post = get_post($post_id);
if (empty($post) || $post->post_author != get_current_user_id()) {
    echo '<h1>无法回复</h1>';

    return;
}
setup_postdata($post);
?>
<style type="text/css"></style>
<div class="feedback">
    <form>
    <?php  if(true) : ?>
        <div>
            <div class="feedback-title">
                <div class="feedback-mwz">
                    <li class="feedback-cur">工单信息</li>
                </div>
            </div>
            <div class="feedback-btx">
                <div>
                    <h4 class="form-title">工单标题：</h4>
                </div>
                <div>
                    <div class="form-group">
                        <span><?php the_title(); ?></span>
                        <br>
                    </div>
                </div>
            </div>
            <div class="feedback-btx">
                <div>
                    <h4 class="form-title">工单内容：</h4>
                </div>
                <div>
                    <div id="editor22" class="form-group">
                        <?php the_content(); ?>
                    </div>
                </div>

            </div>
        </div>
        <div class="form-box">

            <div id="comments" class="comments-area">
                <div>
                    <h4 class="form-title">工单回复：</h4>
                </div>
                <?php
                $comments = get_comments(array("post_id" => $post_id,'order'=>'asc'));
                if($comments){
                    foreach ($comments as $v){
                        ?>
                        <div class="row" style="height: 100px;">
                            <div style="width: 100%">
                                <div class="form-group">
                                    <div style="float: left"><span class="avatar-img"><?php echo get_avatar($v->user_id,26).'  '.$v->comment_author; ?>:</span>
                                        <br>
                                        <br>
                                        <?php echo $v->comment_content; ?>
                                    </div>
                                    <div style="float: right"><?php echo $v->comment_date; ?></div>
                                </div>
                            </div>
                        </div>
                <?php } } ?>
            </div>

            <div class="">
                <div class="">
                    <h4 class="form-title">回复*</h4>
                </div>
                <div class="">
                    <div id="editor2">
                    </div>
                </div>

            </div>
        </div>
        <a href="#" class="ceo-display-inline-block profile-btn btn-reply_feedback btn btn--primary" data-nonce="<?php echo $wp_create_nonce; ?>" style="margin-top: 20px;">回复</a>
    <?php else: ?>
        <div class="form-box">
            <div class="col-12 _404">
                <div class="_404-inner">
                    <div class="entry-content">您没有发布权限，请联系管理员开通！</div>
                </div>
            </div>
        </div>
    <?php endif; ?>
    </form>
</div>

<script src="<?php echo get_template_directory_uri() ?>/static/assets/js/wangEditor.min.js"></script>
<script src="<?php echo get_template_directory_uri() ?>/static/assets/js/sweetalert2.min.js"></script>
<?php
wp_enqueue_style('sweetalert2css', get_template_directory_uri()."/static/assets/css/sweetalert2.min.css");
?>

<script>
    $(function () {


//发布任务 go-task_write_post write_post

        if (document.getElementById("editor2")) {

            //插入编辑器
            var E = window.wangEditor
            var editor2 = new E('#editor2')
            editor2.customConfig.uploadImgServer = erphpdown_ajax_url
            // 将图片大小限制为 2M
            editor2.customConfig.uploadImgMaxSize = 2 * 1024 * 1024
            editor2.customConfig.uploadImgParams = {
                nonce2: $(".go-add_feedback").data("nonce2"),
                action: 'update_img'
            }
            editor2.customConfig.uploadFileName = 'file'
            editor2.create()

        }

        $('.btn-reply_feedback').on('click', function(event){
            event.preventDefault()
            var _this = $(this)
            var deft = _this.html()
            var feedback_content = editor2.txt.html();

            $.post(erphpdown_ajax_url,
                {
                    post_id: <?php echo $post_id; ?>,
                    feedback_content: feedback_content,
                    nonce: "<?php echo wp_create_nonce('feedback') ?>",
                    action: 'reply_feedback'
                },
                function (data) {
                    if (data.status == 1) {
                        _this.html(deft)
                        Swal.fire({
                            html: data.msg,
                            type: 'success',
                        }).then((result) => {
                            if (result.value) {
                                location.reload()
                            }
                        })
                    }else{
                        _this.html(deft)
                        Swal.fire('',data.msg,'warning')
                    }
                }
            );
        });
    })
</script>
<style>

.feedback{
    padding: 30px;
}
.feedback-title{
    margin-bottom: 20px;
}
.feedback-mwz{
    display: block;
    width: 100%;
    background: #fff;
    border-bottom: #f8f8f8 1px solid;
    height: 45px;
    line-height: 31px;
}
.feedback-cur{
    display: inline-block;
    border-bottom: #036eff 2px solid;
    height: 43px;
    color: #036eff;
}
.feedback .mwz {
    display: block;
    width: 100%;
    background: #fff;
    border-bottom: #e5e5e5 1px solid;
    height: 45px;
    line-height: 45px;
}
.feedback .mwz li.cur {
    display: inline-block;
    color: #333;
    border-bottom: var(--primary-color) 1px solid;
    margin-bottom: -3px;
    height: 45px;
    color: var(--primary-color);
}
.avatar-img img{
    display: inline;
    border-radius: 50px;
    margin-top: -4px;
}
.form-group {
    margin-bottom: 15px;
    position: relative;
}
label {
    display: inline-block;
    margin-bottom: 5px;
    vertical-align: middle;
}
.comments-area {
    background-color: #fff;
    border-radius: 4px;
    padding: 30px 0px 30px 0px;
    margin-top: 0;
}
.form-title{
    margin-bottom: 10px;font-size: 17px;
}
.feedback-btx{
    border-bottom: 1px solid #f4f4f4;margin-bottom: 15px;
}
.form-group p{
    border-radius: 4px;
    color: #888;
    background: #f8f8f8;
    padding: 10px 10px;
    font-size: 14px;
}
.night .feedback-btx{
    border-bottom: 1px solid #151617;margin-bottom: 15px;
}
.night .feedback .mwz {
    background: #151617;
    border-bottom: #111111 1px solid;
}
.night .comments-area {
    background-color: #151617;
}
.night .form-title {
    color: #999;
}
@media (max-width: 800px) {
.feedback{
    font-size: 12px;
    padding: 0!important;
}
.form-title {
    font-size: 12px;
}
.form-group {
    font-size: 12px;
}
}
</style>
