<?php
namespace platyFramework;

/**
 * @var ptyFormModel $form
 * @var string $title
 * @var string $title_desc
 * @var string $post_url
 * @var ptyBoardItemModel $item
 * @var theme $this
 */
?>
<section class="content-header">
    <h1>
        <?= $title ?>
        <small><?= $title_desc ?></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="<?= $this->request->appUrl ?>"><i class="fa fa-dashboard"></i> Home</a>
        </li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <!-- Horizontal Form -->
            <div class="box box-info">
                <form class="form-horizontal" method="post" action="<?= $postUrl ?>" enctype="multipart/form-data">
                    <input type="hidden" name="id" value="<?= $item->id ?>">
                    <div class="box-body">
                        <div class='col-md-12'>
                            <? // , $this->request->req->sec_board
                            ?>
                            <? if ($this->signedInItem->isSuperAdmin()) { ?>
                                <?= $form->getInput("서비스고객ID", "serviceUserId"); ?>
                            <? } ?>
                            <? if ($this->request->functionName == "edit") { ?>
                                <?= $form->getInput("등록ID", "userId");?>
                                <?= $form->getDateTime("등록일", "regDateTime", "required placeholder='2018-01-01 00:00:00'"); ?>
                            <? } ?>
                            <? if ($item->commentIndex < 1) { ?>
                                <? if ($this->request->functionName == "edit") { ?>
                                    <?= $form->getInput("노출순서", "descId"); ?>
                                <? } ?>
                                <?= $form->getSelect("노출여부", "enabled", array("활성화" => "1", "비활성화" => 0)); ?>
                                <?= $form->getSelect("공개여부", "public", array("전체 조회 가능" => "1", "글쓴이만 조회 가능" => 0), "", "", 1); ?>
                                <?= $form->getInput("게시판ID", "boardName"); ?>
                                <?= $form->getInput("카테고리ID", "categoryName"); ?>
                                <?= $form->getInput("제목", "title"); ?>
                                <?= $form->getTextEdit("내용", "content"); ?>
                                <?= $form->getFile("미리보기 이미지", "previewImageUrl", "0", "onchange='checkUpload(this, 1);'"); ?>
                                <? if ($this->request->ip == "61.77.83.16") { ?>
                                    <?= $form->getInput("출처 URL", "sourceUrl", "", "출처URL"); ?>
                                <? } ?>
                                <?= $form->getInput("유튜브 ID", "youtubeId", "", "유튜브 영상ID 를 입력하세요"); ?>
                            <? } else { ?>
                                <?= $form->getTextEdit("내용", "content"); ?>
                            <? } ?>

                            <? $o = "";
                            $this->callAction("onAdminTableItemEditBottom", $this, $item, $o);
                            echo $o; ?>
                        </div>
                    </div>
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary">저장</button>
                        <button type="button" class="btn btn-default ptyGoBack pull-right">취소</button>

                        <? if (false && $this->request->functionName == "edit") { ?>
                            <? $replyUrl = "{$this->request->classUrl}/reply/" . ptyStayParams("id={$item->id}"); ?>
                            <button type="button" class="btn btn-primary" onclick="location.href='<?= $replyUrl ?>'">답글
                                달기
                            </button>
                        <? } ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <? if (true && $this->request->functionName == "edit") { ?>
        <div class="row">
            <div class="col-md-12">
                <!-- Box Comment -->
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">댓글 목록</h3>
                    </div>
                    <div class="box-footer box-comments" style="background-color: white;">
                        <? /** @var ptyboardItemModel $item */
                        foreach ($item->getComments() as $commentItem) { ?>
                            <div class="box-comment"
                                 style='margin-left: <?= strlen($commentItem->commentDepth) * 15 ?>px;'>
                                <!-- User image -->
                                <img class="img-circle img-sm" src="<?= $commentItem->user_profileImageUrl ?>" alt="">
                                <div class="comment-text">
		                            <span class="username">
		                                <a href="<?= PLATYFRAMEWORK_APP_URL ?>/ptyuser/items/?sq_userId=<?= urlencode("\"$commentItem->userId\"") ?>">
                                            <?= $commentItem->userId ?> (<?= $commentItem->user_nickName ?>)</a>
			                            <span class="text-muted pull-right"><?= $commentItem->regDateTime ?></span>
		                            </span>
                                    <? if ($commentItem->enabled == 1) { ?>
                                        <?= nl2br(html_entity_decode($commentItem->content)) ?>
                                        &nbsp;
                                        <button type="button" href="<?= $this->request->controllerUrl ?>/item/setEnableDisable/<?= ptyStayParams("id={$commentItem->id}") ?>" title='안내' content='게시물을 숨김 처리하시겠습니까?' class="ptyShowConfirmDialog pull-right btn btn-default btn-xs"><i class="fa fa-trash"></i> 숨김</button>
                                    <? } else { ?>
                                        <del><?= nl2br(html_entity_decode($commentItem->content)) ?></del>
                                        &nbsp;
                                        <button type="button" href="<?= $this->request->controllerUrl ?>/item/setEnableDisable/<?= ptyStayParams("id={$commentItem->id}") ?>" title='안내' content='게시물을 노출 처리하시겠습니까?' class="ptyShowConfirmDialog pull-right btn btn-default btn-xs"><i class="fa fa-eye"></i> 노출</button>
                                    <? } ?>
                                    <a type="button" href='<?= "{$this->request->classUrl}/edit/" . ptyStayParams("id={$commentItem->id}"); ?>' style='margin-right: 5px' class="pull-right btn btn-primary btn-xs"><i class="fa fa-edit"></i> 수정 </a>
                                </div>
                            </div>
                        <? } ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <!-- Box Comment -->
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">댓글 작성</h3>
                    </div>
                    <?
                    $commentForm = ptyFormModel::build($this->itemsModel->tableName, null, "commentItem");
                    ?>
                    <? /** @see ptyboardItemController::writeComment() */ ?>
                    <form class="form-horizontal" method="post" action="<?= $this->request->controllerUrl ?>/item/writeComment/"
                          enctype="multipart/form-data">
                        <input type="hidden" name="parentId" value="<?= $item->id ?>">
                        <div class="box-body">
                            <div class='col-md-12'>
                                <?= $commentForm->getTextEdit("", "comment"); ?>
                            </div>
                        </div>
                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">댓글 추가</button>
                        </div>

                    </form>

                </div>

                <!-- /.box -->
            </div>

        </div>
    <? } ?>
