<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.user_id = ${/session/@user_id}" fetchAll="true" model="basic.sys_user" rootPath="user_name_path"/>
        <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="${/parameter/@layout_code}_con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="${/parameter/@layout_code}_con660_calc_change_data_link" model="cont.CON660.calc_ccr_data" modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}_con660_calc_cashflow_link" model="cont.CON660.calc_ccr_data" modelaction="update"/>
        <a:link id="${/parameter/@layout_code}_con660_calc_change_req_submit_link" model="cont.CON660.submit_change_req" modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}_con731_cancel_link" model="cont.CON731.cancel_change_req" modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="${/parameter/@layout_code}_cdd_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <script src="${/request/@context_path}/javascripts/calculate.js" type="text/javascript"/>
        <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="et_apply_content_link_id" model="cont.CON660.et_apply_content_create" modelaction="update"/>
        <a:link id="${/parameter/@layout_code}_file_copy_from_template_link_id" url="${/request/@context_path}/modules/cont/CON505/file_copy_from_template.lsc"/>
        <!-- <a:link id="${/parameter/@layout_code}_con_batch_dl_link_id" url="${/request/@context_path}/modules/cont/CON505/con_atm_batch_dl.lsc"/> -->
        <a:link id="con_atm_batch_download_pdf_link_id" url="${/request/@context_path}/modules/cont/CON500/con_atm_batch_download_pdf.lsc"/>
        <a:link id="${/parameter/@layout_code}_creat_con_contract_content_link_id" model="cont.CON660.et_apply_content_create" modelaction="execute"/>
        <!-- <a:link id="con_contract_update_print_fin_link_id" url="${/request/@context_path}/modules/cont/CON504/con_contract_update_print_word.lsc"/> -->
        <a:link id="et_con_batch_dl_link_id" url="${/request/@context_path}/modules/cont/CON500/con_atm_batch_dl.lsc"/>
        <script type="text/javascript"><![CDATA[
            Ext.ux.Lightbox.register('a[ref=img]', true);
            var g_save_flag = 'Y';
            var g_et_principal;
            
            //加载时调用(grid,table,gridBox)
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_grid_load'] = function(ds, record, config_records, bp_seq) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                var lease_item_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_lease_item');
                var contract_record = $(ds_id).getCurrentRecord();
                var records = $(lease_item_ds_id).getAll();
            
                if (ds.id == lease_item_ds_id) {
                    var selected_finance_amount = 0;
                    var et_car_count = 0;
                    for (var i = 0;i < records.length;i++) {
                        if (records[i].get('et_flag') == 'Y') {
                            records[i].getField('this_time_et_flag').setReadOnly(true);
                        } else if (records[i].get('this_time_et_flag') == 'Y') {
                            selected_finance_amount = plus(selected_finance_amount, records[i].get('finance_amount'));
                            et_car_count++;
                        }
                    }
                    contract_record.set('et_car_count', et_car_count);
                    contract_record.set('et_principal', minus(g_et_principal, selected_finance_amount));
                }
            };
            
            function calc_et_data(contract_id, et_date) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                var contract_record = $(ds_id).getCurrentRecord();
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
            
                if (et_date) {
                    Leaf.request({
                        url: $('${/parameter/@layout_code}_con660_calc_change_data_link').getUrl(),
                        para: {
                            contract_id: contract_id,
                            et_date: et_date
                        },
                        success: function(res) {
                            g_et_principal = res.result.et_principal;
                            contract_record.set('et_interest', res.result.et_interest);
                            contract_record.set('et_principal', minus(g_et_principal, selected_finance_amount));
                            contract_record.set('et_total_amt', plus(minus(plus(contract_record.get('et_amount'), res.result.et_interest), contract_record.get('et_ins_derate_amount')), selected_finance_amount));
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        failure: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        error: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        scope: this
                    });
                } else {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
            }
            
            //更新时调用
            var selected_finance_amount = 0;
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
                if (name == 'et_date') {
                    calc_et_data(record.get('contract_id'), value);
                    g_save_flag = 'N';
                } else if (name == 'et_ins_derate_amount') {
                    g_save_flag = 'N';
                    record.set('et_total_amt', plus(minus(plus(record.get('et_amount'), record.get('et_interest')), value), selected_finance_amount));
                } else if (name == 'et_amount') {
                    g_save_flag = 'N';
                    record.set('et_total_amt', plus(minus(plus(record.get('et_interest'), value), record.get('et_ins_derate_amount')), selected_finance_amount));
                } else if (name == 'this_time_et_flag') {
                    g_save_flag = 'N';
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                    var lease_item_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_lease_item');
                    if (ds.id == lease_item_ds_id) {
                        var contract_record = $(ds_id).getCurrentRecord();
                        var records = $(lease_item_ds_id).getAll();
                        selected_finance_amount = 0;
                        var et_car_count = 0;
                        for (var i = 0;i < records.length;i++) {
                            if (records[i].get('this_time_et_flag') == 'Y') {
                                selected_finance_amount = plus(selected_finance_amount, records[i].get('finance_amount'));
                                et_car_count++;
                            }
                        }
                        contract_record.set('et_car_count', et_car_count);
                        contract_record.set('et_principal', minus(g_et_principal, selected_finance_amount));
                        contract_record.set('et_total_amt', plus(minus(plus(contract_record.get('et_interest'), selected_finance_amount), contract_record.get('et_ins_derate_amount')), contract_record.get('et_amount')));
                    }
                }
            };
            
            //计算
            window['${/parameter/@layout_code}_quote_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                var cashflow_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_cashflow');
                var contract_record = $(ds_id).getCurrentRecord();
                if ($(ds_id).validate()) {
                    if (g_save_flag == 'N') {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                    } else if (contract_record.get('et_car_count') == 0) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '请选择需要提前结清的车辆！');
                    } else {
                        Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认计算？', function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                            Leaf.request({
                                url: $('${/parameter/@layout_code}_con660_calc_cashflow_link').getUrl(),
                                para: {
                                    contract_id: '${/parameter/@contract_id}',
                                    et_date: contract_record.get('et_date')
                                },
                                success: function(res) {
                                    Leaf.SideBar.show({
                                        msg: '操作成功',
                                        duration: 2000
                                    });
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    $(cashflow_ds_id).query();
                                },
                                failure: function() {
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                error: function() {
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                scope: this
                            });
                        });
                    }
                }
            };
            
            //提交审批
            window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
                var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                var req_record = $(req_ds_id).getAt(0);
            
                var header_id = '${/parameter/@change_req_id}';
                var document_type = '${/parameter/@document_type}';
                if ($(req_ds_id).validate()) {
                    if (g_save_flag == 'N') {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                        return;
                    } else if (req_record.get('et_car_count') <= 0) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '请选择需要结清的车辆！');
                        return;
                    }
            
                    Leaf.showConfirm('${l:HLS.PROMPT}', '${l:HLS.ARE_YOU_SURE_TO_SUBMIT}', function() {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                        Leaf.request({
                            url: $('${/parameter/@layout_code}_con660_calc_change_req_submit_link').getUrl(),
                            para: {
                                change_req_id: ${/parameter/@change_req_id}
                            },
                            success: function(res) {
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                $('${/parameter/@winid}').close();
                            },
                            failure: function() {
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            error: function() {
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            scope: this
                        });
                    });
                }
            };
            
            //取消结清
            window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
                Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认取消结清？', function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                    Leaf.request({
                        url: $('${/parameter/@layout_code}_con731_cancel_link').getUrl(),
                        para: {
                            change_req_id: ${/parameter/@change_req_id}
                        },
                        success: function(res) {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            $('${/parameter/@winid}').close();
                        },
                        failure: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        error: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        scope: this
                    });
                });
            };
            
            //保存submitsuccess调用
            window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'] = function(ds, record, res, bp_seq) {
                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                g_save_flag = 'Y';
            };
            
            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 upload_file(id, name, query_only) {
                var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
                if (record.get('check_id')) {
                    var url;
                    if (query_only == 'Y' || '${/parameter/@function_usage}' == 'QUERY' || '${/parameter/@maintain_type}' == 'READONLY') {
                        url = $('${/parameter/@layout_code}_cdd_downloadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                    } else {
                        url = $('${/parameter/@layout_code}_cdd_uploadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                    }
                    var win = new Leaf.Window({
                        url: url,
                        title: '${l:HLS.SUPPORTING_DOCUMENT}',
                        id: 'prj500_cdd_uploadFile_screen_id',
                        width: 850,
                        height: 400
                    });
                    win.on('close', function() {
                        record.ds.query();
                    });
                } else {
                    Leaf.showMessage('${l:HLS.PROMPT}', '请先保存!');
                }
            }
            
            //超链接渲染
            window['${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record) {
                window['${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
                if (name == 'attachment') {
                    link_function = 'upload_file';
                    if (record.get('attach_count') == 0 || !record.get('attach_count')) {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '</a>';
                    } else {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '(' + record.get('attach_count') + ')' + '</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;
                    }
                }
                return value;
            };
            
            
            //结清单打印
            window['${/parameter/@layout_code}_user_button5_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                var record = $(ds_id).getCurrentRecord();
                var content_id;
                Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认打印结清申请单？', function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                    Leaf.request({
                        url: $('et_apply_content_link_id').getUrl(),
                        para: {
                            change_req_id: ${/parameter/@change_req_id},
                            content_id: content_id
                        },
                        success: function(rsc) {
                            Leaf.request({
                                url: $('${/parameter/@layout_code}_creat_con_contract_content_link_id').getUrl(),
                                para: {
                                    content_id: rsc.result.content_id,
                                    file_path: '${/parameter/@file_path}'
                                },
                                success: function() {
                                    Leaf.request({
                                        url: $('${/parameter/@layout_code}_file_copy_from_template_link_id').getUrl(),
                                        para: {
                                            contract_id: record.get('contract_id'),
                                            content_id: rsc.result.content_id,
                                            file_path: '${/parameter/@file_path}',
                                            batch_flag: 'Y'
                                        },
                                        success: function() {
                                            var doc_code = '结清申请单';
                                            // var url_l = $('con_atm_batch_download_pdf_link_id').getUrl() + '?content_id=' + rsc.result.content_id + '&doc_code=' + encodeURI(doc_code) + '&type=SIN&fnd_atm_flag=Y';
                                            var url_l = $('et_con_batch_dl_link_id').getUrl() + '?content_id=' + rsc.result.content_id + '&doc_code=' + encodeURI(doc_code) + '&type=SIN&fnd_atm_flag=Y';
            
                                            window.open(href = url_l, target = "_self");
            
                                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                        }
                                    });
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                error: function() {
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                failure: function() {
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                scope: this
                            });
                        },
                        error: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            
                        },
                        failure: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            
                        },
                        scope: this
                    });
            
                });
            };
            
            
            // function init() {
            // var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            // var record = $(ds_id).getCurrentRecord();
            // if (record) {
            // record.set('user_id', ${/session/@user_id});
            // record.set('user_id_n', '${/model/user_name_path/record/@description}');
            // // record.set('req_status', '${/parameter/@req_status}');
            // record.set('req_status', '${/parameter/@req_status_n}');
            // if (record.get('outstanding_principal')) {
            // g_et_principal = record.get('outstanding_principal');
            // } else {
            // calc_et_data(record.get('contract_id'), record.get('et_date'));
            // }
            // }
            // }
            // Leaf.onReady(init);
            //新增和加载时调用(form)
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_form_add_and_load'] = function(ds, record, config_records, bp_seq) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                if (ds.id == ds_id) {
                    record = $(ds_id).getCurrentRecord();
                    record.set('user_id', '${/session/@user_id}');
                    record.set('user_id_n', '${/model/user_name_path/record/@description}');
                    // record.set('req_status', '${/parameter/@req_status}');
                    record.set('req_status', '${/parameter/@req_status_n}');
                    if (record.get('outstanding_principal')) {
                        g_et_principal = record.get('outstanding_principal');
                    } else {
                        if (record.get('et_date')) {
                            calc_et_data(record.get('contract_id'), record.get('et_date'));
                        }
                    }
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
            
            };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
