/*************************
*功能：客户基本资料
*创建人：张振华
*时间：2010-08-16
*备注：
*************************/

var strSelRowIdP0603;   //选中行ID
var objSelRowP0603; //行对象
var strTableNameP0603 = "Tkhxx";
var strDialogP0603 = "divP0603";
var strBhP0603; //编号
var strOpraType = "1";  //新增(1), 修改(2), 新增下一级(3), 删除(4)

//修改状态
function setZtP0603() {

    //一行数据
    var objResult;

    //改变状态
    if (objSelRowP0603.zt == "已启用") {
        objResult = updateDBState(strTableNameP0603, objSelRowP0603.bh, "False", "khdm");
        if (objResult != null && objResult.ReturnCode == "0") {
            $("#" + strTableNameP0603).setCell(objSelRowP0603.bh, "zt", "False");
            changeComboZt("Tkhxx")
        }

    }
    else {
        objResult = updateDBState(strTableNameP0603, objSelRowP0603.bh, "True", "khdm");
        if (objResult != null && objResult.ReturnCode == "0") {
            $("#" + strTableNameP0603).setCell(objSelRowP0603.bh, "zt", "True");
            changeComboZt("Tkhxx")
        }
    }

    //清除内存
    objResult = null;
}

//格式化状态
function Formatter(cellvalue, options, rowObject) {

    if (cellvalue == null || cellvalue == "True") {
        return "已启用"
    }
    else if (cellvalue == "False") {
        return "已停用"
    }
}

//单击行
function selectRow_P0603(rowId) {
    //选中行ID
    strSelRowIdP0603 = rowId;

    //选中行对象
    objSelRowP0603 = $("#" + strTableNameP0603).getRowData(strSelRowIdP0603);

    //当前行编号
    strBhP0603 = objSelRowP0603.bh;

    //浮动按钮
    var objResult = { addNext: true, update: true, delet: true, setZt: true };

    //创建浮动层
    var btnHtml = "";
    // debugger
    //父节点
    var objParent = $("#" + strTableNameP0603).getNodeParent(objSelRowP0603);
    var objSons = $("#" + strTableNameP0603).getNodeChildren(objSelRowP0603);

    //状态停用不能增加下一级
    if (objSelRowP0603.zt == "已停用" || objSelRowP0603.zt == "False") {
        objResult.addNext = false;
    }

    //只能增加到二级
    if (objParent != null) {
        objResult.addNext = false;
    }

    //子节点都停用了父节点才能停用
    for (var i = 0; i < objSons.length; i++) {
        if (objSons[i].zt == "已启用" || objSons[i].zt == "True") {
            objResult.setZt = false;
        }
    }

    //有子节点的不能删除
    if (objSons.length != 0) {
        objResult.delet = false;
    }


    // 组成浮动条

    //判断该节点是否是特殊值
    if (objSelRowP0603.isLeaf == "false") {

        //当前节点是父节点
        var bIsAllStop = true;

        //判断停用启用
        for (i = 0; i < objSons.length; i++) {
            if (objSons[i].zt == "True" || objSons[i].zt == "已启用") {
                bIsAllStop = false;
            }
        }
        if (bIsAllStop == false) {
            objResult.setZt = false;
        }
        objResult.delet = false;

    }
    else {

        if (objParent == null) {
            //当前节点是顶级节点
            objResult.setZt = true;
        }
        else {
            //当前节点是子节点
            if (objParent.zt == "True" || objParent.zt == "已启用") {
                objResult.setZt = true;
            }
            else {
                objResult.setZt = false;
            }
        }

    }


    if (objResult.addNext == true) {
        btnHtml += "<a href=\"#\" onclick=\"showDialog_P0603('3')\">新增下一级</a>";
    }

    if (objResult.update == true) {
        if (btnHtml == "") {
            btnHtml = "<a href=\"#\" onclick=\"showDialog_P0603('2');return false;\">修改</a>";
        }
        else {
            btnHtml += "-<a href=\"#\" onclick=\"showDialog_P0603('2');return false;\">修改</a>";
        }
    }

    //debugger
    if (objResult.setZt == true) {
        if (btnHtml == "") {
            if (objSelRowP0603.zt == "已启用" || objSelRowP0603.zt == "True") {
                btnHtml = "<a href=\"#\" onclick=\"setZtP0603();return false;\">停用</a>";
            }
            else {
                btnHtml = "<a href=\"#\" onclick=\"setZtP0603();return false;\">启用</a>";
            }

        }
        else {
            if (objSelRowP0603.zt == "已启用" || objSelRowP0603.zt == "True") {
                btnHtml += "-<a href=\"#\" onclick=\"setZtP0603();return false;\">停用</a>";
            }
            else {
                btnHtml += "-<a href=\"#\" onclick=\"setZtP0603();return false;\">启用</a>";
            }

        }

    }

    if (objResult.delet == true) {
        btnHtml += "-<a href=\"#\" onclick=\"deleteData_P0603('4');return false;\">删除</a>";
    }

    //回调
    createFloatButton.call(this, rowId, btnHtml);
}

