/*------------------------文档信息---------------------------
///功能:      职员基本信息
///创建人：    ----------
///创建时间：   ---------
///修改人：     zhaixl
///修改时间：    2011.5.27
///详细说明：  本js文档在修改，新增，设置状态，加载浮动层中使用；
///             公共变量：savesign:1,保存；2，修改。
------------------------------------------------------------*/

var strTableNameP0605 = "TZyxx";        //表格ID
var strDialogId = "divP0605";           //职员信息编辑窗口
var selRowId = 0;                      //行ID

/*-----------------------------------------------------------------------------
//功能：点击添加按钮，弹出对话框
//参数：
//strType:1：添加
//2：修改
-----------------------------------------------------------------------------*/
function alterRecord(strType) {
    try {
        $(".operate").hide();
        $("#" + strDialogId).empty();
        //添加
        if (strType == "1") {
            selRowId = "0";
            savesign = "1"; //初次保存insert数据
            //创建弹出添加窗口按钮及风格 common.js
            creatDiv_ShowDialog(strDialogId, "职员信息", false, true, strTableNameP0605);
        }
        //修改
        else if (strType == "2") {
            selRowId = jQuery('#' + strTableNameP0605).jqGrid('getGridParam', 'selrow');
            savesign = "2"; //update数据        

            if (selRowId == "" || selRowId == null) {
                window.parent.cihoAlert(msg.edit.selEditRow);
                return;
            }
            //创建弹出修改窗口按钮及风格
            creatDiv_ShowDialog(strDialogId, "职员信息", false, true, strTableNameP0605);
        }
        //添加表单到窗口中
        tableFormZyjbxx(strDialogId, strType);
        setTimeout(function () { $("#a3", $("#" + strDialogId + "_divform")).focus(); }, 100);
    } catch (e) { }
}

