<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script">
    <a:init-procedure>
        <s:server-script import="contract_print_path.js"><![CDATA[
            $ctx.parameter.file_path = con_print_path['con_print_path'];
            $ctx.parameter.tomcat_source = con_print_path['tomcat_source'];
            ]]>
        </s:server-script>
    </a:init-procedure>
    <a:view>
        <a:link id="prj500_cdd_uploadFile_id"
                url="${/request/@context_path}/uploadFile.lview"/>
        <link href="${/request/@context_path}/css/lightbox.css" rel="stylesheet" type="text/css"/>
        <script src="${/request/@context_path}/javascripts/lightbox.js" type="text/javascript"/>
        <a:link id="con707_save_atth_note_link" model="cont.CON707.con_contract_atth_note" modelaction="batch_update"/>
        <a:link id="con707_con_contract_info_update_link"
                model="cont.CON1100.con_contract_excuted" modelaction="execute"/>

        <script type="text/javascript"><![CDATA[
        Ext.ux.Lightbox.register('a[ref=img]', true);

        /******************  上传文件  *************************/

        function open_uploadfile_win_c(record_id) {
            new Leaf.Window({
                url: $('con_contract_uploadfile_link_id').getUrl(),
                title: '${l:HLS.SUPPORTING_DOCUMENT}',
                params: {
                    'table_name': 'CON_CONTENT_FILE_ATTACHMENT',
                    'header_id': record_id
                },
                width: 850,
                height: 400
            });
        }
        //从app更新信息
        function update_con_from_app(){
            var url = $('con707_con_contract_info_update_link').getUrl();
            var contract_id='${/parameter/@contract_id}';

            Leaf.showConfirm('提示', '是否确认执行该操作?', function () {



                    csh707_lock_current_window();
                    Leaf.request({
                        url: url,
                        para: {
                            contract_id: contract_id
                        },
                        success: function (res) {

                            Leaf.SideBar.show({
                                msg: '执行成功！',
                                duration: 2000
                            });
                            $('con_contract_all_file_result_ds').query();
                            csh707_unlock_current_window();

                        },
                        failure: function () {
                            csh707_unlock_current_window();
                        },
                        error: function () {
                            csh707_unlock_current_window();
                        },
                        scope: this
                    });

            });
        }

        function ds_doc_render_func(value, record, name) {
            var check_id = record.get('check_id');
            var url;
            var text;
            var save_table = record.get('save_table');
            var save_table_pk = record.get('save_table_pk');
            if (name == 'attachment') {
                return '<a href="javascript:hls_attachment_window_open(\'' + save_table + '\',\'' + save_table_pk + '\',\'' + record.id + '\',\'' + record.ds.id + '\');">' + '附件上传' + '</a>';
            } else if (name == 'attach_file_name') {
                if (value != null) {
                    var link = '${/request/@context_path}/atm_download.lsc?attachment_id=';
                    var str = value.split(';;');
                    var url = '';
                    for (var i = 0; i < str.length; i++) {
                        var temp = str[i].split('--');
                        if (!Leaf.isEmpty(temp[0])) {
                            var file_name = temp[0].toUpperCase();
                            var file_suffix = temp[0].substr(temp[0].lastIndexOf('.') + 1).toUpperCase();
                            if (file_name.indexOf('.PDF') >= 0) {
                                url = url + '<a href=javascript:view_pdf(\'' + temp[1] + '\')>' + temp[0] + '</a>' + ',';
                            } else if (file_suffix == 'BMP' || file_suffix == 'JPG' || file_suffix == 'JPEG' || file_suffix == 'PNG' || file_suffix == 'GIF') {
                                url = url + '<a href=' + link + temp[1] + ' ref="img">' + temp[0] + '</a>' + ',';
                            } else {
                                url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                            }
                        }
                    }
                    return url;

                }
            } else if (name == 'access_record') {
                return '<a href="javascript:hls_access_record_window_open(\'' + save_table + '\',\'' + save_table_pk + '\',\'' + record.id + '\',\'' + record.ds.id + '\');">' + '调阅记录' + '</a>';

            }
        }

        function hls_attachment_window_open(save_table, save_table_pk, record_id, ds_id) {
            url = $('prj500_cdd_uploadFile_id').getUrl() + '?table_name=' + save_table + '&header_id=' + save_table_pk;
            wd = new Leaf.Window({
                id: 'open_attachment_window',
                url: url,
                title: '附件上传',
                height: 350,
                width: 850
            });
            wd.on('close', function () {
                $('con_contract_all_file_result_ds').query();
            });

        }

        function view_pdf(attachment_id) {
            Leaf.request({
                url: '${/request/@context_path}/autocrud/fnd.fnd_atm_attachment/query',
                para: {
                    attachment_id: attachment_id
                },
                success: function (res) {
                    var path = res.result.record.file_path;
                    path = path.substr(path.indexOf('hls_attachment'));
                    var tomcat_source = '${/parameter/@tomcat_source}';
                    var source_path = 'http://' + window.location.host + '/' + tomcat_source + '/' + path;
                    var oWin = window.open(source_path);
                },
                scope: this
            });
        }

        function csh707_exit() {
            $('con_archive_atm_link_winId').close();
        }


        function csh707_save() {
            var records = $('con_contract_all_file_result_ds').getSelected();
            var post_data = [];
            if (records.length == 0) {
                Leaf.showMessage('${l:HLS.PROMPT}', '请至少选择一条数据!');
                return;
            }
            for (var i = 0; i < records.length; i++) {
                post_data.push({
                    'contract_id': records[i].get('contract_id'),
                    'save_table': records[i].get('save_table'),
                    'save_table_pk': records[i].get('save_table_pk'),
                    'note': records[i].get('note'),
                    '_status': 'update'
                })
            }
            csh707_lock_current_window();
            Leaf.request({
                url: $('con707_save_atth_note_link').getUrl(),
                para: post_data,
                success: function () {
                    Leaf.SideBar.show({
                        msg: '操作成功',
                        duration: 2000
                    });
                    csh707_unlock_current_window();
                    $('con_contract_all_file_result_ds').query();
                },
                failure: function () {
                    csh707_unlock_current_window();
                },
                error: function () {
                    csh707_unlock_current_window();
                },
                scope: this
            });

        }


        function csh707_lock_current_window(msg) {
            Leaf.Masker.mask(Ext.getBody(), msg);
        }

        function csh707_unlock_current_window() {
            Leaf.Masker.unmask(Ext.getBody());
        }


        ]]></script>
        <a:dataSets>
            <a:dataSet id="basic_clause_tmplet_usage_ds" lookupCode="CON_TMPLET_USAGE"/>
            <a:dataSet id="con_content_pigeonhole_type_ds" lookupCode="PIGEONHOLE_TYPE"/>
            <a:dataSet id="con_contract_all_file_result_ds" autoQuery="true" fetchAll="true" selectable="true"
                       selectionModel="multiple"
                       model="cont.CON707.con_contract_file"
                       queryUrl="${/request/@context_path}/autocrud/cont.CON707.con_contract_file/query?contract_id=${/parameter/@contract_id}">
                <a:fields>

                </a:fields>
            </a:dataSet>

        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="csh707_exit" text="HLS.EXIT"/>
                <a:gridButton click="csh707_save" text="HLS.SAVE"/>
                <a:gridButton click="update_con_from_app" text="从app更新信息"/>

            </a:screenTopToolbar>
            <a:grid id="con_contract_all_file_grid_id" bindTarget="con_contract_all_file_result_ds"
                    marginHeight="250" marginWidth="130" navBar="true" style="margin:20px">
                <a:columns>
                    <a:column name="file_name" prompt="文档名称" width="200"/>
                    <!-- <a:column name="access_record" prompt="调阅记录" align="center"  width="250"/>-->
                    <a:column name="attachment" renderer="ds_doc_render_func" align="center" prompt="附件上传" width="150"/>
                    <a:column name="attach_file_name" prompt="附件名称" renderer="ds_doc_render_func"
                              width="280"/>
                    <a:column name="atm_count" prompt="附件数量" align="center" width="80"/>
                    <a:column name="note" prompt="备注" editor="file_result_grid_textArea_id" width="250"/>
                    <a:column name="save_table" prompt="表名" hidden="true" width="250"/>
                    <a:column name="save_table_pk" prompt="主键" hidden="true" width="250"/>
                </a:columns>
                <a:editors>
                    <a:checkBox id="file_result_grid_checkBox_id"/>
                    <a:comboBox id="file_result_grid_comboBox_id"/>
                    <a:lov id="file_result_grid_lov_id"/>
                    <a:datePicker id="file_result_grid_datePicker_id"/>
                    <a:textArea id="file_result_grid_textArea_id" height="100" width="300"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>
