/*-------------------------------文档信息-----------------------------------
///功能：税金支付维护单
///创建人: 翟新乐
///创建时间：2011-07-01
///修改人：------------------------
///修改时间：-----------------------
///修改原因：-----------------------
///详细说明：-----------------------
---------------------------------------------------------------------*/
var strDivP040301 = "divP0403";            //弹出层
var strTableTp040301 = "Tp040301";          //gridId
var strOptrTypeP040301 = "1";               //操作标志位
var objRecords=[];                             //修改时的数据

//数据状态
function dataChangeStateP040301(bState) {
    //修改Data change状态
    $("#" + strDivP040301 + "_divform").data("change", bState);
}

//打开窗口方法，strType:新增(1),点击修改弹出(2)
function showDialog_P040301(strType) {
    try {
        $(".operate").remove();
        strOptrTypeP040301 = strType;
        objRecords = null;
        creatDiv_ShowDialog(strDivP040301, "税金支付", false, true, strTableTp040301, "650");           //创建窗口
        if (strOptrTypeP040301 == "1") {
            creatDialogItem_P040301("");
        }
        else if (strOptrTypeP040301 == "2") {       //修改时，显示djbh
            $("#idMust", $("#" + strDivP040301)).append("NO:" + objSelRowP0403.djbh);
            creatDialogItem_P040301(objSelRowP0403.djbh);
        }
        //$("#" + strDivP040301).dialog("option", "position", "center");
    } catch (e) { }
}

