/*-------------------------------文档信息-----------------------------------
///功能：预提管理
///创建人: 翟新乐
///创建时间：---------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0505 = "Tp0505";    //表格ID
var strDialogP0505 = "divP0505"; //信息编辑窗口ID
var strPageP0505 = "divPageP0505"; // 信息编辑窗口ID
var objSelRowP0505 = {};        //选择行的数据
var intSelRowIDP0505 = 0;       //选择行的ID
var objPageParamP0505 = { sortfield: "djdate", orderby: "desc", where: "", flag: 0 };  //分页控件使用

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp050501") {
            //预提单保存
            saveMyData_P050501(strType);
        }
        if (strTable == "Tp050605") {
            saveDataP050605(strType);
        }
    } catch (e) { }
}

//修改
function updateP0505() {
    if (objSelRowP0505.djbh != null) {
        showDialog_P050501(objSelRowP0505.djbh);
    }
    else {
        window.parent.cihoAlert(msg.edit.selEditRow);
    }
}

//删除
function deleteData_P0505() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"djbh\":\"" + objSelRowP0505.djbh + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0505, 'Delete', strJsonData));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objPageParamP0505.flag = 2;
                    loadGridDataP0505();
                    objPageParamP0505.flag = 0;
                    //resizeGrid(strTableP0505);
                }
                else {
                    window.parent.cihoAlert(msg.del.selDelRow);
                }
                strJsonData = null;
                objResult = {};
            }
        });
    } catch (e) { }
}

//关闭窗口
function closeMyDlg(strDialog, strType, strTableName) {
    if ($("#" + strDialog + "_divform").data("change")) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         saveMyData(strDialog, strType, strTableName);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         cihoDlgClose(strDialog);
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        cihoDlgClose(strDialog);
    }
}

/*------------------------以下是页面私有事件方法-----------------------------*/
//页面加载方法
function pageLoad_P0505() {
    try {
        //选择日期范围
        dateExtent_P0505();
        //加载费用科目信息
        loadKmData_P0505();
        //加载客户信息
        loadKhData_P0505();
        //经手人下拉框信息的加载
        loadJsrData_P0505();
        //预提对象下拉框信息的加载
        loadObjData_P0505();
        //列表数据的取得
        var objConfig = ParseData(window.external.DoOptions(strTableP0505, "GetTableConfig", "{}")); //获得jqGrid的列名和属性
        addColFormatter_P0505(objConfig.colModel);
        createGrid(strTableP0505, objConfig.colNames, objConfig.colModel, [], null, null, true);
        //添加事件
        addGridEvent_P0505();
        //设置表格的高度
        resizeGrid(strTableP0505, true, true);

        //创建分页
        createPage(strPageP0505, strTableP0505);
        searchData_P0505();
        //resizeGrid(strTableP0505);
        objConfig = {};
    } catch (e) { }
}

//查询按钮的事件
function searchData_P0505() {
    try {
        objPageParamP0505.sortfield = "djdate";     //排序字段
        objPageParamP0505.orderby = "desc";         //排序顺序
        objPageParamP0505.flag = 1;                 //表示点击查询按钮
        loadGridDataP0505();                        //加载数据
        objPageParamP0505.flag = 0;                 //使用完后，设置为零
    } catch (e) { }
}

//加载数据的事件
function loadGridDataP0505(strFlag) {
    var objResult = [];
    if ($("#cmbFykmP0505").combobox('getValue') == undefined) {
        $("#cmbFykmP0505").combobox('setValue', "");
    }
    if ($("#cmbKhDataP0505").combobox('getValue') == undefined) {
        $("#cmbKhDataP0505").combobox('setValue', "");
    }
    if ($("#cmbCzyP0505").combobox('getValue') == undefined) {
        $("#cmbCzyP0505").combobox('setValue', "");
    }
    if ($("#cmbObjP0505").combobox('getValue') == undefined) {
        $("#cmbObjP0505").combobox('setValue', "");
    }
    if (!strFlag) {
        objPageParamP0505.where = getSelJson_P0505();
    }
    objResult = loadGridData_Page(objPageParamP0505);
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP0505).footerData("set", { rn: "合计", a1: objResult.je }, true);
        }
        else {
            $("#" + strTableP0505).footerData("set", { rn: "合计", a1: "&nbsp;" }, false);
        }
    }
    intSelRowIDP0505 = "";
    objSelRowP0505 = {};
    objResult = {};
}

//表格事件的添加
function addGridEvent_P0505() {
    $("#" + strTableP0505).setGridParam({
        onSelectRow: function (rowId) {//选择行使用方法
            //当前选择行数据
            intSelRowIDP0505 = rowId;
            objSelRowP0505 = $(this).getRowData(rowId);
            var strBtnHtml = "";
            if (objSelRowP0505.djbh != null) {
                //浮动按钮内容
                strBtnHtml = "<a onclick=\"updateP0505()\">修改</a>-<a onclick=\"deleteData_P0505()\">删除</a>";
            }
            createFloatButton.call(this, rowId, strBtnHtml);
        },
        ondblClickRow: function (rowId) {
            updateP0505();
        },
        onSortCol: function (index, colindex, sortorder) {
            $("div.operate").hide();                    //浮动层隐藏
            objPageParamP0505.sortfield = index;        //排序字段
            objPageParamP0505.orderby = sortorder;      //排序顺序
            loadGridDataP0505(true);                        //加载数据
            return "stop";
        }
    });
}

