{{ define "kas/archive_article_main.htm" }}
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <title>管理文章 - 后台管理 - 看丽人</title>
    <script type="text/javascript">
        const ISGOKEEP = true;
    </script>
    {{ template "_common.htm" .}}
    <script src="{{.global.host_img}}/assets/js/jsrender.min.js"></script>
    <style type="text/css">
        .litimg{
            width: 208px;
            height: 156px;
        }
    </style>
    <script type="text/javascript">
    LoadMemberInfo();
    </script>
</head>

<body>
    {{ template "kas/_header.htm" .}}
    <main role="main">
        <div class="album py-3 bg-light">
            <div class="container">
                {{ template "kas/_nav.htm" .}}
                <div class="alert alert-dark border-bottom" id="posNav"><a href="/kas/archive/article_main">管理文章</a><span
                        id="_searchResultInfo"></span><span class="float-right"><a href="/kas/archive/article_add"
                            class="btn btn-sm btn-warning">添加文章</a></span></div>
                <div id="frmSearch">
                    <div class="form-group">
                        <label for="iptUnqID">文章唯一ID</label>
                        <input name="unqid" type="text" class="form-control" id="iptUnqID" value=""
                            placeholder="输入文章的唯一ID，内容地址为“/view/唯一ID”">
                    </div>
                    <div class="form-group">
                        <label for="iptTitle">内容标题</label>
                        <input name="title" type="text" class="form-control" id="iptTitle" value="">
                    </div>
                    <div class="form-group">
                        <label for="selTypeid">分类</label>
                        <select id="selTypeid" name="typeid" class="form-control">
                            <option value="0">请选择栏目...</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label><input id="ckIsShow" name="isshow" value="1" type="checkbox"> 首页推荐</label>
                        <label><input id="ckIsWillPub" name="iswillpub" value="1" type="checkbox"> 预发布</label>
                    </div>
                    <div class="form-group">
                        <label for="iptMoney">来源</label>
                        <input name="source" type="text" class="form-control" id="iptSource" value="">
                    </div>
                    <button id="btnSearch" class="btn btn-danger btn-block" type="submit">搜索</button>
                </div><!-- end frmSearch -->

                <div id="frmResult" class="row">

                </div>

                <nav id="frmPagination" style="display: none">
                    <ul class="pagination justify-content-center">
                        <li class="page-item" id="prePage">
                            <a class="page-link" href="javascript:GoPre()">上一页</a>
                        </li>
                        <li class="page-item disabled" id="totalPage">
                            <a class="page-link"><span id="spCurrentPage">..</span>/<span
                                    id="spTotalPage">..</span>页</a>
                        </li>
                        <li class="page-item" id="nextPage">
                            <a class="page-link" href="javascript:GoNext()">下一页</a>
                        </li>
                    </ul>
                </nav>


            </div><!-- end container -->

        </div>
    </main>

    {{ template "kas/_footer.htm" .}}

    <script id="tplImageRow" type="text/x-jsrender">
<div class="col-md-3">
  <div class="card mb-4 shadow-sm">
    <a href="/kas/archive/article_edit?unqid={%:UnqID%}"><img class="litimg" src="{%:~uploads_url(Litpic)%}" width="100%" height="100%"></a>
    <div class="card-body">
      <div class="pb-2"><small>{%:Title%}</small></div>
      <div class="d-flex justify-content-between align-items-center">
        <div class="btn-group">
          <a href="/kas/archive/article_edit?unqid={%:UnqID%}" class="btn btn-sm btn-outline-secondary" title="编辑"><i class="fa fa-pencil-square-o"></i></a>
          <a href="javascript:DelArticle('{%:UnqID%}','{%:Title%}');" class="btn btn-sm btn-outline-secondary" title="删除"><i class="fa fa-trash-o"></i></a>
          {%if Arcrank != -9%}
          <a target="_blank" href="{{.global.host_www}}/view/{%:UnqID%}" class="btn btn-sm btn-outline-secondary" title="查看"><i class="fa fa-globe"></i></a>
          {%/if%}
        </div>
        <small class="text-muted">{%:~unix_to_str(Sortrank)%}</small>
      </div>
    </div>
  </div>