//创建表单元素方法
function creatDialogItem_P040301(strDjbh) {
    var config = ParseData(window.external.DoOptions(strTableTp040301, "GetTableConfig", "{}"));    //获取colModel
    if (config == null) {
        window.parent.cihoAlert(msg.other.modelInfoFail);
        return;
    }
    if (strDjbh != "") {    //修改取得相应数据
        objRecords = ParseData(window.external.DoOptions(strTableTp040301, 'GetList', "{\"djbh\":\"" + strDjbh + "\"}"));
    }
    //创建头部
    var i = 0;
    var strForm = "";
    var Item;
    strForm += "<ul>";
    for (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>";
    $("#" + strDivP040301 + "_divform").append(strForm);

    //下拉框的值
    var objKp = getComboboxData('Tkpdwxx'); // 开票单位
    objKp.push({ "mc": "<font color='red'>添加开票单位</font>", "dm": "-1" });
    var objKm = getComboboxData('TKjkm');   //账户
    objKm.push({ "mc": "<font color='red'>添加付款账户</font>", "dm": "-1" });
    //开票单位
    $("#kpdwdm", $("#" + strDivP040301+ "_divform")).combobox({
        textField: "mc",
        valueField: "dm",
        onChange: function (newValue, oldValue) {
            dataChangeStateP040301(true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {     //添加开票单位
                ciho.IniData({ id: "divAddType", title: "开票单位", table: "Tflxx" }, { jquery: $("#kpdwdm", "#" + strDivP040301), model: "Tkpdwxx" });
                ciho.showTypeDialog();
                $("#kpdwdm", "#" + strDivP040301).combobox("setValue", "");
            }
        }
    });
    if (objKp != "{}") {
        $("#kpdwdm", $("#" + strDivP040301+ "_divform")).combobox("loadData", objKp);
    }
    //付款账户
    $("#kmdm", $("#" + strDivP040301+ "_divform")).combobox({
        textField: "mc",
        valueField: "dm",
        panelHeight: getComHeight(objKm),
        onChange: function (newValue, oldValue) {
            dataChangeStateP040301(true);
        },
        afterChange: function (newValue, newText) {
            if (newValue == "-1") {     //添加付款账户
                ciho.IniData({ id: "divAddType", title: "付款账户", table: "Tflxx" }, { jquery: $("#kmdm", "#" + strDivP040301), model: "Tkjkm" });
                ciho.showTypeDialog();
                $("#kmdm", "#" + strDivP040301).combobox("setValue", "");
            }
        }
    });
    if (objKm != "{}") {
        $("#kmdm", $("#" + strDivP040301+ "_divform")).combobox("loadData", objKm);
    }
    $(".layoutpanel").css("z-index", "50000009");
    //设置默认值
    $("#djdate").val(getNowDate());
    inputAndBindData("djdate", false, "#" + strDivP040301, dateChange_P040301);
    if (strOptrTypeP040301 == "1") {
        $("#czydm", "#" + strDivP040301).val(window.parent.objLoginInfo.name);
    }
    $("#czydm", $("#" + strDivP040301+ "_divform")).attr("disabled", true);
    $("#a1", $("#" + strDivP040301 + "_divform")).css("ime-mode", "disabled").autoNumeric({ mDec: window.parent.strMoneyPlace, aSep: "" });
    if (strOptrTypeP040301 == "2") {        //修改，加载数据
        //日期
        $("#djdate", $("#" + strDivP040301 + "_divform")).attr("disabled", true);
        $("#djdate", $("#" + strDivP040301 + "_divform")).val(objRecords.djdate);
        //金额
        $("#a1", $("#" + strDivP040301 + "_divform")).val(parseFloat(objRecords.a1).toFixed(window.parent.strMoneyPlace));
        //付款账户
        $("#kmdm", $("#" + strDivP040301 + "_divform")).combobox('setValue', objRecords.kmdm);
        $("#kmdm", $("#" + strDivP040301 + "_divform")).combobox('setText', objRecords.kmmc);
        //开票单位
        $("#kpdwdm", $("#" + strDivP040301 + "_divform")).combobox('setValue', objRecords.kpdwdm);
        $("#kpdwdm", $("#" + strDivP040301 + "_divform")).combobox('setText', objRecords.kpdwmc);
        //制单人
        $("#czydm", $("#" + strDivP040301 + "_divform")).val(objRecords.czymc);
        //备注
        $("#bz", $("#" + strDivP040301 + "_divform")).val(escape(objRecords.bz));
    }
    //设定状态
    $(":input", $("#" + strDivP040301 + "_divform")).change(function () {
        $("#" + strDivP040301 + "_divform").data("change", true);
    });
    $("#" + strDivP040301 + "_divform").data("change", false);

    setTimeout('$("#a1", "#" + strDivP040301).focus()', 100);

    //释放内存
    config = null;
    objKm = {};
    objKp = {};
    strForm = null;
    Item = null;
    i = null;
}

//日期控件变更，datachange
function dateChange_P040301() {
    dataChangeStateP040301(true);
}

//保存数据
function saveMyData_P040301(save) {
    var strRecord = saveMyDataTp040301(save);
    if (!strRecord) {
        return;
    }
    if (strOptrTypeP040301 == "1") {    //新增
        var objResult = ParseData(window.external.DoOptions(strTableTp040301, "Add", strRecord));
    }
    else {      //修改
        var objResult = ParseData(window.external.DoOptions(strTableTp040301, "Edit", strRecord));
    }
    if (!objResult) {
        window.parent.cihoAlert(msg.other.dbOprateErr);
        return;
    }
    if (objResult.ReturnCode != "0") {
        window.parent.cihoAlert(objResult.MSG);
        return;
    }
    loadGridDataP0403();
    //resizeGrid(strTableP0403);
    //清除修改数据
    strOptrTypeP040301 = "1";
    strRecord = null;
    objRecords = {};
    objResult = null;
    if (save == "2") {  //保存新增
        $(":input", $("#" + strDivP040301+ "_divform")).val("");
        //清除单据编号
        $("#idMust", $("#" + strDivP040301)).empty();
        $("#kpdwdm", $("#" + strDivP040301 + "_divform")).combobox('setValue', '');
        $("#kmdm", $("#" + strDivP040301+ "_divform")).combobox('setValue', '');
        $("#czydm", $("#" + strDivP040301 + "_divform")).val(window.parent.objLoginInfo.name);
        $("#djdate", $("#" + strDivP040301+ "_divform")).val(getNowDate());
        $("#djdate", $("#" + strDivP040301+ "_divform")).attr("disabled", false);
        dataChangeStateP040301(false);
        setTimeout(function () { $("#a1", "#" + strDivP040301).focus(); }, 100);
    }
    else {
        //$("#" + strDivP040301).dialog('close');
        cihoDlgClose(strDivP040301);
    }
}

//json串组成方法
function saveMyDataTp040301(save) {
    //头部
    var strData = "{";
    if ($("#djdate ", $("#" + strDivP040301 + "_divform")).val() == "") {    //日期为空
        window.parent.cihoAlert(msg.dt.dtExtentErr, null, function () { $("#djdate").focus(); });
        return false;
    }
    if (($("#djdate", $("#" + strDivP040301 + "_divform")).val().replace(/-/g, "/")) < (window.parent._startDate.replace(/-/g, "/"))) { //日期超出系统时间范围
        window.parent.cihoAlert(msg.other.djrqExtent, null, function () { $("#djdate").select(); });
        return;
    }
    strData += "\"djdate\":\"" + $("#djdate", $("#" + strDivP040301 + "_divform")).val() + "\"";
    if (strOptrTypeP040301 == "2") {
        strData += ",\"djbh\":\"" + objSelRowP0403.djbh + "\"";
    }  
    //开票单位数据
    var strKpValue = $("#kpdwdm", $("#" + strDivP040301 + "_divform")).combobox('getValue');
    if (!strKpValue) {
        if ((strOptrTypeP040301 == "2") && objRecords.Records != "{}" && $("#kpdwdm", $("#" + strDivP040301+ "_divform")).combobox('getText') == objRecords.kpdwmc) {
            strKpValue = objRecords.kpdwdm;
        }
        else {
            window.parent.cihoAlert(msg.sz.kp.SelKpdw, null, function () { $("#kpdwdm", $("#" + strDivP040301 + "_divform")).combobox('focus'); });
            return false ;
        }
    }
    strData += ",\"kpdwdm\":\"" + strKpValue + "\"";

    //支付账户数据
    var strKm = $("#kmdm", $("#" + strDivP040301+ "_divform")).combobox('getValue');
    if (!strKm) {
        if ((strOptrTypeP040301 == "2") && objRecords.Records != "{}" && $("#kmdm", $("#" + strDivP040301 + "_divform")).combobox('getText') == objRecords.kmmc) {
            strKm = objRecords.kmdm;
        } else {
            window.parent.cihoAlert(msg.other.inputFkzh, null, function () { $("#kmdm", $("#" + strDivP040301 + "_divform")).combobox('focus'); });
            return false;
        }
    }
    strData += ",\"kmdm\":\"" + strKm + "\"";
    if ($("#a1 ", $("#" + strDivP040301+ "_divform")).val() == "") {    //金额
        window.parent.cihoAlert(msg.fp.inputZfje, null, function () { $("#a1",$("#"+strDivP040301+ "_divform")).focus(); });
        return false;
    }
    strData += ",\"a1\":\"" + trim($("#a1", $("#" + strDivP040301 + "_divform")).val()) + "\"";

    if (strOptrTypeP040301 == "1") {    //新增时，制单人数据取自登陆用户的dm
        strData += ",\"czydm\":\"" + window.parent.objLoginInfo.code + "\"";
    }
    else {   //修改时，制单人取自数据库返回的数据
        strData += ",\"czydm\":\"" + objRecords.czydm + "\"";
    }    
    strData += ",\"bz\":\"" + trim($("#bz", $("#" + strDivP040301 + "_divform")).val()) + "\"";
    strData += "}";
    strKm = null;
    strKpValue = null;
    return strData;
}