/*--------------------------文档信息-------------------------------------------
///
///功能：    收款管理-收款单-回款分配
///创建人：  李孟昌
///创建时间：2011年0月12日
///修改人：  王思波
///修改时间：2011年07月18日
///修改原因：功能实现有问题
///详细说明：--------------
///
-----------------------------------------------------------------------------*/

//定义全局变量
var strTableP020302 = "Tp020302";       //表格ID
var strDialogP020302 = "divP020302";    //窗口ID
var strDelGuidP020302 = "";             //删除数据ID
var strReceiptRowIDP020302 = "";        //收款单明细行的ID
var intRowCountP020302 = 5;             //表格默认行数
var intMaxRowIDP020302 = 0;             //记录最大的行ID
var objReceiptRowP020302 = {};          //收款单明细行的数据
var objCellPlaceP020302 = { iRow: 0, iCol: 0, value: "" };   //编辑状态单元格位置

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//弹出回款分配窗口方法
function showDialog_P020302(rowid) {
    //保存当前单元格
    $("#" + strTableP020301).saveCell(objCellPlaceP020301.iRow, objCellPlaceP020301.iCol);

    //明细数据的取得
    strReceiptRowIDP020302 = rowid;
    objReceiptRowP020302 = $("#" + strTableP020301).getRowData(rowid);

    //当前实收金额大于0时，才能分配回款
    if (objReceiptRowP020302.bcsk != "" && parseFloat(objReceiptRowP020302.bcsk) != 0) {
        //创建窗口对象
        creatDiv_ShowDlgHaveTbl(strDialogP020302, "回款分配", false, false, strTableP020302, 500);

        //添加窗口元素
        creatDialogItem_P020302();

        //创建表格
        createGrid_P020302();

        //加载表格数据
        loadGridData_P020302();
    }
    else {
        window.parent.cihoAlert(msg.skd.notFenPei);
    }

    //去除回车动作
    $("#" + strDialogP020302).unbind("keypress").bind("keypress", function () { if (event.keyCode == 13) { return false; } });
}

//添加行按钮事件
function addBlankRow_P0200302() {
    $("#" + strTableP020302).addRowData("blank_" + intMaxRowIDP020302.toString(), {}); //添加空行
    intMaxRowIDP020302++; //记录最大行ID

    //调整表格宽度
    resizeGridScroll(strTableP020302);
}

//删除数据行方法
function deleteDataRow_P020302(rowid) {
    //保存当前单元格
    $("#" + strTableP020302).saveCell(objCellPlaceP020302.iRow, objCellPlaceP020302.iCol);

    //删除行有信息时，弹出提示信息
    if (checkRowNull_P020301(rowid) == false) {
        //提示用户是否删除数据
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (strRet) {
            if (msgBox.dlgRst.Yes == strRet) {
                //当GUID不为空时，再弹出提示信息
                if ($("#" + strTableP020302).getCell(rowid, "mxguid") != "") {
                    //记录删除数据的GUID
                    if (strDelGuidP020302 != "") {
                        strDelGuidP020302 += "/" + $("#" + strTableP020302).getCell(rowid, "mxguid");
                    }
                    else {
                        strDelGuidP020302 = $("#" + strTableP020302).getCell(rowid, "mxguid");
                    }

                    //窗口信息变量标志
                    $("#" + strDialogP020302 + "_divform").data("change", true);
                }

                //删除行
                $("#" + strTableP020302).delRowData(rowid);
            }
        });
    }
    else {
        $("#" + strTableP020302).delRowData(rowid); //删除行
    }

    //调整表格宽度
    resizeGridScroll(strTableP020302);
}

