<%@ page language="C#" masterpagefile="~/Member/MyShopMasterPage.master" autoeventwireup="true" inherits="Member_StockManage_DepotList, WebApp_deploy" title="存货报损单" %>

<asp:Content ID="cphContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <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;<asp:Literal ID="ltrPageTitle" runat="server">存货报损单</asp:Literal>
    </div>
    <div class="TabText">
        <table class="ShopOrderStyle">
            <caption>
                &nbsp;<asp:Literal ID="ltrTitle" runat="server" Text=""></asp:Literal>
            </caption>
        </table>
        <table class="ShopOrderStyle" id="tab_AddOrEdit">
            <tr>
                <td class="OrderTitle">
                    单据编号 ：
                </td>
                <td style="width: 220px">
                    <asp:TextBox ID="txtStockNumber" runat="server" Enabled="false" Width="200px"></asp:TextBox>
                </td>
                <td class="OrderTitle">
                    <asp:Label ID="lblStockType" runat="server">出库类型 ：</asp:Label>
                </td>
                <td style="width: 220px">
                    <asp:DropDownList ID="ddlStockTypeStatue" runat="server">
                    </asp:DropDownList>
                </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>
                <td class="OrderTitle">
                    记录人 ：
                </td>
                <td>
                    <asp:TextBox ID="txtInputer" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="OrderTitle">
                    金额 ：
                </td>
                <td>
                    <asp:Label ID="lblMoneyTotal" runat="server"></asp:Label>
                </td>
                <td class="OrderTitle">
                    说明 ：
                </td>
                <td>
                    <asp:TextBox ID="txtRemarks" runat="server" TextMode="MultiLine" Width="260px" Height="60px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <asp:HiddenField ID="hfProductStr" runat="server" />
                    <%-- hfProductStr  ID,数量,价格,单位|ID,数量,价格,单位--%>
                    <asp:HiddenField ID="hfProductID" runat="server" />
                    <%--hfProductID 商品ID--%>
                    <asp:HiddenField ID="hfProductIDCount" runat="server" Value="" />
                    <%--hfProductIDCount  商品ID,商品数量|商品ID,商品数量--%>
                    <asp:HiddenField ID="hfItemID" runat="server" Value="" />
                    <asp:HiddenField ID="hfMoneyTotal" runat="server" />
                    <%--金额--%>
                    <div id="divProduct" runat="server">
                    </div>
                </td>
            </tr>
            <tr>
                <td align="center" valign="bottom" colspan="4" style="line-height: 30px">
                    <span style="margin-left: 160px">&nbsp;</span>
                    <input type="button" id="btnSelectProduct" onclick="SelectProduct()" value="选择商品" />&nbsp;
                    <input type="button" id="btnCheckAoucher" onclick="javascript:window.location.href = 'AccountAoucherInfo.aspx?Action=checkinfo&InfoID=<%=InfoID %>'"
                        value="查看凭证" />&nbsp;
                    <asp:Button ID="btnConfirm" runat="server" Text="确认信息" OnClientClick="return checkPass()"
                        OnClick="btnConfirm_Click"></asp:Button>&nbsp;
                    <asp:Button ID="btnDeleteInfo" runat="server" Text="删除信息" OnClick="btnDeleteInfo_Click">
                    </asp:Button>&nbsp;
                    <asp:Button ID="btnSave" runat="server" Text="保存" OnClientClick="return checkPass()"
                        OnClick="btnSave_Click"></asp:Button>&nbsp;
                    <asp:Button ID="btnCancle" runat="server" Text="取消" OnClick="btnCancle_Click"></asp:Button>
                </td>
            </tr>
        </table>
        <br />
    </div>
    <script type="text/javascript">
        //基本信息
        function checkInputTime() {
            var inputTime = $("#<%=txtInputTime.ClientID %>"); //记录时间
            if (inputTime.val().length == 0) {
                alert("记录时间不能为空!");
                inputTime.focus();
                return false;
            }
            return true;
        }
        function checkOutStock() {
            //获取用户控件中的dropdownlist的选择值 
            var outStock = document.getElementById('<%=ddlStockTypeStatue.ClientID %>');
            var bojvalue = "";
            for (i = 0; i < outStock.options.length; i++) {
                if (outStock.options[i].selected)
                    bojvalue = outStock.options[i].value;
            }
            if (bojvalue.length == 0) {
                alert("请选择库存类型！");
                outStock.focus();
                return false;
            }
            return true;
        }
        function checkPass() {
            if (checkInputTime() && checkOutStock() && GetAddProducts()) {
                return true;
            } else {
                return false;
            }
        }


        //商品
        function GetAddProducts() {
            var productstr = $("#<%=hfProductStr.ClientID %>"); //商品（ID,数量,价格,单位）
            var productlist = $("#tb_rptProduct tr"); //商品信息
            if (productlist.length <= 1) {
                alert("没有需入/出库的商品！");
                return false;
            }
            var lblMoneyTotal = $("#<%=lblMoneyTotal.ClientID %>"); //金额
            var hfMoneyTotal = $("#<%=hfMoneyTotal.ClientID %>");
            hfMoneyTotal.val(lblMoneyTotal.text());
            var str = "";
            var newstr = "";
            if (productlist.length - 1 > 0) {
                for (var i = 0; i < productlist.length - 1; i++) {
                    var vproductid = $("#productID" + i); //商品ID
                    var vcount = $("#count" + i); //数量
                    var vprice = $("#price" + i); //价格
                    var vunit = $("#unit" + i); //单位

                    if (parseInt(vcount.val()) < 1) {
                        alert('商品数量不能小于0！');
                        vcount.focus();
                        vcount.val("1");
                        return false;
                    }
                    newstr = vproductid.val() + "," + vcount.val() + "," + vprice.text() + "," + vunit.text() + "|";
                    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 %>";
            var Ownerpower = "<%=Ownerpower %>";
            if (Ownerpower.toString().toLowerCase() == "false") {
                document.getElementById("btnCheckAoucher").style.display = "none";
            } else {
                document.getElementById("btnCheckAoucher").style.display = "none";
                var StockOrderType = "<%=StockOrderType %>";
                if (StockOrderType == "baoyi") {
                    //报溢单
                    document.getElementById("btnCheckAoucher").style.display = "";
                }
            }
            if (actionInfo == "addinfo") {
                document.getElementById("<%=btnSave.ClientID %>").disabled = true; //保存按钮不可用
                document.getElementById("btnCheckAoucher").disabled = true; //查看凭证按钮不可用
            } else if (actionInfo == "editinfo") {
                document.getElementById("btnSelectProduct").disabled = true; //选择商品按钮不可用
                document.getElementById("btnCheckAoucher").disabled = true; //查看凭不证可用

                var ctlName = document.getElementById("<%=hfProductID.ClientID %>");
                GetProductByOrderProdcutIDs(ctlName.value);
            } else if (actionInfo == "confirminfo") {
                document.getElementById("btnSelectProduct").disabled = true; //选择商品按钮可用
                document.getElementById("btnCheckAoucher").disabled = true; //查看凭证不可用

                var ctlIDs = document.getElementById("<%=hfProductID.ClientID %>");
                GetProductByOrderProdcutIDs(ctlIDs.value);
            }
        });
    </script>
    <script type="text/javascript">
        //选择商品
        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;
                document.getElementById("<%=btnSave.ClientID %>").disabled = false; //保存
                GetProductByOrderProdcutIDs(arr);
            }
        }
        //商品信息
        function GetProductByOrderProdcutIDs(ids) {
            var hfProductIDCount = $("#<%=hfProductIDCount.ClientID %>"); //ID，数量

            //添加：addinfo、修改：editinfo
            var actionInfo = "<%=Action %>";
            var prodcutStr = "";
            $("#<%=divProduct.ClientID %>").html("");
            prodcutStr += "<table class='tableHead' width='100%' id='tb_rptProduct'>" +
                           "<tr><th style='text-align: center;'>行号</th>" +
                           "<th style='text-align: center;'>商品名称</th>" +
                           "<th style='text-align: center;'>商品类型</th>" +
                          "<th style='text-align: center;'>数量</th>" +
                          "<th style='text-align: center;'>价格</th>" +
                          "<th style='text-align: center;'>单位</th>" +
                          "<th style='text-align: center;'>金额</th></tr>";
            $.post("../../WebService/ShopService.ashx", { 'Action': 'GetProductByProductIDs', 'ProductIDs': ids }, 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 productname = d.table1[i].ProductName;
                            var pt = d.table1[i].ProductType;
                            var producttype = "普通商品";
                            //商品类型（一口价：1、拍卖：2、团购：3、促销(秒杀)：4、积分商品：5）
                            if (pt == "5") {
                                producttype = "积分商品";
                            } else if (pt == "4") {
                                producttype = "促销(秒杀)";
                            } else if (pt == "3") {
                                producttype = "团购";
                            } else if (pt == "2") {
                                producttype = "拍卖";
                            } else if (pt == "1") {
                                producttype = "一口价";
                            }
                            var stock_before = d.table1[i].Stocks; //库存
                            var productunit = d.table1[i].PUnit; //单位
                            var productcount = 0; //数量
                            var productprice = d.table1[i].StandPrice; //单价
                            if (productname.length > 20) {
                                productname = productname.substring(0, 18) + "...";
                            }

                            if (actionInfo == "editinfo" || actionInfo == "confirminfo") {
                                //修改、确认信息
                                if (hfProductIDCount.val() != "") {
                                    var position = hfProductIDCount.val().indexOf('|');
                                    if (position > 0) {
                                        var temparray = hfProductIDCount.val().split('|');
                                        for (var s = 0; s < temparray.length; s++) {
                                            var temp = temparray[s].split(',');
                                            if (temp != null && temp.length > 1) {
                                                if (productid == temp[0]) {
                                                    productcount = temp[1]; //数量
                                                }
                                            }
                                        }
                                    } else {
                                        var temp1 = hfProductIDCount.val().split(',');
                                        if (temp1 != null && temp1.length > 1) {
                                            if (productid == temp1[0]) {
                                                productcount = temp1[1];
                                            }
                                        }
                                    }
                                }
                            }
                            var goodsMoney = parseFloat(productprice) * parseInt(productcount); //金额

                            prodcutStr += "<tr><td style='text-align: center;'>" + (i + 1) + "</td>" +
                                        "<td style='text-align: center;'><input type='hidden' id='productID" + i + "' value='" + productid + "' />" + productname + "</td>" +
                                        "<td style='text-align: center;'>" + producttype + "</td>" +
                                        "<td style='text-align: center;'><input type='hidden' id='stock_before" + i + "' value='" + stock_before + "' /><input type='text' style='width:80px' id='count" + i + "' onblur='SumKuCun(this," + i + ")' value='" + productcount + "'/></td>" +
                                        "<td style='text-align: center;'><span style='color:black' id='price" + i + "'>" + productprice + "</span></td>" +
                                        "<td style='text-align: center;'><span style='color:black' id='unit" + i + "'>" + productunit + "</span></td>" +
                                        "<td style='text-align: center;'><span style='color:black' id='goodsMoney" + i + "'>" + goodsMoney + "</span></td>";

                        });
                    } else {
                        prodcutStr += "<tr><td style='text-align: center;' colspan='7'>没有数据！</td>";
                    }
                    prodcutStr += "</tr></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.val())) {
                alert("输入的数量不能大于当前的商品库存数！当前商品库存数量为" + beforestocks.val() + "！");
                proCount.focus();
                return;
            }

            if (goodsPrice.text() != null) {
                sumMoney = parseFloat(goodsPrice.text()) * parseInt(proCount.val());
            }
            goodsMoney.text(sumMoney);
            TotalFun();
        }

        //总金额
        function TotalFun() {
            var lblMoneyTotal = $("#<%=lblMoneyTotal.ClientID %>"); //商品总额
            var productlist = $("#tb_rptProduct tr"); //商品信息
            var moneygoods = 0;

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