<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Transfer_Document.aspx.cs" Inherits="Stock_Transfer_Transfer_Document" %>
<%@ Register Src="~/Scripts/AWGridRuntime/UserControl/DocumentHeader.ascx" TagPrefix="uc1" TagName="ListHeader" %>
<!DOCTYPE html>

<html>
<head>
    <title></title>
    <uc1:ListHeader runat="server" ID="ListHeader" />
    <script type="text/javascript" src="../../Scripts/ModuleScript/StockFunction.js"></script>
    <script type="text/javascript" src="Transfer.js"></script>
    <script type="text/javascript">
        var id = "<%=id%>";
        var action = "<%=action%>";
        //搜索
        function search_grid() {
            var fg = document.getElementById("frame_grid");
            var url = "Transfer_Document_Grid.aspx?id=" + id + "&action=" + action;
            fg.src = url;
        }
        //新增单据
        function add_doc() {
            window.location = "Transfer_Document.aspx?action=Add";
        }
        //保存单据
        function save_doc() {
            <%=xview["Script_GetInput"] %>
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'Transfer_Service.asmx/Save',
                data: "{action:'" + action + "',<%=xview["Script_SaveJSON"]%>}",
                dataType: 'json',
                success: function (result) {
                    HideLoadingMask(); //关闭遮罩
                    var arr = result.d.split("|");
                    if (arr[0] == "success") {
                        AlertOK('保存成功!');
                        if(action == "Add"){
                            action = "Update";
                            id = arr[1];
                            window.location = 'Transfer_Document.aspx?action=Update&id=' + id;
                        }
                        CloseDivWin(); //关闭窗口
                    } else {
                        AlertNO(arr[0]);
                    }
                }
            });
        }
        //删除单据
        function delete_doc(){
            asyncbox.confirm('确定要删除本单据吗?', '警告', function (buttonResult) {
                if (buttonResult == "ok") {
                    ShowLoadingMask(); //显示遮罩
                    $.ajax({
                        type: "POST",
                        contentType: "application/json",
                        url: 'Transfer_Service.asmx/Delete',
                        data: "{ids:'" + id + "'}",
                        dataType: 'json',
                        success: function (result) {
                            HideLoadingMask(); //关闭遮罩
                            var arr = result.d.split('|');
                            if(arr[0] == "success"){
                                AlertOK("删除成功！");
                                if(arr[1] != "0"){
                                    window.location = 'Transfer_Document.aspx?action=Update&id=' + arr[1];
                                }else{
                                    add_doc();
                                }
                            }else{
                                AlertNO(result.d);
                            }
                        }
                    });
                }
            });
        }
        //审核单据
        function verify_doc(action_type){
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'Transfer_Service.asmx/Verify',
                data: "{ids:'" + id + "',action_type:'" + action_type + "'}",
                dataType: 'json',
                success: function (result) {
                    HideLoadingMask(); //关闭遮罩
                    if(result.d == "success"){
                        var title = '审核';
                        if(action_type==-1)
                            title = '弃审';
                        asyncbox.success(title + '成功！', '操作成功！', function (buttonResult) {
                            window.location.reload();
                        });
                   }else{
                        AlertNO(result.d);
                    }
                }
            });
        }
        //检查单据
        function check_doc(){
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'Transfer_Service.asmx/Check',
                data: "{id:'" + id + "'}",
                dataType: 'json',
                success: function (result) {
                    HideLoadingMask(); //关闭遮罩
                    if(result.d == "success"){
                        AlertOK("检查没问题！");
                    }else{
                        AlertNO(result.d);
                    }
                }
            });
        }
        //新增行
        function add_row() {
            if(action == "Add")return;
            if(action == "Add"){
                AlertNO("请先保存表头后才进行表体数据添加！");
                return;
            }
            asyncbox.open({
                id: 'win_modify',
                url: 'Transfers_Edit.aspx?action=Add&id='+id,
                modal: true,
                title: '新增'
            });
            
        }
        //修改行
        function edit_row() {
            if(action == "Add")return;
            var fg = document.getElementById("frame_grid"); //得到grid控件所在iframe
            var cw = fg.contentWindow;
            var did = cw.aw_GetSelectRows(cw._tss_id); //返回一维数组
            if (did == "")
                return;
            if (did.length == 0) {
                AlertNO('请选择要查看的行!');
                return;
            }
            if (did.length > 1) {
                AlertNO('一次只可以查看一条记录!');
                return;
            }
            asyncbox.open({
                id: 'win_modify',
                url: 'Transfers_Edit.aspx?action=Update&id=' + id + '&did=' + did,
                modal: true,
                title: '修改'
            });
        }
        //删除行
        function del_row(){
            if(action == "Add")return;
            var fg = document.getElementById("frame_grid"); //得到grid控件所在iframe
            var cw = fg.contentWindow;
            var ids = cw.aw_GetSelectRows(cw._tss_id); //返回一维数组
            if (ids.length == 0) {
                AlertNO('请选择要删除的记录!');
                return;
            }
            asyncbox.confirm('确定要删除该行吗?', '警告', function (buttonResult) {
                if (buttonResult == "ok") {
                    ShowLoadingMask(); //显示遮罩
                    $.ajax({
                        type: "POST",
                        contentType: "application/json",
                        url: 'Transfers_Service.asmx/Delete',
                        data: "{ids:'" + ids + "'}",
                        dataType: 'json',
                        success: function (result) {
                            HideLoadingMask(); //关闭遮罩
                            if(result.d != "success"){
                                AlertNO(result.d);
                                return;
                            }
                            fg.src=fg.src;
                        }
                    });
                }
            });
        }
        //上张下张
        function prev_next_doc(type){
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'Transfer_Service.asmx/GetPrevNextID',
                data: "{id:'" + id + "',type:'" + type + "'}",
                dataType: 'json',
                success: function (result) {
                    HideLoadingMask(); //关闭遮罩
                    if(result.d==0){
                        AlertNO("不能再翻了！");
                        return;
                    }
                    window.location = 'Transfer_Document.aspx?action=Update&id=' + result.d;
                }
            });
        }

        //禁用按钮
        function disable_button(){
            if(action == "Add"){
                $('#btn_delete_doc').linkbutton('disable');
                $('#btn_save_doc').linkbutton('enable');
                $('#btn_check_doc').linkbutton('disable');
                $('#btn_verify_doc').linkbutton('disable');
                $('#btn_add_row').linkbutton('disable');
                $('#btn_del_row').linkbutton('disable');
                $('#btn_prev_doc').linkbutton('enable');
                $('#btn_next_doc').linkbutton('enable');
                $('#m_trace').menubutton('disable');
                $('#btn_print').linkbutton('disable');
            }else{
                $('#btn_delete_doc').linkbutton('enable');
                $('#btn_save_doc').linkbutton('enable');
                $('#btn_verify_doc').linkbutton('enable');
                $('#btn_add_row').linkbutton('enable');
                $('#btn_del_row').linkbutton('enable');
                $('#btn_prev_doc').linkbutton('enable');
                $('#btn_next_doc').linkbutton('enable');
                $('#m_trace').menubutton('enable');
                $('#btn_print').linkbutton('enable');
                if(b_verify){
                    //禁止所有文本框进行输入
                    disable_all_input();
                    $('#btn_add_row').linkbutton('disable');
                    $('#btn_del_row').linkbutton('disable');
                    $('#btn_delete_doc').linkbutton('disable');
                    $('#btn_save_doc').linkbutton('disable');
                    $('#btn_check_doc').linkbutton('disable');
                }
            }
            
        }
        //找单
        function find_doc(){
            var url = '../../Stock/Report/Transfer_Details.aspx';
            var tab_id = 'Stock_Report_Transfer_Details';
            top.OpenWindow('Activate', tab_id, '调拨单明细表', url);
        }
        //打印表头用的SQL
        var sql_print_main_session_id = "<%=xview["sql_print_main_session_id"]%>";
        //选择打印模板
        function grf_print(){
            var fg = document.getElementById("frame_grid"); //得到grid控件所在iframe
            var cw = fg.contentWindow;
            choice_print_template('<%=xview["auth_code"]%>',sql_print_main_session_id,cw.sql_print_detail_session_id);
        }
        //联查-转入转出单
        function trace_inout() {
            var url = '../../Stock/Report/InOut_Details.aspx?ts_id=' + id;
            top.OpenWindow('Activate', 'Stock_Report_InOut_Details_Transfer_' + id, '联查-转入转出单', url);
        }
    </script>
