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

?>
<style type="text/css"></style>
<div class="feedback">
    <form>
    <?php  if(true) : ?>
        <div class="form-box">
            <div>
                <div class="feedback-title">
                    <div class="feedback-mwz">
                    <li class="feedback-cur">创建工单</li>
                    </div>
                </div>

                <div>
                    <div class="form-group">
                        <label class="form-title">工单标题：</label>
                        <input type="text" class="form-control" name="post_title" placeholder="请输入标题" value="" aria-required='true' required>
                    </div>
                </div>
                <?php $categories = get_categories( array('hide_empty' => 0,'taxonomy'=>'feedback-cat') );?>
                <div>
                    <div class="form-group">
                        <label class="form-title">工单分类：</label>
                        <div class="select--box">
                            <select class="fdropdown" name="post_cat">
                                <?php foreach($categories as $category){ ?>
                                <option value="<?php echo $category->name; ?>"><?php echo $category->name; ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>

            </div>
        </div>
        <div>
            <div>
                <div>
                    <h4 class="form-title">工单内容：</h4>
                </div>
                <div>
                    <div id="editor2">
                        <p></p>
                    </div>
                </div>

            </div>
        </div>
        <a href="#" class="profile-btn btn-add_feedback btn btn--primary" data-status="pending" 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-add_feedback').on('click', function(event){
            event.preventDefault()
            var _this = $(this)
            var deft = _this.html()

            var feedback_title = $("input[name='post_title']").val();
            var feedback_cat = $("select[name='post_cat']").val();
            var feedback_content = editor2.txt.html();

            if (feedback_title.length < 6) {
                _this.html(deft)
                Swal.fire('','工单标题最低6个字符','warning')
                return false;
            }

            $.post(erphpdown_ajax_url,
                {
                    feedback_title: feedback_title,
                    feedback_cat: feedback_cat,
                    feedback_content: feedback_content,
                    nonce: "<?php echo wp_create_nonce('feedback') ?>",
                    action: 'add_feedback'
                },
                function (data) {
                    if (data.status == 1) {
                        _this.html(deft)
                        Swal.fire({
                            html: data.msg,
                            type: 'success',
                        }).then((result) => {
                            if (result.value) {
                                window.location.href = '/users?pd=feedback'
                            }
                        })
                    }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;
}
.form-group label{
    font-weight: 400;
    font-size: 15px;
    color: #666;
    margin-bottom: 12px;
}
.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);
}
input[type="text"]{
    border: 0;
    border-radius: 0;
    font-family: Lato,sans-serif;
    line-height: 36px;
    margin-top: 12px;
    margin-bottom: 20px;
    padding: 0;
    transition: border-color cubic-bezier(0.77,0,0.175,1);
    width: 100%;
    border: 1px solid #f1f1f1;
}
select{
    font-family: 'Poppins',sans-serif;
    background-color: #f8f8f8;
    border: 1px solid #eeeeee;
    font-size: 14px;
    color: #aaaaaa;
    line-height: 42px;
    margin-bottom: 0;
    box-shadow: none;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    padding: 0 18px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}
.fdropdown {
    position: relative;
    width: 100%;
    border: 1px solid #ccc;
    cursor: pointer;
    background: #fff;
    margin-top: 12px;
    border-radius: 3px;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    margin-bottom: 20px;
    font-size: 14px;
    color: #aaaaaa;
    height: 36px;
    line-height: 36px;
    box-shadow: none;
    -moz-box-shadow: none;
    -webkit-box-shadow: none;
    padding: 0 18px;
}
.form-title{
    font-weight: 400;
    font-size: 16px;
    color: #666;
    margin-bottom: 12px;
}
.night .feedback .mwz {
    background: #151617;
    border-bottom: #111111 1px solid;
}
.night .fdropdown {
    border: 1px solid #111;
    background: #111;
}
@media (max-width: 800px) {
.feedback{
    font-size: 12px;
    padding: 0!important;
}
.form-title {
    font-size: 12px;
}
}
</style>