//保存数据
function saveData_P020302(strFuncType) {
    var objDataGrid = $("#" + strTableP020302); //表格对象
    var arrDataID = objDataGrid.getDataIDs();   //数据行ID
    var strJsonData = "";                       //回款分配信息
    var strSalePersonName = "";                 //销售员姓名
    var strSalePersonCode = "";                 //销售员编码
    var strCellTitle = "";                      //单元格气泡信息
    var intPrivilegeMoney = 0;                  //分配优惠
    var intAllocateMoney = 0;                   //分配金额

    //保存当前单元格
    $("#" + strTableP020302).saveCell(objCellPlaceP020302.iRow, objCellPlaceP020302.iCol);

    //数据的验证
    for (var intI = 0; intI < arrDataID.length; intI++) {
        //行信息都是空时，做删除操作
        if (objDataGrid.getCell(arrDataID[intI], "zymc") == "" && objDataGrid.getCell(arrDataID[intI], "a1") == "" && objDataGrid.getCell(arrDataID[intI], "a2") == "") {
            if (objDataGrid.getCell(arrDataID[intI], "mxguid")) {
                //记录数据的GUID 
                if (strDelGuidP020302 != "") {
                    strDelGuidP020302 += "/" + objDataGrid.getCell(arrDataID[intI], "mxguid");
                }
                else {
                    strDelGuidP020302 = objDataGrid.getCell(arrDataID[intI], "mxguid");
                }
            }
        }
        else if (!objDataGrid.getCell(arrDataID[intI], "zydm")) {
            window.parent.cihoAlert(msg.skd.hkfpZydmEmp.replace("NO", (intI + 1).toString()));
            return;
        }
        else if (!objDataGrid.getCell(arrDataID[intI], "a1") || objDataGrid.getCell(arrDataID[intI], "a1") == parseFloat("0").toFixed(window.parent.strMoneyPlace)) {
            window.parent.cihoAlert(msg.skd.hkfpJeEmp.replace("NO", (intI + 1).toString()));
            return;
        }
        else {
            //验证表格信息
            for (var intJ = intI + 1; intJ < arrDataID.length; intJ++) {
                //验证销售员是否重复
                if (objDataGrid.getCell(arrDataID[intJ], "zydm") == objDataGrid.getCell(arrDataID[intI], "zydm")) {
                    window.parent.cihoAlert(msg.skd.hkfpNameQqual2.replace("NO", (intI + 1).toString()));
                    return;
                }

                //当优惠为空时，转换成数字
                if (objDataGrid.getCell(arrDataID[intJ], "zydm") != "" && objDataGrid.getCell(arrDataID[intJ], "a2") == "") {
                    objDataGrid.setCell(arrDataID[intJ], "a2", parseFloat("0").toFixed(window.parent.strMoneyPlace));
                }
            }

            //分配信息
            if (strJsonData != "") { strJsonData += ","; }
            strJsonData += "{mxguid:\"" + objDataGrid.getCell(arrDataID[intI], "mxguid") + "\",";   //GUID
            strJsonData += "zymc:\"" + trim(objDataGrid.getCell(arrDataID[intI], "zymc")) + "\",";  //销售员姓名
            strJsonData += "zydm:\"" + objDataGrid.getCell(arrDataID[intI], "zydm") + "\",";        //销售员编码
            strJsonData += "a1:\"" + objDataGrid.getCell(arrDataID[intI], "a1") + "\",";            //分配金额
            strJsonData += "a2:\"" + (objDataGrid.getCell(arrDataID[intI], "a2") == "" ? "0" : objDataGrid.getCell(arrDataID[intI], "a2")) + "\","; //优惠金额
            strJsonData += "xsmxguid:\"" + objReceiptRowP020302.xsmxguid + "\"}";                   //销售明细GUID

            //分配合计
            intAllocateMoney += objDataGrid.getCell(arrDataID[intI], "a1") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "a1")); //实收
            intPrivilegeMoney += objDataGrid.getCell(arrDataID[intI], "a2") == "" ? 0 : parseFloat(objDataGrid.getCell(arrDataID[intI], "a2")); //优惠

            //销售人员名称、气泡信息
            if (strSalePersonCode != "") {
                strSalePersonCode += "/" + objDataGrid.getCell(arrDataID[intI], "zydm");
                strCellTitle += "\n" + objDataGrid.getCell(arrDataID[intI], "zymc") + " | 优惠:" + objDataGrid.getCell(arrDataID[intI], "a2") + " | 实收:" + objDataGrid.getCell(arrDataID[intI], "a1");
            }
            else {
                strSalePersonCode = objDataGrid.getCell(arrDataID[intI], "zydm");
                strSalePersonName = objDataGrid.getCell(arrDataID[intI], "zymc");
                strCellTitle = objDataGrid.getCell(arrDataID[intI], "zymc") + " | 优惠:" + objDataGrid.getCell(arrDataID[intI], "a2") + " | 实收:" + objDataGrid.getCell(arrDataID[intI], "a1");
            }
        }
    }

    //当有分配信息时再验证
    //优惠的验证
    if (intPrivilegeMoney > 0 && intPrivilegeMoney > parseFloat(objReceiptRowP020302.bcyh)) {
        window.parent.cihoAlert(msg.skd.hkfpYh);
        return;
    }
    //分配金额的验证
    else if (intAllocateMoney > 0 && intAllocateMoney > parseFloat(objReceiptRowP020302.bcsk)) {
        window.parent.cihoAlert(msg.skd.hkfpJe);
        return;
    }

    //回款分配按钮的添加
    if (strSalePersonCode.indexOf("/") >= 0) {
        strSalePersonName += "...";
    }
    strSalePersonName = "<div class=\"name\">" + strSalePersonName;
    strSalePersonName += "    <div class=\"add\">";
    strSalePersonName += "        <a title=\"回款分配\" href=\"#\" onclick=\"showDialog_P020302('" + strReceiptRowIDP020302 + "')\"></a>";
    strSalePersonName += "    </div>";
    strSalePersonName += "</div>";

    //回款分配信息保存明细表格中
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "glxsymc", strSalePersonName);  //关联销售员姓名
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "glxsy", strSalePersonCode == "" ? "&nbsp;" : strSalePersonCode);  //关联销售员代码
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "fpyh", intPrivilegeMoney);     //分配优惠
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "fpss", intAllocateMoney);      //分配金额
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "records", strJsonData == "" ? "&nbsp;" : strJsonData);            //保存信息
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "del", strDelGuidP020302 == "" ? "&nbsp;" : strDelGuidP020302);    //删除信息
    $("#" + strTableP020301).setCell(strReceiptRowIDP020302, "glxsymc", "", "", { title: strCellTitle });                       //气泡提示信息的设置
    freeMemory_P020302();           //释放内存
    cihoDlgClose(strDialogP020302); //关闭窗口
}

