<%@ Page Language="c#" Inherits="Web.Wpdcc.selectItem" EnableEventValidation="false" CodeBehind="selectItem.aspx.cs" %>

<!DOCTYPE html>
<html>
<head>
    <title>栏目选择</title>
    <base target="_self" />
    <!--#include file = "../Css/head.htm" -->

    <script src="../Js/jQuery.js" type="text/javascript"></script>
    <script type="text/javascript">
        // 加入已选
        function Add() {
            if (parseInt("<%=mode%>") < 5) {
                GetBigClassByTemplate();
            }
            var selectFrom = document.getElementById("d_b1"); //var selectFrom=document.myform.d_b1;此原来写法不兼容一些浏览器。 施键201407 统一修改，下同
            var selectTo = document.getElementById("d_b2");
            if (selectFrom.selectedIndex < 0) {
                alert("请选择待选栏目！");
                return;
            }

            for (var loop = 0; loop < selectFrom.options.length; loop++) {
                if (selectFrom.options[loop].selected == true) {
                    var optionName = selectFrom.options[loop].text;
                    var optionValue = selectFrom.options[loop].value;
                    var flag = false;
                    var ii;
                    for (var i = 0; i < selectTo.options.length; i++) {
                        if (optionValue == selectTo.options[i].value) {
                            flag = true;
                            ii = i;
                            break;
                        }
                    }
                    if (flag == true) {
                        alert("“" + selectFrom.options[loop].text + "”栏目已被选择！\n\r对应已选栏目“" + selectTo.options[ii].text + "”");
                    }
                    else {
                        selectTo.add(new Option(optionName, optionValue));
                    }
                }
            }
        }

        function AddAll() {
            var selectFrom = document.getElementById("d_b1");
            var selectTo = document.getElementById("d_b2");
            for (var loop = 0; loop < selectFrom.options.length; loop++) {
                var optionName = selectFrom.options[loop].text;
                var optionValue = selectFrom.options[loop].value;
                var flag = false;
                for (var i = 0; i < selectTo.options.length; i++) {
                    if (optionValue == selectTo.options[i].value) {
                        flag = true;
                        break;
                    }
                }

                if (flag == false) {
                    selectTo.add(new Option(optionName, optionValue));
                }
            }
        }

        // 从已选中删除
        function Del() {
            var selectTo = document.getElementById("d_b2");
            var nIndex = selectTo.selectedIndex;
            var nLen = selectTo.length;
            if (nLen < 1) {
                return;
            }
            if (nIndex < 0) {
                alert("请选择已选栏目！");
                return;
            }
            for (var loop = 0; loop < selectTo.options.length; loop++) {
                if (selectTo.options[loop].selected) {
                    selectTo.options[loop] = null; loop--;
                }
            }
        }

        function DelAll() {
            var selectTo = document.getElementById("d_b2");
            var nLen = selectTo.length;
            if (nLen < 1) {
                return;
            }
            for (var loop = nLen; loop >= 0; loop--) {
                selectTo.options[loop] = null;
            }
        }

        // 排序：向上移动
        function Up() {
            var sel = document.getElementById("d_b2");
            var nIndex = sel.selectedIndex;
            var nLen = sel.length;
            if ((nLen < 1) || (nIndex == 0)) return;
            if (nIndex < 0) {
                alert("请选择一个要移动的已选栏目！");
                return;
            }
            var sValue = sel.options[nIndex].value;
            var sHTML = sel.options[nIndex].innerHTML;
            sel.options[nIndex].value = sel.options[nIndex - 1].value;
            sel.options[nIndex].innerHTML = sel.options[nIndex - 1].innerHTML;
            sel.options[nIndex - 1].value = sValue;
            sel.options[nIndex - 1].innerHTML = sHTML;
            sel.selectedIndex = nIndex - 1;
        }

        // 排序：向下移动
        function Down() {
            var sel = document.getElementById("d_b2");
            var nIndex = sel.selectedIndex;
            var nLen = sel.length;
            if ((nLen < 1) || (nIndex == nLen - 1)) return;
            if (nIndex < 0) {
                alert("请选择一个要移动的已选栏目！");
                return;
            }
            var sValue = sel.options[nIndex].value;
            var sHTML = sel.options[nIndex].innerHTML;
            sel.options[nIndex].value = sel.options[nIndex + 1].value;
            sel.options[nIndex].innerHTML = sel.options[nIndex + 1].innerHTML;
            sel.options[nIndex + 1].value = sValue;
            sel.options[nIndex + 1].innerHTML = sHTML;
            sel.selectedIndex = nIndex + 1;
        }

        // 提交处理
        function checkform() {
            var sel = document.getElementById("d_b2");
            var nLen = sel.length;
            var str1 = "";
            var str2 = "";
            for (var i = 0; i < nLen; i++) {
                if (i > 0)
                { str2 += ","; str1 += ","; }
                str2 += sel.options[i].value;
                str1 += sel.options[i].text;
            }
            document.getElementById("d_name").value = str1;
            document.getElementById("d_value").value = str2;
            return true;
        }

        function GetBigClassByTemplate() {
            var templateId = document.getElementById("<%=ddlTemplate.ClientID %>").value;
            var searchName = document.getElementById("searchText").value;
            var classTypeId = document.getElementById("classType").value;
            var mode = "<%=mode%>";
            //Web.Service.LoginService.GetBigClassByTemplate(templateId, searchName, classTypeId, mode, onGetBigClassByTemplateSuccess, onError, null);
            $.ajax({
                type: "post",
                url: "selectItem.aspx/GetBigClassByTemplate",
                data: "{'templateId':'" + templateId + "','searchName':'" + searchName + "','classTypeId':'" + classTypeId + "','mode':'" + mode + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    //return data;
                    onGetBigClassByTemplateSuccess(data);
                },
                error: function (err) {
                    alert(err);
                }
            });
        }

        function onGetBigClassByTemplateSuccess(result) {
            var db = document.getElementById("<%=d_b1.ClientID %>");
            db.length = 0;
            if (result.length != 0) {
                for (var i = 0; i < result.length; i++) {
                    var item = result[i];
                    db.options[i] = new Option(item.Name, item.PkId);
                }
            }
        }

        //关闭弹出层，返回树菜单
        function delayTreeClose(parentid) {
            parent.$.fancybox.close();
            //关闭后刷新布局列表
            parent.location = "manageMenuItem.aspx?parentid=" + parentid;
        }
        //关闭弹出层，返回列表菜单
        function delayListClose(parentid) {
            parent.$.fancybox.close();
            //关闭后刷新布局列表
            parent.location = "manageMenu.aspx?parentid=" + parentid + "&istype=2";
        }
        //保存成功后关闭当前选择页面并回传parentid
        function success(parentId) {
            // window.returnValue = parentId;
            window.close();
        }

    </script>

