var strTableP0201 = "Tp0201";
var strPageP0201 = "divPageP0201";
var objSelRowP0201 = -1;
var objRowIdP0201 = -1;
var objPageParamP0201 = { sortfield: "", orderby: "", where: "", flag: 0 };    //分页控件使用

//基本资料修改刷新
function refreshMyCombobox(strModels) {
    for (var i = 0; i < strModels.length; i++) {
        var data;

        if (strModels[i] == "Tspxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部药品" });
            reLoadCombobox("cmbSpP0201", data);
        }
        if (strModels[i] == "Tkhxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部客户" });
            reLoadCombobox("cmbKhP0201", data);
        }
        if (strModels[i] == "TZyxx") {
            data = getComboboxData(strModels[i] + "2");
            data.unshift({ dm: "", mc: "全部销售员" });
            data.push({ dm: "-1", mc: "其他销售员" });
            reLoadCombobox("cmbZyP0201", data);
        }

        data = null;
    }
}

//格式化数量
function formatterP0201Count(cellValue) {

    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strCountPlace);
    }
    else {
        return "&nbsp;";
    }

}

//格式化单价
function formatterP0201Price(cellValue) {

    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strPricePlace);
    }
    else {
        return "&nbsp;";
    }
}

//格式化金额
function formatterP0201Money(cellValue) {

    if (!isNaN(parseFloat(cellValue))) {
        return parseFloat(cellValue).toFixed(window.parent.strMoneyPlace);
    }
    else {
        return "&nbsp;";
    }
}

//是否带票
function formatterP0201A4(cellValue) {
    if (cellValue == "True" || cellValue == "是") {
        return "是";
    }
    else if (cellValue == "False" || cellValue == "否") {
        return "否";
    }
    else {
        return "&nbsp;";
    }
}

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        //采购单保存
        if (strTable == "Tp020101") {
            afterSaveClick(strDialog, strType)
        }
        //退货单保存
        else if (strTable == "Tp020102") {
            saveMyDataTp020102(strDialog, strType);
        }
        //收款单保存
        else if (strTable == "Tp020301") {
            saveData_P020301(strType);
        }
        //回款分配单保存
        else if (strTable == "Tp020302") {
            saveData_P020302();
        }
        //添加分类
        else if (strTable == ciho.layal.table) {
            ciho.saveType();
        }
    }
    catch (e) {
        proceError("P0201", 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_P020102();
                    }

                }
            }
        );
    }
    else {
        cihoDlgClose(strDialog);
        if (strDialog != "divAddType") {
            freeMemory_P020102();
        }

    }
}

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

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