//单元格编辑状态前的事件
function beforeEditCell_P020302(rowid, cellname, value, iRow, iCol) {
    //保存当前单元的索引
    objCellPlaceP020302.iRow = iRow;
    objCellPlaceP020302.iCol = iCol;
    //单元格旧的内容
    objCellPlaceP020302.value = value;
}

//单元格编辑事件
function afterEditCell_P020302(rowid, cellname, value, iRow, iCol) {
    var objCellItem = $("#" + rowid + " :input[name='" + cellname + "']", this);
    var objEmployee = {};
    if (cellname == "zymc") {
        //属性的设置
        objCellItem.combobox({ width: 185, listWidth: 185, textField: "mc", valueField: "dm" });
        //加载数据
        objEmployee = getComboboxData("TZyxx");
        objEmployee.push({ "mc": "添加销售员", "dm": "-1" });
        objCellItem.combobox("loadData", objEmployee);
        objCellItem.combo_ciho("layoutpanel").find("div.combobox-item[value='-1']").css("color", "red");
        //默认值的设定
        objCellItem.combobox("setValue", $("#" + strTableP020302).getCell(rowid, "zydm"));
        objCellItem.combobox("setText", objCellPlaceP020302.value);
        objCellItem.combobox("focus"); //获取焦点
        //添加事件
        objCellItem.combobox({
            afterChange: function (newValue, newText) {
                if (newValue == "-1") {
                    ciho.IniData({ id: "divAddType", title: "销售员", table: "" }, { jquery: $(this), model: "TZyxx" }); //添加功能
                    ciho.showTypeDialog();
                }
                else if (newValue) {
                    $("#" + strTableP020302).setCell(rowid, "zydm", newValue);
                }
                else {
                    $("#" + strTableP020302).setCell(rowid, "zydm", "&nbsp;");
                }
            }
        });
    }
    else {
        objCellItem.css("text-align", "right"); //设置文本右对齐
        objCellItem.autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "", aNeg: '-' }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    }

    //释放内容
    objCellItem = null;
    objEmployee = null;
}

