/***************************
*供应商药品仓库职员添加类型
****************************/


/*命名空间ciho*/
var ciho = {};

///空间变量
ciho.layal = { id: "divAddType", title: "分类", table: "Tflxx", isShowBoxTop: false, isShowButton: true, width: 400, page: "" };
ciho.control = { jquery: {}, model: "TSpxx", other: null };

/*初始化数据*/
ciho.IniData = function (layal, control) {
    if (layal.id == undefined || control.jquery == undefined ||
     control.model == undefined || layal.table == undefined) {
        return false;
    }
    if (layal.id != undefined) {
        ciho.layal.id = layal.id;
    }
    if (layal.title != undefined) {
        ciho.layal.title = layal.title;
    }
    if (layal.table != undefined) {
        ciho.layal.table = layal.table;
    }
    if (layal.isShowBoxTop != undefined) {
        ciho.layal.isShowBoxTop = layal.isShowBoxTop;
    }
    if (layal.isShowButton != undefined) {
        ciho.layal.isShowButton = layal.isShowButton;
    }
    if (layal.width != undefined) {
        ciho.layal.width = layal.width;
    }
    if (layal.page != undefined) {
        ciho.layal.page = layal.page;
    }

    if (control.jquery != undefined) {
        ciho.control.jquery = control.jquery;
    }
    if (control.model != undefined) {
        ciho.control.model = control.model;
    }
    if (control.other != undefined) {
        ciho.control.other = control.other;
    }
}