//重新加载数据
function reloadData(tbId) {
    var s = ParseData(window.external.DoOptions(tbId, 'GetList', '{}'));
    loadTreeData(tbId, s.Records);
}

//释放内存
function freeMemory_P0603() {
    strBhP0603 = null;
    objSelRowP0603 = null;
    strSelRowIdP0603 = null;
}

//弹出对话框
function showDialog_P0603(strType) {
    try {

        //双击图标时直接退出
        if (strType != "1" && objSelRowP0603 == null) {
            return;
        }

        //保存标志位
        strOpraType = strType;

        //创建空层
        if (strType == "1") {
            //新增
            creatDiv_ShowDialog(strDialogP0603, "客户信息", false, true, strTableNameP0603);
        }
        else if (strType == "2") {
            //修改
            creatDiv_ShowDialog(strDialogP0603, "客户信息", false, true, strTableNameP0603);
        }
        else if (strType == "3") {
            //新增下一级 有所属
            creatDiv_ShowDialog(strDialogP0603, "客户信息", true, true, strTableNameP0603);

            var strHtml = "<h3 id='idH3'>所属:";
            strHtml += trim(objSelRowP0603.a3);
            strHtml += "</h3>";

            $("#" + strDialogP0603 + "_pop_box_top").append(strHtml);
        }

        //向弹出层里添加内容
        creatDialogItem_P0603(strType);
        setTimeout(function () {
            $("#a3", $("#" + strDialogP0603 + "_divform")).focus();
        }, 100);
    }
    catch (e) {
        proceError("P0603", e.ToString());
    }
}

//加载分类的下拉框的数据
function LoadSelectData(strSelectId) {

    var objType = ParseData(window.external.DoOptions("TFlsz", "GetList", "{\"a5\":\"1\"}"));
  
    $("#lxdm").combobox({
        textField: "a3",
        valueField: "lxdm",
        editable: false,
        afterChange: function (newValue, newText) {
            $("#" + strDialogP0603 + "_divform").data("change", true);
            if ("-1" == newValue) {
                $(".layoutpanel").css("display", "none");
                $("#lxdm").combobox("setValue", "");        //清空当前选择信息
                showDialog_P06020101("11", 0); //添加分类窗口
            }
        }
    });
    objType.Records.unshift({ a3: "", lxdm: "" });
    objType.Records.push({ lxdm: "-1", a3: "<font color='red'>添加客户分类</font>" });

    if (objType.Records != "{}") {
        $("#lxdm", "#" + strDialogP0603).combobox("loadData", objType.Records);
    }
    // $(".layoutpanel").css("z-index", "50000009");

    //添加分类后，选中该分类
    if (strSelectId != undefined && strSelectId != "") {
        $("#lxdm", "#" + strDialogP0603).combobox("setValue", strSelectId);
    }
    else {
        $("#lxdm", "#" + strDialogP0603).combobox("setValue", "");
    }

    //清楚内存
    objType = null;
}

