{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}
<link href="../res/doublebox/css/hdw.css" rel="stylesheet" type="text/css">
<style>.myred {
        color: red;
    }</style>
<script src="../res/doublebox/js/hdw.js"></script>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <section class="content-header">
        <h1>
            {{/*<small><a class="btn btn-primary opr" opr="new">新增版本</a></small>*/}}
            <small>
                <button type="button" class="btn btn-primary" data-toggle="modal" onclick='createOneVersion("")'>新增站点
                </button>
            </small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="/admin/version"><i class="fa fa-dashboard"></i>抽取版本</a></li>
            <li class="active"><a href="/admin/site_management?vid={{.vid}}">站点列表</a></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">
                        <table id="dataTable" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>站点名称</th>
                                <th>描述</th>
                                <th>网址</th>
                                <th>脚本集</th>
                                <th>站点状态</th>
                                <th>抽取合并</th>
                                <th>站点规则</th>
                                <th>站点操作</th>
                            </tr>

                            </thead>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
</div>
<!--新增函数-->
<div class="modal fade" id="modal-info-version">
    <div class="modal-dialog">
        <form id="dataform" class="form-horizontal" role="form">
            <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="version_title">新增站点</h4>
                </div>
                <div class="modal-body">
                    <input name="name" id="vid" type="hidden" value={{.vid}}>
                    <div class="form-group">
                        <label for="code" class="col-sm-2 control-label myred">站点名称:</label>
                        <div class="col-sm-10">
                            <input name="name" type="text" id="site_name" value="" class="form-control"
                                   placeholder="站点名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="code" class="col-sm-2 control-label myred">描述:</label>
                        <div class="col-sm-10">
                            <input name="name" type="text" id="site_descript" value="" class="form-control"
                                   placeholder="描述信息">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="code" id="fieldname" class="col-sm-2 control-label myred">网址:</label>
                        <div class="col-sm-10">
                            <input name="name" type="text" id="site_href" value="" class="form-control"
                                   placeholder="网址信息">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="code" id="fieldname" class="col-sm-2 control-label myred">脚本集:</label>
                        <div class="col-sm-10">
                            <input name="name" type="text" id="site_script" value="" class="form-control"
                                   placeholder="脚本号，多个以','号分割">
                        </div>
                    </div>
                    <div class="form-group ckbox">
                        <label for="code" id="fieldname" class="col-sm-2 control-label myred">启用:</label>
                        <div class="col-sm-10">
                            <input id="isuse" name="isuse" type="radio" checked="checked" value="true"><label  for="isuse">启用</label>
                            <input id="isuse" name="isuse" type="radio" value="false"><label for="isuse">停用</label>
                        </div>
                    </div>
                    <div class="form-group ckbox">
                        <label for="code" id="fieldname" class="col-sm-2 control-label myred">抽取合并:</label>
                        <div class="col-sm-10">
                            <input id="ismerge" name="ismerge" type="radio" checked="checked" value="true"><label  for="isuse">启用</label>
                            <input id="ismerge" name="ismerge" type="radio" value="false"><label for="isuse">停用</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default cancel" data-dismiss="modal" onclick="reset()">取消
                    </button>
                    <button type="button" class="btn btn-primary save" onclick="save()">保存</button>
                </div>
            </div>
            <!-- /.modal-content -->
            <input type="hidden" id="_id" value="">
        </form>
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- footer -->
{{template "dialog"}}
{{template "footer"}}

