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

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

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

        //添加表单元素到窗口中
        tableFormP041102_jxfpd(strDivP041102_jxfpd, strDjbh, strZt);

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

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

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

//创建表单
function tableFormP041102_jxfpd(strDivP041102_jxfpd, strDjbh, strZt) {
    var strHeadP041102_jxfpd = "";          //默认页头为空
    var strTablep041102_jxfpd = "";         //默认表格为空
    var strDownP041102_jxfpd = "";         //默认页尾为空
    var objP041102_jxfpd = {};                  //存放表格的列配置
    var strPageHeader = [];                        //页头的配置
    var strpageFooter = [];                         //页尾的配置

    //加载编号的语句
    if (strZt == "0") {
        $("#idMust", $("#" + strDivP041102_jxfpd)).append("(未抵扣)");
    }
    else if (strZt == "1") {
        $("#idMust", $("#" + strDivP041102_jxfpd)).append("(已抵扣)");
    }
    else if (strZt == "2") {
        $("#idMust", $("#" + strDivP041102_jxfpd)).append("(退票)");
    }

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

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

    strHeadP041102_jxfpd += "<ul>";
    for (var i = 0; i < strPageHeader.length; i++) {
        if (strPageHeader[i]["name"] == "gysdm" || strPageHeader[i]["name"] == "kpdwdm") {
            continue;
        }
        if (strPageHeader[i]["name"] == "sfzjgp") {         //创建页头
            strHeadP041102_jxfpd += "<li class='unline'>";
            strHeadP041102_jxfpd += "<input type='checkbox'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP041102_jxfpd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP041102_jxfpd += strPageHeader[i]["xsmc"];
            strHeadP041102_jxfpd += "</li>";
        }
        else {
            strHeadP041102_jxfpd += "<li class='inp_70'>";
            strHeadP041102_jxfpd += strPageHeader[i]["xsmc"] + "：";
            strHeadP041102_jxfpd += "<input type='text'  value=''  name='" + strPageHeader[i]["name"] + "' disabled";
            strHeadP041102_jxfpd += " id='" + strPageHeader[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strHeadP041102_jxfpd += "</li>";
        }
    }
    strHeadP041102_jxfpd += "</ul>";
    $("#" + strDivP041102_jxfpd + "_divform").append(strHeadP041102_jxfpd);

    strTablep041102_jxfpd += "<table id='Tp040101'></table>";           //创建表格
    $("#" + strDivP041102_jxfpd + "_divform").append(strTablep041102_jxfpd);

    strDownP041102_jxfpd += "<ul>";
    for (var i = 0; i < strpageFooter.length; i++) {                                       //创建页尾
        if (strpageFooter[i]["name"] == "czydm") {
            continue;
        }
        if (strpageFooter[i]["name"] == "bz") {
            strDownP041102_jxfpd += "<li class='bz'>";
            strDownP041102_jxfpd += strpageFooter[i]["xsmc"] + "：";
            strDownP041102_jxfpd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP041102_jxfpd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP041102_jxfpd += "</li>";
        }
        else {
            strDownP041102_jxfpd += "<li>";
            strDownP041102_jxfpd += strpageFooter[i]["xsmc"] + "：";
            strDownP041102_jxfpd += "<input type='text'  value=''  name='" + strpageFooter[i]["name"] + "' disabled";
            strDownP041102_jxfpd += " id='" + strpageFooter[i]["name"] + "' maxlength='50'/>"; //限制输入长度
            strDownP041102_jxfpd += "</li>";
        }
    }
    strDownP041102_jxfpd += "</ul>";
    $("#" + strDivP041102_jxfpd + "_divform").append(strDownP041102_jxfpd);

    /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**绘制表单 结束**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

    //释放内存
    strHeadP041102_jxfpd = null;
    strTablep041102_jxfpd = null;
    strDownP041102_jxfpd = null;
    objP041102_jxfpd = null;
    strPageHeader = null;
    strpageFooter = null;
}

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

        //获得jqGrid的列名和属性
        objP041102_jxfpd = ParseData(window.external.DoOptions(strTableP041102_jxfpd, 'GetTableConfig', '{}'));
        objResultP041102_jsfpd = ParseData(window.external.DoOptions(strTableP041102_jxfpd, 'GetList', '{"djbh":"' + strDjbh + '"}'));

        //创建表格
        createGrid_P041102_jxfpd(strTableP041102_jxfpd, objP041102_jxfpd.colNames, objP041102_jxfpd.colModel);

        //给发票金额格式化数据
        if (objResultP041102_jsfpd.a1) {
            $("#a1", $("#" + strDivP041102_jxfpd + "_divform")).val(parseFloat(objResultP041102_jsfpd.a1).toFixed(window.parent.strMoneyPlace));
        }

        //数据的格式化
        $("#" + strTableP041102_jxfpd).setColProp("sl", { formatter: formatCountPlace });
        $("#" + strTableP041102_jxfpd).setColProp("a2", { formatter: formatMoneyPlace });
        $("#" + strTableP041102_jxfpd).setColProp("je", { formatter: formatMoneyPlace });

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

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

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

        //给收票日期绑定值
        $("#djdate", $("#" + strDivP041102_jxfpd + "_divform")).val(objResultP041102_jsfpd.djdate);
        //给供应商赋值
        $("#gysmc", $("#" + strDivP041102_jxfpd + "_divform")).val(objResultP041102_jsfpd.gysmc);
        //给发票日期赋值
        $("#fprq", $("#" + strDivP041102_jxfpd + "_divform")).val(objResultP041102_jsfpd.fprq);
        //给发票编号赋值
        $("#fpbh", $("#" + strDivP041102_jxfpd + "_divform")).val(objResultP041102_jsfpd.fpbh);
        //给收票单位赋值
        $("#kpdwmc", $("#" + strDivP041102_jxfpd + "_divform")).val(objResultP041102_jsfpd.kpdwmc);
        //给是否直接购票赋值
        if (objResultP041102_jsfpd.sfzjgp == "True") {
            objResultP041102_jsfpd.sfzjgp = true;
        }
        else {
            objResultP041102_jsfpd.sfzjgp = false;
        }
        $("#sfzjgp", $("#" + strDivP041102_jxfpd + "_divform")).attr("checked", objResultP041102_jsfpd.sfzjgp);
        //给制单人赋值
        $("#czymc", $("#" + strDivP041102_jxfpd + "_divform")).val(objResultP041102_jsfpd.czymc);
        //给备注赋值
        $("#bz", $("#" + strDivP041102_jxfpd + "_divform")).val(escape(objResultP041102_jsfpd.bz));

        //加上合计行
        $("#" + strTableP041102_jxfpd).footerData("set", { rn: "合计", je: jxfpje.toFixed(window.parent.strMoneyPlace) });

        //释放内存
        objP041102_jxfpd = null;
        objResultP041102_jsfpd = null;
        myList = null;
    } catch (e) { }
}

//数量的格式化方法
function formatCountPlace(cellValue) {
    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }
}

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

/*-------------------------------------------------------------
功能：创建普通表格
*****①strTableID：表格的ID
*****②objColNames:表格需要显示的列名
*****③objColModel：表格的列配置
-----------------------------------------------------------------*/
function createGrid_P041102_jxfpd(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);
}