/**
 * 提报套打模板Edit.js
 */
var fields;
shr.defineClass("shr.custom.SubmitShemeTemplateEdit", shr.framework.Edit, {
    initalizeDOM: function () {
        shr.custom.SubmitShemeTemplateEdit.superClass.initalizeDOM.call(this);
        var _self = this;
        $("#addRow_entry").hide();
        $("#deleteRow_entry").hide();
        if ($("#state").val() == "1") {
            //启用状态的模板数据,不允许编辑
            $("#edit").hide();
            $("#enable").hide();
        } else if ($("#state").val() == "0") {
            //未启用状态的模板数据
            $("#disable").hide();
        } else if ($("#state").val() == "2") {
            //禁用状态的模板数据
            $("#disable").hide();
        }
        if (this.getOperateState() != "VIEW") {
            $('[id^="attachment_"][id$="_null0"]').shrAttachment("option", {
                //删除成功回调
                deleteSuccessCallback: function (value) {
                    console.log(value);
                    //清空分录数据
                    _self.deleteEntry();
                },
                //上传成功回调
                uploadSuccessCallback: function (value) {
                    //获取上传文件的id
                    _self.deleteEntry();
                    _self.updateEntry();
                }
            });
            //提报方案
            $("#calSubmitScheme").shrPromptBox("option", {
                //值改变
                onchange: function (e, value) {
                    if (!value.current) {
                        _self.deleteEntry();
                        return;
                    }
                    var id = value.current.id;
                    if (!value.previous) {
                        _self.deleteEntry();
                        _self.updateEntry(id);
                        return;
                    }
                    var oldId = value.previous.id;
                    //附件id
                    var attachmentId = _self.getAttachmentId();
                    if (attachmentId && id) {
                        //更新分录
                        if (id != oldId) {
                            _self.deleteEntry();
                            _self.updateEntry(id);
                        }
                    } else {
                        _self.deleteEntry();
                    }
                }
            });
            if (this.getOperateState() == "EDIT") {
                //更新分录表格字段属性
                _self.setEditGridOptions(this.initData.submitSchemeFields)
            }
        }
    },
    /**
     * 
     * @param {*} submitSchemeFields 设置编辑表格列属性
     */
    setEditGridOptions: function (submitSchemeFields) {
        $entry = $("#entry");
        $entry.jqGrid("getGridParam", "colModel").forEach(function (col) {
            //设置列属性
            if (col.name == "column" && col.editable && !col.hidden) {
                // col.formatter = "shrselect";
                // col.edittype = "shrselect";
                // col.sorttype = "enum";
                // col.required = true;
                col.editoptions = {
                    enumSource: {
                        data: submitSchemeFields
                    }
                }
                col.formatoptions = {
                    enumSource: {
                        data: submitSchemeFields
                    }
                }
            };
        });
    },
    /**
     * 
     * @returns {String} attachmentId 上传文件的id
     */
    getAttachmentId: function () {
        //附件只能上传一个文件
        var attachmentId = $('[id^="attachment_"][id$="_null0_uploadFileGrid"]').find('tr').eq(1).attr("id");
        return attachmentId;
    },
    /**
     * 更新分录
     */
    updateEntry: function (submitSchemeId) {
        var _self = this;
        //获取上传文件的id
        var attachmentId = _self.getAttachmentId();
        //提报方案id
        if (!submitSchemeId) {
            submitSchemeId = $("#calSubmitScheme").shrPromptBox("getValue").id
        }
        var hrOrgUnit = $('#hrOrgUnit').shrPromptBox('getValue');
        var hrOrgUnitId = hrOrgUnit ? hrOrgUnit.id : "";
        if (attachmentId && submitSchemeId) {
            console.log(attachmentId);
            //获取模板字段
            var entryData = _self.remoteCall({
                method: 'addEntryData',
                param: {
                    attachmentId: attachmentId,
                    submitSchemeId: submitSchemeId,
                    datasource: "2",
                    costTypeId: "",
                    hrOrgUnitId: hrOrgUnitId
                },
                isTransformParam: true,
                async: false
            });
            //更新分录表格字段属性
            _self.setEditGridOptions(entryData.submitSchemeFields)
            //新增分录
            $("#entry").jqGrid("addLocalRowData", entryData.entryInfos);
        }
    },

    /**
     * 删除分录
     */
    deleteEntry: function () {
        $("#entry").jqGrid('clearGridData');
    },
});
