<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>设置 - {{.Model.BookName}} - {{.SITE_NAME}}</title>

    <!-- Bootstrap -->

    <link href="{{$.StaticDomain}}/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="{{$.StaticDomain}}/static/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="/static/css/toast.css" rel="stylesheet" type="text/css">

    <link href="{{$.StaticDomain}}/static/webuploader/webuploader.css" rel="stylesheet">
    <link href="{{$.StaticDomain}}/static/cropper/2.3.4/cropper.min.css" rel="stylesheet">
    <link href="{{$.StaticDomain}}/static/bootstrap/plugins/tagsinput/bootstrap-tagsinput.css" rel="stylesheet">


    <link href="/static/css/main.css?version={{$.Version}}" rel="stylesheet">
    <script src="/static/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="/static/respond.js/1.4.2/respond.min.js"></script>
</head>
<body>
<div class="manual-reader">
    {{template "widgets/header.html" .}}
    <div class="container manual-body">
        <div class="row">
            <div class="page-left">
                <ul class="menu">
                    <li><a href="{{urlfor "BookController.Dashboard" ":key" .Model.Identify}}" class="item"><i class="fa fa-dashboard" aria-hidden="true"></i> 概要</a> </li>
                    <li><a href="{{urlfor "BookController.Users" ":key" .Model.Identify}}" class="item"><i class="fa fa-users" aria-hidden="true"></i> 成员</a> </li>
                    <li class="active"><a href="{{urlfor "BookController.Setting" ":key" .Model.Identify}}" class="item"><i class="fa fa-gear" aria-hidden="true"></i> 设置</a> </li>
                </ul>

            </div>
            <div class="page-right">
                <div class="m-box">
                    <div class="box-head">
                        <strong class="box-title"> 书籍设置</strong>

                        <div class="btn-group pull-right btn-group-sm" style="margin-left: 5px;">
                            <button type="button" class="btn btn-default"><i class="fa fa-cloud-upload"></i> 导入书籍</button>
                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="caret"></span>
                                <span class="sr-only">Toggle Dropdown</span>
                            </button>
                            <ul class="dropdown-menu">
                                <li>
                                    <a href="javascript:void(0);" class="btn-pull-by-zip" data-toggle="tooltip" data-identify="{{.Model.Identify}}" title="从任意源拉取zip压缩的markdown书籍">
                                        <i class="fa fa-link"></i> ZIP 拉取导入
                                    </a>
                                </li>
                                <li>
                                    <a href="javascript:void(0);" class="btn-pull-by-git" data-toggle="tooltip" data-identify="{{.Model.Identify}}" title="从Git仓库导入markdown书籍">
                                        <i class="fa fa-git"></i> Git Clone 导入
                                    </a>
                                </li>
                            </ul>
                        </div>

                        <a style="margin-left: 5px;" href="/api/{{.Model.Identify}}/edit" class="btn btn-default btn-sm pull-right">编辑文档</a>

                        {{if eq .Model.RoleId 0}}
                        <button type="button"  class="btn btn-success btn-sm pull-right" data-toggle="modal" data-target="#transferBookModal">转让书籍</button>
                        {{if eq .Model.PrivatelyOwned 1}}
                        <button type="button"  class="btn btn-success btn-sm pull-right" data-toggle="modal" data-target="#changePrivatelyOwnedModal" style="margin-right: 5px;">转为公有</button>
                        {{else}}
                        <button type="button"  class="btn btn-danger btn-sm pull-right" data-toggle="modal" data-target="#changePrivatelyOwnedModal" style="margin-right: 5px;">转为私有</button>
                        {{end}}
                        <a href="{{urlfor "BookController.Generate" ":key" .Model.Identify}}" style="margin-right: 5px;"  class="btn btn-success btn-sm ajax-get  pull-right">
                            生成下载文档
                        </a>
                        <button type="button"  class="btn btn-danger btn-sm pull-right" style="margin-right: 5px;" data-toggle="modal" data-target="#deleteBookModal">删除书籍</button>
                        {{end}}



                    </div>
                </div>
                <div class="box-body">
                    <div class="form-right">
                        <label>书籍封面 &nbsp; &nbsp; &nbsp;</label>
                        <label>
                            <a href="javascript:;" data-toggle="modal" data-target="#upload-logo-panel">
                                <img src="{{showImg .Model.Cover "cover"}}" onerror="this.src='/static/images/book.png'" alt="封面" style="max-width: 120px;" class="border-cover-img" id="headimgurl">
                            </a>
                        </label>
                    </div>
                    <div class="form-left">
                        <form method="post" id="bookEditForm" action="{{urlfor "BookController.SaveBook"}}">
                            <input type="hidden" name="identify" value="{{.Model.Identify}}">
                            <div class="form-group">
                                <label>标题</label>
                                <input type="text" class="form-control" name="book_name" id="bookName" placeholder="书籍名称" value="{{.Model.BookName}}">
                            </div>
                            <div class="form-group">
                                <label>标识</label>
                                <input type="text" class="form-control" value="{{.BaseUrl}}{{urlfor "DocumentController.Index" ":key" .Model.Identify}}" placeholder="书籍唯一标识" disabled>
                            </div>
                            <div class="form-group">
                                <label>语种</label>
                                <select name="lang" class="form-control">
                                    <option {{if eq .Model.Lang "zh"}}selected{{end}} value="zh">中文</option>
                                    <option {{if eq .Model.Lang "en"}}selected{{end}} value="en">英文</option>
                                    <option {{if eq .Model.Lang "other"}}selected{{end}} value="other">其他</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>来源</label>
                                <div class="input-group">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="button">来源名称</button>
                                    </span>
                                                <input type="text" placeholder="选填" value="{{.Model.Author}}" name="author" id="author" class="form-control">
                                                <span class="input-group-btn">
                                        <button class="btn btn-default" style="border-left: 0px;border-right: 0px;border-radius: 0px;" type="button">来源链接</button>
                                    </span>
                                                <input type="text" placeholder="选填" value="{{.Model.AuthorURL}}" name="author_url" id="author_url" class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <label>文字广告</label>
                                <div class="input-group">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="button">广告标题</button>
                                    </span>
                                    <input type="text" placeholder="选填" value="{{.Model.AdTitle}}" name="ad_title" class="form-control">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" style="border-left: 0px;border-right: 0px;border-radius: 0px;" type="button">广告链接</button>
                                    </span>
                                    <input type="text" placeholder="选填" value="{{.Model.AdLink}}" name="ad_link" class="form-control">
                                </div>
                                <p class="text help-block">文字广告，可以放原作者的打赏和赞助二维码链接，也可以放开源书籍的购买链接以支持作者</p>
                            </div>

                            <div class="form-group">
                                <label>分类 (<span class="text-muted">支持选择多个分类</span>)</label>
                                {{range $idx,$cate:=$.Cates}}
                                    {{if eq $cate.Pid 0}}
                                    <div class="panel panel-default">
                                        <div class="panel-heading"><h3 class="panel-title">{{$cate.Title}}</h3></div>
                                        <div class="panel-body">
                                            {{range $idx1,$item:=$.Cates}}
                                                {{if eq $item.Pid $cate.Id}}
                                                    <label><input type="checkbox"  {{if (inMap $.Maps $item.Id)}}checked{{end}} name="cid" value="{{$item.Id}}">{{$item.Title}}</label>
                                                {{end}}
                                            {{end}}
                                        </div>
                                    </div>
                                    {{end}}
                                {{end}}
                            </div>
                            <div class="form-group">
                                <label>描述</label>
                                <textarea rows="3" class="form-control" name="description" style="height: 90px" placeholder="书籍描述">{{.Model.Description}}</textarea>
                                <p class="text help-block">描述信息不超过500个字符,支持Markdown语法</p>
                            </div>
                            <div class="form-group">
                                <label>标签</label>
                                <input type="text" class="form-control" name="label" placeholder="书籍标签" value="{{.Model.Label}}">
                                <p class="text help-block">最多允许添加10个标签，多个标签请用“,”分割</p>
                            </div>
                            {{if eq .Model.PrivatelyOwned 1}}
                            <div class="form-group">
                                <label>私密访问</label>
                                <div class="row">
                                    <div class="col-sm-10">
                                        <input type="text" name="token" id="token" class="form-control" placeholder="生成私密访问链接和密码" readonly value="{{.Model.PrivateToken}}">
                                    </div>
                                    <div class="col-sm-2">
                                        <button type="button" class="btn btn-success btn-sm" id="createToken" data-loading-text="生成" data-action="create">生成</button>
                                        <button type="button" class="btn btn-danger btn-sm" id="deleteToken" data-loading-text="删除" data-action="delete">删除</button>
                                    </div>
                                </div>
                            </div>
                            {{end}}
                        <div class="form-group">
                            <button type="submit" id="btnSaveBookInfo" class="btn btn-success" data-loading-text="保存中...">保存修改</button>
                            <span id="form-error-message" class="error-message"></span>
                        </div>
                </form>
            </div>

            <div class="clearfix"></div>

        </div>
    </div>
