/*************************
*功能：预提管理
*创建人：张振华
*时间：2010-08-15
*备注：此时间是加注释时间
*************************/

var strTableP0506 = "Tp0506";   //tableID
var strPageP0506 = "divPageP0506";  //分页层
var objPageParamP0506 = { sortfield: "", orderby: "", where: "", flag: 0 }; //分页控件使用
var objSelRowP0506; //选中的行对象


//格式化预提方式
function formatterP0506YtType(cellValue) {
    if (cellValue == "0") {
        return "按销售";
    }
    else {
        return "按回款";
    }
}

//格式化数量金额
function formatterP0506Slje(cellValue) {
    if (cellValue == "0") {
        return "按数量";
    }
    else {
        return "按金额";
    }
}

//格式化预提时间
function formatterP0506Ytsj(cellValue) {

    switch (cellValue) {
        case "0":
            return "每笔";
            break;
        case "1":
            return "月末";
            break;
        case "2":
            return "季度末";
            break;
        case "3":
            return "年末";
            break;
    }
}

//格式化状态
function formatterP0506Zt(cellValue) {
    if (cellValue == "True") {
        return "已启用";
    }
    else {
        return "已停用";
    }
}

//格式化规则
function formatterP0506Gz(cellValue, option) {
    return "<div class = 'name'><div class='wfp'><div class='add'><a herf='#' style=' cursor:pointer; ' onclick=\"showDialogP050602('2','" + option.rowId + "');\"  title=\"修改预提规则区间\" ></a></div></div></div>";
}

//反格式化预提方式
function unFormatterP0506YtType(cellValue, option) {
    if (cellValue == "按销售") {
        return "0";
    } else {
        return "1";
    }
}

//格式化预提数量
function unFormatterP0506Ytsj(cellValue, option) {
    switch (cellValue) {
        case "每笔":
            return "0";
            break;
        case "月末":
            return "1";
            break;
        case "季度末":
            return "2";
            break;
        case "年末":
            return "3";
            break;
    }
}

//格式化数量金额
function unFormatterP0506Slje(cellValue) {
    if (cellValue == "按数量") {
        return "0";
    }
    else {
        return "1";
    }
}

//搜索头部信息
function getSearchInfoP0506() {

    //设置combobox
    $("#cmbKmP0506").combobox();
    $("#cmbJsrP0506").combobox();
    $("#cmbDxP0506").combobox();

    //下拉箭头样式
    addArrowClass();

    //全部科目，全部经手人，全部预提对象
    var objKm = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'2'}")).Records;
    var objJsr = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", '{"fb":"1"}')).Records;
    var objDx = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'3'}")).Records;

    //费用科目
    $("#cmbKmP0506").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        textField: "kmmc",
        valueField: "kmdm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部费用科目") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部费用科目");
            }
        }
    });
    if (objKm != null && objKm != "{}") {
        objKm.unshift({ kmdm: "", kmmc: "全部费用科目", selected: true });
        $("#cmbKmP0506").combobox("loadData", objKm);
    }

    //费用经手人
    $("#cmbJsrP0506").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        textField: "jsrmc",
        valueField: "jsrdm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部费用经手人") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部费用经手人");
            }
        }
    });
    if (objJsr != null && objJsr != "{}") {
        objJsr.unshift({ jsrdm: "", jsrmc: "全部费用经手人", selected: true });
        $("#cmbJsrP0506").combobox("loadData", objJsr);
    }

    //预提对象
    $("#cmbDxP0506").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        textField: "ytdxmc",
        valueField: "ytdx",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部预提对象") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部预提对象");
            }
        }
    });
    if (objDx != null && objDx != "{}") {
        objDx.unshift({ ytdx: "", ytdxmc: "全部预提对象", selected: true });
        $("#cmbDxP0506").combobox("loadData", objDx);
    }

}

//改变状态
function setZtP0506(rowId) {

    //表格对象，数据库操作返回的结果
    var objTable = $("#" + strTableP0506);
    var objResult;

    if (objSelRowP0506.zt == "已启用") {
        objResult = ParseData(window.external.DoOptions(strTableP0506, "Edit", "{gzguid:\"" + objSelRowP0506.gzguid + "\",zt:\"" + false + "\"}"));
        if (objResult != null && objResult.ReturnCode == "0") {
            objTable.setCell(rowId, "zt", "False");
        }

    }
    else {
        objResult = ParseData(window.external.DoOptions(strTableP0506, "Edit", "{gzguid:\"" + objSelRowP0506.gzguid + "\",zt:\"" + true + "\"}"));
        if (objResult != null && objResult.ReturnCode == "0") {
            objTable.setCell(rowId, "zt", "True");
        }
    }

    //释放内存
    objTable = null;
}

//单击事件
function onSelectGridP0506(rowId) {

    //取选中行
    objSelRowP0506 = $(this).getRowData(rowId);

    //浮动层
    var strButtonHtml = "";

    if (objSelRowP0506.zt == "已停用") {
        strButtonHtml = "<a onclick=\"setZtP0506('" + rowId + "')\">启用</a>-<a onclick=\"showDialogP050601('2');\">修改</a>-<a onclick=\"deleteDataP0506('" + rowId + "')\">删除</a>";
    }
    else if (objSelRowP0506.zt == "已启用") {
        strButtonHtml = "<a onclick=\"setZtP0506('" + rowId + "')\">停用</a>-<a onclick=\"showDialogP050601('3');\">查看</a>";
    }

    //回调
    createFloatButton.call(this, rowId, strButtonHtml);
}

