@using GBLab.Common.Expand;
@{
    string sign = string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"];

    #region 构建链接
    string _url = "/manage/tags/list?1=1&MvcClassID=11889&ShowType=ReturnData";
    string TagsModuleType = PageData["TagsModuleType"] == null ? "" : PageData["TagsModuleType"];
    string TagsIds = PageData["TagsIds"]==null?"": PageData["TagsIds"].ToString();
    string TagsName = PageData["Tags"] == null ? "" : PageData["Tags"];

    switch (TagsModuleType)
    {
        case "coresubject":
            _url = "/manage/CoreSubject/list?1=1&MvcClassID=19118&ShowType=ReturnData&Template=Select";
            break;

        case "coreclass":
            _url = "/manage/CoreClass/list?1=1&MvcClassID=19122&ShowType=ReturnData&Template=Selec";
            break;

        case "dormitorygoods":
            _url = "/manage/DormitoryGoods/list?1=1&MvcClassID=19099&ShowType=ReturnData&Template=Select";
            break;

        case "tags":
            _url = "/manage/tags/list?1=1&MvcClassID=11889&ShowType=ReturnData&Template=Select";
            break;

        case "users":
            _url = "/manage/user/list?1=1&MvcClassID=10063&ShowType=ReturnData&Template=Select";
            break;

        case "cmsimgs":
            _url = "/manage/cmsimg/list?1=1&MvcClassID=10188&ShowType=ReturnData&Template=Select";
            break;

        case "types":
            _url = "/manage/type/list?1=1&MvcClassID=10007&ShowType=ReturnData&Template=Select";
            break;

        case "functions":
            _url = "/manage/function/list?1=1&MvcClassID=1&ShowType=ReturnData&Template=Select";
            break;

        case "functionactions":
            _url = "/manage/functionaction/list?1=1&MvcClassID=1&ShowType=ReturnData&Template=Select";
            break;

        case "keyvalue":
            _url = "/manage/keyvalue/list?1=1&MvcClassID=1&ShowType=ReturnData&Template=Select";
            break;

        case "keyvalueitem":
            _url = "/manage/keyvalueitem/list?1=1&MvcClassID=1&ShowType=ReturnData&Template=Select";
            break;

        case "dormitoryroombed":
            _url = "/manage/DormitoryRoomBed/list?1=1&MvcClassID=1&ShowType=ReturnData&Template=Select";
            break;

        case "dormitoryuser":
            _url = "/manage/DormitoryUser/list?1=1&MvcClassID=19107&ShowType=ReturnData&Template=Select";
            break;
    }
    #endregion
}

@*脚本预加载*@
<script>
    //判定是否已存在加载的js或css引用
    function isInclude(name) {
        var js = /js$/i.test(name);
        var es = document.getElementsByTagName(js ? 'script' : 'link');
        for (var i = 0; i < es.length; i++)
            if (es[i][js ? 'src' : 'href'].indexOf(name) != -1) return true;
        return false;
    }

    // 资源的预先校验
    if (!isInclude('jquery.tag.js')) {
        document.write("<script src=\"@Url.Content("~/App_Scripts/Jquery/plugin/jquery.tag/jquery.tag.js")\"><\/script>");
    }
    if (!isInclude('jquery.tag/tag.css')) {
        document.write('<link href=\"@Url.Content("~/App_Scripts/Jquery/plugin/jquery.tag/tag.css")\" rel="stylesheet" />');
    }
</script>

@*展示内容*@
<div id="wrapper">
    <input type="text" value="@(PageData["TagsIds"])" id="@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_TagsId" : PageData["TagsName"] + "Id")" name="@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_TagsId" : PageData["TagsName"] + "Id")" style="display:none;" />
    <input type="text" value="@(PageData["Tags"])" id="@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])" name="@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])" 
           style="@(PageData["Width"]==null?"width:500px;":"width:"+PageData["Width"].ToString()+"px;")" onfocus=this.blur()>
    @*多个信息使用","分割
    &nbsp;&nbsp;*@
    <a href="javascript:@(sign)Tags_List('@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])','@PageData["TagsModuleType"]');" class="easyui-linkbutton">选 择</a>
    <a href="javascript:@(sign)remo('@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])');" class="easyui-linkbutton">清 除</a>
</div>
<div id="form-window@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])" title="编辑/添加" class="easyui-window" data-options="modal:true,closed:true,resizable:true,shadow:true" style="width:1200px; height: 720px; padding: 0px 0px 6px 0px;"></div>