</head>
<body>
    <form id="myform" method="post" runat="server">
        <div class="edit-func-box func-box">
            <div class="hd b">
                栏目选择<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
            <div class="bd nopadding">
                <table class="edit-table" style="border: 0px;">
                    <tr runat="server" id="trTemplate">
                        <td colspan="5">类型：
                        <asp:DropDownList ID="ddlTemplate" runat="server" DataTextField="name" DataValueField="pkId" onchange="javascript:GetBigClassByTemplate();">
                        </asp:DropDownList>
                            &nbsp;
                        <select id="classType" onchange="javascript:GetBigClassByTemplate();">
                            <option value="0">名称</option>
                            <option value="1">说明</option>
                        </select>：<input type="text" id="searchText" class="input-md" onpropertychange="javascript:GetBigClassByTemplate();" onchange="javascript:GetBigClassByTemplate();" />
                        </td>
                    </tr>
                    <tr>
                        <td width="15%">可选栏目:</td>
                        <td>
                            <asp:ListBox ID="d_b1" runat="server" SelectionMode="Multiple" Width="168" Height="272"></asp:ListBox>
                        </td>
                        <td width="15%" align="center">
                            <div class="btn-group-vertical" role="group">
                                <input onclick="Add()" type="button" value="→" name="b1" class="btn btn-mini btn-primary" />
                                <input onclick="Del()" type="button" value="←" name="b1" class="btn btn-mini btn-primary" />
                                <input onclick="AddAll()" type="button" value=">>" name="b1" class="btn btn-mini btn-primary" />
                                <input onclick="DelAll()" type="button" value="<<" name="b1" class="btn btn-mini btn-primary" />
                            </div>
                        </td>
                        <td>
                            <asp:ListBox ID="d_b2" runat="server" SelectionMode="Multiple" Width="168" Height="272"></asp:ListBox>
                        </td>
                        <td width="15%">
                            <div class="btn-group-vertical" role="group">
                                <input onclick="Up()" type="button" value="↑" name="b3" class="btn btn-mini btn-primary" />
                                <input onclick="Down()" type="button" value="↓" name="b4" class="btn btn-mini btn-primary" />
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="5" style="text-align: center">
                            <asp:Button ID="btnSave" runat="server" CssClass="btn btn-sm btn-primary" Text="确定" OnClientClick="return checkform();"
                                OnClick="btnSave_Click" />&emsp;&emsp;
                        <asp:Button ID="btnClose" runat="server" CssClass="btn btn-sm btn-primary" Text="取消" OnClick="btnClose_Click" />
                            <%--  <input type="button" value="取消"  runat="server"  class="btn"  onclick="btnClose_Click()"/>--%>
                            <input id="d_name" type="hidden" name="d_button" runat="server" />
                            <input id="d_value" type="hidden" name="d_button" runat="server" />
                        </td>
                    </tr>
                </table>
            </div>

        </div>
    </form>
</body>
</html>
