/*-------------------------------文档信息-----------------------------------
///功能：提成单
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strDivTcd = "";            //弹出层
var strTableP051001_tcd = "Tp051001";          //gridId
var strRecordsP051001_tcd = 5;                       //数据的行数
var objRecords = [];                              //修改时取得的数据
var objGzbh = 0;                         //提成单的提成方式

//格式金额方法
function formatterTcdb1(cellValue) {
    if (!isNaN(parseFloat(cellValue)) && parseFloat(cellValue) != 0) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式金额方法
function formatterTcdMoney(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式数量方法
function formatterTcdb2(cellValue) {
    if (!isNaN(parseFloat(cellValue)) && parseFloat(cellValue) != 0) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化百分号方法
function formatPercentPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue * 100).toFixed(window.parent.strPercentPlace) + "%";
    }
    else {
        return "&nbsp;";
    }
}

//自定义grid方法
function createGridTcd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        data: [],
        rownumbers: true,
        rowNum: 9999,
        sortname: "id",
        autowidth: true,
        //height: "100%",
        footerrow: true,
        cellEdit: false,
        cellsubmit: "clientArray",
        gridview: true,
        rownumWidth: 30
    }); //.parent(".ui-jqgrid-bdiv").siblings().css("width", "100%");
    //增加表头‘序号’列
    //$("table.ui-jqgrid-htable th:first", $("#gbox_" + strTableID)).text("序号");
    $("#" + strTableID).setLabel("rn", "序号");
}

//添加空白行方法
function AddEmptyRowP051001() {
    for (var i = 999999999; i < 999999999 + strRecordsP051001_tcd; i++) {
        $("#" + strTableP051001_tcd).addRowData(i, { flag: "" });
    }
    resizeGridScroll(strTableP051001_tcd);
}

//打开窗口的方法
function showDialog_Tcd(strDiv,strDjbh) {
    try {
        //$(".operate").remove();
        objRecords = null;
        strDivTcd = strDiv;
        creatDiv_ShowDlgHaveTbl(strDivTcd, "提成单", false, false, strTableP051001_tcd, "800");
        $("#idMust", $("#" + strDivTcd)).append("NO:" + strDjbh);
        creatDialogItem_Tcd(strDjbh);
    } catch (e) { }
}

//创建表单元素的方法
function creatDialogItem_Tcd(strDjbh) {
    var config = ParseData(window.external.DoOptions(strTableP051001_tcd, "GetTableConfig", "{}"));    //取得列配置信息
    if (config == null) {
        window.parent.cihoAlert(msg.other.modelInfoFail);
        return;
    }

    if (strDjbh != "") {    //取得数据
        objRecords = ParseData(window.external.DoOptions(strTableP051001_tcd, 'GetList', "{\"djbh\":\"" + strDjbh + "\"}"));
    }

    //创建头部
    var i = 0;
    var strForm = "";
    var Item;
    strForm += "<ul>";
    for (i = 0; i < config.pageHeader.length; i++) {
        Item = config.pageHeader[i];
        if (Item.hidden == true) {
            continue;
        }
        if (Item.edittype == "text") {
            strForm += "<li class='inp_100'>";
            strForm += Item.xsmc + "：";
            strForm += "<input type='text'  name='" + Item.index + "'disabled ";
            strForm += " value=''  ";
            strForm += " id='" + Item.index + "'";
            strForm += " maxlength='50'   onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
            strForm += "</li>";
        }
        else {
            strForm += "<li>";
            strForm += Item.xsmc + "：";
            strForm += "<select name='" + Item.index + "' id='" + Item.index + "'";
            strForm += " class='easyui-combobox' >";
            strForm += "</select>";
            strForm += "</li>";
        }
    }
    strForm += "</ul>";
    strForm += "<table id=" + strTableP051001_tcd + "></table>";
    strForm += "</br>";
    $("#" + strDivTcd + "_divform").append(strForm);
    //下拉框的值
    $("#ywlx", "#" + strDivTcd).combobox({
        textField: "mc",
        valueField: "dm",
        width: _iWidth80,
        listWidth: _iWidth80,
        panelHeight: 42
    });
    var strYwlx = [{ "dm": "0", "mc": "终端销售" }, { "dm": "1", "mc": "回款"}];
    $("#ywlx", "#" + strDivTcd).combobox("loadData", strYwlx);
    $(".layoutpanel").css("z-index", "50000009");
    //预提方式：0，按数量；1，按金额
    if (objRecords.Records.length != "0") {
        objGzbh = objRecords.Records[0].ytfs;   //预提方式
        $("#ywlx", "#" + strDivTcd).combobox("setValue", objRecords.Records[0].ywlx);   //业务类型
        $("#ywdjbh", "#" + strDivTcd).val(escape(objRecords.Records[0].ywdjbh));        //业务单据编号
        $("#jsrmc", "#" + strDivTcd).val(escape(objRecords.Records[0].jsrmc));          //经手人
        $("#kmmc", "#" + strDivTcd).val("工资提成");                                    //预提科目
    }
    //创建表格
    createGridTcd(strTableP051001_tcd, config.colNames, config.colModel);
    //$("#gbox_" + strTableP051001_tcd).css("height", "157");
    $("#" + strTableP051001_tcd).setGridHeight(141);
    resizeGridScroll(strTableP051001_tcd);
    //初始化grid设置
    initGridSetTcd(strTableP051001_tcd, strDivTcd);
    if (objRecords != undefined) {
        //加载表格数据
        loadTreeData(strTableP051001_tcd, objRecords.Records);

        if (objRecords.Records == "{}") {
            objRecords.Records = "";
        }
        strRecordsP051001_tcd = objRecords.Records.length > 5 ? objRecords.Records.length - objRecords.Records.length : (5 - objRecords.Records.length);
    }
    else {
        strRecordsP051001_tcd = 5;
    }
    //合计
    GetTotalTcd();
    //空白行
    AddEmptyRowP051001();
    //释放内存
    strYwlx = null;
    config = null;
    Item = null;
    i = null;
    strForm = null;
}

//初始化grid事件方法
function initGridSetTcd(tbId, dlgId) {
    if (objGzbh == "1") {   //预提方式为金额时，格式化为百分号
        $("#" + tbId).setColProp("a3", { formatter: formatPercentPlace });
    }
    else {       //预提方式为数量时，格式化为money
        $("#" + tbId).setColProp("a3", { formatter: formatterTcdMoney });
    }
    $("#" + tbId).setColProp("a2", { formatter: formatterTcdb1 });
    $("#" + tbId).setColProp("a1", { formatter: formatterTcdb2 });
    $("#" + tbId).setColProp("a4", { formatter: formatterTcdMoney });
}


//计算合计方法
function GetTotalTcd() {
    var objTable = $("#" + strTableP051001_tcd);
    var arrIds = objTable.getDataIDs();
    var strSl = 0;      //数量
    var strJe = 0;      //金额
    var strYtje = 0;    //预提金额
    for (var i = 0; i < arrIds.length; i++) {
        var intNumSl = parseFloat(objTable.getCell(arrIds[i], "a1"));
        if (!isNaN(intNumSl)) {
            strSl += intNumSl;
        }
        var intNumJe = parseFloat(objTable.getCell(arrIds[i], "a2"));
        if (!isNaN(intNumJe)) {
            strJe += intNumJe;
        }
        var intNumYtje = parseFloat(objTable.getCell(arrIds[i], "a4"));
        if (!isNaN(intNumYtje)) {
            strYtje += intNumYtje;
        }
    }
    objTable.footerData("set", { rn: "合计",
        a1: parseFloat(strSl).toFixed(window.parent.strCountPlace),
        a2: parseFloat(strJe).toFixed(window.parent.strMoneyPlace),
        a4: parseFloat(strYtje).toFixed(window.parent.strMoneyPlace)
    });
    //释放内存
    objTable = null;
    arrIds = null;
    strSl = null;
    strJe = null;
    strYtje = null;
}