//添加列格式化方法
function addColFormatter_P0505(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        if (objColModel[intI]["name"] == "a1") {
            objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                if (cellvalue != "") {
                    return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                }
                else {
                    return "&nbsp;";
                }
            }
        }       
    }
}

//日期范围的选择方法
function dateExtent_P0505() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0505", "dateEndTimeP0505");

    //添加选择事件
    $("#cmbDateExtentP0505").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 82,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0505", "dateEndTimeP0505");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0505", "dateEndTimeP0505");
            //searchData_P0505();
        }
    });
    //加载数据
    $("#cmbDateExtentP0505").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
     { dm: "上一月", mc: "上一月" },
     { dm: "过去三个月", mc: "过去三个月" }, 
     { dm: "本年", mc: "本年"}]);
}

//科目下拉框信息的加载
function loadKmData_P0505() {
    $("#cmbFykmP0505").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "kmdm",
        textField: "kmmc",       
        onfocus: function () {  //获得焦点
            if ($('#cmbFykmP0505').combobox('getText') == "全部费用科目") {
                $("#cmbFykmP0505").combobox("setText", "");
            }
        },       
        onblur: function () {    //失去焦点
            if ($("#cmbFykmP0505").combobox("getText") == "") {
                $("#cmbFykmP0505").combobox("setValue", "");
            }
        },       
        enterDown: function () {     //按回车键，进行查询
            //searchData_P0505();
        }
    });
    var objFykm = ParseData(window.external.DoOptions(strTableP0505, "GetComboBoxData", "{'fb':'2'}")).Records;
    if (objFykm == "{}") {
        objFykm = [];
    }
    objFykm.unshift({ "kmdm": "", "kmmc": "全部费用科目", "selected": true });
    $("#cmbFykmP0505").combobox("loadData", objFykm);
    objFykm = {};
}

function loadKhData_P0505() {
    $("#cmbKhDataP0505").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbKhDataP0505").combobox("getText") == "") {
                $("#cmbKhDataP0505").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbKhDataP0505").combobox("getText") == "全部客户") {
                $("#cmbKhDataP0505").combobox("setText", "");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbKhDataP0505").combobox("loadData", objProvider);
    objProvider = {};
}

//费用经手人下拉框信息的加载
function loadJsrData_P0505() {
    $("#cmbCzyP0505").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "jsrdm",
        textField: "jsrmc",        
        onfocus: function () {  //获得焦点
            if ($('#cmbCzyP0505').combobox('getText') == "全部费用经手人") {
                $("#cmbCzyP0505").combobox("setText", "");
            }
        },        
        onblur: function () {   //失去焦点
            if ($("#cmbCzyP0505").combobox("getText") == "") {
                $("#cmbCzyP0505").combobox("setValue", "");
            }
        },        
        enterDown: function () {    //按回车键，进行查询
            //searchData_P0505();
        }
    });
    var objFyjsr = window.external.DoOptions(strTableP0505, "GetComboBoxData", "{'fb':'1'}");
    if (objFyjsr) {
       var objFyjsrdm = ParseData(objFyjsr);
    }
    objFyjsrdm.Records.unshift({ "jsrdm": "", "jsrmc": "全部费用经手人", "selected": true });
    $("#cmbCzyP0505").combobox("loadData", objFyjsrdm.Records);
    objFyjsr = {};
    objFyjsrdm = {};
}

//预提对象下拉框信息的加载
function loadObjData_P0505() {
    $("#cmbObjP0505").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "ytdx",
        textField: "ytdxmc",       
        onfocus: function () {   //获得焦点
            if ($('#cmbObjP0505').combobox('getText') == "全部预提对象") {
                $("#cmbObjP0505").combobox("setText", "");
            }
        },       
        onblur: function () {   //失去焦点
            if ($("#cmbObjP0505").combobox("getText") == "") {
                $("#cmbObjP0505").combobox("setValue", "");
            }
        },        
        enterDown: function () {    //按回车键，进行查询
            //searchData_P0505();
        }
    });
    var objRet = window.external.DoOptions(strTableP0505, "GetComboBoxData", "{'fb':'3'}");
    if (objRet) {
        var objYtdx = ParseData(objRet);
        objYtdx.Records.unshift({ "ytdx": "", "ytdxmc": "全部预提对象", "selected": true });
        $("#cmbObjP0505").combobox("loadData", objYtdx.Records);
    }
    objRet = {};
    objYtdx = {};
}

//日期范围修改方法
function dateTimeChange_P0505() {
    if ($("#cmbDateExtentP0505").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0505").combobox("setValue", "");
        $("#cmbDateExtentP0505").combobox("setText", "自定义");
    }
}

//得到查询字符串
function getSelJson_P0505() {
    var strRecord = "";
    strRecord += "\"sdate\":\"" + $("#dateStartTimeP0505").val() + "\",";
    strRecord += "\"edate\":\"" + $("#dateEndTimeP0505").val() + "\",";
    strRecord += "\"kmdm\":\"" + $("#cmbFykmP0505").combobox('getValue') + "\",";
    strRecord += "\"khdm\":\"" + $("#cmbKhDataP0505").combobox('getValue') + "\",";
    strRecord += "\"jsrdm\":\"" + $("#cmbCzyP0505").combobox('getValue') + "\",";
    strRecord += "\"ytdx\":\"" + $("#cmbObjP0505").combobox('getValue') + "\"";
    return strRecord;
}

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