<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.parent_table is null and t1.tab_type!=&apos;TAB&apos; and t1.enabled_flag=&apos;Y&apos; and rownum=1" fetchAll="true" model="cont.CON500.con_hls_doc_layout_tab_query" rootPath="base_table_path"/>
        <a:model-query fetchAll="true" model="get_report_save_path" rootPath="chance_report_save_path"/>
        <!--  <s:server-script ><![CDATA[
           println($ctx.parameter.toXML());
           println('***********');
    
       ]]> </s:server-script> -->
    </a:init-procedure>
    <a:view>
        <a:link id="${/parameter/@layout_code}_hls_fin_doc_quotation_link_id" url="${/request/@context_path}/modules/hls/HLS500/hls_fin_doc_quotation.lsc"/>
        <a:link id="${/parameter/@layout_code}_hls_fin_calculator_update_link_id" url="${/request/@context_path}/modules/hls/HLS500N/hls_fin_calculator_update_n.lview"/>
        <a:link id="${/parameter/@layout_code}_hls_fin_calculator_readonly_link_id" url="${/request/@context_path}/modules/hls/HLS500/hls_fin_calculator_readonly.lview"/>
        <a:link id="${/parameter/@layout_code}_special_fields_link_id" model="cont.CON500.con_contract_get_special_fields" modelaction="update"/>
        <a:link id="${/parameter/@layout_code}_con110_submit_link" model="cont.CON110.submit_change_req" modelaction="execute"/>
        <a:link id="${/parameter/@tab_code}_con_contract_balance_link_id" url="${/request/@context_path}/modules/cont/CON305/con_contract_balance.lview"/>
        <a:link id="${/parameter/@layout_code}_cdd_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <a:link id="${/parameter/@layout_code}_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="prj401_chance_report_print_link_1" url="${/request/@context_path}/modules/prj/PRJ401/prj_chance_report_print.lsc"/>
        <a:link id="prj401_visit_report_print_link" url="${/request/@context_path}/modules/cont/CON801/con_visit_report_print.lsc"/>
        <a:link id="download_file_link_id" url="${/request/@context_path}/modules/prj/PRJ501N/file_download.lsc"/>
        <a:link id="prj_chance_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="hls_bp_master_modify_link" url="${/request/@context_path}/modules/hls/HLS214N/hls_bp_master_modify.lview"/>
        <a:link id="letter_of_intention_link_id" url="${/request/@context_path}/modules/prj/PRJ401/letter_of_intention_print.lview"/>
        <script type="text/javascript"><![CDATA[
            /*add by wcs
             //desc:该页面为南山项目所有非树类的动态页面
             */
            var dynamic_document_id = '${/parameter/@document_id}';
            
            var submit_flag = 'N';
            window['${/parameter/@layout_code}_ignore_required_before_save'] = function() {
            
                if (submit_flag == 'N') {
                    $('${/parameter/@layout_code}_virtual_ds').setValidateEnable(false);
                }
            };
            
          
            
            //dataset字段更新事件
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
                if (check_releated_layout('VISIT_REPORT_MODIFY')) {
                    //
                    /*  var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                     var rec = $(ds_id).getAt(0);
                     if (ds.id == ds_id) {
                     if (name == 'tenant_id') {
                     rec.getField('contract_id').setLovPara('tenant_id', rec.get('tenant_id'));
                     }
                     }
                     var line_ds = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_lns');
                     for (var i = 0;i < $(line_ds).getAll().length;i++) {
                     $(line_ds).getAt(i).set('tenant_id', rec.get('tenant_id'));
                     } */
                }
                // add by shz 2017-11-02
                 if(check_releated_layout('CHANCE_MODIFY')){
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'prj_chance');
                    var business_type_n=record.get('business_type_n');
                    if (business_type_n == '保理业务'){
                         record.set('division','05');
                         record.set('division_n','保理');
                        record.getField('division_n').setReadOnly(true);
                        record.getField('division_n').setRequired(false);
                        record.getField('pre_rental_business').setRequired(false);
                        record.getField('pre_rental_business_n').setReadOnly(true);
                        record.getField('pre_rental_business_n').setRequired(false);
                        record.getField('annual_energy_output').setReadOnly(true);
                        record.getField('annual_energy_output').setRequired(false);
                        record.getField('annual_using_hours').setReadOnly(true);
                        record.getField('annual_using_hours').setRequired(false);
                        record.getField('total_investment').setReadOnly(true);
                        record.getField('total_investment').setRequired(false);
                        record.getField('static_investment').setReadOnly(true);
                        record.getField('static_investment').setRequired(false);
                        record.getField('prj_scale').setReadOnly(true);
                        record.getField('prj_scale').setRequired(false);
                    }
                }
            };
            
            //新增时调用(grid,table,gridBox)
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_grid_add'] = function(ds, record, config_records, bp_seq) {

                if (check_releated_layout('VISIT_REPORT_MODIFY')) {
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                    var rec = $(ds_id).getAt(0);
                    //
                    record.set('tenant_id', rec.get('tenant_id'));
                }
            
            
                if (check_releated_layout('CHANCE_MODIFY')) {
                    var records = ds.getAll(),
                        version_num = 0;
                    if (records.length) {
                        for (var i = 0;i < records.length;i++) {
                            var current_record = records[i],
                                version = current_record.get('version');
                            if (!Ext.isEmpty(version)) {
                                index = version.indexOf('v');
                                if (index == -1) {
                                    version_num = version_num;
                                } else {
                                    var current_version = version.substring(index + 1);
                                    if (isNaN(current_version) || parseFloat(current_version) <= parseFloat(version_num)) {
                                        version_num = version_num;
                                    } else {
                                        version_num = current_version;
                                    }
                                }
                            }
                        }
                    }
                    record.set('version', 'v' + parseInt(plus(version_num, 1)).toFixed(1));
                }
            
            };
            
            //加载时候调用
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_grid_load'] = function(ds, record, config_records, bp_seq) {
                if (check_releated_layout('VISIT_REPORT_MODIFY')) {
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                    var rec = $(ds_id).getAt(0);
            
                    var line_ds = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_lns');
                    if (ds.id == line_ds) {
                        for (var i = 0;i < ds.getAll().length;i++) {
                            ds.getAt(i).set('tenant_id', rec.get('tenant_id'));
                        }
                    }
                }
                //
                if(check_releated_layout('CHANCE_MODIFY')){
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'prj_chance');
                    var rec = $(ds_id).getAt(0);
                    var business_type_n=rec.get('business_type_n');
                    if (business_type_n == '保理业务'){
                        rec.getField('division').setReadOnly(true);
                        rec.getField('division').setRequired(false);
                        rec.getField('division_n').setReadOnly(true);
                        rec.getField('division_n').setRequired(false);
                        rec.getField('pre_rental_business').setRequired(false);
                        rec.getField('pre_rental_business_n').setReadOnly(true);
                        rec.getField('pre_rental_business_n').setRequired(false);
                        rec.getField('annual_energy_output').setReadOnly(true);
                        rec.getField('annual_energy_output').setRequired(false);
                        rec.getField('annual_using_hours').setReadOnly(true);
                        rec.getField('annual_using_hours').setRequired(false);
                        rec.getField('total_investment').setReadOnly(true);
                        rec.getField('total_investment').setRequired(false);
                        rec.getField('static_investment').setReadOnly(true);
                        rec.getField('static_investment').setRequired(false);
                        rec.getField('prj_scale').setReadOnly(true);
                        rec.getField('prj_scale').setRequired(false);
                    }
                }
            };
            
            function check_releated_layout(releated_table) {
                var str = '${/parameter/@layout_code}';
                //商机
                if (str.indexOf(releated_table) >= 0) {
                    return true;
                } else {
                    return false;
                }
            }
            
            function opwn_bp_more_info(name, record_id) {
                var prj_chance_bp_ds = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance_bp');
                var prj_chance_bp_record = $(prj_chance_bp_ds).findById(record_id);
                var param = prj_chance_bp_record.data;
                param['document_id'] = prj_chance_bp_record.get('bp_id');
                param['cond_para2'] = prj_chance_bp_record.get('bp_type');
                param['function_code'] = 'HLS306';
                param['function_usage'] = 'QUERY';
                param['url_title'] = '${l:HLS212.BP_MASTER_MAINTAIN}';
                hls_doc_get_layout_code('prj_chance_get_layout_code_link_id', param, 'hls_bp_master_modify_link', null, '${/parameter/@layout_code}', '${/parameter/@winid}');
            }
            
            function quote_link(type, record_id) {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window'];
                var prj_chance_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance');
                var prj_chance_record = $(prj_chance_ds_id).getAt(0);
                var prj_quotation_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_quotation');
                //var prj_quotation_record = $(prj_quotation_ds_id).getCurrentRecord();
                var prj_quotation_record = $(prj_quotation_ds_id).findById(record_id);
                var calc_recreate_L_formula;
                var saveData = [];
                if (type == 'quote') {
                    url = $('${/parameter/@layout_code}_hls_fin_calculator_update_link_id').getUrl();
                } else {
                    url = $('${/parameter/@layout_code}_hls_fin_calculator_readonly_link_id').getUrl();
                    new Leaf.Window({
                        id: '${/parameter/@layout_code}_hls_fin_calc_readonly_link_winid',
                        params: {
                            document_id: '${/parameter/@document_id}',
                            document_category: '${/parameter/@document_category}',
                            maintain_type: 'READONLY',
                            calc_session_id: prj_quotation_record.get('calc_session_id'),
                            winId: '${/parameter/@layout_code}_hls_fin_calc_readonly_link_winid',
                            global_flag: 'Y',
                            calc_type: 'CLASSIC_CALCULATOR'
                        },
                        url: url,
                        fullScreen: true,
                        draggable: true
                    });
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window'];
                    return;
                }
                prj_quotation_record.set('to_doc_table', 'HLS_FIN_CALCULATOR_HD');
                prj_quotation_record.set('function_code', '${/parameter/@function_code}');
                prj_quotation_record.set('function_usage', '${/parameter/@function_usage}');
                prj_quotation_record.set('document_id', prj_quotation_record.get('chance_id'));
            
                if (prj_quotation_record.get('quotation_id') && !prj_quotation_record.get('calc_session_id')) {
                    calc_recreate_L_formula = 'Y';
                    prj_quotation_record.set('_status', 'update');
                } else if (prj_quotation_record.get('quotation_id') && prj_quotation_record.get('calc_session_id')) {
                    calc_recreate_L_formula = 'N';
                    prj_quotation_record.set('_status', 'execute');
                } else if (!prj_quotation_record.get('chance_id')) {
                    Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                    return;
                } else {
                    calc_recreate_L_formula = 'Y';
                    prj_quotation_record.set('_status', 'insert');
                }
            
                prj_quotation_record.set('from_doc_table', 'PRJ_QUOTATION');
                prj_quotation_record.set('from_doc_pk', prj_quotation_record.get('quotation_id'));
                prj_quotation_record.set('calculate_flag', 'N');
                saveData.push(prj_quotation_record.data);
                Leaf.request({
                    url: $('${/parameter/@layout_code}_hls_fin_doc_quotation_link_id').getUrl(),
                    para: saveData,
                    success: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window'];
                        //$(prj_quotation_ds_id).setQueryParameter('chance_id', prj_quotation_record.get('chance_id'));
                        //$(prj_quotation_ds_id).query();
                        var quotation_id = prj_quotation_record.get('quotation_id') || res.result.quotation_id;
                        var win = new Leaf.Window({
                            id: '${/parameter/@layout_code}_hls_fin_calc_quotation_link_winid',
                            params: {
                                document_id: prj_quotation_record.get('chance_id'),
                                document_category: '${/parameter/@document_category}',
                                maintain_type: '${/parameter/@maintain_type}',
                                calc_session_id: res.result.record.calc_session_id,
                                quotation_id: quotation_id,
                                dsId: prj_quotation_ds_id,
                                winId: '${/parameter/@layout_code}_hls_fin_calc_quotation_link_winid',
                                global_flag: 'Y',
                                id_num: 0,
                                calc_type: '${/parameter/@calc_type}',
                                recreate_L_formula: calc_recreate_L_formula
                            },
                            url: url,
                            // title: '${l:HLS.FIN_CALCULATOR}',
                            fullScreen: true,
                            draggable: true
                        });
                        win.on('close', function() {
                            $(prj_quotation_ds_id).setQueryParameter('chance_id', prj_quotation_record.get('chance_id'));
                            //$(hls_fin_calculator_ln_ds_id).setQueryParameter('project_id', project_id);
                            $(prj_quotation_ds_id).query();
                            //$(hls_fin_calculator_ln_ds_id).query();
                            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
                });
            }
            //按钮渲染
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record, bp_seq) {
                //商机
                if (check_releated_layout('CHANCE')) {
                    if (!record.isNew) {
                        if (name.indexOf('quote') >= 0) {
                            return '<a href="javascript:quote_link(\'' + name + '\',\'' + record.id + '\')">报价</a>';
                        } else if (name === 'more') {
                            return '<a href="javascript:opwn_bp_more_info(\'' + name + '\',\'' + record.id + '\')">明细</a>';
                        }
                    }
                }
            
                //回访报告维护-承租人合同明细
                if (check_releated_layout('VISIT_REPORT_MODIFY')) {
                    if (name == 'tenant_contract_detail') {
                        return '<a href="javascript:open_visit_tenant_window(' + record.get('tenant_id') + ')">实际租金支付表</a>';
                    }
                }
            
                //处理附件上传超链接
                var link_function = '';
                window['${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
                if (name == 'attachment') {
                    link_function = '${/parameter/@layout_code}_hls213n_cdd_attachtment_upload';
                    return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '</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])) {
                                url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                            }
                        }
                        return url;
                    }
                }
            };
            
            
            window['${/parameter/@layout_code}_hls213n_cdd_attachtment_upload'] = function(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') {
                        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: '${/parameter/@layout_code}${/parameter/@tree_code}_hls213n_cdd_uploadFile_screen_id',
                        width: 850,
                        height: 400
                    });
                    win.on('close', function() {
                        record.ds.query();
                    });
                } else {
                    Leaf.showMessage('${l:HLS.PROMPT}', '请先保存!');
                }
            };
            
            
            
            //承租人合同明细子窗口
            
            function open_visit_tenant_window(tenant_id) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                var rec = $(ds_id).getAt(0);
                url = $('${/parameter/@tab_code}_con_contract_balance_link_id').getUrl();
                if (!rec.get('tenant_id')) {
                    Leaf.showMessage('${l:PROMPT}', '${l:CON801.MAINTAIN_TENANT_INFO_FIRST}');
                    return;
                }
                new Leaf.Window({
                    id: '${/parameter/@tab_code}_con_contract_balance_link_winid',
                    params: {
                        bp_id: rec.get('tenant_id'),
                        winid: '${/parameter/@tab_code}_con_contract_balance_link_winid'
                    },
                    url: url,
                    fullScreen: true,
                    draggable: true
                });
            }
            
            function submit_visit_report(visit_report_id) {
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/cont.CON801.con_visit_report_submit/execute',
                    para: {
                        visit_report_id: visit_report_id
                    },
                    success: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        Leaf.SideBar.enable = true;
                        Leaf.SideBar.show({
                            msg: '${l:HLS.SUBMIT_SUCCESS}',
                            duration: 2000
                        });
                        $('${/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
                });
            }
            
            //提交标志(全局)
            var submit_wfl_flag = 'N';
            
            function confirm_submit() {
                if (check_releated_layout('CHANCE')) {
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance');
                    if (ds_id) {
                        if (submit_wfl_flag == 'Y') {
                            var prj_chance_record = $(ds_id).getAt(0);
                            submit_wfl_flag = 'N';
                            //提交立项审批流
                            Leaf.request({
                                url: '${/request/@context_path}/autocrud/cont.CON500.con_contract_submit/update',
                                para: {
                                    document_id: prj_chance_record.get('chance_id'),
                                    document_category: '${/parameter/@document_category}',
                                    function_code: '${/parameter/@function_code}',
                                    function_usage: '${/parameter/@function_usage}'
                                },
                                success: function(res) {
                                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    Leaf.SideBar.enable = true;
                                    Leaf.SideBar.show({
                                        msg: '${l:HLS.SUBMIT_SUCCESS}',
                                        duration: 2000
                                    });
                                    $('${/parameter/@winid}').close();
                                },
                                failure: function() {
                                    submit_wfl_flag = 'N';
                                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                error: function() {
                                    submit_wfl_flag = 'N';
                                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                scope: this
                            });
                        }
                    }
                } //浮动利率提交
                else if (check_releated_layout('CONTRACT_INTEREST_FLOAT')) {
                    var root_ds = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
                    var req_record = root_ds.getAt(0);
                    if (root_ds.validate()) {
                        if (req_record.dirty) {
                            Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                            return;
                        }
                        var winid = '${/parameter/@winid}';
                        Leaf.showConfirm('${l:HLS.PROMPT}', '${l:HLS.ARE_YOU_SURE_TO_SUBMIT}', function() {
                            Leaf.request({
                                url: $('${/parameter/@layout_code}_con110_submit_link').getUrl(),
                                para: {
                                    change_req_id: '${/parameter/@change_req_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 if (check_releated_layout('VISIT_REPORT_MODIFY')) { // 回访报告提交
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                    var rec = $(ds_id).getAt(0);
                    if (!rec.get('visit_report_id')) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                        return;
                    }
                    submit_visit_report(rec.get('visit_report_id'));
                }
            }
            
            //保存前调用,生成单据编号
            window['${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function(ds, record) {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                window['${/parameter/@layout_code}_ignore_required_before_save']();
                //商机提交
                if (check_releated_layout('CHANCE')) {
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance');
                    var prj_chance_record = $(ds_id).getAt(0);
                    if (prj_chance_record.get('chance_id')) {
                        $('${/parameter/@layout_code}_virtual_ds').setSubmitParameter('document_id', prj_chance_record.get('chance_id'));
                        $('${/parameter/@layout_code}_virtual_ds').setSubmitParameter('document_category', 'CHANCE');
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return true;
                    } 
                    if (prj_chance_record.get('document_category') == 'CHANCE' && !prj_chance_record.get('chance_number')) {
                        Leaf.request({
                            url: $('${/parameter/@layout_code}_special_fields_link_id').getUrl(),
                            para: {
                                document_category: '${/parameter/@document_category}',
                                document_type: '${/parameter/@document_type}',
                                function_code: '${/parameter/@function_code}',
                                function_usage: '${/parameter/@function_usage}'
                            },
                            success: function(res) {
                                var document_number = res.result.document_number;
                                if (prj_chance_record.get('document_category') == 'CHANCE') {
                                    prj_chance_record.set('chance_number', document_number);
                                }
            
                            },
                            error: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            failure: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            sync: true,
                            scope: this
                        });
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return true;
                    } else {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return true;
                    }
            
                } else {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    return true;
                }
            
            };
            
            //保存submitsuccess调用
            window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'] = function(ds, record, res) {
                if (submit_wfl_flag == 'Y') {
                    window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                    Leaf.showConfirm('${HLS.PROMPT}', '${l:HLS.ARE_YOU_SURE_TO_SUBMIT}', confirm_submit, function() {
                        submit_wfl_flag = 'N';
                        submit_flag = 'N';
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    }).on('close', function() {
                        submit_wfl_flag = 'N';
                        submit_flag = 'N';
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    });
                } else if (check_releated_layout('CHANCE')) {
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance');
                    record = $(ds_id).getAt(0);
                    $('${/parameter/@layout_code}_ACY_prj_cdd_item_doc_ref_ds').setQueryParameter('cdd_list_id', record.get('cdd_list_id'));
                    $('${/parameter/@layout_code}_ACY_prj_cdd_item_doc_ref_ds').query();
                }
            };
            
            
            
            //保存方法
            window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK'] = function(nextStep) {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                var root_ds = $('${/parameter/@layout_code}_virtual_ds');
                window['${/parameter/@layout_code}_ignore_required_before_save']();
                if (root_ds.validate()) {
                    var winid = 'hls_doc_get_layout_code_winid';
                    var root_record = root_ds.getCurrentRecord();
                    root_ds.setSubmitParameter('layout_code', '${/parameter/@layout_code}');
            
                    function ON_LAYOUT_DYNAMIC_BEFORE_SAVE() {
                        root_ds.un('beforesubmit', ON_LAYOUT_DYNAMIC_BEFORE_SAVE);
                        var flag = false;
                        for (var name in root_record.data) {
                            if (root_record.data[name].data[0].ds && root_record.data[name].data[0].ds instanceof $L.DataSet) {
                                var current_record = root_record.data[name].data[0].ds.getCurrentRecord();
                                flag = window['${/parameter/@layout_code}_on_layout_dynamic_before_submit'](root_record.data[name].data[0].ds, current_record);
                            }
                        }
                        if (!flag) {
                            UN_SUBMIT_REF_FUNCTION();
                        }
                        return flag;
                    }
            
                    function UN_SUBMIT_REF_FUNCTION() {
                        root_ds.un('submitsuccess', ON_LAYOUT_DYNAMIC_INNER_SUBMITSUCCESS);
                        root_ds.un('submitfailed', ON_LAYOUT_DYNAMIC_INNER_SUBMITFAILED);
                        root_ds.un('submiterror', ON_LAYOUT_DYNAMIC_INNER_SUBMITERROR);
                    }
            
                    function ON_LAYOUT_DYNAMIC_INNER_SUBMITSUCCESS(ds, res) {
                        UN_SUBMIT_REF_FUNCTION();
                        window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'](ds, root_record, res);
                        if (typeof(nextStep) == 'function') {
                            nextStep(ds);
                        }
                    }
            
                    function ON_LAYOUT_DYNAMIC_INNER_SUBMITFAILED() {
                        UN_SUBMIT_REF_FUNCTION();
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            
                    }
            
                    function ON_LAYOUT_DYNAMIC_INNER_SUBMITERROR() {
                        UN_SUBMIT_REF_FUNCTION();
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            
                    }
                    root_ds.on('beforesubmit', ON_LAYOUT_DYNAMIC_BEFORE_SAVE);
                    root_ds.on('submitsuccess', ON_LAYOUT_DYNAMIC_INNER_SUBMITSUCCESS);
                    root_ds.on('submitfailed', ON_LAYOUT_DYNAMIC_INNER_SUBMITFAILED);
                    root_ds.on('submiterror', ON_LAYOUT_DYNAMIC_INNER_SUBMITERROR);
            
                    root_ds.submit();
                } else {
                    submit_wfl_flag = 'N';
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
            };
            
            
            window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
                submit_flag = 'Y';
                $('${/parameter/@layout_code}_virtual_ds').setValidateEnable(true);
                if (check_releated_layout('CHANCE')) { // 商机提交
                    submit_wfl_flag = 'Y';
                    window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']();
                } else if (check_releated_layout('VISIT_REPORT_MODIFY')) { // 回访报告提交
                    submit_wfl_flag = 'Y';
                    window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']();
                }
            };
            
            //上传按钮
            window['${/parameter/@layout_code}_upload_layout_dynamic_click'] = function() {
            
                if (check_releated_layout('VISIT_REPORT_MODIFY')) {
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                    var rec = $(ds_id).getAt(0);
                    if (!rec.get('visit_report_id')) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                        return;
                    } else {
                        var dynamic_base_table = '${/model/base_table_path/record/@base_table}'.toUpperCase();
                        var url = '${/request/@context_path}/uploadFile.lview?table_name=' + dynamic_base_table + '&header_id=' + rec.get('visit_report_id');
                        new Leaf.Window({
                            url: url,
                            title: '${l:HLS.SUPPORTING_DOCUMENT}',
                            id: '${/parameter/@attach_tab_code}_con500_cdd_uploadFile_screen_id',
                            width: 850,
                            height: 400
                        });
                    }
                }
            };
            
            
            function download_file(tmp_id, document_category, document_table, document_id, tab_group, file_name, file_path) {
                window.open($('download_file_link_id').getUrl() + '?tmp_id=' + tmp_id + '&document_category=' + document_category + '&document_table=' + document_table + '&document_id=' + document_id + '&tab_group=' + tab_group + '&file_name=' + file_name + '&file_path=' + file_path, "_self");
            }
            
            var g_flag = 'N';
            
            function sleep(numberMillis) {
                var now = new Date();
                var exitTime = now.getTime() + numberMillis;
                while (true) {
                    now = new Date();
                    if (now.getTime() > exitTime) {
                        g_flag = 'Y';
                        return;
                    }
                }
            }
            
            window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance');
                var prj_chance_result_ds = $(ds_id);
                var rec = prj_chance_result_ds.getCurrentRecord();
                if (rec.get('chance_id')) {
                    //Leaf.showConfirm('提示', '确定要打印选中项吗?', function() {
                    window['${/parameter/@layout_code}_lock_layout_dynamic_window'](); /* var templt_name = 'project_chance_report.xml'; */
                    var templt_name = 'prj_project_approved.xml';
                    var chance_id = rec.get('chance_id');
                    var chance_number = rec.get('chance_number');
                    var bp_id = 0;
                    var bp_id_d = 0;
                    if (typeof(rec.get('bp_id')) != "undefined") {
                        bp_id = rec.get('bp_id');
                    }
                    if (typeof(rec.get('bp_id_d')) != "undefined") {
                        bp_id_d = rec.get('bp_id_d');
                    }
                    var file_path = $('chance_report_ds').getAt(0).get('save_path');
                    var document_table = 'PRJ_CHANCE';
                    var document_id = chance_id;
                    var document_category = 'CHANCE';
                    var tab_group = '';
                    var file_name = chance_number + '项目立项报告.doc';
                    var description = '项目立项报告';
                    Leaf.request({
                        url: $('prj401_chance_report_print_link_1').getUrl(),
                        para: {
                            chance_id: chance_id,
                            chance_number: chance_number,
                            bp_id: bp_id,
                            bp_id_d: bp_id_d,
                            file_name: file_name,
                            templt_name: templt_name,
                            description: description,
                            file_path: file_path
                        },
                        success: function(res) {
                            if (!Ext.isEmpty(res.result.tmp_id)) {
                                Leaf.showConfirm('提示', '立项报告已经自动上传至支撑性文件,是否需要下载一份至本地?', function() {
                                    download_file(res.result.tmp_id, document_category, document_table, document_id, tab_group, file_name, file_path);
                                });
                                Leaf.request({
                                    url: '${/request/@context_path}/autocrud/prj.PRJ501N.bgfl_generate_word_tmp/execute',
                                    para: {
                                        document_category: document_category,
                                        document_table: document_table,
                                        document_id: document_id,
                                        tmp_id: res.result.tmp_id,
                                        tab_group: tab_group
                                    },
                                    success: function(res) {
                                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                        $('${/parameter/@layout_code}_ACY_prj_cdd_item_doc_ref_ds').query();
                                    },
                                    failure: function() {
                                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    error: function() {
                                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    sync: true,
                                    scope: this
                                });
                            } else {
                                Leaf.showErrorMessage('提示', '无返回结果,请重新生成!');
                            }
                        },
                        failure: function() {},
                        error: function() {},
                        scope: this
                    });
                    //});
                } else {
                    Leaf.showMessage('提示', '请先保存!');
                }
            };
            
            window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {            
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_visit_report_hds');
                var cvr_hds_result_ds = $(ds_id);
                var rec = cvr_hds_result_ds.getCurrentRecord();            
                if (rec.get('visit_report_id')) {
                    Leaf.showConfirm('提示', '确定要生成选中项吗?', function() {
                        window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                        var templt_name = 'sdic_managing_info_after_leasing.xml';
                        var visit_report_id = rec.get('visit_report_id');
                        var file_path = $('chance_report_ds').getAt(0).get('save_path');
                        var document_table = '${/model/base_table_path/record/@base_table}'.toUpperCase();
                        var document_id = visit_report_id;
                        var document_category = 'VISIT';
                        var tab_group = '';
                        var file_name = '回访报告.doc';
                        var description = '回访报告';
                        Leaf.request({
                            url: $('prj401_visit_report_print_link').getUrl(),
                            para: {
                                visit_report_id: visit_report_id,
                                file_name: file_name,
                                templt_name: templt_name,
                                description: description,
                                file_path: file_path
                            },
                            success: function(res) {
                                if (!Ext.isEmpty(res.result.tmp_id)) {
                                    download_file(res.result.tmp_id, document_category, document_table, document_id, tab_group, file_name, file_path);
                                    Leaf.request({
                                        url: '${/request/@context_path}/autocrud/prj.PRJ501N.bgfl_generate_word_tmp/execute',
                                        para: {
                                            document_category: document_category,
                                            document_table: document_table,
                                            document_id: document_id,
                                            tmp_id: res.result.tmp_id,
                                            tab_group: tab_group
                                        },
                                        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']();
                                        },
                                        sync: true,
                                        scope: this
                                    });
                                } else {
                                    Leaf.showErrorMessage('提示', '无返回结果,请重新生成!');
                                }
                            },
                            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('提示', '请先保存!');
                }
            };
            
            window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_chance');
                var prj_chance_ds = $(ds_id);
                var record = prj_chance_ds.getCurrentRecord();
                var chance_id = record.get('chance_id');   
                var templt_name ='letter_of_intention.xml';
               // var file_name =record.get('project_name');           	
                  if (chance_id) {                   
                    Leaf.Masker.unmask(Ext.getBody()); //解锁

                    // var url = $('letter_of_intention_link_id').getUrl() + '?templt_name=' + templt_name + '&chance_id=' + chance_id + '&file_name=' + file_name;
                     var url = $('letter_of_intention_link_id').getUrl() + '?templt_name=' + templt_name + '&chance_id=' + chance_id;
                    var form = document.createElement("form");
                    form.target = "word_export_window";
                    form.method = "post";
                    form.action = url;
                    var iframe = Ext.get('word_export_window') || new Ext.Template('<iframe id ="word_export_window" name="word_export_window" style="position:absolute;left:-10000px;top:-10000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true);
                    document.body.appendChild(form);
                    form.submit();
                    Ext.fly(form).remove();
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                };
            };
            //导出
            window['${/parameter/@layout_code}_user_button4_layout_dynamic_click'] = function() {
            
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_journal_detail');
                var grid_id = ds_id.replace('ds', 'layout_grid_id');
                $(grid_id)._export();
            
            
            };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
        <a:dataSets>
            <a:dataSet id="chance_report_ds">
                <a:datas dataSource="/model/chance_report_save_path"/>
            </a:dataSet>
        </a:dataSets>
    </a:view>
</a:screen>