/* 弹出对话框 */
ciho.showTypeDialog = function () {

    //关闭panel
    ciho.control.jquery.combobox("hidePanel", ciho.control.jquery.attr("id"));

    //清楚panel
    $("#" + ciho.layal.id).empty();

    //弹出层
    if (ciho.control.model == "Tkhxx" || ciho.control.model == "Tkjkm") {
        creatDiv_ShowDialog(ciho.layal.id, "添加" + ciho.layal.title, true, ciho.layal.isShowButton, ciho.layal.table, ciho.layal.width);
    }
    else {
        creatDiv_ShowDialog(ciho.layal.id, "添加" + ciho.layal.title, ciho.layal.isShowBoxTop, ciho.layal.isShowButton, ciho.layal.table, ciho.layal.width);
    }

    var strForm;
    strForm = "<ul>";
    strForm += "<li>";
    strForm += ciho.layal.title + "名称：";
    strForm += "<input type='text'  name='ciho_mc'";
    strForm += " value=''  maxlength='50'  ";
    strForm += " id='ciho_mc' onkeypress='return checkValidate(event)' onpaste='return validatePaste()'";
    strForm += "  >&nbsp;&nbsp;&nbsp;&nbsp;";
    strForm += "</li>";
    if (ciho.control.model == "Tkpdwxx") {
        strForm += "<li>";
        strForm += "开票税率：";
        strForm += "<input type='text'  name='ciho_kpsl'";
        strForm += " value=''  ";
        strForm += " id='ciho_kpsl' >%";
        strForm += "</li>";
        strForm += "<li>";
        strForm += "抵扣税率：";
        strForm += "<input type='text'  name='ciho_dksl'";
        strForm += " value=''  ";
        strForm += " id='ciho_dksl' >%";
        strForm += "</li>";
    }
    strForm += "</ul>"
    $("#" + ciho.layal.id + "_divform").append(strForm);

    $("#ciho_mc", "#" + ciho.layal.id).focus();

    $("#" + ciho.layal.id).unbind("keydown");
    $("#" + ciho.layal.id).bind("keydown", function (event) {
        if (event.keyCode == 13) {
            ciho.saveType();
            if (ciho.layal.page == "p0607") {
                getCombobxData();
            }
        }
    });

    //添加税率
    $("#ciho_kpsl", "#" + ciho.layal.id).autoNumeric({ mDec: window.parent.strPercentPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");
    $("#ciho_dksl", "#" + ciho.layal.id).autoNumeric({ mDec: window.parent.strPercentPlace, aSep: "" }).focus(function () { this.select(); }).css("ime-mode", "disabled");

    //设置样式
    $(".btn02", "#" + ciho.layal.id).remove();
    $(".btn03 a", "#" + ciho.layal.id).css("margin-left", "-50");

    //客户,会计科目提示
    if (ciho.control.model == "Tkhxx") {
        $("#" + ciho.layal.id + "_pop_box_top").append("<p class=\"box_p\"  id='idH3'>此处只提供添加一级客户，如需要在一级客户下面添加二级客户，请到档案模块下的客户基本信息处添加</p>");
    }
    else if (ciho.control.model == "Tkjkm") {
        $("#" + ciho.layal.id + "_pop_box_top").append("<p class=\"box_p\"  id='idH3'>该科目是添加到银行存款下面的</p>");
    }
    //定位层
    $("#" + ciho.layal.id).dialog("option", "position", "center");

    ciho.control.jquery.combobox("setValue", "");

};

ciho.checkType = function () {

    var strMc = trim($("#ciho_mc", "#" + ciho.layal.id).val());
    if (strMc == "") {
        window.parent.cihoAlert(ciho.layal.title + msg.other.typeNameNoEmpty);
        return false;
    }
    var oldData = ciho.getComboDataAllStop(ciho.control.model).Records;
    for (var i = 0; i < oldData.length; i++) {
        if (oldData[i].mc == strMc) {
            window.parent.cihoAlert(ciho.layal.title + msg.other.typeNameSame);
            return false;
        }
    }

    //开票单位 验证抵扣税率和开票税率
    if (ciho.control.model == "Tkpdwxx") {
        var strKpsl = $("#ciho_kpsl", "#" + ciho.layal.id).val();
        var strDksl = $("#ciho_dksl", "#" + ciho.layal.id).val();
        if (strKpsl == "") {
            window.parent.cihoAlert(msg.sz.kp.SelKpNoEmpty);
            return false;
        }
        if (strDksl == "") {
            window.parent.cihoAlert(msg.sz.kp.DkKpNoEmpty);
            return false;
        }
//        if (!isNaN(parseFloat(strKpsl)) && parseFloat(strKpsl) > 100) {
//            window.parent.cihoAlert(msg.sz.kp.slLength);
//            return false;
//        }
//        if (!isNaN(parseFloat(strDksl)) && parseFloat(strDksl) > 100) {
//            window.parent.cihoAlert(msg.sz.kp.slLength);
//            return false;
//        }
    }
}

/*保存 */
ciho.saveType = function () {

    if (ciho.checkType() == false) {
        return;
    }
    //常用变量
    var strBh;
    var objRecult;
    var mc;
    var newData;
    var strDm;
    var oldData;
    var strID;
    var otherDm;

    //获取编号
    if (ciho.control.model == "Tkjkm") {
        strBh = ParseData(window.external.DoOptions(ciho.control.model, "MakeBH", "{\"ParentBH\":\"1002\"}")).BH;
    }
    else {
        strBh = ParseData(window.external.DoOptions(ciho.control.model, "MakeBH", "{}")).BH;
    }
    mc = trim($("#ciho_mc", "#" + ciho.layal.id).val());
    //保存数据
    var strData = "{";
    if (ciho.control.model == "Tkhxx") {
        strData += "\"bh\":\"" + strBh + "\"";
        strData += ",\"parent\":\"0\"";
    }
    else if (ciho.control.model == "Tkjkm") {
        strData += "\"bh\":\"" + strBh + "\"";
        strData += ",\"parent\":\"1002\"";
        strData += ",\"level\":\"2\"";
    }
    else if (ciho.control.model == "Tkpdwxx") {
        strData += "\"a2\":\"" + strBh + "\"";
        strData += ",\"a10\":\"" + parseFloat($("#ciho_dksl", "#" + ciho.layal.id).val()) / 100 + "\"";
        strData += ",\"a11\":\"" + parseFloat($("#ciho_kpsl", "#" + ciho.layal.id).val()) / 100 + "\"";
    }
    else if (ciho.control.model == "Tspxx") {
        strData += "\"a2\":\"" + strBh + "\"";
        strData += ",\"a13\":\"\"";
        strData += ",\"a14\":\"\"";
        strData += ",\"a15\":\"\"";
        strData += ",\"a23\":\"\"";
    }
    else {
        strData += "\"a2\":\"" + strBh + "\"";
    }
    strData += ",\"a3\":\"" + mc + "\"";
    strData += ",\"zt\":\"true\"";
    strData += "}";
    objRecult = ParseData(window.external.DoOptions(ciho.control.model, "Add", strData));

    if (objRecult.ReturnCode != 0) {
        window.parent.cihoAlert(objRecult.MSG);
        return;
    }
    strID = objRecult.id;

    //更新下拉框的值
    if (ciho.control.model == "Tkjkm") {
        changeComboZt("TKjkm");
    }
    else {
        changeComboZt(ciho.control.model);
    }
    window.parent.refreshCombobox(null);

    switch (ciho.control.model) {
        case "Tgysxx":
            strDm = objRecult.gysdm;
            break;
        case "TZyxx":
            strDm = objRecult.zydm;
            break;
        case "TCkxx":
            strDm = objRecult.ckdm;
            break;
        case "Tkhxx":
            strDm = objRecult.khdm;
            break;
        case "Tspxx":
            strDm = objRecult.spdm;
            break;
        case "Tkjkm":
            strDm = objRecult.kmdm;
            break;
        case "Tkpdwxx":
            strDm = objRecult.kpdwdm;
            break;
        default:

    }
    if (ciho.control.model == "Tkhxx") {
        newData = getComboboxData(ciho.control.model + "1");
    }
    else if (ciho.control.model == "Tkjkm") {
        newData = getComboboxData("TKjkm");
    }
    else {
        oldData = ciho.getComboDataAllStop(ciho.control.model).Records;
        newData = getComboboxData(ciho.control.model);
    }
    newData.push({ "dm": "-1", "mc": "<font color='red'>添加" + ciho.layal.title + "</font>" });

    if (ciho.control.model == "Tkjkm") {
        ciho.control.jquery.combobox({
            panelHeight: getComHeight(newData)
        });
    }

    ciho.control.jquery.combobox("loadData", newData);
    ciho.control.jquery.combobox('setValue', strDm);

    //对于一个画面有两个相同的控件的问题（预提）
    if (ciho.control.other) {
        otherDm = ciho.control.other.combobox("getValue");
        ciho.control.other.combobox("loadData", newData);
        ciho.control.other.combobox("setValue", otherDm);
    }

    //重新取一下名称
    mc = rtrim(ltrim($("#ciho_mc", "#" + ciho.layal.id).val()));

    //更新基本资料
    var objFrames = window.parent.frames;
    for (var i = 0; i < objFrames.length; i++) {
        if (objFrames[i] == objFrames["idIframe-601"] && ciho.control.model == "Tgysxx") {
            $(objFrames["idIframe-601"].document).find("#Tgysxx").addRowData(strID, { "a2": strBh, "gysdm": strDm, "a3": mc, "zt": "True" });
        }
        else if (objFrames[i] == objFrames["idIframe-602"] && ciho.control.model == "Tspxx") {
            $(objFrames["idIframe-602"].document).find("#TSpxx").addRowData(strID, { "a2": strBh, "spdm": strDm, "a3": mc, "zt": "True" });
        }
        else if (objFrames[i] == objFrames["idIframe-603"] && ciho.control.model == "Tkhxx") {
            $(objFrames["idIframe-603"].document).find("#Tkhxx").addChildNode(strBh, null, { "bh": strBh, "khdm": strDm, "a3": mc, "zt": "True" });
        }
        else if (objFrames[i] == objFrames["idIframe-605"] && ciho.control.model == "TZyxx") {
            $(objFrames["idIframe-605"].document).find("#TZyxx").addRowData(strID, { "a2": strBh, "zydm": strDm, "a3": mc, "zt": "True" });
        }
        else if (objFrames[i] == objFrames["idIframe-610"] && ciho.control.model == "TCkxx") {
            $(objFrames["idIframe-610"].document).find("#TCkxx").addRowData(strID, { "a2": strBh, "ckdm": strDm, "a3": mc, "zt": "True" });
        }
        else if (objFrames[i] == objFrames["idIframe-606"] && ciho.control.model == "Tkjkm") {
            //$(objFrames["idIframe-606"].document).find("#TKjkm").addChildNode(strBh, "1002", { "bh": strBh, "kmdm": strDm, "a3": mc, "zt": "True", "a16": "0", "parent": "1002", "level": "2" });
            window.parent.arrRefresh.push({ page: "ciho", flag: "1" });
        }
        else if (objFrames[i] == objFrames["idIframe-609"] && ciho.control.model == "Tkpdwxx") {
            $(objFrames["idIframe-609"].document).find("#Tkpdwxx").addRowData(strID, { "a2": strBh, "kpdwdm": strDm, "a3": mc, "a10": parseFloat($("#ciho_dksl", "#" + ciho.layal.id).val()) / 100, "a11": parseFloat($("#ciho_kpsl", "#" + ciho.layal.id).val()) / 100, "zt": "True" });
        }
        else if (objFrames[i] == objFrames["idIframe-607"] && (ciho.control.model == "Tqcwh" || ciho.control.model == "Tkjkm")) {
            window.parent.arrRefresh.push({ page: "ciho", flag: "1" });
        }
    }
    cihoDlgClose(ciho.layal.id);

}

/*取全局变量包括停用的值*/
ciho.getComboDataAllStop = function () {
    switch (ciho.control.model) {
        case "Tgysxx":
            return window.parent._arrCmbGys2;
            break;
        case "TCkxx":
            return window.parent._arrCmbCk2;
            break;
        case "Tspxx":
            return window.parent._arrCmbYp2;
            break;
        case "TZyxx":
            return window.parent._arrCmbZy2;
            break;
        case "Tkhxx":
            return window.parent._arrCmbKh2;
            break;
        case "Tkjkm":
            return window.parent._arrCmbKjkm2;
            break;
        case "TKjkm":
            return window.parent._arrCmbKjkm2;
            break;
        case "Tkpdwxx":
            return window.parent._arrCmbKpdw2;
            break;
        default:

    }
}
