/*--------------------------文档信息-------------------------------------------
///功能：      进项抵扣单的查看单据
///创建人：  王群
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
-----------------------------------------------------------------------------*/
//先定义一下全局变量
strTableP041201_jxfpdkd = "Tp040102";

//查看单据
function showDialogP041201_jxfpdkd(strDivP041201_jxfpdkd, strDjbh) {
    try {
        $(".operate").remove();                                     //清除浮动按钮层
        $("#" + strDivP041201_jxfpdkd).empty();         //清空弹出窗口内容

        //把弹出窗口框架及底部按钮添加到父窗口创建dialog中
        creatDiv_ShowDlgHaveTbl(strDivP041201_jxfpdkd, "进项发票抵扣", false, false, strTableP041201_jxfpdkd, "810");

        //添加表单元素到窗口中
        tableFormP041201_jxfpdkd(strDivP041201_jxfpdkd, strDjbh);

        //调用所查数据同时加载到所对应的控件
        myListP041201_jxfpdkd(strDivP041201_jxfpdkd, strDjbh);

        //重新定位dialog的位置
        //$("#" + strDivP041201_jxfpdkd).dialog("option", "position", "center");

        //释放内存
        strDivP041201_jxfpdkd = null;
        strDjbh = null;
    } catch (e) { }
}

//创建表单
function tableFormP041201_jxfpdkd(strDivP041201_jxfpdkd, strDjbh) {
    var strHeadP041201_jxfpdkd = "";                //页头默认为空
    var strTablep041201_jxfpdkd = "";               //表格默认为空
    var objP041201_jxfpdkd = {};                        //存放列配置
    var strPageHeader = [];                                 //存放页头配置

    //获得jqGrid的列名和属性
    objP041201_jxfpdkd = ParseData(window.external.DoOptions(strTableP041201_jxfpdkd, 'GetTableConfig', '{}'));
    strPageHeader = objP041201_jxfpdkd.pageHeader;

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**绘制表单 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    strHeadP041201_jxfpdkd += "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {            //创建页头
        if (strPageHeader[i]["name"] == "kpdwmc") {
            continue;
        }
        strHeadP041201_jxfpdkd += "<li>";
        strHeadP041201_jxfpdkd += strPageHeader[i]["xsmc"] + "：";
        strHeadP041201_jxfpdkd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
        strHeadP041201_jxfpdkd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
        strHeadP041201_jxfpdkd += "</li>";
    }
    strHeadP041201_jxfpdkd += "</ul>";
    $("#" + strDivP041201_jxfpdkd + "_divform").append(strHeadP041201_jxfpdkd);

    strTablep041201_jxfpdkd += "<table id='Tp040102'></table>";            //创建表格
    $("#" + strDivP041201_jxfpdkd + "_divform").append(strTablep041201_jxfpdkd);
    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**绘制表单 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    //释放内存
    strHeadP041201_jxfpdkd = null;
    strTablep041201_jxfpdkd = null;
    objP041201_jxfpdkd = null;
    strPageHeader = null;
}

//从后台加载数据到前台表单实现展示功能
function myListP041201_jxfpdkd(strDivP041201_jxfpdkd, strDjbh) {
    try {
        var objP041201_jxfpdkd = {};                //存放表格的列配置
        var objResultP041201_jsfpdkd = {};      //存放返回信息
        var myList = [];                                        //存放返回的数据
        var count = 5;                                          //最少显示5条数据

        //获得jqGrid的列名和属性
        objP041201_jxfpdkd = ParseData(window.external.DoOptions(strTableP041201_jxfpdkd, 'GetTableConfig', '{}'));
        objResultP041201_jsfpdkd = ParseData(window.external.DoOptions(strTableP041201_jxfpdkd, 'GetList', '{"djbh":"' + strDjbh + '","fb":"2"}'));

        //创建表格
        createGrid_P041201_jxfpdkd(strTableP041201_jxfpdkd, objP041201_jxfpdkd.colNames, objP041201_jxfpdkd.colModel);

        //格式化表格中的数据
        $("#" + strTableP041201_jxfpdkd).setColProp("a1", { formatter: formatMoneyPlace });
        $("#" + strTableP041201_jxfpdkd).setColProp("dksl", { formatter: formatterPercent });
        $("#" + strTableP041201_jxfpdkd).setColProp("je", { formatter: formatMoneyPlace });

        myList = objResultP041201_jsfpdkd.Records;                  //把返回的数据赋给【myList】
        count = myList.length > 5 ? myList.length : 5;                  //表格最少显示【5】条数据

        //添加行并为行分配id，并计算合计
        var zkje = 0;
        for (var i = 0; i < count; i++) {
            if (myList[i]) {
                if (myList[i].je) {                                                        //进项发票金额存在时
                    zkje += parseFloat(myList[i].je);
                }
                $("#" + strTableP041201_jxfpdkd).addRowData(i + 1, myList[i]);      //添加后台数据行
            }
            else {                                                        //添加空行
                $("#" + strTableP041201_jxfpdkd).addRowData(i + 1, {});
            }
        }

        //调整表格的自适应的宽度  
        resizeGridScroll(strTableP041201_jxfpdkd);

        //抵扣日期绑定值
        $("#dkdate", $("#" + strDivP041201_jxfpdkd + "_divform")).val(myList[0].dkdate);
        // 收票单位绑定值
        $("#kpdwdm", $("#" + strDivP041201_jxfpdkd + "_divform")).val(myList[0].kpdwmc);
        
        //加上合计行
        $("#" + strTableP041201_jxfpdkd).footerData("set", { rn: "合计", je: zkje.toFixed(window.parent.strMoneyPlace) });

        //释放内存
        objP041201_jxfpdkd = null;
        objResultP041201_jsfpdkd = null;
        myList = null;
    } catch (e) { }
}

//抵扣税率的格式化
function formatterPercent(cellvalue, options, rowObject) {
    if (isNaN(parseFloat(cellvalue))) {
        return "&nbsp;"
    }
    else {
        return (parseFloat(cellvalue) * 100).toFixed(window.parent.strPercentPlace) + "%";
    }
}

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

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
-----------------------------------------------------------------*/
function createGrid_P041201_jxfpdkd(strTableID, objColNames, objColModel) {
    $("#" + strTableID).jqGrid({
        datatype: "local",
        colNames: objColNames,
        colModel: objColModel,
        rownumbers: true,
        rownumWidth: 30,
        footerrow: true,
        autowidth: true
    });
    //给表格加上序号
    $("#" + strTableID).setLabel("rn", "序号");
    //设置表格的高度
    $("#" + strTableID).setGridHeight(141);
}