</div>
</script>

    <script type="text/javascript">
        var currentpage = 1;
        const pagesize = 20;
        var gokeep_token = GetCookie(TokenName())
        if (gokeep_token == null) {
            DirectTo("/kas");
        }
        $.views.settings.delimiters("{%", "%}"); // 设置js render标签

        // 加载栏目选项
        LoadTypeidSelectOptions("#selTypeid", 0, 23);

        function unix_to_str(time) {
            time = parseInt(time) * 1000;
            var datetime = new Date();
            datetime.setTime(time);
            var year = datetime.getFullYear();
            var month = datetime.getMonth() + 1;
            var date = datetime.getDate();
            var hour = datetime.getHours();
            var minute = datetime.getMinutes();
            return year + "-" + month + "-" + date + " " + hour + ":" + minute;
        }

        $.views.helpers({
            uploads_url: function (val) {
                if (val == "") {
                    return "/assets/img/logo.png";
                }
                return val.replace("public/", "/");
            },
            unix_to_str: unix_to_str
        })

        var channel = 0;
        var typeid = 0;
        var title = "";
        var unqid = "";
        var isshow = 0;
        var source = "";
        var page = currentpage;
        var iswillpub = 0;

        var urlParams = new URLSearchParams(window.location.search);
        if (parseInt(urlParams.get('typeid')) > 0) {
            typeid = parseInt(urlParams.get('typeid'));
        }
        if (parseInt(urlParams.get('channel')) > 0) {
            channel = parseInt(urlParams.get('channel'));
        }
        if (urlParams.get('title') != "") {
            title = urlParams.get('title');
        }
        if (urlParams.get('unqid') != "") {
            unqid = urlParams.get('unqid');
        }
        if (parseInt(urlParams.get('isshow')) != 0) {
            isshow = parseInt(urlParams.get('isshow'));
        }
        if (parseInt(urlParams.get('iswillpub')) != 0) {
            iswillpub = parseInt(urlParams.get('iswillpub'));
        }
        if (urlParams.get('source') != "") {
            source = urlParams.get('source');
        }
        if (urlParams.get('page') > 0) {
            page = parseInt(urlParams.get('page'));
            currentpage = page;
        }
        if (urlParams.has('page') && urlParams.has('page_size')) {
            GotoPage(page);
        }

        function DelArticle(unqid, title) {
            var footer = "<p><a href='?' class='btn btn-outline-primary' data-dismiss='modal' aria-label='Close'>返回</a> <a href='javascript:DelArticleDo(\"" + unqid + "\",\"~modalID~\")' class='btn btn-primary'>确认删除</a></p>"
            var order_info = "<p>标题：" + title + "</p>";
            ShowMsg("<p>是否确定删除当前图片</p>" + order_info, {
                footer: footer,
                noClose: true,
            });
        }

        function DelArticleDo(unqid, modalID) {
            CloseModal("GKModal" + modalID);
            setTimeout(function () {
                $.ajax({
                    url: '/api/v1/auth/archive/del',
                    type: 'GET',
                    dataType: 'json',
                    data: { unqid: unqid },
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + gokeep_token);
                    },
                })
                    .done(function (data) {
                        Log(data);
                        if (data.code == 0) {
                            if (data.result == true) {
                                PageReload();
                            } else {
                                ShowMsg("删除文章失败");
                            }
                        }
                    })
                    .fail(function () {
                        ShowMsg("删除文章失败");
                    });
            }, 500);
        }

        function GoNext() {
            currentpage = currentpage + 1;
            GotoPage(currentpage);
        }

        function GoPre() {
            currentpage = currentpage - 1;
            GotoPage(currentpage);
        }

        function GotoPage(page) {
            if (page == 1) {
                $("#prePage").addClass('disabled');
            } else {
                $("#prePage").removeClass('disabled');
            }
            $("#spCurrentPage").text(page);
            var data = {
                typeid: typeid,
                channel: 1,
                title: title,
                unqid: unqid,
                isshow: isshow,
                source: source,
                page: page,
                page_size: pagesize,
                iswillpub: iswillpub,
            };

            window.history.pushState("searchResult", "搜索结果", "/kas/archive/article_main?" + $.param(data));

            $.ajax({
                type: "GET",
                dataType: 'json',
                data: data,
                url: "/api/v1/auth/archive/list",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('Authorization', 'Bearer ' + gokeep_token);
                }
            }).done(function (data) {
                Log(data);
                if (data.code == 0) {
                    var total = Math.ceil(data.total / pagesize);
                    if ((data.result.length < pagesize) || (page == total)) {
                        $("#nextPage").addClass('disabled');
                    } else {
                        $("#nextPage").removeClass('disabled');
                    }

                    $("#spTotalPage").text(total);
                    var content = $("#tplImageRow").render(data.result);
                    $("#frmResult").html(content);
                    $("#frmSearch").hide();
                    $("#frmPagination").show();
                    $("#_searchResultInfo").text(' > 搜索结果')
                }
            }).fail(function (data) {
                Log("fail:", data);
                ShowMsg("获取列表失败");
            }); // end ajax
        }

        $("#btnSearch").click(function (event) {
            typeid = parseInt($("#selTypeid").val());
            title = $("#iptTitle").val();
            unqid = $("#iptUnqID").val();
            isshow = $("#ckIsShow").is(":checked") ? 1 : 0;
            iswillpub = $("#ckIsWillPub").is(":checked") ? 1 : 0;
            source = $("#iptSource").val();
            page_size = pagesize;

            GotoPage(1);
        });
    </script>
</body>

</html>
{{ end }}