</div>
</div>
<!-- {{template "widgets/footer.html" .}} -->
</div>
<!-- Modal -->
<div class="modal fade" id="changePrivatelyOwnedModal" tabindex="-1" role="dialog" aria-labelledby="changePrivatelyOwnedModalLabel">
    <div class="modal-dialog" role="document">
        <form method="post" action="{{urlfor "BookController.PrivatelyOwned" }}" id="changePrivatelyOwnedForm">
            <input type="hidden" name="identify" value="{{.Model.Identify}}">
            <input type="hidden" name="status" value="{{if eq .Model.PrivatelyOwned 0}}close{{else}}open{{end}}">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">
                        {{if eq .Model.PrivatelyOwned 0}}
                        转为私有
                        {{else}}
                        转为共有
                        {{end}}
                    </h4>
                </div>
                <div class="modal-body">
                    {{if eq .Model.PrivatelyOwned 0}}
                    <span style="font-size: 14px;font-weight: 400;">确定将书籍转为私有吗？</span>
                    <p></p>
                    <p class="text error-message">转为私有后需要通过阅读令牌才能访问该书籍。</p>
                    {{else}}
                    <span style="font-size: 14px;font-weight: 400;"> 确定将书籍转为公有吗？</span>
                    <p></p>
                    <p class="text error-message">转为公有后所有人都可以访问该书籍。</p>
                    {{end}}
                </div>
                <div class="modal-footer">
                    <span class="error-message" id="form-error-message1"></span>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary" data-loading-text="变更中..." id="btnChangePrivatelyOwned">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- Start Modal -->