//生成表单，修改时，添加数据
function tableFormZyjbxx(dlgId, strType) {
    var names = $('#' + strTableNameP0605).jqGrid('getGridParam', "colNames");
    var indexs = $('#' + strTableNameP0605).jqGrid('getGridParam', "colModel");
    var sign = "";
    //表单开始
    var strForm = "";
    var strAppendTmp = "";
    strForm += "<ul>";
    for (var i = 1; i < names.length; i++) {
        strAppendTmp = "";
        //必填项，标识为红色星号
        if (indexs[i]["editrules"].required == true) {
            sign = "<font color='red'>*</font>";
        }
        else {
            sign = "";
        }
        //以下子段不显示a1 a5 gl a10  zt bmmc
        if (indexs[i]["name"] == "id" || indexs[i]["name"] == "zydm" || indexs[i]["name"] == "a5"
         || indexs[i]["name"] == "gl" || indexs[i]["name"] == "a10" || indexs[i]["name"] == "zt" || indexs[i]["name"] == "bmmc") {
            continue;
        }
        //编号自动生成，不可编辑
        if (indexs[i]["name"] == "a2") {
            var strBh = "";            
            if (strType == "1") {   //新增
                //系统生成编号
                var strbh = ParseData(window.external.DoOptions(strTableNameP0605, 'MakeBH', '{}'));
                strBh = strbh.BH;
            }
            else {//修改
                strBh = jQuery('#' + strTableNameP0605).jqGrid('getCell', selRowId, i);
            }
            strAppendTmp += "<li>";
            strAppendTmp += names[i] + "：";
            strAppendTmp += "<input type='text'  name='" + indexs[i]["name"] + "'";
            strAppendTmp += " value='" + strBh + "'";
            strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='10'";
            strAppendTmp += "onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>";            
            strAppendTmp += "</li>";
            strForm += strAppendTmp;
            continue;
        }
        //text框判断。
        if (indexs[i]["edittype"] == "text") {
            strAppendTmp += "<li>";
            strAppendTmp += sign;
            strAppendTmp += names[i] + "：";
            strAppendTmp += "<input type='text' value='' name='" + indexs[i]["name"] + "'";
            strAppendTmp += " id='" + indexs[i]["name"] + "' maxlength='50'"; //限制输入长度
            strAppendTmp += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'>"; //限制输入，粘贴特殊字符
            strAppendTmp += "</li>";
            //}
        }       
        else if (indexs[i]["edittype"] == "select") {   //下拉框            
            if (indexs[i]["name"] == "bmdm") {//部门
                strAppendTmp += "<li>";
                strAppendTmp += sign + names[i] + "：";
                strAppendTmp += "<select name='" + indexs[i]["name"] + "' id='" + indexs[i]["name"] + "'";
                strAppendTmp += "class='easyui-combobox' editable=\"false\">";
                strAppendTmp += "</select>";
                strAppendTmp += "</li>";
            }
        }
        strForm += strAppendTmp;
    } //FOR循环结束
    strForm += "</ul>";
    $("#" + dlgId + "_divform").append(strForm);
    //部门combobox
    $("#bmdm").combobox();
    var objBmdm = getComboboxData('Tbmxx');
    $("#bmdm", "#" + dlgId + "_divform").combobox({
        valueField: "dm",
        textField: "mc"
    });
    objBmdm.unshift({ "dm": "", "mc": "" });
    $("#bmdm", "#" + dlgId + "_divform").combobox("loadData", objBmdm);
    $(".layoutpanel").css("z-index", "50000009");
    //check电话
    checkphone(dlgId);
    //添加日期选择控件
    inputAndBindData("a6", false, "#" + dlgId, dateChange_P0605);
    inputAndBindData("a7", false, "#" + dlgId, dateChange_P0605);
    //修改时，加载value
    if (strType.toString() == '2') {
        for (var i = 0; i < indexs.length; i++) {
            if (indexs[i]["name"] == "bmdm") {
                var strBmName = $("#" + strTableNameP0605).jqGrid('getCell', selRowId, i);
                $("#bmdm").combobox("setValue", strBmName);
            }
            $('#' + indexs[i].name, '#' + dlgId + '_divform').val(jQuery('#' + strTableNameP0605).jqGrid('getCell', selRowId, i));

        }
    }
    $("#" +dlgId).dialog("option", "position", "center");
    //form change设定    
    $(":input", $("#" + dlgId + "_divform")).change(function () {
        $("#" + dlgId + "_divform").data("change", true);
    });
    //combobox 选择事件
    $("#bmdm").combobox({
        onChange: function (option) {
            $("#" + dlgId + "_divform").data("change", true);
        }
    });
    strForm = null;
    strAppendTmp = null;
    names = null;
    indexs = null;
    sign = null;
    objBmdm = {};
}

//日期控件变更,datachange
function dateChange_P0605() {
    //修改Data change状态
    $("#" + strDialogId + "_divform").data("change", true);
}

//check form中的数据是否改变
function closeMyDlg(dlgId, type, strTableName) {
    //数据有变化
    if ($("#" + dlgId + "_divform").data("change")) {
        window.parent.cihoAlert(
                msg.other.haveChange,
                 msgBox.dlgBtn.YesNoCancel,
                 function (strRet) {
                     if (msgBox.dlgRst.Yes == strRet) {
                         saveMyData(dlgId, type, strTableName);
                     }
                     else if (msgBox.dlgRst.No == strRet) {
                         cihoDlgClose(dlgId);
                     }
                 });
    }
    else {//0：关闭。关闭编辑窗口
        cihoDlgClose(dlgId);
    }
}

