<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PMC_Document.aspx.cs" Inherits="Plan_PMC_PMC_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">
        var id = "<%=id%>";
        var action = "<%=action%>";
        //搜索
        function search_grid() {
            var fg = document.getElementById("frame_grid");
            var url = "PMC_Document_Grid.aspx?id=" + id + "&action=" + action;
            fg.src = url;
        }
        //新增单据
        function add_doc() {
            window.location = "PMC_Document.aspx?action=Add";
        }
        //保存单据
        function save_doc() {
            <%=xview["Script_GetInput"] %>
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'PMC_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 = 'PMC_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: 'PMC_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 = 'PMC_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: 'PMC_Service.asmx/Verify',
                data: "{id:'" + id + "',action_type:'" + action_type + "'}",
                dataType: 'json',
                success: function (result) {
                    HideLoadingMask(); //关闭遮罩
                    if(result.d == "success"){
                        var title = '审核';
                        if(action_type==0)
                            title = '弃审';
                        asyncbox.success(title + '成功！', '操作成功！', function (buttonResult) {
                            window.location.reload();
                        });
                   }else{
                        AlertNO(result.d);
                    }
                }
            });
        }
        //检查单据
        function check_doc(){
            ShowLoadingMask(); //显示遮罩
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: 'PMC_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: 'PMCs_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._pmcs_id); //返回一维数组
            if (did.length == 0) {
                AlertNO('请选择要查看的行!');
                return;
            }
            if (did.length > 1) {
                AlertNO('一次只可以查看一条记录!');
                return;
            }
            asyncbox.open({
                id: 'win_modify',
                url: 'PMCs_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._pmcs_id); //返回一维数组
            if (ids.length == 0) {
                AlertNO('请选择要删除的记录!');
                return;
            }
            asyncbox.confirm('确定要删除该行吗?', '警告', function (buttonResult) {
                if (buttonResult == "ok") {
                    ShowLoadingMask(); //显示遮罩
                    $.ajax({
                        type: "POST",
                        contentType: "application/json",
                        url: 'PMCs_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: 'PMC_Service.asmx/GetPrevNextID',
                data: "{id:'" + id + "',type:'" + type + "'}",
                dataType: 'json',
                success: function (result) {
                    HideLoadingMask(); //关闭遮罩
                    if(result.d==0){
                        AlertNO("不能再翻了！");
                        return;
                    }
                    window.location = 'PMC_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 = '../../Plan/PMC/PMC_List.aspx';
            var tab_id = 'Plan_PMC_List';
            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);
        }
    </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("Plan_PMC_Edit")%>'>新增</a>
        <a plain="true" class="easyui-linkbutton" onclick="delete_doc()" id="btn_delete_doc" style='<%=auth.AuthView("Plan_PMC_Delete")%>'>删单</a>
        <a plain="true" class="easyui-linkbutton" onclick="save_doc()" id="btn_save_doc" style='<%=auth.AuthView("Plan_PMC_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("Plan_PMC_Verify")%>'>审核</a>
        <a plain="true" class="easyui-linkbutton" onclick="verify_doc(0)" id="btn_unverify_doc" style='<%=auth.AuthView("Plan_PMC_CancelVerify")%>'>弃审</a>
        <a plain="true" class="easyui-linkbutton" onclick="add_row()" id="btn_add_row" style='<%=auth.AuthView("Plan_PMC_Edit")%>'>增行</a>
        <a plain="true" class="easyui-linkbutton" onclick="del_row()" id="btn_del_row" style='<%=auth.AuthView("Plan_PMC_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("Plan_PMC_Print")%>'>打印</a>
        <a plain="true" id="m_format" class="easyui-menubutton" menu="#menu_format" style='<%=auth.AdminView()%>'>格式</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('Plan_PMC_Header')">表头</div>
        <div onclick="DesignModuleFormat('Plan_PMC_Input')">明细</div>
	</div>
    <script type="text/javascript">

        var b_verify = false;//是否审核
        var Main_Service = "PMC_Service.asmx";
        //初始化页面
        function init_page() {
            FastSearch_Warehouse(); //查询仓库
            FastSearch_Department(); //查询部门
            FastSearch_Inventory(); //查询母件

            <%if(action.Equals("Update")){ %>
                $F("dep_name").value = '<%=m.department==null?"":m.department.dep_name%>';
                $F("wh_name").value = '<%=m.warehouse==null?"":m.warehouse.wh_name%>';
                $F("p_i_code").value = '<%=m.p_inventory==null?"":m.p_inventory.i_code%>';
                $F("p_i_name").value = '<%=m.p_inventory==null?"":m.p_inventory.i_name%>';
                $F("p_standard").value = '<%=m.p_inventory==null?"":m.p_inventory.standard%>';
                $F("p_munit").value = '<%=m.p_inventory==null?"":m.p_inventory.munit%>';
                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';
                
            <%} %>
            disable_button();
        }
        $(document).ready(function(){
            init_page();
        });

        //查询存货
        function FastSearch_Inventory() {
            $("#p_i_code").fast_search({
                url: '../../Component/FastSearch/FastSearch_Inventory.aspx',
                replaceNull: true,
                searchButton: true,
                colModel: [{ 'columnName': 'i_id', 'hidden': true, 'width': '80', 'label': 'id' },
                    { 'columnName': 'i_code', 'width': '120', 'label': '编码' },
                    { 'columnName': 'i_name', 'width': '120', 'label': '名称' },
                    { 'columnName': 'standard', 'width': '120', 'label': '规格型号' },
                    { 'columnName': 'munit', 'width': '60', 'label': '单位' }//,
                    /*{ 'columnName': 'small_pic_url', 'width': '150', 'label': '图片'}*/],
                select: function (event, ui) {
                    SearchService_Inventory(ui.item.i_id);
                    return false;
                }
            });
        }


        //通过主键_获取存货
        function SearchService_Inventory(i_id) {
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: '../../Base/Inventory/Inventory_Service.asmx/Get',
                data: "{i_id:'" + i_id + "'}",
                dataType: 'json',
                success: function (result) {
                    $("#p_i_id").val(result.d.i_id); //存货id
                    $("#p_i_code").val(result.d.i_code); //存货编码
                    $("#p_i_name").val(result.d.i_name); //存货名称
                    $("#p_standard").val(result.d.standard); //规格型号
                    $("#p_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 {
                        $("#num").attr("disabled", false); //允许输件数
                        $("#convert_rate").attr("disabled", false); //允许输换算率
                    }*/
                }
            });
        }
    </script>
</body>
</html>
