<%@ Page Title="" Language="C#" MasterPageFile="~/Styles/MasterPage/MasterPage_Edit.master" AutoEventWireup="true" CodeFile="StockCounts_Edit.aspx.cs" Inherits="Stock_StockCount_StockCounts_Edit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript">
        var id = '<%=id%>';
        var action = '<%=xview["action"]%>';
        function Save() {
            <%=xview["Script_GetInput"] %>
            var action = '<%=xview["action"]%>';
            parent.ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'StockCounts_Service.asmx/Save',
                data: "{action:'" + action + "',<%=xview["Script_SaveJSON"]%>}",
                dataType: 'json',
                success: function (result) {
                    parent.HideLoadingMask(); //关闭遮罩
                    var r = result.d;
                    if (r == "success") {
                        if(action == "Add"){
                            asyncbox.confirm('保存成功!\n是否要继续新增？','提示',function(buttonResult){
　　　                            if(buttonResult == 'ok'){
　　　　　                            window.location.reload();
　　　                            }else{
                                    parent.search_grid();
                                    CloseDivWin(); //关闭窗口
　　　                            }
                            });
                        }else{
                            parent.search_grid();
                            asyncbox.success('保存成功!','提示',function(buttonResult){
                                CloseDivWin(); //关闭窗口
                            });
                        }
                    } else {
                        parent.AlertNO(r);
                    }
                }
            });
        }
    </script>
    <!--工具栏-->
    <div class="list_menu_bar">
        <a plain="true" class="easyui-linkbutton" iconcls="icon-save" onclick="Save()" id="btn_save" style='<%=auth.AuthView("Stock_StockCount_Edit")%>'>保存</a>
    </div>
    <div id="edit_div" style="width:800px;height:500px;overflow:auto;">
        <div class="edit_area" style='margin-left:40px;border:solid 0px red;float:left'>
        <input type="hidden" id='d_wh_id' value='<%=tm.wh_id%>' />
        <%=xview["input_ui"]%>
        </div>
        <div style='float:left'>
            <a id="inventory_photo_view_href" href='<%=xview["inventory_photo_view_href"]%>' target="_blank">
                <img id="inventory_photo_view" style='border:0px' id='small_pic_url' src="<%=xview["inventory_photo_view"]%>"/></a>
        </div>
    </div>
    <script type="text/javascript">
        var dgt_quan = <%=xview["digit_quantity"]%>;
        var dgt_num = <%=xview["digit_number"]%>;
        var dgt_pri = <%=xview["digit_unit_price"]%>;
        var dgt_exch = <%=xview["digit_exch"]%>;
        
        //初始化页面
        function init_page() {
            FastSearch_Inventory(); //查询存货

            //选择自定义项档案
            <%for(int i=1;i<=5;i++){ %>
                $F("trigger_d_sdefine<%=i%>").onclick = function () {
                    AdvancedSearch_DefineItem($("#d_sdefine<%=i%>").val(),'StockCounts_d_sdefine<%=i%>','d_sdefine<%=i%>');
                };
            <%}
              for(int i=1;i<=5;i++){ %>
                $F("trigger_d_ndefine<%=i%>").onclick = function () {
                    AdvancedSearch_DefineItem($("#d_ndefine<%=i%>").val(),'StockCounts_d_ndefine<%=i%>','d_ndefine<%=i%>');
                };
            <%} %>

            //弹窗选择自由项
            $F("trigger_free1").onclick = function () {
                AdvancedSearch_DefineItem($("#free1").val(),'free1','free1');
            };
            $F("trigger_free2").onclick = function () {
                AdvancedSearch_DefineItem($("#free2").val(),'free2','free2');
            };
            //弹窗选择批次
            $F("trigger_batch").onclick = function () {
                AdvancedSearch_Batch($("#batch").val(),'batch','batch');
            };
            $F("sc_id").value = "<%=id%>";
            var verify_uid = '<%=tm.verify_uid%>';
            //假如已审核
            if(verify_uid != ''){
                $('#btn_save').linkbutton('disable');
                disable_all_input();//禁止输入
            }
            <%if(action.Equals("Update")){ %>
                $F("i_code").value = '<%=m.inventory.i_code%>';
                $F("i_name").value = '<%=m.inventory.i_name%>';
                $F("standard").value = '<%=m.inventory.standard%>';
                $F("munit").value = '<%=m.inventory.munit%>';
                $F("aunit").value = '<%=m.inventory.aunit%>';

                $F("d_inv_sdefine1").value = '<%=m.inventory.sdefine1%>';//存货字符自定义项1
                $F("d_inv_sdefine2").value = '<%=m.inventory.sdefine2%>';//存货字符自定义项2
                $F("d_inv_sdefine3").value = '<%=m.inventory.sdefine3%>';//存货字符自定义项3
                $F("d_inv_sdefine4").value = '<%=m.inventory.sdefine4%>';//存货字符自定义项4
                $F("d_inv_sdefine5").value = '<%=m.inventory.sdefine5%>';//存货字符自定义项5

                $F("d_inv_ndefine1").value = '<%=m.inventory.ndefine1%>';//存货数值自定义项1
                $F("d_inv_ndefine2").value = '<%=m.inventory.ndefine2%>';//存货数值自定义项2
                $F("d_inv_ndefine3").value = '<%=m.inventory.ndefine3%>';//存货数值自定义项3
                $F("d_inv_ndefine4").value = '<%=m.inventory.ndefine4%>';//存货数值自定义项4
                $F("d_inv_ndefine5").value = '<%=m.inventory.ndefine5%>';//存货数值自定义项5

            <%}else{ %>
                $F("d_wh_id").value = '<%=tm.warehouse==null?0:tm.warehouse.wh_id%>';
                $F("d_wh_name").value = '<%=tm.warehouse==null?"":tm.warehouse.wh_name%>';
                $F("d_dep_id").value = '<%=tm.department==null?0:tm.department.dep_id%>';
                $F("d_dep_name").value = '<%=tm.department==null?"":tm.department.dep_name%>';
            <%}%>
            //如果辅计量单位为空
            if($F("aunit").value == ""){
                $F('num').disabled = true;//禁止输件数
                $F('convert_rate').disabled = true;//禁止输换算率
            }
        }
        //改数量
        $F("quantity").onchange = function(){
            if(IsSpaceOrNaNOrZero($F('quantity').value)){
                $F('quantity').value = '';
                return;
            }
            //辅计单位不为空
            if($F('aunit').value != ""){
                //如果件数不为空
                if($F('num').value != ""){
                    //换算率=数量/件数
                    $F('convert_rate').value = ($F('quantity').value/$F('num').value).toFixed(dgt_exch);
                }
                //如果换算率不为空但件数为空
                if($F('convert_rate').value != "" && $F('num').value == ""){
                    //件数=数量/换算率
                    $F('num').value = ($F('quantity').value/$F('convert_rate').value).toFixed(dgt_num);
                }
            }
        }
        //改件数
        $F("num").onchange = function(){
            if(IsSpaceOrNaNOrZero($F('num').value)){
                $F('num').value = '';
                return;
            }
            if($F('num').value != "" && $F('convert_rate').value != "" && $F('aunit').value != ""){
                //数量=换算率*件数
                $F('quantity').value = ($F('convert_rate').value*$F('num').value).toFixed(dgt_quan);
            }
        }
        //改换算率
        $F("convert_rate").onchange = function(){
            if(IsSpaceOrNaNOrZero($F('convert_rate').value)){
                $F('convert_rate').value = '';
                return;
            }
            if($F('num').value != "" && $F('convert_rate').value != "" && $F('aunit').value != ""){
                //数量=换算率*件数
                $F('quantity').value = ($F('convert_rate').value*$F('num').value).toFixed(dgt_quan);
           }
        }
        $(document).ready(function(){
            init_page();
            $F("i_code").focus();//存货编码设置焦点
            //在存货编码回车自动带出信息
            $F("i_code").onkeydown = function (evt) {
                evt = evt ? evt : (window.event ? window.event : null);
                if (evt.keyCode == "13") {
                    var i_id = SearchService_InventoryByCode($F("i_code").value);
                }
            }
        });


        //通过主键_获取存货
        function SearchService_Inventory(i_id) {
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: '../../Base/Inventory/Inventory_Service.asmx/Get',
                data: "{i_id:'" + i_id + "'}",
                dataType: 'json',
                success: function (result) {
                    $("#i_id").val(result.d.i_id); //存货id
                    $("#i_code").val(result.d.i_code); //存货编码
                    $("#i_name").val(result.d.i_name); //存货名称
                    $("#standard").val(result.d.standard); //规格型号
                    $("#munit").val(result.d.munit); //主单位
                    $("#aunit").val(result.d.aunit); //辅计量单位
                    if ($("#aunit").val() == "") {//如果辅计量单位不为空
                        $("#num").attr("disabled", true); //禁止输件数
                        $("#num").val(""); //清空件数
                        $("#convert_rate").attr("disabled", true); //禁止输换算率
                        $("#convert_rate").val(""); //清空换算率
                    } else {
                        $("#convert_rate").val(result.d.convert_rate); //辅计量单位
                        $("#num").attr("disabled", false); //允许输件数
                        $("#convert_rate").attr("disabled", false); //允许输换算率
                    }
                    if(result.d.on_batch == 1){
                        $("#batch").attr("disabled", false); //允许输批次
                        $("#trigger_batch").css("display","");
                    }else{
                        $("#batch").attr("disabled", true); //禁止输批次
                        $("#batch").val("");
                        $("#trigger_batch").css("display","none");
                    }
                    var small_pic_url = result.d.inventory_photo.small_pic_url; //缩略图
                    if (small_pic_url != null) {
                        $("#inventory_photo_view").attr("src", result.d.inventory_photo.small_pic_url);
                    }
                    var pic_url = result.d.inventory_photo.pic_url; //原图
                    if (pic_url != null) {
                        $("#inventory_photo_view_href").attr("href", result.d.inventory_photo.pic_url);
                    }
                    $("#d_inv_sdefine1").val(result.d.sdefine1); //存货字符自定义项1
                    $("#d_inv_sdefine2").val(result.d.sdefine2); //存货字符自定义项2
                    $("#d_inv_sdefine3").val(result.d.sdefine3); //存货字符自定义项3
                    $("#d_inv_sdefine4").val(result.d.sdefine4); //存货字符自定义项4
                    $("#d_inv_sdefine5").val(result.d.sdefine5); //存货字符自定义项5
                    $("#d_inv_ndefine1").val(result.d.ndefine1); //存货数值自定义项1
                    $("#d_inv_ndefine2").val(result.d.ndefine2); //存货数值自定义项2
                    $("#d_inv_ndefine3").val(result.d.ndefine3); //存货数值自定义项3
                    $("#d_inv_ndefine4").val(result.d.ndefine4); //存货数值自定义项4
                    $("#d_inv_ndefine5").val(result.d.ndefine5); //存货数值自定义项5
                }
            });
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'StockCounts_Service.asmx/GetZhangMian',
                data: "{sc_id:'" + id + "',i_id:'" + i_id + "'}",
                dataType: 'json',
                success: function (result) {
                    var r = unescape(result.d);
                    var arr = r.split("||");
                    $('#quantity_zm').val(arr[0]);//账面数量
                    $('#num_zm').val(arr[1]);//账面件数
                    HideLoadingMask(); //关闭遮罩

                }
            });
        }

    </script>
</asp:Content>