//单元格保存事件
function afterSaveCell_P020302(rowid, cellname, value, iRow, iCol) {
    $("#" + strDialogP020302 + "_divform").data("change", true); //窗口信息变量标志
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//添加表单元素到窗口中
function creatDialogItem_P020302() {
    var strItemHtml = "";
    $("#" + strDialogP020302 + "_divform").append("<table id=\"" + strTableP020302 + "\"></table>");
    //添加按钮
    strItemHtml = "<div class=\"clr\"></div>";
    strItemHtml += "<div class=\"box_btn_width\">";
    strItemHtml += "    <div class=\"box_btn\">";
    strItemHtml += "        <div class=\"btn01\"><a onclick=\"saveMyData('" + strDialogP020302 + "', 1, '" + strTableP020302 + "')\">保存退出</a></div>";
    strItemHtml += "    </div>";
    strItemHtml += "</div>";
    $(".pop_box", "#" + strDialogP020302).append(strItemHtml);

    //释放内存
    strItemHtml = null;
}

//加载表格数据
function loadGridData_P020302() {
    var objDataGrid = $("#" + strTableP020302); //表格对象
    var arrRecords = [];    //数据集
    var intRowLength = 0;   //行数

    //表格数据的取得
    if (objReceiptRowP020302.records != "" || objReceiptRowP020302.del != "") {
        arrRecords = ParseData("[" + objReceiptRowP020302.records + "]"); //从隐藏列中取得数据
    }
    else if ($("#" + strDialogP020301 + "_divform").data("saveFlag") == "2") {
        //从数据库取数据
        arrRecords = ParseData(window.external.DoOptions("Tp020302", "GetList", "{'xsmxguid':'" + objReceiptRowP020302.xsmxguid + "','djbh':'" + objSelRowP0203.djbh + "'}"));
        if (arrRecords) {
            arrRecords = arrRecords.Records;
        }
    }
    else if (objReceiptRowP020302.glxsy != "") {
        //默认数据的取得
        arrRecords = [{ zymc: $(".name", "#" + strReceiptRowIDP020302).text(), zydm: objReceiptRowP020302.glxsy, a1: objReceiptRowP020302.bcsk, a2: objReceiptRowP020302.bcyh}];
    }

    //表格数据的长度
    intRowLength = arrRecords.length <= intRowCountP020302 ? intRowCountP020302 : arrRecords.length;
    //添加数据
    for (var intI = 0; intI < intRowLength; intI++) {
        if (intI < arrRecords.length) {
            objDataGrid.addRowData(intI.toString(), arrRecords[intI]);
        }
        else {
            objDataGrid.addRowData("blank_" + intI.toString(), {});
        }
    }

    //调整表格宽度
    resizeGridScroll(strTableP020302);

    //记录最大行的ID
    intMaxRowIDP020302 = intI;

    //释放内存
    objDataGrid = null;
    arrRecords = null;
    intRowLength = null;
}

//添加行的鼠标移动事件
function addMouseMove_P020302() {
    $("#" + strTableP020302).delegate("tr[id]:visible", "hover", function (e) {
        if (e.type == "mouseenter") {
            $("#" + strTableP020302).setCell(this.id, "cz", "<a href=\"#\" style=\"color:red;text-decoration:none;\" onclick=\"deleteDataRow_P020302('" + this.id + "');return false;\">删除</a> ");
        }
        if (e.type == "mouseleave") {
            $("#" + strTableP020302).setCell(this.id, "cz", "&nbsp;");
        }
    });
}

//创建表格
function createGrid_P020302() {
    //列配置信息的取得
    var objConfig = getPageConfig_P020302();
    //创建表格
    $("#" + strTableP020302).jqGrid({
        datatype: "local",
        sortname: "id",
        colNames: objConfig.colNames,
        colModel: objConfig.colModel,
        rownumbers: true,
        autowidth: true,
        gridview: true,
        cellEdit: true,
        cellsubmit: "clientArray",
        rownumWidth: 30,
        rowNum: 9999999,
        beforeEditCell: beforeEditCell_P020302,
        afterEditCell: afterEditCell_P020302,
        afterSaveCell: afterSaveCell_P020302
    });

    //设置表格高度
    $("#" + strTableP020302).setGridHeight(141);

    //设置序号列的名称
    $("#" + strTableP020302).setLabel("rn", "序号");

    //操作列的设置
    $("#" + strTableP020302).setLabel("cz", "<img onclick=\"addBlankRow_P0200302()\" style=\"cursor:pointer;padding-top:5px;\"  title=\"添加行\" src=\"../../Content/Default/images/edit_add.gif\"/>");

    //添加行的鼠标移动事件
    addMouseMove_P020302();

    //释放内存
    objConfig = null;
}

//表格列的格式化方法
function getPageConfig_P020302() {
    var objConfig = ParseData(window.external.DoOptions(strTableP020302, "GetTableConfig", "{}")); //列配置信息的取得
    if (objConfig) {
        //循环列配置
        for (var intI = 0; intI < objConfig.colModel.length; intI++) {
            //销售员
            if (objConfig.colModel[intI].name == "zymc") {
                objConfig.colModel[intI].edittype = "custom";
                objConfig.colModel[intI].editoptions = {
                    custom_element: function (value, options) { return "<select class=\"easyui-combobox\" name=\"" + options.name + "\"  id=\"" + options.id + "\">"; },
                    custom_value: function (e) { return e.combobox("getText"); }
                };
            }
            //分配金额、优惠
            else if (objConfig.colModel[intI].name == "a1" || objConfig.colModel[intI].name == "a2") {
                objConfig.colModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue) {
                        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                    }
                    else {
                        return "&nbsp;";
                    }
                };
            }
        }
    }
    else {
        objConfig = { colNames: [], colModel: [] };
    }
    return objConfig;
}

//释放全局变量的内存
function freeMemory_P020302() {
    strDelGuidP020302 = "";         //删除数据ID
    strReceiptRowIDP020302 = "";    //收款单明细行的ID
    intMaxRowIDP020302 = 0;         //记录最大的行ID
    objReceiptRowP020302 = {};      //收款单明细行的数据
    objCellPlaceP020302 = { iRow: 0, iCol: 0, value: "" }; //编辑状态单元格位置
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/