//双击事件
function dbClickP0506(strType, rowId) {

    //取选中行
    objSelRowP0506 = $(this).getRowData(rowId);

    //双击时弹出对话框
    if (objSelRowP0506.zt == "已启用") {
        showDialogP050601('3');
    }
    else {
        showDialogP050601('2');
    }
}

//排序
function sortDataP0506(index, colindex, sortorder) {

    objPageParamP0506.sortfield = index;
    objPageParamP0506.orderby = sortorder;

    //查询数据
    loadGridData_Page(objPageParamP0506);

    //设置分页参数
    objPageParamP0506.flag = 0;

    //清楚浮动按钮
    $("div.operate").hide();

    return 'stop';
}

//搜索数据
function searchDataClickP0506() {

    //费用科目
    if (!$("#cmbKmP0506").combobox('getValue')) {
        $("#cmbKmP0506").combobox('setValue', "");
    }

    //费用经手人
    if (!$("#cmbJsrP0506").combobox('getValue')) {
        $("#cmbJsrP0506").combobox('setValue', "");
    }

    //预提对象
    if (!$("#cmbDxP0506").combobox('getValue')) {
        $("#cmbDxP0506").combobox('setValue', "");
    }

    //组查询条件
    var strWhereJson = "";
    strWhereJson += "\"kmdm\":\"" + $("#cmbKmP0506").combobox("getValue") + "\"";
    strWhereJson += ",\"zydm\":\"" + $("#cmbJsrP0506").combobox("getValue") + "\"";
    strWhereJson += ",\"ytdx\":\"" + $("#cmbDxP0506").combobox("getValue") + "\"";

    objPageParamP0506.where = strWhereJson;

    //查询数据
    loadGridData_Page(objPageParamP0506);

    //设置分页参数
    objPageParamP0506.flag = 0;

    //清楚浮动按钮
    $("div.operate").hide();
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    var isTextChange = $("#" + strDialog + "_divform").data("change");

    //当页面信息已修改时，弹出提示保存
    if (isTextChange == true) {
        //函数名称
        window.parent.cihoAlert(
            msg.other.haveChange, msgBox.dlgBtn.YesNoCancel, function (strRet) {
                if (msgBox.dlgRst.Yes == strRet) {
                    saveMyData(strDialog, 0, strTable);
                }
                else if (msgBox.dlgRst.No == strRet) {
                    cihoDlgClose(strDialog);
                    if (strDialog == "divP050601") {
                        freeMemory_P050601();
                    }
                    else {
                        freeMemory_P050602();
                    }
                }
            });
    }
    else {
        cihoDlgClose(strDialog);
        if (strDialog == "divP050601") {
            freeMemory_P050601();
        }
        else {
            freeMemory_P050602();
        }
    }
}

//保存数据
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == strTableP050601) {
            saveDataP050601(strDialog, strType);
            changePageZt("p0506", "1");
        }
        else if (strTable == strTableP050602) {
            saveDataP050602(strDialog, strType);
        }
        else if (strTable == "TFlsz") {
            saveData_P06020101(strType);
        }
        else if (strTable == "Tp050604") {
            saveDataP050604();
        }
        else if (strTable == "TKjkm") {
            saveMyDataTp060601(strDialog, strType);
        }
        //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();

            //            //重新加载职员的信息
            //            var objZy = getComboboxData("TZyxx");
            //            var strZydm = $("#zydm", "#" + strDivP050601).combobox("getValue");
            //            var strZymc = $("#zydm", "#" + strDivP050601).combobox("getText");
            //            var strJsrdm = $("#jsrdm", "#" + strDivP050601).combobox("getValue");
            //            var strJsrmc = $("#jsrdm", "#" + strDivP050601).combobox("getText");

            //            objZy.push({ "mc": "<font color='red'>添加职员</font>", "dm": "-1" });
            //            $("#zydm", "#" + strDivP050601).combobox("loadData", objZy);
            //            $("#jsrdm", "#" + strDivP050601).combobox("loadData", objZy);
            //            $("#jsrdm", "#" + strDivP050601).combobox("setValue", strZydm);
            //            $("#zydm", "#" + strDivP050601).combobox("setValue", strZydm);
            //            $("#jsrdm", "#" + strDivP050601).combobox("setText", strJsrmc);
            //            $("#zydm", "#" + strDivP050601).combobox("setText", strZymc);

        }
    }
    catch (e) {
        proceError("P0506", e.ToString());
    }
}

//删除数据
function deleteDataP0506() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {

                //删除数据信息
                var objResult = ParseData(window.external.DoOptions(strTableP0506, 'Delete', "{\"gzguid\":\"" + objSelRowP0506.gzguid + "\"}"));

                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objPageParamP0506.flag = 2;
                    searchDataClickP0506();
                    changePageZt("p0506", "1");
                    changePageZt("p0506", "2");
                    //重新刷新下拉框值
                    reLoadCombo();
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
            }
        });
    }
    catch (e) {
        proceError("P0506", e.ToString());
    }
}

//重新刷新下拉框的值
function reLoadCombo() {
    var objKm = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'2'}")).Records;
    var objJsr = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", '{"fb":"1"}')).Records;
    var objDx = ParseData(window.external.DoOptions("Tp0505", "GetComboBoxData", "{'fb':'3'}")).Records;
    objKm.unshift({ kmdm: "", kmmc: "全部费用科目", selected: true });
    objJsr.unshift({ jsrdm: "", jsrmc: "全部费用经手人", selected: true });
    objDx.unshift({ ytdx: "", ytdxmc: "全部预提对象", selected: true });
    $("#cmbKmP0506").combobox("loadData", objKm);
    $("#cmbJsrP0506").combobox("loadData", objJsr);
    $("#cmbDxP0506").combobox("loadData", objDx);
}