//保存时，验证数据
function saveMyData(dlgId, type, strTableName) {
    if (true == formDataCheck(strTableName, dlgId,type)) {
        saveP0605(dlgId, strTableName, type);
    }       
    strTableName = null;
    dlgId = null;
    type = null;
}
/*-----------------------------------------------------------------------------
//数据有效性验证：
//              必填项，是否为空；
//              数据格式是否正确。
//正常：返回true
//      否则false
-----------------------------------------------------------------------------*/
function formDataCheck(strTableName, dlgId,savetype) {
    //check必填项是否为空
    if (false == dataRequired(strTableName, dlgId)) {
        return false;
    }
    //验证邮箱格式是否正确
    var strMail = trim($("#a11", $("#" + dlgId + "_divform")).val());
    if (strMail != "") {
        var strRet = checkEmail(strMail);
        if (strRet == false) {
            window.parent.cihoAlert(msg.other.emailTypeErr, null, function () { $("#a11", $("#" + dlgId + "_divform")).focus(); });
            return false;
        }
    }
    var arrIds = $("#" + strTableNameP0605).jqGrid("getDataIDs");
    //check编号是否有重复
    var strBh = trim($("#a2", $("#" + dlgId + "_divform")).val());
    //check职员名称是否重复
    var strZymc = trim($("#a3", $("#" + dlgId + "_divform")).val());
    for (var i = 0; i < arrIds.length; i++) {
        if (strZymc == $("#" + strTableNameP0605).jqGrid("getCell", arrIds[i], "a3") && arrIds[i] != selRowId) {
            window.parent.cihoAlert(msg.sz.zy.nameQqual, null, function () { $("#a3", $("#" + dlgId + "_divform")).focus(); });
            return false;
        }
    }
    for (var intI = 0; intI < arrIds.length; intI++) {
        if (strBh!=""&&strBh == $("#" + strTableNameP0605).jqGrid("getCell", arrIds[intI], "a2") && arrIds[intI] != selRowId) {
            window.parent.cihoAlert(msg.sz.zy.codeQqual, msgBox.dlgBtn.YesNo, function (result) {
                if (result == msgBox.dlgRst.Yes) {
                    saveP0605(dlgId, strTableNameP0605, savetype);
                }
                else{
                    $("#a2", $("#" + dlgId + "_divform")).focus();
                }
            });
            return false;
        }
    }
    return true;
}

//保存数据
function saveP0605(dlgId, strTableName, saveType) {
    var strRecord = getSaveDataInfo(dlgId);
    var strJsonData = "";
    var strBmdm = "";
    if ($("#bmdm").combobox("getValue") != null) {
        strBmdm = $("#bmdm").combobox('getValue');
    }
    else {
        strBmdm = "";
    }
    var myStr = ",\"bmdm\":" + "\"" + strBmdm + "\"";
    strRecord = strRecord.replace("}", myStr);
    if (savesign == "1") {  //新增时的json串
        strRecord += "}";
        strJsonData = strRecord;
    }
    else if (savesign == "2") {     //修改时的json串
        strJsonData = strRecord + ",\"zydm\":\"" + $("#" + strTableName).jqGrid("getCell", selRowId, "zydm") + "\"}";
    }
    var strRet = saveData(strTableName, strJsonData, savesign);
    //selRowid = strRet.id;
    if (strRet.ReturnCode == "0") {
        if (savesign == "1") {
            selRowId = strRet.id;
            savesign = "2";
            $("#" + strTableName).jqGrid("addRowData", selRowId, ParseData(strJsonData), "first");
            $("#" + strTableName).jqGrid("setCell", selRowId, "zt", "已启用");
            $("#" + strTableName).jqGrid("setCell", selRowId, "zydm", strRet.zydm);
            //设定selet控件的选中的值和工龄到jqGrid中
            var strgl = formatterGl();
            $("#" + strTableName).jqGrid("setCell", selRowId, "gl", strgl);
            $("#" + strTableName).jqGrid("setCell", selRowId, "bmmc", $("#bmdm").combobox('getText'));
        }
        else {
            $("#" + strTableName).jqGrid("setRowData", selRowId, ParseData(strJsonData));
            //设定selet控件的选中的值和工龄到jqGrid中
            var strgl = formatterGl();
            $("#" + strTableName).jqGrid("setCell", selRowId, "gl", strgl);
            $("#" + strTableName).jqGrid("setCell", selRowId, "bmmc", $("#bmdm").combobox('getText'));
        }       
        //数据标志为没有change
        $("#" + dlgId + "_divform").data("change", false);
        savesign = "1";
        strJsonData = null;
        strRecord = null;
        strRet = null;
        myStr = null;
        //保存新增
        if (saveType == '2') {
            selRowId = "0";
            $(":input", $("#" + dlgId + "_divform")).val("");
            $("#bmdm", $("#" + dlgId + "_divform")).combobox("setValue", "");
            //系统生成编号
            var strBh = ParseData(window.external.DoOptions(strTableName, 'MakeBH', '{}'));
            strbh = strBh.BH;
            $("#a2").val(strbh);
            $("#" + dlgId + "_divform").data("change", false);
            $("#a3", $("#" + dlgId + "_divform")).focus();
        }        
        else {  //退出
            cihoDlgClose(dlgId);
            dlgId = null;
            strTableName = null;
        }
        changeComboZt("TZyxx");
        resizeGridScroll(strTableNameP0605);
    }
}

