/*-------------------------------文档信息-----------------------------------
///功能：提成调整单
///创建人: 翟新乐
///创建时间：----------------------
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
//页面全局变量
var strTableP051002 = "Tp051002";   //表格ID
var strDialogP051002 = "divP051002";    //弹出层ID
var strOptrTypeP051002 = "1";               //操作标志位
var objRecords=[];                         //修改取得数据
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//编辑提成调整单方法
function showDialog_P051002(strtype) {
    try {
        $(".operate").remove();
        $("#" + strDialogP051002).empty();
        //弹出窗口的创建
        strOptrTypeP051002 = strtype;
        objRecords = null;
        creatDiv_ShowDialog(strDialogP051002, "提成调整单", false, false, strTableP051002, "400");

        //创建窗口元素
        if (strOptrTypeP051002 == "1") { //新增
            creatDialogItem_P051002("");
        }
        else if (strOptrTypeP051002 == "2") {   //修改
            $("#idMust", $("#" + strDialogP051002)).append("NO:" + objSelRowP0510.djbh);
            creatDialogItem_P051002(objSelRowP0510.djbh);
        }
        //设置窗口居中显示
        $("#" + strDialogP051002).dialog("option", "position", "center");
        setTimeout(function () { $("#a1", "#" + strDialogP051002).focus(); }, 100);
    } catch (e) { }
}

//创建表单元素
function creatDialogItem_P051002(strDjbh) {
    //列配置
    var config = { "pageHeader": [{ "edittype": "text", "editable": true, "index": "djdate", "hidden": false, "width": 80, "xsmc": "日&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;期", "name": "djdate", "align": "center" },
        { "edittype": "select", "editable": true, "index": "bmdm", "hidden": false, "width": 80, "xsmc": "部&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;门", "name": "bmdm", "align": "center" },
        { "edittype": "select", "editable": true, "index": "zydm", "hidden": false, "width": 80, "xsmc": "姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名", "name": "zydm", "align": "center" },
        { "edittype": "select", "editable": true, "index": "djtypedm", "hidden": false, "width": 80, "xsmc": "调整类型", "name": "djtypedm", "align": "center" },
        { "edittype": "text", "editable": true, "index": "a1", "hidden": false, "width": 80, "xsmc": "调整金额", "name": "a1", "align": "center" },
        { "edittype": "text", "editable": true, "index": "czydm", "hidden": false, "width": 80, "xsmc": "制&nbsp;&nbsp;单&nbsp;&nbsp;人", "name": "czydm", "align": "center" },
        { "edittype": "text", "editable": true, "index": "bz", "hidden": false, "width": 80, "xsmc": "备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注", "name": "bz", "align": "center" },
        { "edittype": "text", "editable": true, "index": "djtypemc", "hidden": true, "width": 80, "xsmc": "", "name": "djtypemc", "align": "center" },
        { "edittype": "text", "editable": true, "index": "czymc", "hidden": true, "width": 80, "xsmc": "", "name": "czymc", "align": "center" },
        { "edittype": "text", "editable": true, "index": "djbh", "hidden": true, "width": 80, "xsmc": "", "name": "djbh", "align": "center"}]};
    if (strDjbh != "") {
        objRecords = ParseData(window.external.DoOptions(strTableP051002, 'GetList', "{\"djbh\":\"" + strDjbh + "\"}"));
    }
    //创建头部
    var strForm = "";
    var Item;
    strForm += "<ul>";
    for (var i = 0; i < config.pageHeader.length; i++) {
        Item = config.pageHeader[i];
        if (Item.hidden == true) {
            continue;
        }
        if (Item.edittype == "text") {
            if (Item.index == "bz") {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "'";
                strForm += " onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
                strForm += "</li>";
            }
            else {
                strForm += "<li>";
                strForm += Item.xsmc + "：";
                strForm += "<input type='text'  name='" + Item.index + "' ";
                strForm += " value=''  ";
                strForm += " id='" + Item.index + "'";
                strForm += " maxlength='50'   onkeypress='return checkValidate(event)' onpaste='return validatePaste()'/>";
                strForm += "</li>";
            }
        }
        else {
            strForm += "<li>";
            strForm += Item.xsmc + "：";
            strForm += "<select name='" + Item.index + "' id='" + Item.index + "'";
            strForm += " class='easyui-combobox' >";
            strForm += "</select>";
            strForm += "</li>";
        }

    }
    strForm += "</ul>";
    $("#" + strDialogP051002 + "_divform").append(strForm);
    //添加按钮
    strForm = "<div class=\"clr\"></div>";
    strForm += "<div class=\"box_btn_width\">";
    strForm += "    <div class=\"box_btn\">";
    strForm += "        <div class=\"btn01\"><a onclick=\"saveMyData('" + strDialogP051002 + "', 1, '" + strTableP051002 + "')\">保存退出</a></div>";
    strForm += "    </div>";
    strForm += "</div>";
    $(".pop_box", $("#" + strDialogP051002)).append(strForm);

    //下拉框的值
    loadComData_P051002();

    //日期控件
    inputAndBindData("djdate", false, "#" + strDialogP051002, dateChange_P051002);
    $("#djdate", $("#" + strDialogP051002 + "_divform")).css("width", "185");
    $("#a1", $("#" + strDialogP051002 + "_divform")).css("ime-mode", "disabled").autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" });
    $("#czydm", $("#" + strDialogP051002 + "_divform")).attr("disabled", true);

    //设置默认值
    if (strOptrTypeP051002 == "1") {
        $("#djdate", $("#" + strDialogP051002 + "_divform")).val(getNowDate());
        $("#czydm", $("#" + strDialogP051002 + "_divform")).val(window.parent.objLoginInfo.name);
    } else if (strOptrTypeP051002 == "2") {     //修改数据加载
        $("#djdate", $("#" + strDialogP051002 + "_divform")).attr("disabled", true);
        //金额
        $("#a1", $("#" + strDialogP051002 + "_divform")).val(parseFloat(objRecords.Records[0].a1).toFixed(window.parent.strMoneyPlace));
        //日期
        $("#djdate", $("#" + strDialogP051002 + "_divform")).val(objRecords.Records[0].djdate);        
        if (objRecords.Records[0].bmdm=="") {           //没有部门的职员就给部门设为“其他部门”
            $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox('setValue', objRecords.Records[0].bmdm);
        }
        else {
            $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox('setValue', objRecords.Records[0].bmdm);
            $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox('setText', objRecords.Records[0].bmmc);
        }
        //姓名
        $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox('setValue', objRecords.Records[0].zydm);
        $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox('setText', objRecords.Records[0].zymc);
        //调整类型
        $("#djtypedm", $("#" + strDialogP051002 + "_divform")).combobox('setValue', objRecords.Records[0].tzlx);
        //制单人
        $("#czydm", $("#" + strDialogP051002 + "_divform")).val(objRecords.Records[0].czymc);
        //备注
        $("#bz", $("#" + strDialogP051002 + "_divform")).val(escape(objRecords.Records[0].a2));
    }    
    //设定状态
    $(":input", $("#" + strDialogP051002 + "_divform")).change(function () {
        $("#" + strDialogP051002 + "_divform").data("change", true);
    });
    $("#" + strDialogP051002 + "_divform").data("change", false);
    //释放内存
    config = {};
    Item = null;
    strForm = null;
}

//日期控件变更,datachange
function dateChange_P051002() {
    dataChangeStateP051002(true);
}

//加载部门，姓名combobox的方法
function loadComData_P051002() {
    var objBm = getComboboxData('Tbmxx');
    var objZy = getComboboxData('TZyxx');
    objZy.push({ "mc": "<font color='red'>添加职员</font>", "dm": "-1" });
    //var objzydm = ParseData(window.external.DoOptions("Tp0510", "GetComboBoxData", "{'fb':'1'}")).Records;
    //bm
    $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox({
        width: _iWidth190,
        listWidth: _iWidth190,
        textField: "mc",
        valueField: "dm",
        onChange: function (newValue, oldValue) {
            dataChangeStateP051002(true);
        },
//        onblur: function () {   //失去焦点
//            if ($(this).combobox("getText") == "") {
//                var objzyData = getComboboxData('TZyxx');
//                objzyData.push({ "mc": "<font color='red'>添加职员</font>", "dm": "-1" });
//                $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox("loadData", objzyData);
//                objzyData = {};
//            }
//        },
        onSelect: function (options) {          //关联职员
            var objRet = [];
            var objzydm = ParseData(window.external.DoOptions("Tp0510", "GetComboBoxData", "{'fb':'1'}")).Records;
            var objZyData = getComboboxData('TZyxx');
            objRet = cmbSelectData(options.dm, objzydm, objZyData, 'bmdm');
            objRet.push({ "mc": "<font color='red'>添加职员</font>", "dm": "-1" });
            $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox("setValue", "");
            $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox("loadData", objRet);
            objzydm = {};
            objRet = {};
            objZyData = {};
        }
    });
    objBm.push({ "dm": "", "mc": "其他部门" });
    if (objBm != "{}") {
        $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox("loadData", objBm);
    }
    //zy
    $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox({
        width: _iWidth190,
        listWidth: _iWidth190,
        textField: "mc",
        valueField: "dm",
        onChange: function (newValue, oldValue) {
            dataChangeStateP051002(true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {
                ciho.IniData({ id: "divAddType", title: "职员", table: "Tflxx" }, { jquery: $("#zydm", "#" + strDialogP051002), model: "TZyxx" });
                ciho.showTypeDialog();
                $("#zydm", "#" + strDialogP051002).combobox("setValue", "");
            }
            if (newValue != "") {
                var objzydm = ParseData(window.external.DoOptions("Tp0510", "GetComboBoxData", "{'fb':'1'}")).Records;
                for (var intI = 0; intI < objzydm.length; intI++) {
                    if (newText == objzydm[intI].zymc) {
                        if (objzydm[intI].bmdm != $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox("getValue")) {
                            $("#bmdm", $("#" + strDialogP051002 + "_divform")).combobox("setValue", objzydm[intI].bmdm);
                        }
                    }
                }
                objzydm = {};
            }
        }
    });
    if (objZy != "{}") {
        $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox("loadData", objZy);
    }
    //lx
    $("#djtypedm", $("#" + strDialogP051002 + "_divform")).combobox({
        width: _iWidth190,
        listWidth: _iWidth190,
        textField: "mc",
        valueField: "dm",
        panelHeight: 42,
        editable:false,
        onChange: function (newValue, oldValue) {
            dataChangeStateP051002(true);
        }
    });
    var strTzData = [{ "dm": "0", "mc": "提成增加" }, { "dm": "1", "mc": "提成减少"}];
    $("#djtypedm", $("#" + strDialogP051002 + "_divform")).combobox("loadData", strTzData);
    $(".layoutpanel").css("z-index", "50000009");
    strTzData = null;
    objBm = {};
    objZy = {};
}

//获取部门对应的职员列表
function cmbSelectData(options, objList,objComb, strdm) {
    var objRet = [];
    for (var i = 0; i < objList.length; i++) {
        if (options == objList[i][strdm]) {
            for (var j = 0; j < objComb.length; j++) {
                if (objList[i].zydm == objComb[j].dm) { //"dm": objComb[j].dm, "mc": objComb[j].mc, 
                    objRet.push({ "bmdm": objComb[j].bmdm, "dm": objComb[j].dm, "jc": objComb[j].jc, "jp": objComb[j].jp, "mc": objComb[j].mc, "newbh": objComb[j].newbh });
                }
            }
        }
    }
    return objRet;
}

//保存信息方法
function saveData_P051002() {
    var strZydm = $("#zydm", $("#" + strDialogP051002 + "_divform")).combobox("getValue");  //姓名
    if (!strZydm) {
        if ((strOptrTypeP051002 == "2") && objRecords.Records != "{}" && $("#zydm", "#" + strDialogP051002).combobox('getText') == objRecords.Records[0].zymc) {
            strZydm = objRecords.Records[0].zydm;
        }
        else {
            window.parent.cihoAlert(msg.other.nameNotEmp, null, function () { $("#zydm","#"+strDialogP051002).combobox('focus'); });
            return;
        }
    }
    var strYwlxdm = $("#djtypedm", $("#" + strDialogP051002 + "_divform")).combobox("getValue");    //调整类型
    if (!strYwlxdm) {
        if ((strOptrTypeP051002 == "2") && objRecords.Records != "{}") {
            strYwlxdm = objRecords.Records[0].tzlx;
        }
        else {
            window.parent.cihoAlert(msg.other.typeErr, null, function () { $("#djtypedm", $("#" + strDialogP051002 + "_divform")).combobox('focus'); });
            return;
        }
    }
    var strDjdate = trim($("#djdate", $("#" + strDialogP051002 + "_divform")).val());       //日期
    if (!strDjdate) {
        window.parent.cihoAlert(msg.dt.dtExtentErr, null, function () { $("#djdate").focus(); });
        return;
    }
    else {
        if (($("#djdate", "#" + strDialogP051002).val().replace(/-/g, "/")) < (window.parent._startDate.replace(/-/g, "/"))) {     //日期超出系统时间范围时
            window.parent.cihoAlert(msg.other.djrqExtent, null, function () { $("#djdate").select(); });
            return;
        }
    }
    var strJe = trim($("#a1", $("#" + strDialogP051002 + "_divform")).val());       //调整金额
    if (!strJe) {
        window.parent.cihoAlert(msg.other.jenotEmp, null, function () { $("#a1").focus(); });
        return;
    }
    var strBz = trim($("#bz", $("#" + strDialogP051002 + "_divform")).val());   //bz
    if (strOptrTypeP051002=="1") {
        var strCzydm = window.parent.objLoginInfo.code;
    } else {
        var strCzydm = objRecords.Records[0].czydm;
    }
    var strDataJson = "{\"djdate\":\"" + strDjdate + "\",\"zydm\":\"" + strZydm + "\",\"tzlx\":\"" + strYwlxdm + "\",\"a1\":\"" + strJe + "\",\"czydm\":\"" + strCzydm + "\",\"a2\":\"" + strBz + "\"}";
    if (strOptrTypeP051002 == "2") {        //修改时，加入djbh
        strDataJson = strDataJson.replace("}", ",\"djbh\":\"" + objSelRowP0510.djbh + "\"}");
    }
    if (strOptrTypeP051002=="1") {  //新增
        var objResult = ParseData(window.external.DoOptions(strTableP051002, "Add", strDataJson));
    } else {    //修改
        var objResult = ParseData(window.external.DoOptions(strTableP051002, "Edit", strDataJson));
    }
    if (objResult.ReturnCode == 0) {
        //释放内存
        strOptrTypeP051002 = "1";
        objResult = null;
        strDataJson = null;
        strDjdate = null;
        strZydm = null;
        strYwlxdm = null;
        strJe = null;
        strCzydm = null;
        strBz = null;
        objRecords = {};
        cihoDlgClose(strDialogP051002);
        loadGridDataP0510();
        //resizeGrid(strTableP0510);
    }
    else {
        window.parent.cihoAlert(objResult.MSG);
    }
}

//数据状态
function dataChangeStateP051002(bState) {
    //修改Data change状态
    $("#" + strDialogP051002 + "_divform").data("change", bState);
}
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/