<?xml version="1.0" encoding="UTF-8"?>
<!--
  create by nyl  2014-12-3 
  for contract context create
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query fetchAll="true" model="leg.LEG600.leg_default_law_firm" rootPath="default_law_firm"/>
        <a:model-query fetchAll="true" model="leg.LEG600.get_sysdate" rootPath="sysdate"/>
        <a:model-query fetchAll="true" model="leg.LEG100.get_default_country" rootPath="get_default_country"/>
    </a:init-procedure>
    <a:view>
        <a:link id="legal_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="leg_legal_letter_submit_link_id" model="leg.LEG600.leg_legal_letter_submit" modelaction="update"/>
        <a:link id="leg_legal_letter_fee_link_id" model="leg.LEG600.leg_legal_letter_submit" modelaction="execute"/>
        <a:link id="leg_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="leg_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <a:link id="leg400_print_link_id" url="${/request/@context_path}/modules/leg/LEG400/leg_content_print_detail.lview"/>
        <script type="text/javascript"><![CDATA[
            function leg600_open_contract_win(ds_id, record_id, name, type) {
            
                //  var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                var legal_record;
            
                var url_id = 'leg_uploadFile_id';
                if (type == '1') {
                    ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                    legal_record = $(ds_id).getCurrentRecord();
                    url_id = 'leg_uploadFile_id';
                } else {
                    legal_record = $(ds_id).findById(record_id);
                    url_id = 'leg_downloadFile_id';
                }
                var url;
                //
                if (type == 1) {
                    url = $(url_id).getUrl() + '?table_name=LEG_LEGAL_LETTER&header_id=' + '${/parameter/@legal_letter_id}';
                } else {
                    url = $(url_id).getUrl() + '?table_name=LEG_LEGAL_LETTER&header_id=' + legal_record.data['legal_letter_id'];
                }
                new Leaf.Window({
                    url: url,
                    title: '附件',
                    id: 'leg_legal_letter_id',
                    width: 850,
                    height: 400
                });
            }
            
            
            window['${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record) {
                if (name == 'attachment') {
                    return '<a href="javascript:leg600_open_contract_win(\'' + record.ds.id + '\',\'' + record.id + '\',\'' + 'attachment' + '\',' + '2' + ')">' + '附件' + '</a>';
                }
            
                return value;
            };
            //按钮渲染
            window['${/parameter/@layout_code}_dynamic_button_renderer'] = function(value, record, name, config_record, bp_seq) {
                var link_function = '';
                if (name == 'attachment') {
                    link_function = 'leg600_open_contract_win';
                }
                if (Ext.isIE || Ext.isIE9 || Ext.isIE10) {
                    return '<button style="font-size:11px;height:22px;width:70%;text-align:center;padding:0 4px 4px 0" onclick="window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',' + '1' + ');"><div style="height: 18px; line-height: 16px; text-decoration: none;">' + config_record.get('prompt') + '</div></button>';
                } else {
                    return '<button style="font-size:11px;height:22px;width:70%;text-align:center;" onclick="window[\'' + link_function + '\'](\'' + record.id + '\',null,\'' + name + '\',' + '1' + ');">' + config_record.get('prompt') + '</button>';
                }
            };
            
 
            //保存前调用
            window['${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function(ds, record) {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                var contract_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                var letter_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (contract_ds_id && letter_ds_id) {
                    var contract_record = $(contract_ds_id).getCurrentRecord();
                    var letter_record = $(letter_ds_id).getCurrentRecord();
                    letter_record.set('overdue_amount', contract_record.data['overdue_amount']);
                    letter_record.set('overdue_times', contract_record.data['overdue_times']);
                    var check_flag = true;
                }
                return check_flag;
            };
            //生成律师函
            window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (ds_id) {
                    var record = $(ds_id).getCurrentRecord();
                    var param = new Object();
                    param['legal_letter_id'] = record.get('legal_letter_id');
                    param['contract_id'] = record.get('contract_id');
                    param['winid'] = 'leg_print_detail_winid';
                    param['function'] = 'LEG600D';
                    new Leaf.Window({
                        id: 'leg_print_detail_winid',
                        params: param,
                        url: $('leg400_print_link_id').getUrl(),
                        title: '法务文本打印',
                        width: 900,
                        height: 450
                    });
                }
            };
            //创建费用现金流
            window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (ds_id) {
                    var record = $(ds_id).getCurrentRecord();
                    var legal_letter_id = record.get('legal_letter_id');
                    var contract_id = record.get('contract_id');
                    if (record.dirty != true) {
                        window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                        Leaf.request({
                            url: $('leg_legal_letter_fee_link_id').getUrl(),
                            para: {
                                legal_letter_id: legal_letter_id,
                                contract_id: contract_id
                            },
                            success: function(res) {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            failure: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            error: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            scope: this
                        });
                    } else {
                        Leaf.showMessage('提示', '请先保存!', null, 350);
                    }
                }
            };
            
            //提交审批
            window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
            
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (ds_id) {
                    var record = $(ds_id).getCurrentRecord();
                    var legal_letter_id = record.get('legal_letter_id');
                    var contract_id = record.get('contract_id');
            
                    if (record.dirty != true) {
            
                        window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                        Leaf.request({
                            url: $('leg_legal_letter_submit_link_id').getUrl(),
                            para: {
                                legal_letter_id: legal_letter_id,
                                contract_id: contract_id
                            },
                            success: function(res) {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                $('${/parameter/@winid}').close();
            
                            },
                            failure: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            error: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            scope: this
                        });
                    } else {
                        Leaf.showMessage('提示', '请先保存再提交审批!', null, 350);
                    }
                }
            
            };
            
            //查询时调用(grid,table,gridBox)
            window['${/parameter/@layout_code}_on_layout_dynamic_grid_query'] = function(ds, qpara, bp_seq) {
                var con_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter_history');
                var letter_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                //leg_legal_letter
                if (ds.id == con_ds_id && con_ds_id && letter_ds_id) {
                    qpara.contract_id = ${/parameter/@contract_id};
                }
            
            };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>