<div class="modal fade" id="upload-logo-panel" tabindex="-1" role="dialog" aria-labelledby="修改封面" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">修改封面</h4>
            </div>
            <div class="modal-body">
                <div class="wraper">
                    <div id="image-wraper">

                    </div>
                </div>
                <div class="watch-crop-list">
                    <div class="preview-title">预览</div>
                    <ul>
                        <li>
                            <div class="img-preview preview-lg"></div>
                        </li>
                        <li>
                            <div class="img-preview preview-sm"></div>
                        </li>
                    </ul>
                </div>
                <div style="clear: both"></div>
            </div>
            <div class="modal-footer">
                <span id="error-message"></span>
                <div id="filePicker" class="btn">选择</div>
                <button type="button" id="saveImage" class="btn btn-success" style="height: 40px;width: 77px;" data-loading-text="上传中...">上传</button>
            </div>
        </div>
    </div>
</div>
<!--END Modal-->

<!-- Delete Book Modal -->
<div class="modal fade" id="deleteBookModal" tabindex="-1" role="dialog" aria-labelledby="deleteBookModalLabel">
    <div class="modal-dialog" role="document">
        <form method="post" id="deleteBookForm" action="{{urlfor "BookController.Delete"}}">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">删除书籍</h4>
                </div>
                <div class="modal-body">
                    <div class="help-block">当前书籍标识为 <span class="text-danger">{{.Model.Identify}}</span> ；删除书籍后将无法找回。</div>
                    <div class="help-block">如确定删除，请输入当前文档标识和您的登录密码。</div>
                    <div class="input-group">
                        <span class="input-group-addon">文档标识</span>
                        <input type="text" name="identify" autocomplete="off" class="form-control">
                    </div>
                    <br>
                    <div class="input-group form-group">
                        <span class="input-group-addon">登录密码</span>
                        <input type="password" name="password" autocomplete="off" class="form-control">
                    </div>
                </div>
                <div class="modal-footer">
                    <span id="form-error-message2" class="error-message"></span>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-danger btnDeleteBook" data-loading-text="删除中...">确定删除</button>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="transferBookModal" tabindex="-1" role="dialog" aria-labelledby="transferBookModalLabel">
    <div class="modal-dialog" role="document">
        <form action="{{urlfor "BookController.Transfer"}}" method="post" id="transferBookForm">
            <input type="hidden" name="identify" value="{{.Model.Identify}}">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">书籍转让</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">接收账号</label>
                        <div class="col-sm-10">
                            <input type="text" name="account" class="form-control" placeholder="接收者账号" id="receiveAccount" maxlength="50">
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="modal-footer">
                    <span id="form-error-message3" class="error-message"></span>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" id="btnTransferBook" class="btn btn-primary">确定转让</button>
                </div>
            </div>
        </form>
    </div>