//头部搜索信息
function getSearchInfoP0201() {

    //设置combobox
    $("#cmbSpP0201").combobox();
    $("#cmbKhP0201").combobox();
    $("#cmbZyP0201").combobox();
    $("#cmbDateExtentP0201").combobox();
    $("#cmbBillTypesP0201").combobox();

    //下拉箭头样式
    addArrowClass();

    //客户信息，药品信息，单据类型
    var objKh = getComboboxData("Tkhxx2");
    var objSp = getComboboxData("Tspxx2");
    var objZy = getComboboxData("TZyxx2");
    var objDjType = [{ "dm": "", "mc": "全部单据类型", "selected": true }, { "dm": "201", "mc": "终端销售" }, { "dm": "202", "mc": "配送销售" }, { "dm": "203", "mc": "销售退货"}];

    //日期
    inputAndBindData("dateStartTimeP0201", null, null, dateTimeChangeP0201);
    inputAndBindData("dateEndTimeP0201", null, null, dateTimeChangeP0201);
    dateExtentCalculation("本月", "dateStartTimeP0201", "dateEndTimeP0201");

    //药品信息
    $("#cmbSpP0201").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部药品") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部药品");
            }
        }
    });
    if (objSp != null && objSp != "{}") {
        objSp.unshift({ dm: "", mc: "全部药品", selected: true });
        $("#cmbSpP0201").combobox("loadData", objSp);
    }

    //客户信息
    $("#cmbKhP0201").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部客户") {
                $(this).combobox("setText", "");
            }
        },
        onSelect: function (Record) {
            if (Record.dm != "") {
                $(this).combobox("setText", Record.mc.replace(/&nbsp;/g, ""));
            }

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

    //职员信息
    $("#cmbZyP0201").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        textField: "mc",
        valueField: "dm",
        onfocus: function () {
            if ($(this).combobox("getText") == "全部销售员") {
                $(this).combobox("setText", "");
            }
        },
        onblur: function () {
            if ($(this).combobox("getText") == "") {
                $(this).combobox("setText", "全部销售员");
            }
        }
    });
    if (objZy != null && objZy != "{}") {
        objZy.unshift({ dm: "", mc: "全部销售员", selected: true });
        objZy.push({ dm: "-1", mc: "其他销售员" });
        $("#cmbZyP0201").combobox("loadData", objZy);
    }

    //快捷日期，单据类型
    $("#cmbDateExtentP0201").combobox({
        width: _iWidth100,
        panelHeight: 122,
        editable: false,
        onSelect: function (record) {
            dateExtentCalculation(record.value, "dateStartTimeP0201", "dateEndTimeP0201");
        },
        enterDown: function () {
            dateExtentCalculation($(this).combobox('getValue'), "dateStartTimeP0201", "dateEndTimeP0201");
        }
    });

    //单据类型
    $("#cmbBillTypesP0201").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", "全部单据类型");
            }
        }
    });
    $("#cmbBillTypesP0201").combobox("loadData", objDjType);

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

//单击行
function onSelectGridP0201(rowId) {

    //存储行ID和行对象
    objRowIdP0201 = rowId;
    objSelRowP0201 = $(this).getRowData(rowId);

    //选择行不是空行时，弹出浮动层
    if (objSelRowP0201.djbh != null || 1 == 1) {
        var strButtonHtml = "";
        //浮动按钮内容
        if (objSelRowP0201.djbh.substr(0, 4) == "XSTH") {
            strButtonHtml = "<a onclick=\"showDialogP020102('2');\">修改</a>-<a onclick=\"deleteDataP0201();\">删除</a>";
        }
        else if (objSelRowP0201.djbh.substr(0, 4) == "ZDXS") {
            strButtonHtml = "";
            if (window.parent.bSkZt == true && parseInt(objSelRowP0201.wsk) != 0) {
                strButtonHtml += "<a onclick=\"showDialog_P020301('2','1')\">收款</a>-"
            }
            //strButtonHtml += "<a onclick=\"showDialogP020102('3');\">退货</a>-" +
            strButtonHtml += "<a onclick=\"openSales(2)\">修改</a>-" +
                        "<a onclick=\"deleteDataP0201();\">删除</a>";
        }
        else if (objSelRowP0201.djbh.substr(0, 4) == "PSXS") {
            strButtonHtml = "";
            if (window.parent.bSkZt == true && parseInt(objSelRowP0201.wsk) != 0) {
                strButtonHtml += "<a onclick=\"showDialog_P020301('2','1')\">收款</a>-"
            }
            //strButtonHtml += "<a onclick=\"showDialogP020102('3');\">退货</a>-" +
            strButtonHtml += "<a onclick=\"openSales(2)\">修改</a>-" +
                        "<a onclick=\"deleteDataP0201();\">删除</a>";
        }
        createFloatButton.call(this, rowId, strButtonHtml);
    }

}

//双击事件
function funcDbClick(strType, selRowId) {

    if (objSelRowP0201.djbh.substr(0, 4) == "XSTH") {
        showDialogP020102(strType);
    }
    else {
        openSales(strType);
    }

}