@*逻辑脚本*@
<script type="text/javascript" language="javascript">

    var @(sign)isOnly = '@(PageData["IsOnly"] == null ? "false" : (PageData["IsOnly"].ToString().ToLower() == "true" ? "true" : "false"))';
    //console.log(@(sign)isOnly);

    // 控件名称
    var tagsname = "@(sign)";

    // 控件ID
    var tagId = "@(sign + "Id")";

    //@(sign)activateTagator(tagsname);

    $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').tagbox({
        onRemoveTag: function (e) {
            @*console.log($('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').val());
            console.log(e);*@
            var source = $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').val();
            var removeValue = e;
            var index = source.split(',').indexOf(e);
            console.log(index);
            @(sign)ResetValue(index);
        }
    });
    $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').combobox('disableTextbox', { stoptype: 'readOnly', activeTextArrow: true, stopArrowFocus: true });

    // 移除指定id
    function @(sign)ResetValue(e) {
        var ids = $('#' + tagsname + "Id").val();
        ids = ids.split(',');
        ids.splice(e, 1);
        var result = '';
        for (var i = 0;i<ids.length; i++) {
            if (ids[i] != '') {
                result += ids[i] + ',';
            }
        }
        $('#' + tagsname + "Id").val(result);
    }

    //Tags 列表
    function @(sign)Tags_List(_tagsname, _moduleType) {
        tagsname = _tagsname;
        dialogWin = openWindow("#form-window" + _tagsname, "选择信息<input id='hidcurrTagsName' type='hidden' value='" + _tagsname + "' />", '@(_url)'+'&ids=' + $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_TagsId" : PageData["TagsName"] + "Id")').val() + '&names=' + $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').val()+'&jsSign='+'@(sign)'+"&isOnly="+@(sign)isOnly, "icon-add").dialog('open');
    }

    // 获取返回信息
    function @(sign)Return_Tags(tags, tagsids) {
        var oldIds = $('#' + tagsname + "Id").val().split(',');
        var oldNames = $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').val().split(',');

        //debugger;

        if (@(sign)isOnly == 'true') {
            oldIds = [];
            oldNames = [];
        }

        var newIds = tagsids;
        var newNames = tags;

        //debugger;

        for (var i = 0; i < newIds.length; i++) {
            console.log(oldIds.indexOf(newIds[i]));
            if (oldIds.indexOf(newIds[i]+'') == -1) {
                oldIds.push(newIds[i]);
            }
        }

        var resultIds = '';
        for (var i = 0; i < oldIds.length; i++) {
            if (oldIds[i] != '') {
                resultIds += oldIds[i] + ',';
            }
        }

        for (var i = 0; i < newNames.length; i++) {
            console.log(oldNames.indexOf(newNames[i]));
            if (oldNames.indexOf(newNames[i]) == -1) {
                oldNames.push(newNames[i]);
            }
        }

        var resultNames = '';
        for (var i = 0; i < oldNames.length; i++) {
            if (oldNames[i] != '') {
                resultNames += oldNames[i] + ',';
            }
        }

        $('#@(string.IsNullOrWhiteSpace(PageData["TagsName"]) ? "F_Tags" : PageData["TagsName"])').tagbox('setValues', formatStr(resultNames));
        $('#' + tagsname + "Id").val(formatStr(resultIds));
        dialogWin.dialog('close');
    }

    function formatStr(e) {
        if (e.length > 0) {
            if (e.substr(0, 1) == ',') {
                e = e.substr(1, e.length - 1);
            }
            if (e.length > 0) {
                if (e.substr(e.length - 1, 1) == ',') {
                    e = e.substr(0, e.length - 1);
                }
            }
        }
        return e;
    }

    // =============================== 后续可以抽离的部分 ===============================

    //清除标签输入框
    function @(sign)remo(_tagsname) {
        //debugger;
        $("#" + _tagsname).tagbox('clear');
        $("#" + _tagsname + "Id").val('');
    }

    // Return_Cancel
    function Return_Cancel() {
        dialogWin.dialog('close');
    }

    //判断是否重复
    var IsRepeat = function (_tagsname,text) {
        var tags = $("#" + _tagsname).val().split(',');
        var hasTag = false;
        $.each(tags, function (key, value) {
            if ($.trim(value) === $.trim(text)) {
                hasTag = true;
            }
        });
        return hasTag;
    };

</script>