/*************************
*功能：销项发票管理
*创建人：张振华
*时间：2010-08-15
*备注：此时间是加注释时间
*************************/

var strTableP0402 = "Tp0402";
var strPageP0402 = "divPageP0402";
var objSelRowP0402; //单击行数据
var objPageParamP0402 = { sortfield: "", orderby: "", where: "",flag:0 }; //分页控件使用

//基本资料修改刷新
function refreshMyCombobox(strModels) {
    for (var i = 0; i < strModels.length; i++) {
        var data;
        if (strModels[i] == "Tkpdwxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "开票单位" });
            reLoadCombobox("cmbInvoiceDwP0402", data);
        }
        if (strModels[i] == "Tkhxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部客户" });
            reLoadCombobox("cmbKhP0402", data);
        }
    }
}

//头部搜索条的信息
function getSearchInfoP0402() {

    //设置combobox
    $("#cmbInvoiceDwP0402").combobox();
    $("#cmbKhP0402").combobox();
    $("#cmbInvoiceBhP0402").combobox();
    $("#cmbDateExtentP0402").combobox();
    $("#cmbInvoiceStateP0402").combobox();

    //combobox向下箭头
    addArrowClass();

    //客户，开票单位，发票编号，发票状态
    var objKh = getComboboxData("Tkhxx2");
    var objKpDw = getComboboxData("Tkpdwxx2");
    var objFpBh = ParseData(window.external.DoOptions(strTableP0402, "GetComboBoxData", "{}"));
    var objFpState = [{ dm: "", mc: "全部" }, { dm: "0", mc: "未送" }, { dm: "1", mc: "已送" }, { dm: "2", mc: "已退"}];

    //开始时间，结束时间绑定控件
    inputAndBindData("dateStartTimeP0402", false, null, dateTimeChangeP0402);
    inputAndBindData("dateEndTimeP0402", false, null, dateTimeChangeP0402);
    dateExtentCalculation("本月", "dateStartTimeP0402", "dateEndTimeP0402");

    //快捷日期
    $("#cmbDateExtentP0402").combobox({
        width: _iWidth100,
        panelHeight: 102,
        editable: false,
        onSelect: function (option) {
            dateExtentCalculation(option.value, "dateStartTimeP0402", "dateEndTimeP0402");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0402", "dateEndTimeP0402");
        }
    });

    //开票单位
    $("#cmbInvoiceDwP0402").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "开票单位") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "开票单位");
            }
        }
    });
    if (objKpDw != null && objKpDw != "{}") {
        objKpDw.unshift({ dm: "", mc: "开票单位", selected: true });
        $("#cmbInvoiceDwP0402").combobox("loadData", objKpDw);
    }

    //全部客户
    $("#cmbKhP0402").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部客户") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部客户");
            }
        },
        afterChange: function (newValue, newText) {
            $(this).combobox("setText", newText.replace(/&nbsp;/g, ""));
        }
    });
    if (objKh != null && objKh != "{}") {
        objKh.unshift({ dm: "", mc: "全部客户", selected: true });
        $("#cmbKhP0402").combobox("loadData", objKh);
    }

    //发票编号
    $("#cmbInvoiceBhP0402").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部发票") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部发票");
            }
        }
    });
    if (objFpBh != null && objFpBh.Records != "{}") {
        objFpBh.Records.unshift({ mc: "全部发票", dm: "", selected: true });
        $("#cmbInvoiceBhP0402").combobox("loadData", objFpBh.Records);
    }

    //发票状态
    $("#cmbInvoiceStateP0402").combobox({
        width: _iWidth100,
        panelHeight: 82,
        textField: "mc",
        valueField: "dm",
        editable: false,
        onfocus: function () {
            if ($(this).combobox("getText") == "全部") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部");
            }
        }

    });
    objFpState[0].selected = true;
    $("#cmbInvoiceStateP0402").combobox("loadData", objFpState);

    //设置层序
    $(".layoutpanel").css("z-index", "50000009");
}

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

//改变状态
function changZtP0402(zt) {

    //改变状态
    var objRecord = ParseData(window.external.DoOptions(strTableP0402, "DoOthers", "{\"djbh\":\"" + objSelRowP0402.djbh + "\" ,\"Flag\":\"" + zt + "\"}"));

    //修改失败直接退出
    if (objRecord.ReturnCode != 0) {
        window.parent.cihoAlert(msg.other.StateError);
        return;
    }

    //修改成功改变前台状态
    var objTable = $("#" + strTableP0402);
    switch (zt) {
        case "0":
            objTable.setCell(objSelRowP0402.id, "zt", "0");
            break;
        case "1":
            objTable.setCell(objSelRowP0402.id, "zt", "0");
            break;
        case "2":
            objTable.setCell(objSelRowP0402.id, "zt", "1");
            break;
        default:

    }
    objTable = null;
}

//单击一行
function onSelectGridP0201(rowId) {

    //获取单击的行的数据
    objSelRowP0402 = $(this).getRowData(rowId);

    //选择行不是空行时，浮动条
    var strButtonHtml = "";
    if (objSelRowP0402.zt != null) {
        switch (objSelRowP0402.zt) {
            case "未送":
                strButtonHtml += "<a onclick=\"changZtP0402('2');\">已送</a>-<a onclick=\"showDialogP040203('1');\">退票</a>-<a onclick=\"showDialogP040202('2');\">修改</a>-<a onclick=\"deleteDataP0402();\">删除</a>";
                break;
            case "已送":
                strButtonHtml += "<a onclick=\"showDialogP040202('3');\">查看</a>-<a onclick=\"changZtP0402('1');\">取消已送</a>";
                break;
            case "已退":
                strButtonHtml += "<a onclick=\"showDialogP040202('3');\">查看</a>-<a onclick=\"showDialogP040203('2');\">退票</a>-<a onclick=\"changZtP0402('0');\">取消退票</a>";
                break;
        }

        createFloatButton.call(this, rowId, strButtonHtml);
    }
}