</section>

<script src="<?= $this->request->moduleUrl ?>/ptyadmin/resources/AdminLTE-2.4.5/bower_components/ckeditor/ckeditor.js"></script>

<script>
    function checkUpload(v, i) {
        var txtId = "{첨부이미지" + i + "}";

        // 마지막 위치로 이동
        var html = $("#content").val();
        $("#content").focus().val("").val(html);

        // 텍스트 검색
        if (html.search(txtId) >= 0)
            return;

        if (v.files[0]) {
            var fileSize = v.files[0].size;
            CKEDITOR.instances['content'].insertText(txtId + "\n");
        }
    }

    $(".btnRemoveComment").click(function () {
        itemId = $(this).attr("itemId");
        if (confirm("댓글을 숨기시겠습니까?")) {
            ptyPost("<?=$this->request->appUrl?>/ptyboard/item/setDisable/", {id: itemId, returnCode: "json"}, function (result) {
                if (result == true) {
                    window.location.reload();
                }

            });
        }
    });

    $(".btnEnableComment").click(function () {
        itemId = $(this).attr("itemId");
        if (confirm("댓글을 노출하시겠습니까?")) {
            ptyPost("<?=$this->request->appUrl?>/board/api/setEnable/", {id: itemId, returnCode: "json"}, function (result) {
                if (result == true) {
                    window.location.reload();
                }

            });

        }
    });

</script>