//向弹出层里添加内容
function creatDialogItem_P0603(strType) {

    //表格对象
    var objGrid = $("#" + strTableNameP0603);

    //层对象
    var strDivForm = "#" + strDialogP0603 + "_divform";
    var objDivForm = $(strDivForm);

    //表格的列索引
    var objNames = objGrid.jqGrid('getGridParam', "colNames");
    var objIndexs = objGrid.jqGrid('getGridParam', "colModel");

    //必填项红色*号
    var sign = "";

    //动态html
    var strForm = "";
    strForm += "<ul>";

    for (var i = 1; i < objNames.length; i++) {

        //过滤显示列
        if (objIndexs[i]["name"] == "id" || objIndexs[i]["name"] == "idx" || objIndexs[i]["name"] == "khdm"
        || objIndexs[i]["name"] == "isleaf" || objIndexs[i]["name"] == "lbmc"
        || objIndexs[i]["name"] == "isLeaf" || objIndexs[i]["name"] == "parent"
        || objIndexs[i]["name"] == "level" || objIndexs[i]["name"] == "zt" || objIndexs[i]["name"] == "bh") {
            continue;
        }

        //判断是否必填，加样式
        if (objIndexs[i]["editrules"] != null) {
            if (objIndexs[i]["editrules"].required == true) {
                sign = "<font color='red'>*</font>";
            }
            else {
                sign = "";
            }
        }

        //处理编号
        if (objIndexs[i]["name"] == "a1") {

            if (strType == "1") {           //新增
                strBhP0603 = ParseData(external.DoOptions(strTableNameP0603, 'MakeBH', '{}'));
            }
            else if (strType == "3") {      //新增下一级
                strBhP0603 = ParseData(external.DoOptions(strTableNameP0603, 'MakeBH', '{"ParentBH":"' + objSelRowP0603.bh + '"}'));
            }
            strForm += "<li>";
            strForm += objNames[i] + "：";
            strForm += "<input type='text'  name='" + objIndexs[i]["name"] + "'";
            strForm += " value='" + strBhP0603.a1 + "'";
            strForm += " id='" + objIndexs[i]["name"] + "'   maxlength='10' >";
            strForm += "</li>";
            strBhP0603 = strBhP0603.BH;
            continue;
        }

        if (objIndexs[i]["edittype"] == "text") {
            strForm += "<li>";
            strForm += sign;
            strForm += objNames[i] + "：";
            strForm += "<input type='text'  value=''  name='" + objIndexs[i]["name"] + "'";
            strForm += " id='" + objIndexs[i]["name"] + "' maxlength='50'"; //限制输入长度
            strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>"; //限制输入，粘贴特殊字符
            strForm += "</li>";

        }
        else if (objIndexs[i]["edittype"] == "select") {
            if (objIndexs[i]["name"] == "lxdm") {
                strForm += "<li>";
                strForm += sign;
                strForm += objNames[i] + "：";
                strForm += "<select name='" + objIndexs[i]["name"] + "' id='" + objIndexs[i]["name"] + "'";
                strForm += " class='easyui-combobox' >";
                strForm += "</select>";
                strForm += "</li>";
            }
        }

    }
    strForm += "</ul>";

    //向层里添加html
    objDivForm.append(strForm);

    //加载下拉框数据
    LoadSelectData();

    //修改的时候加载数据
    if (strType.toString() == '2') {
        for (var j = 0; j < objIndexs.length; j++) {
            if (objIndexs[j]["name"] == "a3") {
                $('#' + objIndexs[j].name, strDivForm).val($(objGrid.getCell(strSelRowIdP0603, j)).text());
            }
            else if (objIndexs[j]["name"] == "lxdm") {

                var strTypeName = objGrid.jqGrid('getCell', strSelRowIdP0603, j);
                if (strTypeName != "-1" && strTypeName != "" && strTypeName != "undefined") {
                    $("#lxdm").combobox('setValue', strTypeName);
                }

            }
            else if (objIndexs[j]["name"] == "bh") {
                strBhP0603 = objGrid.getCell(strSelRowIdP0603, j);
            }
            else {
                $('#' + objIndexs[j].name, strDivForm).val(objGrid.getCell(strSelRowIdP0603, j));
            }
        }

    }

    //当前层设置状态
    $(":input", strDivForm).change(function () {
        $("#" + strDialogP0603 + "_divform").data("change", true);
    });
    $("#" + strDialogP0603 + "_divform").data("change", false);

    //清除内存
    objGrid = null;
    strDivForm = null;
    objDivForm = null;
    objNames = null;
    objIndexs = null;
}

//a11设置focus
function setFocusA11() {
    $("#a11").focus();
}

//a3设置focus
function setFocus() {
    $("#a3").focus();
}

//验证表单
function formDataValidate(strDialog, strType) {

    //验证必填项
    if (false == dataRequired(strTableNameP0603, strDialogP0603)) {
        return false;
    }

    //验证邮箱
    if (trim($("#a11").val()) != "") {
        if (false == checkEmail(trim($("#a11").val()))) {
            window.parent.cihoAlert(msg.other.emailTypeErr, null, setFocusA11);
            return false;
        }
    }

    //判断名称是否重复
    var objTable = $("#" + strTableNameP0603);
    var arrIds = objTable.jqGrid("getDataIDs");
    var strMc = trim($("#a3", $("#" + strDialogP0603 + "_divform")).val());

    for (var i = 0; i < arrIds.length; i++) {
        var objTr = objTable.getRowData(arrIds[i]);
        if (strOpraType != "2") {
            
            //新增和新增下一级的时候
            if (strMc == trim(objTr.a3)) {
                window.parent.cihoAlert(msg.sz.kh.nameQqual, null, setFocus);
                return false;
            }
        }
        else {
            //修改的时候
            if (strMc == trim(objTr.a3) && arrIds[i] != objSelRowP0603.bh) {
                window.parent.cihoAlert(msg.sz.kh.nameQqual, null, setFocus);
                return false;
            }
        }
    }
    return true;

}

