/*-------------------------------文档信息-----------------------------------
///功能：提成管理
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP0510 = "Tp0510";    //表格ID
var strPageP0510 = "divPageP0510"; // 信息编辑窗口ID
var objSelRowP0510 = {};        //选择行的数据
var intSelRowIDP0510 = 0;       //选择行的ID
var objPageParamP0510 = { sortfield: "djdate", orderby: "desc", where: "", flag: 0 };  //分页控件使用
var strBmdm = "";               //存放部门代码，比较是否要刷新部门combobox
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp051001") {
            //提成单维护
            saveMyData_P051001(strType);
        }
        else if (strTable == "Tp051002") {
            //调整单
            saveData_P051002(strType);
        }
        //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
    } catch (e) { }
}

//修改
function updateP0510() {
    try {
        if (objSelRowP0510.djbh != null) {
            if (objSelRowP0510.djname == "提成") {
                showDialog_P051001(objSelRowP0510.djbh);
            }
            else if (objSelRowP0510.djname == "提成增加" || objSelRowP0510.djname == "提成减少") {
                showDialog_P051002("2");
            }
        }
        else {
            window.parent.cihoAlert(msg.edit.selEditRow);
        }
    } catch (e) { }
}

//删除
function deleteData_P0510() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"djbh\":\"" + objSelRowP0510.djbh + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableP0510, 'Delete', strJsonData));

                if (objResult.ReturnCode == 0) {    //删除成功后，重新加载数据
                    objPageParamP0510.flag = 2;
                    loadGridDataP0510();
                    objPageParamP0510.flag = 0;
                    //resizeGrid(strTableP0510);
                }
                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_P0510() {
    try {
        //选择日期范围
        dateExtent_P0510();
        //加载部门信息
        loadBmData_P0510();
        //职员下拉框信息的加载
        loadZyData_P0510();
        //列表数据的取得
        var objConfig = ParseData(window.external.DoOptions(strTableP0510, "GetTableConfig", "{}")); //获得jqGrid的列名和属性
        addColFormatter_P0510(objConfig.colModel);
        createGrid(strTableP0510, objConfig.colNames, objConfig.colModel, [], null, null, true);
        //添加事件
        addGridEvent_P0510();
        //设置表格的高度
        resizeGrid(strTableP0510, true, true);

        //创建分页
        createPage(strPageP0510, strTableP0510);
        searchData_P0510();
        //resizeGrid(strTableP0510);
        objConfig = {};
    } catch (e) { }
}

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

//加载数据的事件
function loadGridDataP0510(strFlag) {
    var objResult = [];
    if ($("#cmbBmP0510").combobox('getValue') == undefined) {
        $("#cmbBmP0510").combobox('setValue', "");
        $("#cmbZyP0510").combobox('setValue', "");
    }
    if ($("#cmbZyP0510").combobox('getValue') == undefined) {
        $("#cmbZyP0510").combobox('setValue', "");
    }
    if (!strFlag) {
        objPageParamP0510.where = getSelJson_P0510();
    }
    objResult = loadGridData_Page(objPageParamP0510);
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP0510).footerData("set", { rn: "合计", a1: objResult.je }, true);
        }
        else {
            $("#" + strTableP0510).footerData("set", { rn: "合计", a1: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP0510 = "";
    objSelRowP0510 = {};
}

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

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

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

    //添加选择事件
    $("#cmbDateExtentP0510").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 110,
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0510", "dateEndTimeP0510");
        },
        //按回车键，进行查询
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0510", "dateEndTimeP0510");
            //searchData_P0510();
        }
    });
    //加载数据
    $("#cmbDateExtentP0510").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "近半年", mc: "近半年" }, 
        { dm: "本年", mc: "本年"}]);
}

//部门下拉框信息的加载
function loadBmData_P0510() {
    $("#cmbBmP0510").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onfocus: function () {  //获得焦点
            if ($('#cmbBmP0510').combobox('getText') == "全部部门") {
                $("#cmbBmP0510").combobox("setText", "");
            }
        },
        onblur: function () {   //失去焦点
            if ($("#cmbBmP0510").combobox("getText") == "") {
                $("#cmbBmP0510").combobox("setValue", "");
            }
        },
        enterDown: function () {    //按回车键，进行查询
            //searchData_P0510();
        },
        onSelect: function (options) {
            if (options.dm != strBmdm) {        //选择部门关联所属的职员
                if (options.dm == "") {
                    var objZydm = getComboboxData("TZyxx2");
                    $("#cmbZyP0510").combobox("setValue", "");
                    objZydm.unshift({ "dm": "", "mc": "全部职员", "selected": true });
                    $("#cmbZyP0510").combobox("loadData", objZydm);
                    objZydm = {};
                }
                else {
                    var objRet = [];
                    var objZydm = getComboboxData("TZyxx2");
                    objRet = cmbSelectData_P0510(options.dm, objZydm, 'bmdm');
                    objRet.unshift({ "dm": "", "mc": "全部职员", "selected": true });
                    $("#cmbZyP0510").combobox("loadData", objRet);
                    objZydm = {};
                    objRet = {};
                }
            }
            strBmdm = $("#cmbBmP0510").combobox("getValue");
        }
    });
    var objBmdm = getComboboxData("Tbmxx2");
    if (objBmdm == "{}") {
        objBmdm = [];
    }
    objBmdm.unshift({ "dm": "", "mc": "全部部门", "selected": true });
    objBmdm.push({ "dm": "-1", "mc": "其他部门"});
    $("#cmbBmP0510").combobox("loadData", objBmdm);
    objBmdm = {};
}

//职员下拉框信息的加载
function loadZyData_P0510() {
    $("#cmbZyP0510").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        valueField: "dm",
        textField: "mc",
        onfocus: function () {  //获得焦点
            if ($('#cmbZyP0510').combobox('getText') == "全部职员") {
                $("#cmbZyP0510").combobox("setText", "");
            }
        },
        onblur: function () {   //失去焦点
            if ($("#cmbZyP0510").combobox("getText") == "") {
                $("#cmbZyP0510").combobox("setValue", "");
            }
        },
        enterDown: function () {    //按回车键，进行查询
            //searchData_P0510();
        },
        onSelect: function (options) {
            if (options.dm != "") {                 //选择职员，关联它的部门
                var objZyData = getComboboxData("TZyxx2");
                for (var intI = 0; intI < objZyData.length; intI++) {
                    if (options.mc == objZyData[intI].mc) {
                        //if (objZyData[intI].bmdm != $("#cmbBmP0510").combobox("getValue")) {
                        if (objZyData[intI].bmdm == "") {
                                $("#cmbBmP0510").combobox("setValue", "-1");
                        }
                        else {
                            if (objZyData[intI].bmdm != $("#cmbBmP0510").combobox("getValue")) {
                                $("#cmbBmP0510").combobox("setValue", objZyData[intI].bmdm);
                            }
                        }
                        //}
                    }
                }
                objZyData = {};
                strBmdm = $("#cmbBmP0510").combobox("getValue");
            }
        }
    });
    var objZydm = getComboboxData("TZyxx2"); 
    if (objZydm == "{}") {
        objZydm = [];
    }
    objZydm.unshift({ "dm": "", "mc": "全部职员", "selected": true });
    $("#cmbZyP0510").combobox("loadData", objZydm);
    objZydm = {};
}

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

//得到查询字符串
function getSelJson_P0510() {
    var strRecord = "";
    strRecord += "\"sdate\":\"" + $("#dateStartTimeP0510").val() + "\",";
    strRecord += "\"edate\":\"" + $("#dateEndTimeP0510").val() + "\",";
    strRecord += "\"bmdm\":\"" + $("#cmbBmP0510").combobox('getValue') + "\",";
    strRecord += "\"jsrdm\":\"" + $("#cmbZyP0510").combobox('getValue') + "\"";
    return strRecord;
}

//部门关联对应的职员
function cmbSelectData_P0510(options, objList,strdm) {
    var objRet = [];
    for (var i = 0; i < objList.length; i++) {
        if (options == objList[i][strdm]) {
            objRet.push({ "bmdm": objList[i].bmdm, "dm": objList[i].dm, "jc": objList[i].jc, "jp": objList[i].jp, "mc": objList[i].mc, "newbh": objList[i].newbh });
        }
        else if (options == "-1") {
            if (objList[i][strdm] == "") {
                objRet.push({ "bmdm": objList[i].bmdm, "dm": objList[i].dm, "jc": objList[i].jc, "jp": objList[i].jp, "mc": objList[i].mc, "newbh": objList[i].newbh });
            }
        }
    }
    return objRet;
}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/