//搜索数据
function searchDataClickP0201() {

    //药品不能为空
    if ($("#cmbSpP0201").combobox('getValue') == undefined) {
        $("#cmbSpP0201").combobox('setValue', "");
        // window.parent.cihoAlert(msg.sz.yp.empOrNotExit);
        // return;
    }

    //客户不能为空
    if ($("#cmbKhP0201").combobox('getValue') == undefined) {
        $("#cmbKhP0201").combobox("setValue", "");
        //window.parent.cihoAlert(msg.sz.kh.empOrNotExit);
        //return;
    }

    //职员不能为空
    if ($("#cmbZyP0201").combobox('getValue') == undefined) {
        $("#cmbZyP0201").combobox("setValue", "");
        //window.parent.cihoAlert(msg.sz.kh.empOrNotExit);
        //return;
    }

    //组查询条件
    var strWhereJson = "";

    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0201").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0201").val() + "\"";
    strWhereJson += ",\"khdm\":\"" + $("#cmbKhP0201").combobox("getValue") + "\"";
    strWhereJson += ",\"spdm\":\"" + $("#cmbSpP0201").combobox("getValue") + "\"";
    strWhereJson += ",\"zydm\":\"" + $("#cmbZyP0201").combobox("getValue") + "\"";
    strWhereJson += ",\"djtypebh\":\"" + $("#cmbBillTypesP0201").combobox("getValue") + "\"";

    objPageParamP0201.where = strWhereJson;

    //获取数据
    var objRecords = loadGridData_Page(objPageParamP0201);

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

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

    //计算合计
    if (objRecords.Records.length == 0) {
        $("#" + strTableP0201).footerData('set', { rn: "合计", a2: "&nbsp;", a5: "&nbsp;", a3: "&nbsp;", ysk: "&nbsp;", wsk: "&nbsp;" });
    }
    else {
        $("#" + strTableP0201).footerData('set', { rn: "合计", a2: parseFloat(objRecords.zsl).toFixed(window.parent.strCountPlace), a5: parseFloat(objRecords.ykfpzje).toFixed(window.parent.strMoneyPlace), a3: parseFloat(objRecords.zje).toFixed(window.parent.strMoneyPlace), ysk: parseFloat(objRecords.zysk).toFixed(window.parent.strMoneyPlace), wsk: parseFloat(objRecords.zwsk).toFixed(window.parent.strMoneyPlace) });
    }
}

//排序
function sortDataP0201(index, colindex, sortorder) {

    //设置排序
    objPageParamP0201.sortfield = index;
    objPageParamP0201.orderby = sortorder;

    var objRecords = loadGridData_Page(objPageParamP0201);

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

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

    //计算合计
    if (objRecords.Records.length == 0) {
        $("#" + strTableP0201).footerData('set', { rn: "合计", a2: "&nbsp;", a5: "&nbsp;", a3: "&nbsp;", ysk: "&nbsp;", wsk: "&nbsp;" });
    }
    else {
        $("#" + strTableP0201).footerData('set', { rn: "合计", a2: parseFloat(objRecords.zsl).toFixed(window.parent.strCountPlace), a5: parseFloat(objRecords.ykfpzje).toFixed(window.parent.strMoneyPlace), a3: parseFloat(objRecords.zje).toFixed(window.parent.strMoneyPlace), ysk: parseFloat(objRecords.zysk).toFixed(window.parent.strMoneyPlace), wsk: parseFloat(objRecords.zwsk).toFixed(window.parent.strMoneyPlace) });
    }

    return 'stop';
}

//退货单据变红
function funcChangeRed() {
    var objTable = $("#" + strTableP0201);
    var arrIds = objTable.getDataIDs();
    for (var i = 0; i < arrIds.length; i++) {
        if (objTable.getRowData(arrIds[i]).djbh.toString().substr(0, 4) == "XSTH") {
            $("#" + arrIds[i]).css("color", "#996600");
        }
    }
    objTable = null;
}


