//长春富维江森投料管理模块中产品相关信息的公用方法


function TableStyle(index, row) {

    return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div><div style="padding:2px"><table id="pdv-' + index + '"></table>';
}

//绑定数据列
function BindExpandRow(index, row) {
    
    if (row == undefined) {
        row = $("#QDateGrid").datagrid("getSelected");
    }
    if (index == undefined) {
        index = $("#QDateGrid").datagrid("getRowIndex", row);
    }

    var getDetailUrl = $('#GETDETAILINFO').val();
    var getInjectDetailUrl = $('#GETINJECTDETAILINFO').val();

    $('#ROWINDEX').val(index);
    
    $('#ddv-' + index).datagrid({
        url: getDetailUrl + row.MAINCODE,
        fitColumns: false,
        singleSelect: true,
        rownumbers: true,
        loadMsg: '',
        height: 'auto',
        columns: [[
                { field: 'PID', title: '', width: 40, align: 'center', hidden: true
                },
                { field: 'MATERIALNAME', title: '物料名称', width: 300, align: 'center'
                },
                { field: 'MATERIALCODE', title: '物料号', width: 150, align: 'center'
                },
                { field: 'PROCODE', title: '零件条码', width: 150, align: 'center'
                },
                { field: 'MACHINENAME', title: '设备名称', width: 200, align: 'center'
                }
            ]],
        onResize: function () {
            $('#QDateGrid').datagrid('fixDetailRowHeight', index);
        },
        onLoadSuccess: function () {
            setTimeout(function () {
                $('#QDateGrid').datagrid('fixDetailRowHeight', index);
            }, 0);
        }
    });
    $('#QDateGrid').datagrid('fixDetailRowHeight', index);
    //alert(row.MAINCODE);
    $('#pdv-' + index).datagrid({
        url: getInjectDetailUrl + row.MAINCODE,
        fitColumns: false,
        singleSelect: true,
        rownumbers: true,
        loadMsg: '',
        height: 'auto',
        columns: [[
                { field: 'PID', title: '', width: 20, align: 'center',
                    formatter: function (value, row, index) {
                        return rowformater(value, row, index);
                    }
                },
                { field: 'PPID', title: '产品主键', width: 20, align: 'center', hidden: true
                },
                { field: 'MATERIALNAME', title: '物料名称', width: 280,
                    align: 'center'
                },
                { field: 'MATERIALCODE', title: '物料号', width: 150, align: 'center'
                },
                { field: 'MATERIALTYPE', title: '物料类别', width: 150,
                    align: 'center'
                },
                { field: 'MATERIALBATCH', title: '批次号', width: 200, align: 'center'
                }
            ]],
        onResize: function () {
            $('#QDateGrid').datagrid('fixDetailRowHeight', index);
        },
        onLoadSuccess: function () {
            setTimeout(function () {
                $('#QDateGrid').datagrid('fixDetailRowHeight', index);
            }, 0);
        }
    });
    $('#QDateGrid').datagrid('fixDetailRowHeight', index);
}

function rowformater(value, row, index) {

    return "<a href='#'  onclick='ShowInfo(\"" + value + "\",\"" + row.PPID + "\",\"" + row.MATERIALBATCH + "\",\"" + $('#ROWINDEX').val() + "\")'>修改</a>";
}

//打开 批次号修改
function ShowMatSelectWindow() {
    //alert("批次号修改");
    $('#matSelectWindow').window({
        title: '修改批次号',
        modal: true,
        shadow: false,
        closed: true,
        left: 250,
        top: 50,
        width: 300,
        closable: true,
        minimizable: false,
        maximizable: false,
        height: 200,
        collapsible: false
    });
    $('#matSelectWindow').window('open');

  
}

function MatSelectCancel() {
    
    $("#MATERIALBATCH").val("");
    $("#PRODUCTINID").val("");
    $("#MAINID").val("");
    $('#matSelectWindow').window('close');
}

//批次号修改
function MatSelectOK() {
    //alert("批次号修改");
    var materialBatch = $('#MATERIALBATCH').val();
    var piid = $("#PRODUCTINID").val();
    var proid = $("#MAINID").val();

    if (materialBatch == "" || materialBatch == null) {
        MSI("提示", "批次号不能为空!");
        return;
    }


    var rowindex22 = $("#QDateGrid").datagrid("getRowIndex", proid);

    var SaveUrl = $('#SAVEURL').val();
    var resultData;
    //alert(SaveUrl);
    $.ajax({
        url: SaveUrl,
        data: {
            'PID': piid,
            'MATERIALBATCH': materialBatch,
            'PRODUCTPID': proid
        },
        type: "GET",
        cache: false,
        dataType: "json",
        async: false,
        success: function (data) {
            //重新加载新数据
            //BindExpandRow();

            resultData = data;

        }
    });

    if (resultData[0].isSuccess == true) {
        //关闭窗口
        MatSelectCancel();

        var rowindex = $("#QDateGrid").datagrid("getRowIndex", proid);

        $('#pdv-'+$("#ROWINDEX").val()).datagrid("reload");

        MSI("提示", "保存成功!");
    } else {
        MSI("提示", "保存失败!");
    }
}

function ShowInfo(pid, ppid, materalBatch, index) {
    
    $("#MATERIALBATCH").val(materalBatch);
    $("#PRODUCTINID").val(pid);
    $("#MAINID").val(ppid);
    $("#ROWINDEX").val(index);
    ShowMatSelectWindow();
}