//保存数据
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tkhxx") {
            saveData_P0603(strDialog, strType);
        }
        else if (strTable == "TFlsz") {
            saveData_P06020101(strType);
        }
    }
    catch (e) {
        proceError("P0603", e.ToString());
    }
}

//拼接保存字符串
function GetDataString() {

    var strJsonString = getSaveDataInfo(strDialogP0603);
    var strA3 = "";
    if ($("#lxdm").combobox('getValue')) {
        strA3 = ",\"lxdm\":" + "\"" + $("#lxdm").combobox('getValue') + "\"";
    }

    strJsonString = strJsonString.replace("}", strA3);

    if (strOpraType == "1") {
        //新增
        strJsonString += ",\"parent\":" + "\"0\"";
    }
    else if (strOpraType == "3") {
        //新增下一级
        strJsonString += ",\"parent\":" + "\"" + objSelRowP0603.bh + "\"";
        strJsonString += ",\"level\":" + "\"" + (parseFloat(objSelRowP0603.level) + 1) + "\"";
    }
    else {
        //修改
        strJsonString += ",\"khdm\":" + "\"" + objSelRowP0603.khdm + "\"";

    }
    strJsonString += ",\"bh\":\"" + strBhP0603 + "\"";

    //组状态
    if (strOpraType == "1") {
        //新增
        strJsonString += ",\"zt\":\"True\"";
    }
    else if (strOpraType == "2") {
        //修改
        if (objSelRowP0603.zt == "True" || objSelRowP0603.zt == "已启用") {
            strJsonString += ",\"zt\":\"True\"";
        }
        else {
            strJsonString += ",\"zt\":\"False\"";
        }
    }
    else {
        //新增下一级
        strJsonString += ",\"zt\":\"True\"";
    }

    strJsonString += "}";

    return strJsonString;
}

//保存到数据库中
function saveDataToDbP0603(strDialog, strType) {
    //验证通过保存数据
    var strRecord = GetDataString();
    var objResult;
    if (strOpraType == "1") {
        //新增
        objResult = saveData(strTableNameP0603, strRecord, "1");

        if (objResult.ReturnCode != 0) {
            window.parent.cihoAlert(objResult.MSG);
            return;
        }
        $("#" + strDialogP0603).data("change", false);
        $("#" + strTableNameP0603).addChildNode(strBhP0603, null, ParseData(strRecord));
        resizeGridScroll(strTableNameP0603);
        $("#" + strTableNameP0603).setCell(strBhP0603, "khdm", objResult.khdm);

    }
    else if (strOpraType == "3") {
        //新增下一级
        objResult = saveData(strTableNameP0603, strRecord, "1");
        if (objResult.ReturnCode != 0) {
            window.parent.cihoAlert(objResult.MSG);
            return;
        }
        $("#" + strTableNameP0603).addChildNode(strBhP0603, objSelRowP0603.bh, ParseData(strRecord), "after", $(objSelRowP0603.idx).text());
        resizeGridScroll(strTableNameP0603);
        $("#" + strTableNameP0603).setCell(strBhP0603, "khdm", objResult.khdm);

        //新增下一级之后父节点isLeaf  属性可能改变

        objSelRowP0603.isLeaf = "false";
        $("#" + strTableNameP0603).setRowData(objSelRowP0603.bh, objSelRowP0603);

    }
    else {
        //修改
        objResult = saveData(strTableNameP0603, strRecord, "2");

        if (objResult.ReturnCode != 0) {
            window.parent.cihoAlert(objResult.MSG);
            return;
        }
        $("#" + strTableNameP0603).setRowData(strBhP0603, ParseData(strRecord));

        var objParent = $("#" + strTableNameP0603).getNodeParent(objSelRowP0603);
        if (objParent != null) {
            objSelRowP0603.bh = objParent.bh;
        }

        strOpraType = "1";
    }

    //在表格中设置类型名称
    $("#" + strTableNameP0603).setCell(objResult.bh, "lbmc", $("#lxdm").combobox('getText'));

    //修改全局变量的状态
    changeComboZt("Tkhxx");

    if (strType == "2") {
        //保存新增
        $(":input", $("#" + strDialog + "_divform")).val("");

        if (strOpraType == "1") {
            //新增
            strBhP0603 = ParseData(external.DoOptions(strTableNameP0603, 'MakeBH', '{"ParentBH":""}'));
        }
        else {
            //新增下一级
            strBhP0603 = ParseData(external.DoOptions(strTableNameP0603, 'MakeBH', '{"ParentBH":"' + objSelRowP0603.bh + '"}'));
        }
        $("#a1").val(strBhP0603.a1);
        strBhP0603 = strBhP0603.BH;
        $("#" + strDialogP0603 + "_divform").data("change", false);
        $("#a3", $("#" + strDialogP0603 + "_divform")).focus();
    }
    else {
        //保存退出
        cihoDlgClose(strDialog);
    }

    //清除内存
    strRecord = null;
}

