/*-------------------------------文档信息-----------------------------------
///功能：费用明细
///创建人: 翟新乐
///创建时间：2011-09-13
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量的定义
var strTableP051504 = "Tp051504";   //表格ID
var strPageP051504 = "divPageP051504";
var intSelRowIDP051504 = "";
var objSelRowP051504 = {};
var objPageParamP051504 = { sortfield: "djdate", orderby: "asc", where: "", flag: 0 };  //分页控件使用
var strDivP051504 = "divP051504";
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载方法
function pageLoad_P051504() {
    //选择日期范围
    loadDateExtent_P051504();
    loadKmData_P051504();
    loadKhData_P051504();
    //加载药品下拉信息
    loadDrugData_P051504();
    loadZyData_P051504();
    var objConfig = ParseData(window.external.DoOptions(strTableP051504, "GetTableConfig", "{}"));
    //添加列格式化方法
    for (var i = 0; i < objConfig.colModel.length; i++) {
        objConfig.colModel[i].sortable = true;
        if (objConfig.colModel[i].name == "fyfse") {
            objConfig.colModel[i].formatter = formatterP051504Money;
        }
    }
    createGrid(strTableP051504, objConfig.colNames, objConfig.colModel, [], null, null, true);
    //表格事件的添加
    addGridEvent_P051504(strTableP051504);
    resizeGrid(strTableP051504, true, true);
    //创建分页
    createPage(strPageP051504, strTableP051504);
    //加载数据
    searchDataClick_P051504();
    //resizeGrid(strTableP051504);
    objConfig = {};
}

//查询按钮的方法
function searchDataClick_P051504() {
    try {
        objPageParamP051504.sortfield = "djdate"; //排序字段
        objPageParamP051504.orderby = "asc";     //排序顺序
        objPageParamP051504.flag = 1;             //表示点击查询按钮
        loadGridDataP051504(true);                     //加载数据
        objPageParamP051504.flag = 0;              //使用完后，设置为零
    } catch (e) { }
}

//加载数据
function loadGridDataP051504(strFlag) {
    var objResult = [];
    //combobox的key不存在，赋值空
    if (!$("#cmbKmP051504").cihoComboTree("getValue")) {
        $("#cmbKmP051504").cihoComboTree("setValue", "");
    }
    if (!$("#cmbKhP051504").combobox("getValue")) {
        $("#cmbKhP051504").combobox("setValue", "");
    }
    if (!$("#cmbDrugP051504").combobox("getValue")) {
        $("#cmbDrugP051504").combobox("setValue", "");
    }
    if (!$("#cmbZyP051504").combobox("getValue")) {
        $("#cmbZyP051504").combobox("setValue", "");
    }
    if (strFlag == true) {
        objPageParamP051504.where = getSelJson_P051504();
    }
    objResult = loadGridData_Page(objPageParamP051504);
    //后台返回的合计
    if (objResult) {
        if (objResult.Records.length > 0) {
            $("#" + strTableP051504).footerData("set", { rn: "合计",
                fyfse: objResult.hjje.toFixed(window.parent.strMoneyPlace)
            }, false);
        } else {
            $("#" + strTableP051504).footerData("set", { rn: "合计", fyfse: "&nbsp;" }, false);
        }
    }
    objResult = {};
    intSelRowIDP051504 = "";
    objSelRowP051504 = {};
}

//取得查询条件
function getSelJson_P051504() {
    //查询条件的取得
    var strWhereJson = "";
    //快捷日期是历史以来，传空值
    if ($("#cmbDateExtentP051504").combobox("getValue") == "历史以来") {
        strWhereJson = "\"sdate\":\"0\",";
        strWhereJson += "\"edate\":\"0\",";
    }
    else {
        strWhereJson = "\"sdate\":\"" + $("#dateStartTimeP051504").val() + "\",";
        strWhereJson += "\"edate\":\"" + $("#dateEndTimeP051504").val() + "\",";
    }
    strWhereJson += "\"kmdm\":\"" + $("#cmbKmP051504").cihoComboTree("getValue") + "\",";
    strWhereJson += "\"khdm\":\"" + $("#cmbKhP051504").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP051504").combobox("getValue") + "\",";
    strWhereJson += "\"zydm\":\"" + $("#cmbZyP051504").combobox("getValue") + "\"";
    return strWhereJson;
}

//返回按钮的方法
function pageBack_P051504() {
    //改变标签的Name
    $(window.parent.document).find("#span_515").text("费用统计表");
    //改变标签Iframe的Src
    $(window.parent.document).find("#idIframe-515").css("display", ""); //显示统计页面
    $(window.parent.document).find("#idIframe-51504").css("display", "none"); //隐藏明细页面
    $(window.parent.document).find("#idIframe-51504").attr("src", "");
}

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

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//选择日期范围
function loadDateExtent_P051504() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP051504", "dateEndTimeP051504");

    //添加选择事件
    $("#cmbDateExtentP051504").combobox({
        valueField: "dm",
        textField: "mc",
        editable: false,
        panelHeight: 150,
        onChange: function (newValue, oldValue) {
            if (newValue == "历史以来") {          //历史以来，灰化日期控件框
                $("#dateStartTimeP051504").attr("disabled", true);
                $("#dateEndTimeP051504").attr("disabled", true);
            }
            else {                                  //其它的不灰化
                $("#dateStartTimeP051504").attr("disabled", false);
                $("#dateEndTimeP051504").attr("disabled", false);
            }
            //dateExtentCalculation(newValue, "dateStartTimeP051504", "dateEndTimeP051504");
        },
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP051504", "dateEndTimeP051504");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP051504", "dateEndTimeP051504");
            //searchDataClick_P051504();
        }
    });
    //加载数据
    $("#cmbDateExtentP051504").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
        { dm: "上一月", mc: "上一月" },
        { dm: "过去三个月", mc: "过去三个月" },
        { dm: "本年", mc: "本年" },
        { dm: "上一年", mc: "上一年" },
        { dm: "前年", mc: "前年" },
        { dm: "历史以来", mc: "历史以来"}]);

    var strDateItem = $(window.parent.document).find("#idIframe-51504").attr("dname");
    if (strDateItem != "") {
        $("#cmbDateExtentP051504").combobox("setValue", strDateItem);
        dateExtentCalculation(strDateItem, "dateStartTimeP051504", "dateEndTimeP051504");
    }
    else {
        var strStartTime = $(window.parent.document).find("#idIframe-51504").attr("sdate");
        var strEndTime = $(window.parent.document).find("#idIframe-51504").attr("edate");
        $("#cmbDateExtentP051504").combobox("setText", "自定义");
        $("#dateStartTimeP051504").val(strStartTime);
        $("#dateEndTimeP051504").val(strEndTime);
    }
    strDateItem = null;
    strStartTime = null;
    strEndTime = null;
}

function loadKmData_P051504() {
    var objKmData = [];
    objKmData = ParseData(window.external.DoOptions(strTableP051504, "DoOthers", "{}")).Records;
    objKmData.unshift({ "id": "", "text": "全部费用科目"});
    $("#cmbKmP051504").cihoComboTree("loadData", objKmData);
    $("#cmbKmP051504").cihoComboTree("setValue", $(window.parent.document).find("#idIframe-51504").attr("kmdm"));
    $("#cmbKmP051504").cihoComboTree("setText", $(window.parent.document).find("#idIframe-51504").attr("kmmc"));
    objKmData = {};
}

//客户下拉框信息的加载
function loadKhData_P051504() {
    $("#cmbKhP051504").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbKhP051504").combobox("getText") == "") {
                $("#cmbKhP051504").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbKhP051504").combobox("getText") == "全部客户") {
                $("#cmbKhP051504").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P0515();
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });

    //加载数据
    var objProvider = getComboboxData('Tkhxx2');
    if (objProvider == "{}") {
        objProvider = [];
    }
    objProvider.unshift({ "dm": "", "mc": "全部客户", "selected": true });
    $("#cmbKhP051504").combobox("loadData", objProvider);
    $("#cmbKhP051504").combobox("setValue", $(window.parent.document).find("#idIframe-51504").attr("khdm"));
    objProvider = {};
}

//药品下拉框信息的加载
function loadDrugData_P051504() {
    //添加变更事件
    $("#cmbDrugP051504").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbDrugP051504").combobox("getText") == "") {
                $("#cmbDrugP051504").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbDrugP051504").combobox("getText") == "全部药品") {
                $("#cmbDrugP051504").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051504();
        }
    });

    //药品信息的取得
    var objDrug = getComboboxData('Tspxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    //加载数据
    objDrug.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbDrugP051504").combobox("loadData", objDrug);
    $("#cmbDrugP051504").combobox("setValue", $(window.parent.document).find("#idIframe-51504").attr("spdm"));
    objDrug = {};
}

function loadZyData_P051504() {
    //添加变更事件
    $("#cmbZyP051504").combobox({
        width: _iWidth80,
        listWidth: _iWidth80,
        valueField: "dm",
        textField: "mc",
        onblur: function () {
            if ($("#cmbZyP051504").combobox("getText") == "") {
                $("#cmbZyP051504").combobox("setValue", "");
            }
        },
        onfocus: function () {
            if ($("#cmbZyP051504").combobox("getText") == "全部职员") {
                $("#cmbZyP051504").combobox("setText", "");
            }
        },
        enterDown: function () {
            //searchDataClick_P051504();
        }
    });

    //药品信息的取得
    var objDrug = getComboboxData('TZyxx2');
    if (objDrug == "{}") {
        objDrug = [];
    }
    //加载数据
    objDrug.unshift({ "dm": "", "mc": "全部职员", "selected": true });
    objDrug.push({ "dm": "-1", "mc": "其他职员" });
    $("#cmbZyP051504").combobox("loadData", objDrug);
    $("#cmbZyP051504").combobox("setValue", $(window.parent.document).find("#idIframe-51504").attr("zydm"));
    objDrug = {};
}

//表格事件的添加
function addGridEvent_P051504(strTableID) {
    $("#" + strTableID).setGridParam({
        onSelectRow: function (rowId) {//当前选择行数据取得
            intSelRowIDP051504 = rowId;
            objSelRowP051504 = $(this).getRowData(rowId);
        },
        ondblClickRow: function (rowId) {   //双击事件
            showDetailP051504();
        },
        onSortCol: function (index, colindex, sortorder) {  //排序
            objPageParamP051504.sortfield = index;        //排序字段
            objPageParamP051504.orderby = sortorder;      //排序顺序
            loadGridDataP051504(false);                        //加载数据
            return "stop";
        }
    });
}

//查看单据
function showDetailP051504() {
    try {
        if (intSelRowIDP051504 > 0) {
            if (objSelRowP051504.djname == "费用支出") {
                openZc_P051504(objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "赠送" || objSelRowP051504.djname == "报损") {
                showDialogP031201_bsd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "报溢") {
                showDialogP031201_byd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "预提") {
                showDialogP051102_ytd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "采购") {
                cgd(objSelRowP051504.djbh, strDivP051504);
                //showDialog_P010103(objSelRowP051504.djbh, strDivP051504);
            }
            else if (objSelRowP051504.djname == "付款优惠") {
                showDialog_P010103(objSelRowP051504.djbh, strDivP051504);
            }
            else if (objSelRowP051504.djname == "收款") {
                showDialogP021303_skd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "工资单") {
                openP051801_gzd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "采购返货") {
                showDialogP011202_fhd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "提成调整") {
                showDialog_P051801_tzd(strDivP051504, objSelRowP051504.djbh);
            }
            else if (objSelRowP051504.djname == "提成单") {
                showDialog_Tcd(strDivP051504, objSelRowP051504.djbh);
            }
        }
        else {
            window.parent.cihoAlert(msg.other.selDataRow);
        }
    } catch (e) {

    }
}

//合计
//function gridComplete_P051504() {
//    //设置合计行的数据
//    var objTable = $("#" + strTableP051504);
//    var arrIds = objTable.getDataIDs();
//    var strTotal = 0;
//    for (var i = 0; i < arrIds.length; i++) {
//        strTotal += parseFloat(objTable.getCell(arrIds[i], "fse"));
//    }
//    if (arrIds.length == 0) { //"&nbsp;"
//        $("#" + strTableP051504).footerData("set", { rn: "合计", fse: "&nbsp;" }, false);
//    }
//    else {
//        $("#" + strTableP051504).footerData("set", { rn: "合计", fse: strTotal }, true);
//    }
//    objTable = null;
//    arrIds = null;
//    strTotal = null;
//}

//je格式化money
function formatterP051504Money(cellvalue, options, rowObject) {
    if (!isNaN(parseFloat(cellvalue))) {
        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return parseFloat(0).toFixed(window.parent.strMoneyPlace);
    }
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    cihoDlgClose(strDialog);
}

//创建单据grid，公共
function createDjGrid(tbId, cNames, cModel) {
    $("#" + tbId).jqGrid({
        datatype: "local",
        //height: "100%",
        autowidth: true,
        colNames: cNames,
        colModel: cModel,
        rownumbers: true,
        footerrow: true,
        sortable: false,
        rownumWidth: 30,
        cellEdit: false,
        cellsubmit: "clientArray"
    });
}

//加载窗口grid数据，并计算合计
function loadGridData(tbId, mylist) {
    var count = mylist.length > 5 ? mylist.length : 5;
    var $tbId = $("#" + tbId);
    if (tbId == "Tp050801") {
        for (var i = 0; i < count; i++) {
            if (mylist[i]) {
                $tbId.addRowData(i + 1, mylist[i]);                                   //添加后台数据行
            }
            else {                                                                                  //添加空行
                $tbId.addRowData(i + 1, {});
            }
        }
    } else if (tbId == "Tp050803") {
        var yfhj = 0, sfhj = 0, ye = 0;
        for (var i = 0; i < count; i++) {
            if (mylist[i]) {                                                                         //添加后台数据行
                yfhj += isNaN(mylist[i].yfhj * 1) ? 0 : mylist[i].yfhj * 1;
                sfhj += isNaN(mylist[i].sfgz * 1) ? 0 : mylist[i].sfgz * 1;
                ye += isNaN(mylist[i].ye * 1) ? 0 : mylist[i].ye * 1;
                $tbId.addRowData(i + 1, mylist[i]);                                   //添加后台数据行
            }
            else {                                                                                  //添加空行
                $tbId.addRowData(i + 1, {});
            }
        }
        $tbId.footerData("set", { rn: "合计", yfhj: yfhj, sfgz: sfhj, ye: ye }, true);
    }
    $tbId.data("nextNum", count + 1);
    count = null;
    $tbId = null;
    yfhj = sfhj = ye = null;
    count = null;
}

//创建表单元素
function createInputs(arr) {
    var strForm = "<ul>";
    for (i = 0; i < arr.length; i++) {
        var Item = arr[i];
        if (!Item.hidden) {
            if (Item.edittype == "text") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' disabled ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "' ";
                strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
                strForm += "</li>";
            } else if (Item.edittype == "select") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<select name='" + Item.index + "' id='" + Item.index + "' disabled";
                strForm += " class='easyui-combobox' >";
                strForm += "</select>";
                strForm += "</li>";
            }
        }
    }
    strForm += "</ul>";
    return strForm;
}
///*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/