<%@ page language="C#" masterpagefile="~/Member/MyShopMasterPage.master" autoeventwireup="true" inherits="Member_StockManage_StockAdd, WebApp_deploy" title="添加入/出库单" %>

<asp:Content ID="cphContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <script src="../../Js/My97DatePicker/config.js" type="text/javascript"></script>
    <script src="../../Js/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script type="text/javascript">
        function pickedFunc() {
            $dp.$('<%=txtInputTime.ClientID %>').value = $dp.cal.getNewDateStr('yyyy-MM-dd HH:mm:ss');
        }
    </script>
    <div class="pageTitle" style="line-height: 30px; vertical-align: middle;">
        &nbsp;&nbsp;&nbsp;&nbsp;库单管理</div>
    <div class="TabText">
        <table class="ShopOrderStyle">
            <caption>
                <asp:Literal ID="ltrTitle" runat="server"></asp:Literal>
            </caption>
            <tr>
                <td class="OrderTitle">
                    单据编号 ：
                </td>
                <td>
                    <asp:TextBox ID="txtStockNumber" runat="server" Enabled="false" Width="165px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    记录时间 ：
                </td>
                <td>
                    <asp:TextBox ID="txtInputTime" runat="server" onfocus="WdatePicker({el:this,dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:pickedFunc})"
                        CssClass="Wdate" Width="165px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    备注 ：
                </td>
                <td>
                    <asp:TextBox ID="txtRemarks" runat="server" TextMode="MultiLine" Width="318px" Height="102px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    总金额：
                </td>
                <td style="width: 300px">
                    <asp:Label ID="lblGoodsTotal" runat="server" Text="0"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:HiddenField ID="hfProductID" runat="server" />
                    <asp:HiddenField ID="hfProductStr" runat="server" />
                    <asp:HiddenField ID="hfMoneyGoods" runat="server" />
                    <div id="divProduct" runat="server">
                    </div>
                    <div id="divNotice" runat="server" style="display: none">
                        &nbsp;&nbsp;<asp:CheckBox ID="MessageStatus" runat="server" Checked="true" Text="使用站内信确认到货通知" /><br />
                        &nbsp;&nbsp;<asp:CheckBox ID="EmailStatus" runat="server" Text="使用Email确认到货通知" /><br />
                        &nbsp;&nbsp;<asp:CheckBox ID="MobileStatus" runat="server" Text="使用手机短信确认到货通知" />
                    </div>
                </td>
            </tr>
            <tr>
                <td align="center" valign="bottom" colspan="2">
                    &nbsp;&nbsp; <span style="margin-left: 160px">&nbsp;</span>
                    <asp:HiddenField ID="hfStockStatus" runat="server" /><%--库存状态--%>
                    <input type="button" id="btnSelectProduct" onclick="SelectProduct()" value="选择商品" />
                    <asp:Button ID="btnSave" runat="server" Text="保存" OnClientClick="return checkPass()"
                        OnClick="btnSave_Click"></asp:Button>
                    <asp:Button ID="btnCancle" runat="server" Text="取消" OnClick="btnCancle_Click"></asp:Button>
                </td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">
        //基本信息
        function checkInputTime() {
            var inputTime = $("#<%=txtInputTime.ClientID %>"); //记录时间
            if (inputTime.val().length == 0) {
                alert("记录时间不能为空!");
                inputTime.focus();
                return false;
            }
            return true;
        }
        function checkPass() {
            if (checkInputTime() && GetAddProducts()) {
                return true;
            } else {
                return false;
            }
        }
        //商品
        function GetAddProducts() {
            var productstr = $("#<%=hfProductStr.ClientID %>"); //商品（ID,数量,价格,单位）
            var productlist = $("#tb_rptProduct tr td table"); //商品信息
            if (productlist.length < 1) {
                alert("没有商品数据！");
                return false;
            }

            var lblGoodsTotal = $("#<%=lblGoodsTotal.ClientID %>"); //商品总额
            var hfMoneyGoods = $("#<%=hfMoneyGoods.ClientID %>"); //商品总额
            hfMoneyGoods.val(lblGoodsTotal.text());

            var str = "";
            var newstr = ""; //商品（ID,数量,价格,单位）
            if (productlist.length > 0) {
                for (var i = 0; i < productlist.length; i++) {
                    var vproductid = $("#productID" + i); //商品ID
                    var vprocount = $("#procount" + i); //数量
                    var vprice = $("#price" + i); //价格
                    var vunit = $("#unit" + i); //单位
                    if (parseInt(vprocount.val()) < 1) {
                        alert('商品数量不能小于0！');
                        vprocount.focus();
                        vprocount.val("1");
                        return;
                    }
                    newstr = vproductid.val() + "," + vprocount.val() + "," + vprice.text() + "," + vunit.val() + "|";
                    str += newstr;
                }
                var position = str.lastIndexOf('|');
                if (position > 0) {
                    str = str.substring(0, str.length - 1);
                }
                productstr.val(str);                
            }
            return true;
        }
    </script>
    <script type="text/javascript">
        $(function () {
            //添加：addinfo、修改：editinfo
            var actionInfo = "<%=Action %>";
            if (actionInfo == "editinfo") {
                document.getElementById("btnSelectProduct").disabled = true; //选择商品按钮不可用
                var IDStr = "<%=strID %>";
                GetProductByProductids(IDStr);
            } else if (actionInfo == "addinfo") {
                document.getElementById("<%=btnSave.ClientID %>").disabled = true; //保存按钮不可用
            }
        });

        //添加商品
        function SelectProduct() {
            var ctlName = document.getElementById("<%=hfProductID.ClientID %>");
            var arr = showModalDialog('/Member/ProductManage.aspx?ProductType=0&DefaultValue=' + ctlName.value, '', 'dialogWidth:800px; dialogHeight:650px; help: no; scroll: yes; status: no');
            if (arr != null) {
                ctlName.value = arr; //商品ID
                document.getElementById("<%=btnSave.ClientID %>").disabled = false; //保存按钮可用
                GetProductByProductids(arr);
            }
        }     
    </script>
    <%--商品信息 begin--%>
    <script type="text/javascript">
        //商品信息
        function GetProductByProductids(productids) {
            var prodcutStr = "";
            $("#<%=divProduct.ClientID %>").html("");

            prodcutStr += "<table class='' width='100%' id='tb_rptProduct'>";
            $.post("../../WebService/ShopService.ashx", { 'Action': 'GetProductByProductIDs', 'ProductIDs': productids }, function (data, state) {
                if (state == "success") {
                    if (data.toString() != "") {
                        var d = eval(data);
                        $.each(d.table1, function (i) {
                            var productid = d.table1[i].ProductID;
                            var productimg = "../../Images/show_nopic.gif";
                            if (d.table1[i].ProductSmallImageUrl != "") {
                                productimg = "../../" + d.table1[i].ProductSmallImageUrl;
                            }
                            var productname = d.table1[i].ProductName;
                            var pt = d.table1[i].ProductType;
                            var producttype = "普通商品";
                            //商品类型（一口价：1、拍卖：2、团购：3、促销(秒杀)：4、积分商品：5）
                            if (pt == "4") {
                                producttype = "促销(秒杀)";
                            } else if (pt == "5") {
                                producttype = "积分商品";
                            }
                            var productstocks = d.table1[i].Stocks;
                            var productprice = d.table1[i].StandPrice;
                            var productunit = d.table1[i].PUnit;
                            if (productname.length > 20) {
                                // productname = productname.substring(0, 18) + "...";<input type='button' name='del1' onclick='del(" + productid + ")' value='删除' />
                            }
                            var procount = 0; //数量
                            var goodsMoney = 0; //金额

                            prodcutStr += "<tr><td>" +
                                        "<table class='ShopOrderStyle'><caption><input type='hidden' id='unit" + i + "' value='" + productunit + "' /><input type='hidden' id='productID" + i + "' value='" + productid + "' />" + productname + "</caption><tr>" +
                                       "<tr><td class='OrderTitle'>数量：</td><td style='width:220px;'><input type='text' style='width:80px' id='procount" + i + "' onblur='SumKuCun(this," + i + ")' value='" + procount + "'/>" +
                                       "（当前库存:<span style='color:black' id='stock_before" + i + "'>" + productstocks + "</span>）</td>" +
                                        "<td class='OrderTitle'>当前单价：</td><td style='width:220px;><span style='color:black' id='price" + i + "'>" + productprice + "</span></td></tr>";

                            prodcutStr += "<tr><td class='OrderTitle'>商品金额：</td><td><span style='color:Green' id='goodsMoney" + i + "'>" + goodsMoney + "</span></td>" +
                                        "</tr></table>" +
                                        "</td></tr>";

                        });
                    }
                    prodcutStr += "</table>";
                    $("#<%=divProduct.ClientID %>").append(prodcutStr);
                }
            });
        }  
    </script>
    <script type="text/javascript">
        //入库后的库存量(库存失去焦点时)
        function SumKuCun(sen, i) {
            var goodsPrice = $("#price" + i); //单价
            var goodsMoney = $("#goodsMoney" + i); //金额
            var beforestocks = $("#stock_before" + i); //库存
            var proCount = $(sen); //数量
            var sumMoney = 0;
            //输入的数量是数字且不能小于0
            if (proCount.val() == null || proCount.val() == "") {
                alert("请输入商品的入/出库数量！");
                proCount.focus();
                return;
            }
            if ((isNaN(proCount.val()) == true)) {//判断输入的是否为数字
                alert("请您输入0~9之间的数字！");
                proCount.focus();
                return;
            }
            if (parseInt(proCount.val()) < 1) {
                alert('您输入数量不能小于0！');
                proCount.focus();
                proCount.val("1");
                return;
            }

            //            if (parseInt(proCount.val()) > parseInt(beforestocks.html())) {
            //                alert("输入的数量不能大于当前的商品库存数！当前商品库存数量为" + beforestocks.html() + "！");
            //                proCount.focus();
            //                return;
            //            }

            if (goodsPrice.html() != null) {
                sumMoney = parseFloat(goodsPrice.html()) * parseInt(proCount.val());
            }
            goodsMoney.html(sumMoney); //商品金额
            TotalFun();
        }
        //总金额
        function TotalFun() {
            var lblGoodsTotal = $("#<%=lblGoodsTotal.ClientID %>"); //商品总额
            var productlist = $("#tb_rptProduct tr td table"); //商品信息
            var moneygoods = 0;

            for (var i = 0; i < productlist.length; i++) {
                var goodsMoney = $("#goodsMoney" + i); //商品金额
                if (goodsMoney.html() != null) {
                    moneygoods += parseFloat(goodsMoney.html());
                }
            }
            lblGoodsTotal.text(moneygoods);
        }

        //删除
        function del(num) {
            var productstr = $("#<%=hfProductStr.ClientID %>"); //商品（ID,库存,价格,单位,入库后）
            var productlist = $("#tb_rptProduct tr"); //商品信息
            var producttable = document.getElementById("tb_rptProduct"); //商品信息
            var str = "";
            if (productlist.length - 1 > 0) {
                for (var i = 0; i < productlist.length - 1; i++) {
                    var vproductid = $("#productID" + i); //商品ID
                    if (vproductid.val() == num) {
                        var row = parseInt(i + 1);
                        producttable.deleteRow(row);
                    } else {
                        str += vproductid.val() + ",";
                    }
                }
            }
            var position = str.lastIndexOf(',');
            if (position > 0) {
                str = str.substring(0, str.length - 1);
            }
            GetProductByProductids(str); //商品信息
            return true;
        } 
    </script>
    <%--商品信息 end--%>
</asp:Content>