//保存到数据库
function saveData_P0603(strDialog, strType) {
    
    //验证编号重复
    var objTable = $("#" + strTableNameP0603);
    var arrIds = objTable.jqGrid("getDataIDs");
    var strMc = trim($("#a3", $("#" + strDialogP0603 + "_divform")).val());

    for (var i = 0; i < arrIds.length; i++) {
        var objTr = objTable.getRowData(arrIds[i]);

        //修改的时候，判断不是当前ID
        if (strOpraType == "2" && objSelRowP0603 && objSelRowP0603.bh && objSelRowP0603.bh == arrIds[i]) 
        continue;

        //判断编号是否重复
        if ($("#a1", "#" + strDialogP0603).val() != "" && trim($("#a1", "#" + strDialogP0603).val()) == objTr.a1) {
            window.parent.cihoAlert(msg.sz.kh.codeQqual, msgBox.dlgBtn.YesNo, function (result) {
                if (result == msgBox.dlgRst.Yes) {
                   
                    //验证
                    if (formDataValidate(strDialog, strType) == false) {
                        return;
                    }
                    //保存信息
                    saveDataToDbP0603(strDialog, strType);
                }
                else {
                    $("#a1").focus();
                }
            });
            return false;
        }
    }


    //验证
    if (formDataValidate(strDialog, strType) == false) {
        return;
    }
    saveDataToDbP0603(strDialog, strType);

    //释放内存
    objTable = null;
}

//关闭对话框
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, strType, strTable);
                }
                else if (msgBox.dlgRst.No == strRet) {
                    cihoDlgClose(strDialog);
                    freeMemory_P0603();
                }
                else if (msgBox.dlgRst.Cancel == strRet) {
                    return;
                }
            }
        );

    }
    else {
        //释放内存
        cihoDlgClose(strDialog);
        if (strDialog != "divP06030101") {
            freeMemory_P0603();
        }
    }

}

//删除一行数据
function deleteData_P0603() {
    try {
        //取得要删除数据的名称
        var objCurrent = $("#" + strTableNameP0603).getRowData(objSelRowP0603.bh);

        if ($("#" + strTableNameP0603).getNodeChildren(objCurrent).length != 0) {
            window.parent.cihoAlert(msg.del.haveChild);
            return;
        }
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {

                //删除的数据
                var strJsonData = "{\"khdm\":\"" + $("#" + strTableNameP0603).jqGrid("getCell", objSelRowP0603.bh, "khdm") + "\"}";

                //删除
                objResult = ParseData(window.external.DoOptions(strTableNameP0603, 'Delete', strJsonData));

                if (objResult == null || objResult.ReturnCode == undefined) {
                    window.parent.cihoAlert(msg.del.delFail);
                    return;
                }

                //保存失败时弹出提示信息
                if (objResult.ReturnCode != 0) {
                    window.parent.cihoAlert(objResult.MSG);
                    return;
                }
                var objParent = $("#" + strTableNameP0603).getNodeParent(objCurrent);
                $("#" + strTableNameP0603).jqGrid("delRowData", objSelRowP0603.bh);
                resizeGridScroll(strTableNameP0603);

                if (objParent && $("#" + strTableNameP0603).getNodeChildren(objParent).length == 0) {
                    reloadData(strTableNameP0603);
                }
                //修改全局的状态
                changeComboZt("Tkhxx");
            }
        });
    }
    catch (e) {
        proceError("P0603", e.ToString());
    }
}

//双击行事件
function funcDbClick(rowId) {
    //弹出修改对话框
    showDialog_P0603("2");
}