<script>
    menuActive("version")
    var vid = $("#vid").val();//版本id
    $(function () {
        ttable = $('#dataTable').DataTable({
            "paging": false,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "ajax": {
                "url": "/admin/site_management/dataForVid?vid=" + vid,
                "type": "post",
                "data": {}
            },
            "language": {
                "url": "/res/dist/js/dataTables.chinese.lang"
            },
            "columns": [
                {"data": "site_name"},
                {"data": "site_descript", "text-overflow": "ellipsis"},
                {
                    "data": "site_href", render: function (val, a, row) {
                        return '<a href=' + val + '   target=' + val + '>' + val + '</a>'
                    }
                },
                {"data": "site_script"},
                {
                    "data": "isuse", "width": "7%", render: function (val, a, row) {
                        tmp = ""
                        if (val) {
                            tmp = "<a  title='停用' onclick='use(\"" + row._id + "\"," + false + ")'><i class='fa fa-fw fa-circle text-green'></i></a>已启用"
                        } else {
                            tmp = "<a  title='启用' onclick='use(\"" + row._id + "\"," + true + ")'><i class='fa fa-fw fa-circle text-red'></i></a>未启用"
                        }
                        return tmp
                    }
                },
                {
                    "data": "ismerge", "width": "7%", render: function (val, a, row) {
                        tmp = ""
                        if (val) {
                            tmp = "<a  title='停用' onclick='usemerge(\"" + row._id + "\"," + false + ")'><i class='fa fa-fw fa-circle text-green'></i></a>已启用"
                        } else {
                            tmp = "<a  title='启用' onclick='usemerge(\"" + row._id + "\"," + true + ")'><i class='fa fa-fw fa-circle text-red'></i></a>未启用"
                        }
                        return tmp
                    }
                },
                {
                    "data": "_id", "width": "23%", render: function (val, a, row) {
                        tmp = '<div class="btn-group">' +
                            '<a class="btn btn-sm btn-success" href="/admin/site_management/info?vid=' + row.vid + '&pid=' + row._id + '" >属性</a>' +
                            '<a class="btn btn-sm btn-info" href="/admin/site_management/ruleback?version=' + row.version + '&vid=' + row.vid + '&pid=' + row._id + '">后置规则</a>' +
                            '<a class="btn btn-sm btn-warning" href="/admin/site_management/tag?version=' + row.version + '&vid=' + row.vid + '&pid=' + row._id + '">标签库</a>' +
                            '<a class="btn btn-sm btn-danger" href="/admin/site_management/clear?version=' + row.version + '&vid=' + row.vid + '&pid=' + row._id + '">函数清理</a>' +
                            '</div>';
                        return tmp
                    }
                },
                {
                    "data": "_id", render: function (val, a, row) {
                        tmp_id = row._id
                        tmp = '<div class="btn-group">' +
                            '<button  class="btn btn-sm btn-primary "  onclick="edit_table_btn(\'' + tmp_id + '\');"  value=' + row._id + ' >编&nbsp;&nbsp;辑</button > &nbsp;' +
                            '<button  class="btn btn-sm btn-danger" onclick="delete_table_btn(\'' + tmp_id + '\');"  value=' + row._id + ' >删除站点</button > &nbsp;' +
                            '</div>';
                        return tmp
                    }
                }
            ]
        });

    })

    //创建标签
    function createOneVersion() {
        _id = "";
        $("#version_title").html("新增站点");
        $("#_id").val("");
        $("#site_name").val("");
        $("#site_descript").val("");
        $("#site_href").val("");
        $("#site_script").val("");
        $("#modal-info-version").modal("show");
    }

    //保存
    function save() {
        var _id = $("#_id").val();//版本id
        var vid = $("#vid").val();//版本id
        var site_name = $("#site_name").val();//站点名称
        var site_descript = $("#site_descript").val();//站点描述
        var site_href = $("#site_href").val()//站点网址
        var site_script = $("#site_script").val()//站点脚本
        var isuse = $("input[name='isuse']:checked").val();
        var ismerge = $("input[name='ismerge']:checked").val();
        if (site_name == "" || site_href == "" || site_script == "") {
            alert("站点填写不完整！");
            return false;
        }
        /*
                console.log({
                    "_id": _id,
                    "site_name": site_name,
                    "site_descript": site_descript,
                    "site_href": site_href,
                    "site_script": site_script,
                    "vid": vid,
                    "isuse":isuse
                })
        */
        // {{/*data:{"_id":_id,"s_descript":s_descript,"version":version,"clearJsonStr":clearJsonStr,"username":{{index (session "user") "name" }}},*/}}
        $.ajax({
            url: "/admin/site_management/save",
            type: "post",
            data: {
                "_id": _id,
                "site_name": site_name,
                "site_descript": site_descript,
                "site_href": site_href,
                "site_script": site_script,
                "vid": vid,
                "isuse": isuse,
                "ismerge":ismerge
            },
            success: function (r) {
                if (r.rep) {
                    window.location.href = "/admin/site_management?vid=" + vid
                } else {
                    showTip("已存在！", 1000)
                }
            }
        })
    }

    //use
    function use(_id, isuse) {
        smg = ""
        if (isuse) {
            smg = "确定启用？"
        } else {
            smg = "确定停用？"
        }
        showConfirm(smg, function () {
            $.ajax({
                url: "/admin/site_management/use",
                type: "post",
                data: {"_id": _id, "isuse": isuse},
                success: function (r) {
                    if (r.rep) {
                        window.location.href = "/admin/site_management?vid=" + vid
                    } else {
                        showTip("启用失败", 1000, function () {
                        });
                    }
                }
            })
        });
    }
    //usemerge
    function usemerge(_id, ismerge) {
        smg = ""
        if (ismerge) {
            smg = "确定启用？"
        } else {
            smg = "确定停用？"
        }
        showConfirm(smg, function () {
            $.ajax({
                url: "/admin/site_management/usemerge",
                type: "post",
                data: {"_id": _id, "ismerge": ismerge},
                success: function (r) {
                    if (r.rep) {
                        window.location.href = "/admin/site_management?vid=" + vid
                    } else {
                        showTip("启用失败", 1000, function () {
                        });
                    }
                }
            })
        });
    }
    //修改
    function edit_table_btn(obj) {
        if (obj == "") {
            showTip("id不存在！", 1500)
            return
        }
        $.ajax({
            url: "/admin/site_management/byId",
            type: "post",
            data: {
                "_id": obj,
            },
            success: function (r) {
                console.log(r.data.isuse)
                if (r.rep) {
                    $("#version_title").html("修改站点");
                    $("#_id").val(r.data._id);
                    $("#site_name").val(r.data.site_name);
                    $("#site_descript").val(r.data.site_descript);
                    $("#site_href").val(r.data.site_href);
                    $("#site_script").val(r.data.site_script);
                    if (!r.data.isuse) {
                        $("input[name='isuse'][value='true']").attr("checked", false);
                        $("input[name='isuse'][value='false']").attr("checked", true);
                    }
                    if (!r.data.ismerge) {
                        $("input[name='ismerge'][value='true']").attr("checked", false);
                        $("input[name='ismerge'][value='false']").attr("checked", true);
                    }
                    $("#modal-info-version").modal("show");
                } else {
                    showTip("查询错误！", 1200)
                }
            }
        })
    }

    //删除站点
    function delete_table_btn(obj) {
        showConfirm("确定删除?", function () {
            if (obj == "") {
                showTip("id不存在！", 1500)
                return
            }
            $.ajax({
                url: "/admin/site_management/delete",
                type: "post",
                data: {
                    "_id": obj,
                },
                success: function (r) {
                    if (r.rep) {
                        window.location.href = "/admin/site_management?vid=" + vid
                    } else {
                        showTip("删除失败", 1000, function () {
                        });
                    }
                }
            })
        });
    }

</script>
