<%@ page language="C#" autoeventwireup="true" inherits="Admin_Admin_SelectProduct, WebApp_deploy" title="添加商品" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <base target="_self" />
    <link href="/Admin/Css/admin_style.css" rel="stylesheet" type="text/css" />
    <link href="/Admin/Css/Css/tabpane.css" type="text/css" rel="stylesheet" />
    <script src="Js/Function.js" type="text/javascript"></script>
    <script type="text/javascript">
        //添加
        function add(obj, objID) {
            if (obj == '') { return false; }
            var productform = document.forms[0];
            if (productform.hfProductList.value == '') {
                //名称值
                productform.ProductList.value = obj;
                window.returnValue = productform.ProductList.value;
                //ID值
                productform.hfProductList.value = objID;
                window.returnValue = productform.hfProductList.value;

                SelectedCheckbox();
                return false;
            }
            var singleUser = objID.split(',');
            var singleName = obj.split(',');
            var ignoreUser = '';
            for (i = 0; i < singleUser.length; i++) {
                if (checkInfo(productform.hfProductList.value, singleUser[i])) {
                    ignoreUser = ignoreUser + singleName[i] + " ";
                }
                else {
                    productform.ProductList.value = productform.ProductList.value + ',' + singleName[i];
                    productform.hfProductList.value = productform.hfProductList.value + ',' + singleUser[i]; //ID
                }
            }
            if (ignoreUser != '') {
                alert(ignoreUser + '商品分类已经存在，此操作已经忽略！');
            }
            SelectedCheckbox();
            window.returnValue = productform.ProductList.value;
        }
        //删除（0：全部、1：最后一个）
        function del(num) {
            var productform = document.forms[0];
            if (num == 0 || productform.hfProductList.value == '' || productform.hfProductList.value == ',') {
                productform.ProductList.value = '';
                productform.hfProductList.value = '';
                SelectedCheckbox();
                return false;
            }

            var strDel = productform.ProductList.value;
            var strDelID = productform.hfProductList.value; //ID
            var s = strDel.split(',');
            var sID = strDelID.split(','); //ID
            productform.ProductList.value = strDel.substring(0, strDel.length - s[s.length - 1].length - 1);
            window.returnValue = productform.ProductList.value;

            productform.hfProductList.value = strDelID.substring(0, strDelID.length - sID[sID.length - 1].length - 1);
            window.returnValue = productform.hfProductList.value;
            SelectedCheckbox();
        }
        //检查当前选择的值是否存在已选择的列表值
        function checkInfo(ProductList, thisUser) {
            if (ProductList == thisUser) {
                return true;
            }
            else {
                var s = ProductList.split(',');
                for (j = 0; j < s.length; j++) {
                    if (s[j] == thisUser)
                        return true;
                }
                return false;
            }
        }    
    </script>
    <script type="text/javascript">
        //判断选择的复选框项
        function SelectedCheckbox() {
            var productform = document.forms[0];
            var hf = productform.hfProductList;
            var oItem = document.getElementsByName("chk_select");
            var itemname = "";
            var itemvalue = "";
            for (var i = 0; i < oItem.length; i++) {
                if (oItem[i].type == "checkbox") {
                    oItem[i].checked = false;
                }
            }
            if (hf != null) {
                if (hf.value != "") {
                    var s = hf.value.split(',');
                    for (j = 0; j < s.length; j++) {
                        for (var i = 0; i < oItem.length; i++) {
                            if (oItem[i].type == "checkbox") {
                                if (oItem[i].value == s[j]) {
                                    oItem[i].checked = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        window.onload = SelectedCheckbox;
        //判断选择的复选框  
        function SelectCheckboxes() {
            var oItem = document.getElementsByName("chk_select");
            var oItemN = document.getElementsByName("hidproductname");
            var itemname = "";
            var itemvalue = "";
            for (var i = 0; i < oItem.length; i++) {
                if (oItem[i].type == "checkbox") {
                    if (oItem[i].checked == true) {
                        itemvalue += oItem[i].value + ","
                        itemname += oItemN[i].value + ",";
                    }
                }
            }
            if (itemvalue == "") {
                alert("没有选中商品信息！");
            } else {
                itemname = itemname.substring(0, itemname.length - 1);
                itemvalue = itemvalue.substring(0, itemvalue.length - 1);

                if (itemname == '') { return false; }
                var productform = document.forms[0];
                if (productform.hfProductList.value == '') {
                    //名称值
                    productform.ProductList.value = itemname;
                    window.returnValue = productform.ProductList.value;
                    //ID值
                    productform.hfProductList.value = itemvalue;
                    window.returnValue = productform.hfProductList.value;
                    return false;
                }
                var singleUser = itemvalue.split(',');
                var singleName = itemname.split(',');
                var ignoreUser = '';
                for (i = 0; i < singleUser.length; i++) {
                    if (checkInfo(productform.hfProductList.value, singleUser[i])) {
                        ignoreUser = ignoreUser + singleName[i] + " ";
                    }
                    else {
                        productform.ProductList.value = productform.ProductList.value + ',' + singleName[i];
                        productform.hfProductList.value = productform.hfProductList.value + ',' + singleUser[i]; //ID
                    }
                }
                if (ignoreUser != '') {
                    alert(ignoreUser + '商品分类已经存在，此操作已经忽略！');
                    return false;
                }
                window.returnValue = productform.ProductList.value;
            }
        }
    </script>
</head>
<body>
    <form id="productform" runat="server">
    <asp:ScriptManager ID="sm" runat="server">
    </asp:ScriptManager>
    <div style="margin-left: 10px;">
        <div>
            <asp:UpdatePanel ID="upOk" runat="server">
                <ContentTemplate>
                    <table width='100%' border='0' align='center' cellpadding='2' cellspacing='0' style="margin-left: 10px">
                        <tr class='title'>
                            <td valign='top'>
                                <b>已经选定的商品：</b>
                            </td>
                            <td align='right'>
                            </td>
                        </tr>
                        <tr class='tdbg'>
                            <td>
                                <asp:TextBox ID="ProductList" Rows="6" Columns="50" TextMode="MultiLine" runat="server"
                                    ReadOnly="true" Height="86px" />
                                <asp:HiddenField ID="hfProductList" runat="server" />
                            </td>
                        </tr>
                        <tr>
                            <td style="margin-left: 20px;">
                                <input name="ok" value="确 定" type="button" onclick="javascript:window.returnValue=productform.hfProductList.value;window.close();" />
                                <input type='button' name='del1' onclick='del(1)' value='删除最后' />
                                <input type='button' name='del2' onclick='del(0)' value='删除全部' />
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <div>
            <div style="text-align: right">
                <table style="margin-left: 10px;">
                    <tr>
                        <td>
                            <div style="float: left;">
                                商品分类：<asp:DropDownList ID="ddlClassFirst" runat="server" AutoPostBack="True" AppendDataBoundItems="True"
                                    Width="80px" OnSelectedIndexChanged="ddlClassFirst_SelectedIndexChanged">
                                </asp:DropDownList>
                            </div>
                            <div style="float: left;">
                                <asp:UpdatePanel ID="upanClass" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlClassSecond" runat="server" AppendDataBoundItems="True"
                                            AutoPostBack="true" OnSelectedIndexChanged="ddlClassSecond_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <asp:DropDownList ID="ddlClassThree" runat="server" AutoPostBack="true" AppendDataBoundItems="True"
                                            OnSelectedIndexChanged="ddlClassThree_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <asp:DropDownList ID="ddlClassFour" runat="server" AppendDataBoundItems="True">
                                        </asp:DropDownList>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlClassFirst" EventName="SelectedIndexChanged" />
                                        <asp:AsyncPostBackTrigger ControlID="ddlClassSecond" EventName="SelectedIndexChanged" />
                                        <asp:AsyncPostBackTrigger ControlID="ddlClassThree" EventName="SelectedIndexChanged" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </div>
                        </td>
                    </tr>
                </table>
                <table style="margin-left: 10px;">
                    <tr>
                        <td>
                            商品名称：<asp:TextBox ID="txtProductName" CausesValidation="false" runat="server"></asp:TextBox>&nbsp;&nbsp;
                        </td>
                        <td>
                            <asp:Button ID="SeachBut" runat="server" Text="查询" OnClick="SeachBut_Click" />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </td>
                    </tr>
                </table>
            </div>
            <asp:UpdatePanel ID="upProduct" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="gvManage" runat="server" AllowPaging="True" AutoGenerateColumns="false"
                        CssClass="adminlist" OnRowDataBound="gvManage_RowDataBound" OnPageIndexChanging="gvManage_PageIndexChanging"
                        PageSize="20">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <input type="checkbox" id="chk_select" name="chk_select" value='<%# Eval("ProductID") %>' />
                                    <input type="hidden" id="hidproductname" name="hidproductname" value='<%#Eval("ProductName")%>' />
                                </ItemTemplate>
                                <HeaderTemplate>
                                    <input id="chkAll" onclick="SelectAllCheckboxes(this,'chk_select');" runat="server"
                                        type="checkbox" />
                                </HeaderTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="5%" />
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="ID" DataField="ProductID" ItemStyle-HorizontalAlign="Center" />
                            <asp:TemplateField HeaderText="图片">
                                <HeaderStyle Width="90px" />
                                <ItemTemplate>
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# (Convert.ToString(Eval("ProductSmallImageUrl"))==""?"../Images/show_nopic.gif":"../"+Eval("ProductSmallImageUrl").ToString()) %>'
                                        Height="80px" Width="80px" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="商品名称">
                                <ItemTemplate>
                                    <a style="cursor: pointer;" onclick="add('<%# Eval("ProductName")%>','<%# Eval("ProductID")%>')">
                                        <%#Eval("ProductName")%>
                                    </a>
                                </ItemTemplate>
                                <HeaderStyle Width="30%" />
                                <ItemStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="ClassName" HeaderText="商品分类" ItemStyle-HorizontalAlign="Center" />
                            <asp:TemplateField HeaderText="类型">
                                <ItemTemplate>
                                    <%#GetProductTypeStr(Eval("ProductType"))%>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="库存" DataField="Stocks" ItemStyle-HorizontalAlign="Center" />
                            <asp:BoundField HeaderText="价格" DataField="StandPrice" ItemStyle-HorizontalAlign="Center" />
                        </Columns>
                        <EmptyDataTemplate>
                            <table style="width: 100%" class="adminlist">
                                <tr>
                                    <th>
                                        图片
                                    </th>
                                    <th>
                                        商品名称
                                    </th>
                                    <th>
                                        商品分类
                                    </th>
                                    <th>
                                        类型
                                    </th>
                                    <th>
                                        库存
                                    </th>
                                    <th>
                                        价格
                                    </th>
                                </tr>
                                <tr>
                                    <td colspan="6" style="text-align: center">
                                        没有符合查询条件的记录！
                                    </td>
                                </tr>
                            </table>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <div class="page">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Button ID="btnSelected" runat="server" OnClientClick="return SelectCheckboxes()"
                            Text="选中的项" />&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:Literal ID="ltrPagePanel" runat="server"></asp:Literal>&nbsp;&nbsp;&nbsp;&nbsp;
                    </div>
                    <br />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </div>
    </form>
</body>
</html>
