<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: gaoyang 
    $Date: 2015-6-10 上午11:21:27  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" trace="true">
    <a:init-procedure>
        <a:model-query fetchAll="true" model="cont.CON500.con_doc_file_get_bookmark_value" rootPath="file_bookmark_value_path"/>
        <a:model-query fetchAll="true" model="cont.CON500.con_doc_file_get_content_file" rootPath="get_content_file_path"/>
        <a:model-query fetchAll="true" model="cont.CON500.con_doc_file_para_table_column" rootPath="con_doc_file_para_table_column_path"/>
        <a:model-query fetchAll="true" model="cont.CON500.con_doc_file_templet_get_atm" rootPath="con_doc_file_templet_get_atm_path"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con_doc_file_get_table_list_id" model="cont.CON500.con_doc_file_get_table_list" modelaction="query"/>
        <script type="text/javascript"><![CDATA[
            var TANGER_OCX_OBJ;
            var file_name;
            var file_path;
            
            function SaveServerFile() {
                var saveUrl = '${/request/@context_path}/office_edit_online/file_upload.lsc?file_name=' + encodeURIComponent(file_name) + '&file_path=' + encodeURIComponent(file_path);
                var e = TANGER_OCX_OBJ.SaveToURL(saveUrl, "EDITFILE", "", file_name, 0);
                if ('${/parameter/@batch_flag}' == 'Y') {
                    var times = 0;
                    successInterval = setInterval(function() {
                        function close_interval_window() {
                            clearInterval(successInterval);
                            successInterval = null;
                            $('${/parameter/@winid}').close();
                        }
                        times = times + 1;
                        if (Ext.isChrome || Ext.isGecko) {
                            if (Ext.isDefined(save_success_flag) && save_success_flag == 'Y') {
                                close_interval_window();
                            }
                        } else {
                            close_interval_window();
                        }
                    }, 1000);
                }
            }
            
            function lock_edit_window() {
                Leaf.Masker.mask($('${/parameter/@winid}').wrap, '正在加载。。。');
            }
            
            function unlock_edit_window() {
                Leaf.Masker.unmask($('${/parameter/@winid}').wrap);
            }
            
            function delete_line(bookmark) {
                var doc = TANGER_OCX_OBJ.activedocument;
                doc.range(doc.bookmarks.item(bookmark).range.end, doc.bookmarks.item(bookmark).range.end + 1).select();
                doc.application.selection.expand(5); //选中行
                doc.application.selection.Delete();
            }
            
            function form_table_loop_execute(bookmark_record) {
                var bookmark = bookmark_record.get('bookmark');
                Leaf.request({
                    url: $('con_doc_file_get_table_list_id').getUrl(),
                    para: {
                        bookmark: bookmark,
                        contract_id: '${/parameter/@contract_id}'
                    },
                    success: function(res) {
                        var records = res.result.record;
                        try {
                            var doc = TANGER_OCX_OBJ.activedocument;
                        } catch (e) {
            
                           }
            
                        function set_table_line(record, j) {
                            var num = j + 2;
                            var column_records = $('con_doc_file_para_table_column_ds').getAll();
                            for (var name in record) {
                                if (column_records.length) {
                                    for (var k = 0;k < column_records.length;k++) {
                                        var column_record = column_records[k];
                                        if (name.toLowerCase() == column_record.get('column_name').toLowerCase()) {
                                            try {
                                                var temp_column_name;
                                                if (j === 0) {
                                                    temp_column_name = column_record.get('column_name');
                                                } else {
                                                    temp_column_name = column_record.get('column_name') + j;
                                                }
                                                try {
                                                    doc.formfields.item(temp_column_name).result = record[name];
                                                } catch (e) {
            
                                                   }
                                            } catch (e) {
            
                                               }
                                        }
                                    }
                                }
                            }
                            if (records && records.length && j != records.length - 1) {
                                try {
                                    doc.bookmarks.item(bookmark).range.copy();
                                    doc.range(doc.bookmarks.item(bookmark).range.end, doc.bookmarks.item(bookmark).range.end + 1).select();
                                    doc.application.Selection.TypeParagraph();
                                    doc.application.Selection.TypeText("	   ");
                                    doc.application.selection.paste(); //就多拷贝了一个书签内容了
                                    doc.application.Selection.TypeParagraph();
                                    var formfield = doc.Bookmarks.item(bookmark).Range.FormFields;
                                } catch (e) {
            
                                   }
                                for (var name in record) {
                                    if (column_records.length) {
                                        for (var n = 0;n < column_records.length;n++) {
                                            var column_record = column_records[n];
                                            if (name.toLowerCase() == column_record.get('column_name').toLowerCase()) {
                                                try {
                                                    var q = j * 1 + 1;
                                                    var temp_clear_name;
                                                    if (j === 0) {
                                                        temp_clear_name = column_record.get('column_name');
                                                    } else {
                                                        temp_clear_name = column_record.get('column_name') + j;
                                                    }
                                                    formfield.item(temp_clear_name).result = '';
                                                    formfield.item(temp_clear_name).name = column_record.get('column_name') + q;
                                                } catch (e) {
            
                                                   }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (records && records.length) {
                            for (var j = 0;j < records.length;j++) {
                                var record = records[j];
                                set_table_line(record, j);
                            }
                            //delete_line(bookmark);
                        } else if (records && !records.length) {
                            set_table_line(records, 0);
                        }
                    },
                    error: function() {
            
                       },
                    failure: function() {
            
                       },
                    scope: this,
                    sync: true
                });
            }
            
            function filter_bookmarks() {
                var bookmark_records = $('con_file_bookmark_value_ds').getAll();
                for (var i = 0;i < bookmark_records.length;i++) {
                    var bookmark_record = bookmark_records[i];
                    var bookmark = bookmark_record.get('bookmark');
                    var bookmark_value;
                    if (bookmark_record.get('bookmark_type') == 'TEXT') {
                        bookmark_value = bookmark_record.get('bookmark_value');
                        if (bookmark_value) {
                            try {
                                TANGER_OCX_OBJ.SetBookmarkValue(bookmark, bookmark_value);
                            } catch (e) {}
                        }
                    } else if (bookmark_record.get('bookmark_type') == 'TABLE_AREA') {
                        bookmark_value = bookmark_record.get('bookmark_value');
                        if (bookmark_value) {
                            try {
                                TANGER_OCX_OBJ.activedocument.FormFields.item(bookmark).Result = bookmark_value;
                            } catch (e) {}
                        }
                    } else if (bookmark_record.get('bookmark_type') == 'FORM_TABLE') {
                        form_table_loop_execute(bookmark_record);
                    } else {
                        Leaf.request({
                            url: $('con_doc_file_get_table_list_id').getUrl(),
                            para: {
                                bookmark: bookmark,
                                contract_id: '${/parameter/@contract_id}'
                            },
                            success: function(res) {
                                var records = res.result.record;
                                try {
                                    TANGER_OCX_OBJ.activedocument.bookmarks.item(bookmark).range.tables.item(1).cell(1, 1).select();
                                } catch (e) {
            
                                   }
            
                                function set_table_line(record, i) {
                                    var num = i + 2;
                                    try {
                                        TANGER_OCX_OBJ.ActiveDocument.Application.Selection.InsertRowsBelow(1); //插入新的行
                                    } catch (e) {
            
                                       }
                                    var column_records = $('con_doc_file_para_table_column_ds').getAll();
                                    for (var name in record) {
                                        if (column_records.length) {
                                            for (var m = 0;m < column_records.length;m++) {
                                                var column_record = column_records[m];
                                                if (column_record.get('bookmark') == bookmark && name.toLowerCase() == column_record.get('column_name').toLowerCase()) {
                                                    try {
                                                        TANGER_OCX_OBJ.activedocument.bookmarks.item(bookmark).range.tables.item(1).Cell(num, column_record.get('column_num')).Range.InsertAfter(record[name]);
                                                    } catch (e) {
            
                                                       }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (records && records.length) {
                                    for (var i = 0;i < records.length;i++) {
                                        var record = records[i];
                                        set_table_line(record, i);
                                    }
                                } else if (records && !records.length) {
                                    set_table_line(records, 0);
                                }
                            },
                            error: function() {
            
                               },
                            failure: function() {
            
                               },
                            scope: this,
                            sync: true
                        });
                    }
                }
                if ('${/parameter/@first_copy_flag}' == 'Y' && '${/model/con_doc_file_templet_get_atm_path/record/@pwd}') {
                    try {
                        TANGER_OCX_OBJ.activedocument.trackrevisions = true;
                        // TANGER_OCX_OBJ.activedocument.protect(3, 0, '${/model/con_doc_file_templet_get_atm_path/record/@pwd}');
                        TANGER_OCX_OBJ.setreadonly(true, '${/model/con_doc_file_templet_get_atm_path/record/@pwd}');
                    } catch (e) {
            
                       }
                }
                SaveServerFile();
            }
            
            function openServerFile() {
                var openUrl = '${/request/@context_path}/office_edit_online/weboffice_download_file.lsc?file_name=' + encodeURIComponent(file_name) + '&file_path=' + encodeURIComponent(file_path);
                TANGER_OCX_OBJ.OpenFromURL(openUrl);
                objActiveInterval = setInterval(function() {
                    if (TANGER_OCX_OBJ.ActiveDocument) {
                        clearInterval(objActiveInterval);
                        if ('${/parameter/@first_copy_flag}' == 'Y') {
                            try {
                                if ('${/model/con_doc_file_templet_get_atm_path/record/@pwd}') {
                                    // TANGER_OCX_OBJ.activedocument.unprotect('${/model/con_doc_file_templet_get_atm_path/record/@pwd}');
                                    TANGER_OCX_OBJ.setreadonly(false, '${/model/con_doc_file_templet_get_atm_path/record/@pwd}');
                                }
                                TANGER_OCX_OBJ.activedocument.trackrevisions = false;
                            } catch (e) {
            
                               }
                            filter_bookmarks();
                        }
                    }
                }, 1000);
            }
            
            function closeEditOfficeWindow() {
                $('${/parameter/@winid}').close();
            }
            
            Leaf.onReady(function() {
                file_name = '${/model/get_content_file_path/record/@file_name}';
                file_path = '${/model/get_content_file_path/record/@file_path}';
                objInterval = setInterval(function() {
                    if (document.getElementById("TANGER_OCX")) {
                        TANGER_OCX_OBJ = document.getElementById("TANGER_OCX");
                        clearInterval(objInterval);
                        openServerFile();
                    }
                }, 1000);
            });
        ]]></script>
        <a:dataSets>
            <a:dataSet id="con_file_bookmark_value_ds">
                <a:datas dataSource="/model/file_bookmark_value_path"/>
            </a:dataSet>
            <a:dataSet id="con_doc_file_para_table_column_ds">
                <a:datas dataSource="/model/con_doc_file_para_table_column_path"/>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="closeEditOfficeWindow" text="退出"/>
                <a:gridButton click="SaveServerFile" text="保存"/>
                <!-- <a:gridButton click="filter_bookmarks" text="数据重置"/> -->
            </a:screenTopToolbar>
            <div id="placeholder" style="display:none"/>
            <script src="${/request/@context_path}/office_edit_online/ntkoofficecontrol.js" type="text/javascript"/>
            <script type="text/javascript"><![CDATA[
                ntkoObject();
            ]]></script>
        </a:screenBody>
    </a:view>
</a:screen>
