<?xml version="1.0" encoding="UTF-8"?>
<!--
    2019-9-6  Spencer 3893  合同提前结清申请创建明细
 -->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure/>
    <a:view>
        <a:link id="con_contract_repo_detail_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="con_et002_submit_link" model="cont.CON_ET001.submit_change_req" modelaction="execute"/>
        <a:link id="con_et002_cancel_link" model="cont.CON731.cancel_change_req" modelaction="execute"/>
        <a:link id="con_et001_calc_cashflow_link" model="cont.CON_ET001.calc_cashflow" modelaction="execute"/>
        <a:link id="con_et001_calc_cashflow_link1" model="cont.CON_ET001.insert_calc_cashflow" modelaction="execute"/>
        <a:link id="con_et001_calc_et_date_amount_link" model="cont.CON_ET001.calc_et_date_amount" modelaction="execute"/>
        <script type="text/javascript"><![CDATA[
        var calc_flag='Y';
        //新增和加载时调用form
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_form_add_and_load'] = function(ds, record, config_records, bp_seq) {

        };

        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
            debugger;
            if(value!=old_value){
                calc_flag='N';
            }
            var ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'F_BASE_01', 'con_contract_change_req');
            if (ds == $(ds_id)) {
                if (name == 'termination_date'&&value) {
                    // 根据结清日计算结清相关金额字段
                    Leaf.request({
                        url: $('con_et001_calc_et_date_amount_link').getUrl(),
                        para: {
                            contract_id: record.get('contract_id'),
                            termination_date: value,
                            et_fee:record.get('et_fee'),
                            et_interest_rate:record.get('et_interest_rate'),
                            residual_value:record.get('residual_value')
                        },
                        success: function (res) {
                           //record.set('et_fee', res.result.et_fee);
                            record.set('et_total_amount', res.result.et_total_amount);
                            record.set('et_due_amount', res.result.et_due_amount);
                            record.set('overdue_amount', res.result.overdue_amount||0);
                            record.set('penalty', res.result.penalty||0);
                            record.set('fund_possession_time', res.result.fund_possession_time);
                            record.set('fund_possession_cost', res.result.fund_possession_cost);
                            record.set('fund_possession_rate', res.result.fund_possession_rate);
                            record.set('last_rent_due_date',res.result.last_rent_due_date);
                            record.set('sum_unreceived_principal',res.result.sum_unreceived_principal);
                        },
                        failure: function () {
                            record.set('termination_date','');
                            record.set('et_total_amount','');
                            record.set('et_due_amount','');
                            record.set('overdue_amount', '');
                            record.set('penalty', '');
                            record.set('fund_possession_time','');
                            record.set('fund_possession_cost','');
                            record.set('fund_possession_rate','');
                            record.set('last_rent_due_date','');
                            record.set('sum_unreceived_principal','');
                        },
                        error: function () {
                            record.set('termination_date','');
                            record.set('et_total_amount','');
                            record.set('et_due_amount','');
                            record.set('overdue_amount', '');
                            record.set('penalty', '');
                            record.set('fund_possession_time','');
                            record.set('fund_possession_cost','');
                            record.set('fund_possession_rate','');
                            record.set('last_rent_due_date','');
                            record.set('sum_unreceived_principal','');
                        },
                        scope: this
                    });
                }

            }
        };

        window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
            $('${/parameter/@layout_code}_submit_approval').disable();
                var req_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_BASE_01', 'con_contract_change_req');
                var req_record = $(req_ds_id).getAt(0);
                var case_type = '';
                var document_type = '${/parameter/@document_type}';
                var calc_session_id = req_record.get('calc_session_id');

               // window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                if ($(req_ds_id).validate($('${/parameter/@layout_code}_submit_approval').enable())) {
                    if (req_record.dirty) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                     //   window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return;
                    }
                    if (calc_flag=='N') {
                        Leaf.showMessage('${l:HLS.PROMPT}', '请先计算！');
                   //     window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return;
                    }
                    $('${/parameter/@layout_code}_submit_approval').disable();
                    $('${/parameter/@layout_code}_save').disable();
                    $('${/parameter/@layout_code}_user_button1').disable();
                    $('${/parameter/@layout_code}_user_button3').disable();
                    //setTimeout(window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK'](con_repo001_submit), 2000);
                 // window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK'](con_repo001_submit);
                    con_repo001_submit();
                } else {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
        };
        function con_repo001_submit() {
            Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认提交审批？', function () {
            var req_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_BASE_01', 'con_contract_change_req');
            var req_record = $(req_ds_id).getAt(0);
           // window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
            Leaf.request({
                url: $('con_et002_submit_link').getUrl(),
                para: {
                    change_req_id: ${/parameter/@change_req_id},
                    layout_code: '${/parameter/@layout_code}'
                },
                success: function(res) {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_submit_approval').enable();
                    $('${/parameter/@layout_code}_save').enable();
                    $('${/parameter/@layout_code}_user_button1').enable();
                    $('${/parameter/@layout_code}_user_button3').enable();
                    $('${/parameter/@winid}').close();

                },
                failure: function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_submit_approval').enable();
                    $('${/parameter/@layout_code}_save').enable();
                    $('${/parameter/@layout_code}_user_button1').enable();
                    $('${/parameter/@layout_code}_user_button3').enable();

                },
                error: function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_submit_approval').enable();
                    $('${/parameter/@layout_code}_save').enable();
                    $('${/parameter/@layout_code}_user_button1').enable();
                    $('${/parameter/@layout_code}_user_button3').enable();

                },
                scope: this
            });
            }, null, 300, 150);
        }

        //保存submitsuccess调用
        window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'] = function(ds, record, res, bp_seq) {
            var ca_ds_id_s = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'G_QUOTATION_03', 'con_contract_cashflow');
            if (ca_ds_id_s) {
                $(ca_ds_id_s).query();
            }
            var ca_ds_id_c = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'G_QUOTATION_04', 'con_contract_cashflow');
            if (ca_ds_id_c) {
                $(ca_ds_id_c).query();
            }
            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
        };
        //报价计算
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
            $('${/parameter/@layout_code}_user_button1').disable(); //按钮不可用
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var con_contract_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'F_BASE_01', 'con_contract_change_req');
            var con_record = $(con_contract_ds_id).getAt(0);
            if ($(con_contract_ds_id).validate()) {
                if (con_record.dirty) {
                    Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                    return;
                }
              clac();
            } else {
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
            }
        };

        function clac(){
            Leaf.request({
                url: $('con_et001_calc_cashflow_link1').getUrl(),
                para: {
                    change_req_id: '${/parameter/@change_req_id}',
                },
                success: function(res) {
                    Leaf.SideBar.enable = true;
                    Leaf.SideBar.show({msg: '计算成功！',duration: 2000});
                    calc_flag='Y';
                    //con_record.getField('ccr_start_times').setReadOnly(true);
                    var ca_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'G_QUOTATION_02', 'con_contract_cashflow');
                    if (ca_ds_id) {
                        $(ca_ds_id).query();
                    }
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                },
                failure: function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                },
                error: function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                },
                scope: this
            });
        }

        //取消变更
        window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
            $('${/parameter/@layout_code}_user_button3').disable(); //按钮不可用
            Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认取消变更？', function() {
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('con_et002_cancel_link').getUrl(),
                    para: {
                        change_req_id: ${/parameter/@change_req_id}
                    },
                    success: function(res) {
                        $('${/parameter/@layout_code}_user_button3').enable(); //按钮启用
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $('${/parameter/@winid}').close();
                    },
                    failure: function() {
                        $('${/parameter/@layout_code}_user_button3').enable(); //按钮启用
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function() {
                        $('${/parameter/@layout_code}_user_button3').enable(); //按钮启用
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            });
        };
        window['${/parameter/@layout_code}_on_layout_dynamic_grid_load'] = function (ds, qpara, bp_seq) {

        };

        //图片渲染
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_dynamic_pic_renderer'] = function(record, name, bp_seq) {
            var result = name.match(/(.*)_pic$/);
            if (result) {
                var pic_value = record.get(result[1]);
                if (pic_value == 'OUTFLOW') {
                    return '<img src="${/request/@context_path}/images/outflow.png" style="margin-top:4px"/>';
                } else if (pic_value == 'INFLOW') {
                    return '<img src="${/request/@context_path}/images/inflow.png" style="margin-top:4px"/>';
                } else if (pic_value == 'NONCASH') {
                    return '<img src="${/request/@context_path}/images/noncash.png" style="margin-top:4px"/>';
                } else if (pic_value == 'CASH') {
                    return '<img src="${/request/@context_path}/images/cash.png" style="margin-top:4px"/>';
                }
            }
        };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>