//删除数据
function deleteDataP0402() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                //删除数据信息
                var objResult = ParseData(window.external.DoOptions(strTableP0402, 'Delete', "{\"djbh\":\"" + objSelRowP0402.djbh + "\"}"));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    objPageParamP0402.flag = 2;
                    searchDataClickP0402();
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
            }
        });
    }
    catch (e) {
        proceError("P0402", e.ToString());
    }
}

//双击
function funcDbClickP0402(strType, rowId) {

    //行的数据
    objSelRowP0402 = $(this).getRowData(rowId);

    //改变状态
    switch (objSelRowP0402.zt) {
        case "未送":
            showDialogP040202("2");
            break;
        case "已送":
            showDialogP040202("3");
            break;
        case "已退":
            showDialogP040202("3");
            break;
    }
}

//排序事件
function sortDataP0402(index, colindex, sortorder) {

    objPageParamP0402.sortfield = index;
    objPageParamP0402.orderby = sortorder;

    //取出数据
    var objRecords = loadGridData_Page(objPageParamP0402);

    //重新设置分页标志位
    objPageParamP0402.flag = 0;

    //清楚浮动按钮
    $("div.operate").hide();

    //计算合计
    if (objRecords.Records.length == 0) {
        $("#" + strTableP0402).footerData('set', { rn: "合计", a1: "&nbsp;" });
    }
    else {
        $("#" + strTableP0402).footerData('set', { rn: "合计", a1: objRecords.fpjehj }, true);
    }

    return 'stop';
}

//格式化状态
function formatterP0402Zt(cellValue) {
    var strReturn = cellValue;
    switch (cellValue) {
        case "0":
            strReturn = "未送";
            break;
        case "1":
            strReturn = "已送";
            break;
        case "2":
            strReturn = "已退";
            break;
        default:
    }

    return strReturn;
}

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

//搜索数据
function searchDataClickP0402(sortName, sortOrder) {

    //开票单位
    if ($("#cmbInvoiceDwP0402").combobox("getValue") == undefined) {
        $("#cmbInvoiceDwP0402").combobox("setValue", "");
    }

    //客户
    if ($("#cmbKhP0402").combobox("getValue") == undefined) {
        $("#cmbKhP0402").combobox("setValue", "");
    }

    //全部编号
    if ($("#cmbInvoiceBhP0402").combobox("getValue") == undefined) {
        $("#cmbInvoiceBhP0402").combobox("setValue", "");
    }

    // 发票状态
    if ($("#cmbInvoiceStateP0402").combobox("getValue") == undefined) {
        $("#cmbInvoiceStateP0402").combobox("setValue", "");
    }

    //向后台发送的查询数据
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0402").val() + "\"";
    strWhereJson += ",\"edate\":\"" + $("#dateEndTimeP0402").val() + "\"";
    strWhereJson += ",\"kpdwdm\":\"" + $("#cmbInvoiceDwP0402").combobox("getValue") + "\"";
    strWhereJson += ",\"khdm\":\"" + $("#cmbKhP0402").combobox("getValue") + "\"";
    strWhereJson += ",\"fpbh\":\"" + $("#cmbInvoiceBhP0402").combobox("getValue") + "\"";
    strWhereJson += ",\"zt\":\"" + $("#cmbInvoiceStateP0402").combobox("getValue") + "\"";

    objPageParamP0402.where = strWhereJson;

    //取出数据
    var objRecords = loadGridData_Page(objPageParamP0402);

    //重新设置分页标志位
    objPageParamP0402.flag = 0;

    //清楚浮动按钮
    $("div.operate").hide();
        //计算合计
    if (objRecords.Records.length == 0) {
        $("#" + strTableP0402).footerData('set', { rn: "合计", a1: "&nbsp;" });
    }
    else {
        $("#" + strTableP0402).footerData('set', { rn: "合计", a1: objRecords.fpjehj }, true);
    }

}

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp040202") {
            saveMyDataTp040202(strDialog, strType)
        }
        else if (strTable == "Tp040203") {
            saveMyDataTp040203(strDialog, strType)
        }
        //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
    }
    catch (e) {
        proceError("P0402", e.ToString());
    }
}

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

    var isTextChange = $("#" + strDialog + "_divform").data("change");

    //当页面信息已修改时，弹出提示保存
    if (isTextChange == true) {
        //函数名称
        window.parent.cihoAlert(
            msg.other.haveChange  //message information
            , msgBox.dlgBtn.YesNoCancel     //按钮类型msgBox.dlgBtn.YesNo  msgBox.dlgBtn.YesNoCancel
            , function (strRet) {//回调函数
                if (msgBox.dlgRst.Yes == strRet) {
                    saveMyData(strDialog, 0, strTable);
                }
                else if (msgBox.dlgRst.No == strRet) {
                    cihoDlgClose(strDialog);
                    if (strDialog != "divAddType") {
                        freeMemory_P040202();
                    }
                }
            }
        );
    }
    else {
        cihoDlgClose(strDialog);
        if (strDialog != "divAddType") {
            freeMemory_P040202();
        }
    }
}