</div>

{{template "widgets/pull.html" .}}


{{/*<script src="/static/jquery/1.12.4/jquery.min.js" type="text/javascript"></script>*/}}
<script src="/static/jquery/1.12.4/jquery.min.js" type="text/javascript"></script>
{{/*<script src="/static/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>*/}}
<script src="/static/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>

<script src="{{$.StaticDomain}}/static/webuploader/webuploader.min.js" type="text/javascript"></script>
<script src="{{$.StaticDomain}}/static/cropper/2.3.4/cropper.min.js" type="text/javascript"></script>
<script src="{{$.StaticDomain}}/static/js/jquery.form.js" type="text/javascript"></script>
<script src="{{$.StaticDomain}}/static/bootstrap/plugins/tagsinput/bootstrap-tagsinput.min.js" type="text/javascript"></script>
<script src="{{$.StaticDomain}}/static/js/toast.script.js" type="text/javascript"></script>
<script src="/static/js/main.js?version={{$.Version}}" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $("#upload-logo-panel").on("hidden.bs.modal",function () {
            $("#upload-logo-panel").find(".modal-body").html(window.modalHtml);
        }).on("show.bs.modal",function () {
            window.modalHtml = $("#upload-logo-panel").find(".modal-body").html();
        });

        $('input[name="label"]').tagsinput({
            confirmKeys: [13,44],
            maxTags: 10,
            trimValue: true,
            cancelConfirmKeysOnEmpty : false
        });

        $("#changePrivatelyOwnedForm").ajaxForm({
            beforeSubmit :function () {
                $("#btnChangePrivatelyOwned").button("loading");
            },
            success :function (res) {
                if(res.errcode === 0){
                    window.location = window.location.href;
                    return;
                }else{
                    showError(res.message,"#form-error-message1");
                }
                $("#btnChangePrivatelyOwned").button("reset");
            },
            error :function () {
                showError("服务器异常","#form-error-message1");
                $("#btnChangePrivatelyOwned").button("reset");
            }
        });

        $("#createToken,#deleteToken").on("click",function () {
            var btn = $(this).button("loading");
            var action = $(this).attr("data-action");
            $.ajax({
                url : "{{urlfor "BookController.CreateToken"}}",
                type :"post",
                data : { "identify" : {{.Model.Identify}} , "action" : action },
                dataType : "json",
                success : function (res) {
                    if(res.errcode === 0){
                        $("#token").val(res.data);
                    }else{
                        alert(res.message);
                    }
                    btn.button("reset");
                },
                error : function () {
                    btn.button("reset");
                    alert("服务器错误");
                }
            }) ;
        });
        $("#token").on("focus",function () {
            $(this).select();
        });
        $("#bookEditForm").ajaxForm({
            beforeSubmit : function () {
                var bookName = $.trim($("#bookName").val());
                if (bookName === "") {
                    return showError("书籍名称不能为空");
                }
                $("#btnSaveBookInfo").button("loading");
            },
            success : function (res) {
                if(res.errcode === 0){
                    showSuccess("保存成功")
                }else{
                    showError("保存失败")
                }
                $("#btnSaveBookInfo").button("reset");
            },
            error : function () {
                showError("服务错误");
                $("#btnSaveBookInfo").button("reset");
            }
        });
        $("#deleteBookForm").ajaxForm({
            beforeSubmit : function () {
                $(".btnDeleteBook").button("loading");
            },
            success : function (res) {
                if(res.errcode === 0){
                    window.location = "{{urlfor "BookController.Index"}}";
                }else{
                    showError(res.message,"#form-error-message2");
                }
                $(".btnDeleteBook").button("reset");
            },
            error : function () {
                showError("服务器异常","#form-error-message2");
                $(".btnDeleteBook").button("reset");
            }
        });
        $("#transferBookForm").ajaxForm({
            beforeSubmit : function () {
                var account = $.trim($("#receiveAccount").val());
                if (account === ""){
                    return showError("接受者账号不能为空","#form-error-message3")
                }
                $("#btnTransferBook").button("loading");
            },
            success : function (res) {
                if(res.errcode === 0){
                    window.location = window.location.href;
                }else{
                    showError(res.message,"#form-error-message3");
                }
                $("#btnTransferBook").button("reset");
            },
            error : function () {
                $("#btnTransferBook").button("reset");
            }
        });

        try {
            var uploader = WebUploader.create({
                auto: false,
                swf: '/static/webuploader/Uploader.swf',
                server: '{{urlfor "BookController.UploadCover"}}',
                formData : { "identify" : {{.Model.Identify}} },
                pick: "#filePicker",
                fileVal : "image-file",
                fileNumLimit : 1,
                compress : false,
                accept: {
                    title: 'Images',
                    extensions: 'jpg,jpeg,png',
                    mimeTypes: 'image/jpg,image/jpeg,image/png'
                }
            }).on("beforeFileQueued",function (file) {
                uploader.reset();
            }).on( 'fileQueued', function( file ) {
                uploader.makeThumb( file, function( error, src ) {
                    $img = '<img src="' + src +'" style="max-width: 360px;max-height: 360px;">';
                    if ( error ) {
                        $img.replaceWith('<span>不能预览</span>');
                        return;
                    }

                    $("#image-wraper").html($img);
                    window.ImageCropper = $('#image-wraper>img').cropper({
                        aspectRatio: 175 / 230,
                        dragMode : 'move',
                        viewMode : 1,
                        preview : ".img-preview"
                    });
                }, 1, 1 );
            }).on("uploadError",function (file,reason) {
                console.log(reason);
                $("#error-message").text("上传失败:" + reason);

            }).on("uploadSuccess",function (file, res) {
                if(res.errcode == 0){
                    console.log(res);
                    $("#upload-logo-panel").modal('hide');
                    $("#headimgurl").attr('src',res.data);
                }else{
                    $("#error-message").text(res.message);
                }
            }).on("beforeFileQueued",function (file) {
                if(file.size > 1024*1024*2){
                    uploader.removeFile(file);
                    uploader.reset();
                    alert("文件必须小于2MB");
                    return false;
                }
            }).on("uploadComplete",function () {
                $("#saveImage").button('reset');
            });
            $("#saveImage").on("click",function () {
                var files = uploader.getFiles();
                if(files.length > 0) {
                    $("#saveImage").button('loading');
                    var cropper = window.ImageCropper.cropper("getData");

                    uploader.option("formData", cropper);

                    uploader.upload();
                }else{
                    alert("请选择图片");
                }
            });
        }catch(e){
            console.log(e);
        }
    });
</script>
</body>
</html>