</head>
<body onload="search_grid();">
    <!--工具栏-->
    <div style="height:30px;" class="list_menu_bar" id="list_menu_bar">
        <a plain="true" class="easyui-linkbutton" onClick="add_doc()" id="btn_add_doc" style='<%=auth.AuthView("Stock_Transfer_Edit")%>'>新增</a>
        <a plain="true" class="easyui-linkbutton" onClick="delete_doc()" id="btn_delete_doc" style='<%=auth.AuthView("Stock_Transfer_Delete")%>'>删单</a>
        <a plain="true" class="easyui-linkbutton" onClick="save_doc()" id="btn_save_doc" style='color:Green;<%=auth.AuthView("Stock_Transfer_Edit")%>'>保存</a>
        <a plain="true" class="easyui-linkbutton" onClick="check_doc()" id="btn_check_doc">检查</a>
        <a plain="true" class="easyui-linkbutton" onClick="verify_doc(1)" id="btn_verify_doc" style='<%=auth.AuthView("Stock_Transfer_Verify")%>'>审核</a>
        <a plain="true" class="easyui-linkbutton" onClick="verify_doc(-1)" id="btn_unverify_doc" style='<%=auth.AuthView("Stock_Transfer_CancelVerify")%>'>弃审</a>
        <a plain="true" class="easyui-linkbutton" onClick="add_row()" id="btn_add_row" style='color:Green;<%=auth.AuthView("Stock_Transfer_Edit")%>'>增行</a>
        <a plain="true" class="easyui-linkbutton" onClick="del_row()" id="btn_del_row" style='<%=auth.AuthView("Stock_Transfer_Edit")%>'>删行</a>
        <a plain="true" class="easyui-linkbutton" onClick="search_grid()" id="btn_reflesh">刷新</a>
        <a plain="true" class="easyui-linkbutton" onClick="find_doc()" id="btn_find_doc">找单</a>
        <a plain="true" class="easyui-linkbutton" id="btn_prev_doc" onclick='prev_next_doc(1)'>上张</a>
        <a plain="true" class="easyui-linkbutton" id="btn_next_doc" onclick='prev_next_doc(2)'>下张</a>
        <a plain="true" class="easyui-linkbutton" id="btn_print" onclick='grf_print()' style='<%=auth.AuthView("Stock_Transfer_Print")%>'>打印</a>
        <a plain="true" id="m_trace" class="easyui-menubutton" menu="#menu_trace">联查</a>
        <a plain="true" id="m_format" class="easyui-menubutton" menu="#menu_format" style='<%=auth.FormatView()%>'>格式</a>
    </div>
    
    <!--表头-->
    <div id="doc_head">
        <div id="doc_caption">调拨单</div>
        <%=xview["input_ui"]%>
    </div>
    <!--表体-->
    <%=xview["grid_frame"]%>
    <!--页脚-->
    <div id="doc_footer">
    </div>
    <!--格式定义下拉菜单-->
    <div id="menu_format" style="width:150px;">
        <div onclick="DesignModuleFormat('Stock_Transfer_Header')">表头</div>
        <div onclick="DesignModuleFormat('Stock_Transfer_Input')">明细</div>
	</div>
    <!--联查菜单-->
    <div id="menu_trace" style="width:150px;">
        <div onclick="trace_inout()">转入转出单</div>
	</div>
    <script type="text/javascript">
        var b_verify = false;//是否审核
        var Main_Service = "Transfer_Service.asmx";
        //初始化页面
        function init_page() {
            FastSearch_Warehouse_Out();//查询转出仓库
            FastSearch_Warehouse_In();//查询转入仓库
            FastSearch_Department_Out(); //查询转出部门
            FastSearch_Department_In(); //查询转入部门
            FastSearch_IOType_Out(); //查询出库类型
            FastSearch_IOType_In(); //查询入库类型
            //选择自定义项档案
            <%for(int i=1;i<=5;i++){ %>
                $F("trigger_sdefine<%=i%>").onclick = function () {
                    AdvancedSearch_DefineItem($("#sdefine<%=i%>").val(),'Transfer_sdefine<%=i%>','sdefine<%=i%>');
                };
            <%}
              for(int i=1;i<=5;i++){ %>
                $F("trigger_ndefine<%=i%>").onclick = function () {
                    AdvancedSearch_DefineItem($("#ndefine<%=i%>").val(),'Transfer_ndefine<%=i%>','ndefine<%=i%>');
                };
            <%} %>
            <%if(action.Equals("Update")){ %>
                $F("dep_name_out").value = '<%=m.department_out==null?"":m.department_out.dep_name%>';
                $F("dep_name_in").value = '<%=m.department_in==null?"":m.department_in.dep_name%>';
                $F("wh_name_out").value = '<%=m.warehouse_out==null?"":m.warehouse_out.wh_name%>';
                $F("wh_name_in").value = '<%=m.warehouse_in==null?"":m.warehouse_in.wh_name%>';
                $F("iot_name_out").value = '<%=m.io_type_out==null?"":m.io_type_out.iot_name%>';
                $F("iot_name_in").value = '<%=m.io_type_in==null?"":m.io_type_in.iot_name%>';
                var verify_uid = '<%=m.verify_uid%>';
                if(verify_uid != ''){
                    b_verify = true;
                    $F("btn_verify_doc").style.display = 'none';
                }else{
                    $F("btn_unverify_doc").style.display = 'none';
                }
            <%}else{ %>
                $F("new_date").value = '<%=xview["login_date"]%>';
                $F("btn_unverify_doc").style.display = 'none';
                if ($F("iot_name_out").value == "") {
                    $F("iot_id_out").value = '<%=io_type_out==null?0:io_type_out.iot_id%>';
                    $F("iot_name_out").value = '<%=io_type_out==null?"":io_type_out.iot_name%>';
                }
                if ($F("iot_name_in").value == "") {
                    $F("iot_id_in").value = '<%=io_type_in==null?0:io_type_in.iot_id%>';
                    $F("iot_name_in").value = '<%=io_type_in==null?"":io_type_in.iot_name%>';
                }
            <%} %>
            disable_button();
        }
        $(document).ready(function(){
            init_page();
        });
    </script>
</body>
</html>