//部门设置按钮的方法
function openBmDlg() {
    window.parent.add("部门信息", "06/p0604.htm", "604");    
}

//check是否有汉字
function checkphone(dlgId) {
    $("#a11,#a6,#a7,#a13,#a14", $("#" + dlgId + "_divform")).bind("paste dragenter", function () { return false; }).css("ime-mode", "disabled").attr("maxlength", "50");
}

//计算工龄
function formatterGl() {
    var selRzsj = $("#a7").val();
    var now = new Date();
    var nowYear = now.getFullYear(); ;
    var checkArray = new Array();
    if (selRzsj!="") {
        checkArray = selRzsj.split("-");
        checDate = new Date(checkArray[0], checkArray[1], checkArray[2]);
        var strRzsj = checDate.getFullYear();
        var strGl = parseInt(nowYear - strRzsj);
        return strGl;
    }
    else {
        return "0";
    }
}

//浮动按钮方法
function selectRow_P0605(rowId) {
    selRowId = rowId;
    var btnHtml = "";
    var strZt = $("#" + strTableNameP0605).jqGrid('getCell', selRowId, "zt");
    if (strZt == "True" || strZt == "已启用") {
        btnHtml = "<a href=\"#\" onclick=\"updateP0605('" + selRowId + "');\">修改</a>-<a href=\"#\" onclick=\"deleteP0605('" + selRowId + "');return false;\">删除</a>-<a href=\"#\" onclick=\"setZtP0605('" + selRowId + "');\">停用</a>";
    }
    else {
        btnHtml = "<a href=\"#\" onclick=\"updateP0605('" + selRowId + "');\">修改</a>-<a href=\"#\" onclick=\"deleteP0605('" + selRowId + "');return false;\">删除</a>-<a href=\"#\" onclick=\"setZtP0605('" + selRowId + "');\">启用</a>";
    }
    createFloatButton.call(this, rowId, btnHtml);
    btnHtml = null;
    strZt=null;
}

//修改操作
function updateP0605(rowId) {
    if (rowId != null) {
        alterRecord('2');
    }
    else {
        window.parent.cihoAlert(msg.edit.selEditRow);
    }
}

//删除操作
function deleteP0605(rowId) {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                var strJsonData = "{\"zydm\":\"" + $("#" + strTableNameP0605).jqGrid("getCell", rowId, "zydm") + "\"}";
                var objResult = ParseData(window.external.DoOptions(strTableNameP0605, 'Delete', strJsonData));
                //删除成功后，重新加载数据
                if (objResult.ReturnCode == 0) {
                    $("#" + strTableNameP0605).jqGrid("delRowData", rowId);
                    rowId = "";
                    changeComboZt(strTableNameP0605);
                    resizeGridScroll(strTableNameP0605);
                }
                else {
                    window.parent.cihoAlert(objResult.MSG);
                }
            }
        });
    } catch (e) { }
}

//设置状态
function setZtP0605(rowId) {
    if (true == alterState(strTableNameP0605, rowId, 'zt', 'zydm')) {
        changeComboZt('TZyxx');
    }
}

//列表加载启用方法
function gridComplete_P0605() {
    var arrIds = $("#" + strTableNameP0605).jqGrid("getDataIDs");
    var strState = "1";
    for (var i = 0; i < arrIds.length; i++) {
        strState = $(this).getCell(arrIds[i], "zt");
        if (strState == "True") {
            //zt：状态
            $(this).setCell(arrIds[i], "zt", "已启用");
        }
        if (strState == "False") {
            $(this).setCell(arrIds[i], "zt", "已停用");
        }
    }
}