<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: qwm  
    $Date: 2013-8-19 下午2:20:39  
    $Revision: 1.0  
    $Purpose: 提前结清维护页面
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:view package="leaf.ui.std" template="default">
        <a:link id="con_contract_et_calculate_link" url="${/request/@context_path}/modules/cont/CON701/con_contract_et_calculate.lsc"/>
        <a:link id="con_contract_et_save_link" model="cont.CON701.con_contract_et_save" modelaction="execute"/>
        <a:link id="con_contract_et_submit_link" model="cont.CON701.con_contract_et_submit" modelaction="execute"/>
        <a:link id="attachment_et_link" model="cont.CON701.con_contract_attachment_et" modelaction="update"/>
        <a:link id="con_contract_status" model="cont.CON702.con_contract_status" modelaction="update"/>
        <script src="${/request/@context_path}/javascripts/calculate.js" type="text/javascript"/>
        <script type="text/javascript"><![CDATA[
            function Screen_back() {
                $('${/parameter/@winId}').close();
            }
            
            function Screen_exit() {
                $('${/parameter/@winId}').close();
            }

            function contractCreateScreen_calculate() {
                var record = $('contract_et_Screen_mainDs').getCurrentRecord(),
                    et_ds = $('early_terminationDs');
                var et_record = et_ds.getAt(0);
                var et_agreement_id;
                if (et_record.data.et_agreement_id){
                    et_agreement_id = et_record.data.et_agreement_id;
                }
                if (record.validateRecord(true)) {
                    var termination_date = Leaf.formatDate(record.get ('termination_date'));
                    var contract_id = record.get('contract_id');
                    //sql里面设定了 et_type = ET
                    var et_type = record.get('et_type');
                    //和后端匹配，控制一种结清方式
                    var et_profile = "PRI_BAL_STD";

                    Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                    Leaf.request({
                        url: $('con_contract_et_calculate_link').getUrl(),
                        para: {
                            termination_date: termination_date,
                            contract_id: contract_id,
                            et_agreement_id : et_agreement_id,
                            et_type: et_type,
                            et_profile: et_profile
                        },
                        success: function(args) {
                            var et_agreement_id = args.result.et_agreement_id;
                            et_ds.setQueryParameter('et_agreement_id', et_agreement_id);
                            et_ds.query();

                            $('cashflowPlanDs').setQueryParameter('et_agreement_id', et_agreement_id);
                            $('cashflowPlanDs').query();

                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        failure: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        error: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        scope: this
                    });
                }
            }

            function et_save(value) {
                var et_ds = $('early_terminationDs'),
                    et_record = et_ds.getAt(0);
                var et_agreement_id = et_record.get('et_agreement_id');
                if (et_agreement_id) {
                    var deposit_deduction_flag = et_record.get('deposit_deduction_flag');
                    var deposit_by_agent = et_record.get('deposit_by_agent');

                    var ref_n04 = et_record.get('ref_n04');
                    var ref_n05 = et_record.get('ref_n05');

                    var remittance_date = et_record.get('remittance_date');
                    var remittance_amount = et_record.get('remittance_amount');

                    var reduce_period_interest = et_record.get('reduce_period_interest');
                    var reduce_residual_value = et_record.get('reduce_residual_value');

                    var deposit = et_record.get('deposit');
                    var third_party_deposit = et_record.get('third_party_deposit');
                    var note_s = et_record.get('note_s');
                    var cost_reduction = et_record.get('cost_reduction');
                    Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                    Leaf.request({
                        url: $('con_contract_et_save_link').getUrl(),
                        para: {
                            et_agreement_id: et_agreement_id,
                            deposit_deduction_flag : deposit_deduction_flag,
                            deposit_by_agent:deposit_by_agent,
                            ref_n04: ref_n04,
                            ref_n05: ref_n05,
                            reduce_residual_value:reduce_residual_value,
                            reduce_period_interest:reduce_period_interest,
                            deposit: deposit,
                            third_party_deposit:third_party_deposit,
                            note_s: note_s,
                            cost_reduction: cost_reduction,
                            remittance_date: remittance_date,
                            remittance_amount: remittance_amount
                        },
                        success: function(args) {
                            save_flag = true;
                            if (value == 'sub') {
                                et_submit();
                            } else if (value === 'calc') {
                                contractCreateScreen_calculate();
                            } else {
                                et_ds.setQueryParameter('et_agreement_id', et_agreement_id);
                                et_ds.query();
                                $('cashflowPlanDs').setQueryParameter('et_agreement_id', et_agreement_id);
                                $('cashflowPlanDs').query();
                                Leaf.SideBar.show({
                                    msg: '保存成功！',
                                    duration: 2000
                                });
                            }
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        failure: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        error: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        scope: this
                    });

                } else {
                    Leaf.showWarningMessage('提示', '请先测算！');
                }
            }

            function post(xml) {
                Leaf.request({
                    url: '${/request/@context_path}/doodream/post/url',
                    para: {
                        xml: xml
                    },
                    success: function(res) {
                        window.open(res.message, "", "width=940, height=650, scrollbars=yes, toolbar=no, location=no, status=no, menubar=no");
                        Leaf.SideBar.show({
                            msg: '${l:HLS.SUBMIT_SUCCESS}',
                            duration: 2000
                        });
                    },
                    failure: function() {
                        Leaf.SideBar.show({
                            msg: 'failure',
                            duration: 2000
                        });
                    },
                    error: function() {
                        Leaf.SideBar.show({
                            msg: 'error',
                            duration: 2000
                        });
                    },
                    scope: this
                });
            }

            //调用doodream 审批接口
            function et_submit() {
                var et_ds = $('early_terminationDs'),
                    et_record = et_ds.getAt(0);
                var ref_n04 = et_record.get('ref_n04');
                var ref_n05 = et_record.get('ref_n05');
                var int_rate_display = et_record.get('int_rate_display');

                    var win = Leaf.showConfirm('${l:HLS.PROMPT}', '确定要提交？', function () {
                        Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                        if (ref_n04 == 0 && ref_n05 == 0) {
                            Leaf.request({
                                url: $('con_contract_status').getUrl(),
                                para: {
                                    et_agreement_id: et_record.get('et_agreement_id')
                                },
                                success: function(args) {
                                Leaf.showMessage('${l:PROMPT}', '提交成功');
                                },
                                failure: function() {

                                },
                                error: function() {

                                },
                                scope: this,
                                sync: true
                            });
                        }else if (et_record) {
                            var et_agreement_id = et_record.get('et_agreement_id');
                            Leaf.request({
                                url: $('con_contract_et_submit_link').getUrl(),
                                para: {
                                    et_agreement_id: et_agreement_id
                                },
                                success: function (args) { //
                                    //post xml to doodream
                                    post(args.result.xml);
                                    Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                                },
                                failure: function () {
                                    Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                                },
                                error: function () {
                                    Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                                },
                                scope: this
                            });
                        } else {
                            Leaf.showMessage('提示', '请先测算！');
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        }
                    }, function () {
                        Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                    });
                    win.addListener('close', function () {
                        Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                    });

            }
            
            function et_submit_entrance() {
                $('et_calc_entrance').disable();
                $('et_save').disable();
                $('et_submit_entrance').disable();
                et_save();
                var et_ds = $('early_terminationDs'),
                    et_record = et_ds.getAt(0);
                var et_agreement_id = et_record.get('et_agreement_id');
                Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                Leaf.request({
                    url: $('attachment_et_link').getUrl(),
                    para: {
                        table_name: 'CON_CONTRACT_ET_HD',
                        table_pk_value: et_agreement_id
                    },
                    success: function(res) {
                        var attachment_num = res.result.attachment_num;
                        if (attachment_num == 0) {
                            et_save('sub');
                        } else {
                            et_save('sub');
                        }
                    },
                    failure: function() {
                    },
                    error: function() {
                    },
                    scope: this
                });
            }
            
            function et_calc_entrance() {
                et_save('calc');
            }
            

        ]]></script>
        <a:dataSets>
            <a:dataSet id="deposit_deduction_ds" lookupCode="YES_OR_NO"/>
            <a:dataSet id="early_terminationDs" model="cont.CON701.con_contract_et_hd">
                <a:fields>
                    <a:field name="deposit_deduction_flag"/>
                    <a:field name="deposit_deduction_flag_desc" displayField="code_value_name" options="deposit_deduction_ds" returnField="deposit_deduction_flag" valueField="code_value"/>
                    <a:field name="deposit_by_agent"/>
                    <a:field name="deposit_by_agent_n" displayField="code_value_name" options="deposit_deduction_ds" returnField="deposit_by_agent" valueField="code_value"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="cashflowPlanDs" autoPageSize="true" model="cont.CON701.contract_et_cashflow_ln">
                <a:fields>
                    <a:field name="due_date" datatype="date"/>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:switch test="/parameter/@function_usage">
                <a:case value="QUERY">
                    <a:screenTopToolbar>
                        <a:gridButton click="Screen_exit" text="HLS.EXIT"/>
                    </a:screenTopToolbar>
                </a:case>
                <a:case value="*">
                    <a:screenTopToolbar>
                        <a:gridButton click="Screen_exit" text="HLS.EXIT"/>
                        <a:gridButton id="et_calc_entrance" click="et_calc_entrance" text="测算"/>
                        <a:gridButton id="et_save" click="et_save" text="HLS.SAVE"/>
                        <a:gridButton id="et_submit_entrance" click="et_submit_entrance" text="提交"/>
                    </a:screenTopToolbar>
                </a:case>
            </a:switch>
            <a:form column="4" labelWidth="110" marginWidth="30" title="基础信息">
                <a:textField name="contract_number" bindTarget="contract_et_Screen_mainDs" prompt="合同编号" readOnly="true"/>
                <a:textField name="bp_name" bindTarget="contract_et_Screen_mainDs" prompt="承租人名称" readOnly="true"/>
                <a:textField name="bp_agent" bindTarget="contract_et_Screen_mainDs" prompt="代理商名称" readOnly="true"/>
                <a:datePicker name="termination_date" bindTarget="contract_et_Screen_mainDs" prompt="中途偿还基准日期" renderer="Leaf.formatDate"/>
            </a:form>
            <a:form title="结清信息">
                <div id="query_form">
                    <a:form column="4" labelWidth="110" marginWidth="30">
                        <a:numberField name="undue_principal" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="未到期本金" readOnly="true"/>
                        <a:numberField name="ref_n01" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期本金" readOnly="true"/>
                        <a:numberField name="ref_n02" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期分期手续费" readOnly="true"/>
                        <a:numberField name="ref_n08" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="预付金额" readOnly="true"/>


                        <a:numberField name="ref_n03" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="期间利息" readOnly="true"/>
                        <a:numberField name="ref_n06" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期利息" readOnly="true"/>
                        <a:numberField name="ref_n07" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="未到期分期手续费" readOnly="true"/>
                        <a:numberField name="et_total_amount" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还结清总金额" readOnly="true"/>

                        <a:numberField name="et_fee" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还手续费" readOnly="true"/>
                        <a:numberField name="penalty" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="罚息" readOnly="true"/>
                        <a:numberField name="residual_value" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="留购金" readOnly="true"/>
                        <a:numberField name="deferred_interest" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="延期利息" readOnly="true"/>

                        <a:numberField name="ref_n05" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还手续费减免金额"/>
                        <a:numberField name="ref_n04" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="罚息减免金额"/>
                        <a:numberField name="deposit" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="客户保证金" readOnly="true"/>
                        <a:comboBox name="deposit_deduction_flag_desc" bindTarget="early_terminationDs" prompt="客户保证金是否抵扣"/>

                        <a:numberField name="reduce_period_interest" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="期间利息减免金额" />
                        <a:numberField name="reduce_residual_value" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="留购金减免金额" />
                        <a:numberField name="third_party_deposit" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="代理商代付保证金金额" />
                        <a:comboBox name="deposit_by_agent_n" bindTarget="early_terminationDs" prompt="代理商代付保证金是否抵扣" />

                        <a:datePicker name="remittance_date"  bindTarget="early_terminationDs"  prompt="汇款日期" readOnly="false"/>
                        <a:numberField name="remittance_amount" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="汇款金额" readOnly="false"/>

                    </a:form>
                    <a:form column="1" labelWidth="110" marginWidth="30">
                        <a:textArea name="note_s" width="800" bindTarget="early_terminationDs"  prompt="申请原因说明/背景"/>
                        <a:textArea name="cost_reduction" width="800" bindTarget="early_terminationDs"  prompt="费用减免说明"/>
                    </a:form>
                </div>
                <div id="update_form">
                    <a:form column="4" labelWidth="110" marginWidth="30">
                        <a:numberField name="undue_principal" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="未到期本金" readOnly="true"/>
                        <a:numberField name="ref_n01" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期本金" readOnly="true"/>
                        <a:numberField name="ref_n02" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期分期手续费" readOnly="true"/>
                        <a:numberField name="ref_n08" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="预付金额" readOnly="true"/>

                        <a:numberField name="ref_n03" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="期间利息" readOnly="true"/>
                        <a:numberField name="ref_n06" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期利息" readOnly="true"/>
                        <a:numberField name="ref_n07" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="未到期分期手续费" readOnly="true"/>
                        <a:numberField name="et_total_amount" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还结清总金额" readOnly="true"/>

                        <a:numberField name="et_fee" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还手续费" readOnly="true"/>
                        <a:numberField name="penalty" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="罚息" readOnly="true"/>
                        <a:numberField name="residual_value" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="留购金" readOnly="true"/>
                        <a:numberField name="deferred_interest" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="延期利息" readOnly="true"/>

                        <a:numberField name="ref_n05" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还手续费减免金额"/>
                        <a:numberField name="ref_n04" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="罚息减免金额"/>
                        <a:numberField name="deposit" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="客户保证金" readOnly="true"/>
                        <a:comboBox name="deposit_deduction_flag_desc" bindTarget="early_terminationDs" prompt="客户保证金是否抵扣"/>

                        <a:numberField name="reduce_period_interest" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="期间利息减免金额" />
                        <a:numberField name="reduce_residual_value" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="留购金减免金额" />
                        <a:numberField name="third_party_deposit" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="代理商代付保证金金额" />
                        <a:comboBox name="deposit_by_agent_n" bindTarget="early_terminationDs" prompt="代理商代付保证金是否抵扣" />

                        <a:datePicker name="remittance_date"  bindTarget="early_terminationDs"  prompt="汇款日期" readOnly="false"/>
                        <a:numberField name="remittance_amount" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="汇款金额" readOnly="false"/>

                    </a:form>
                    <a:form column="1" labelWidth="110" marginWidth="30">
                        <a:textArea name="note_s" width="800" bindTarget="early_terminationDs"  prompt="申请原因说明/背景"/>
                        <a:textArea name="cost_reduction" width="800" bindTarget="early_terminationDs"  prompt="费用减免说明"/>
                    </a:form>
                </div>
                <a:tabPanel marginHeight="400" marginWidth="30">
                    <a:tabs>
                        <a:tab prompt="资金计划" width="110">
                            <a:grid bindTarget="cashflowPlanDs" marginHeight="430" marginWidth="100" navBar="true">
                                <a:columns>
                                    <a:column name="times" prompt="期数" align="center"/>
                                    <a:column name="cf_item_n" prompt="现金流类型" align="center"/>
                                    <a:column name="due_date" prompt="预定支付日期" align="center" renderer="Leaf.formatDate"/>
                                    <a:column name="due_amount" align="right" prompt="应收金额" renderer="Leaf.formatMoney"/>
                                    <a:column name="received_amount" align="right" prompt="已收金额" renderer="Leaf.formatMoney"/>
                                    <a:column name="received_principal" align="right" prompt="已收本金" renderer="Leaf.formatMoney"/>
                                    <a:column name="received_interest" align="right" prompt="已收利息" renderer="Leaf.formatMoney"/>
                                    <a:column name="cf_status_n" prompt="现金流状态" align="center"/>
                                </a:columns>
                            </a:grid>
                        </a:tab>
                    </a:tabs>
                </a:tabPanel>
            </a:form>
        </a:screenBody>
        <script type="text/javascript"><![CDATA[
            Leaf.onReady(init);

            function init() {
                var record = $('contract_et_Screen_mainDs').getCurrentRecord();
                $('cashflowPlanDs').setQueryParameter('et_agreement_id', record.get('et_agreement_id'));
                $('cashflowPlanDs').query();

                $('early_terminationDs').setQueryParameter('et_agreement_id', record.get('et_agreement_id'));
                $('early_terminationDs').query();

                if ('${/parameter/@function_usage}' == 'QUERY') {
                    record.getField('termination_date').setReadOnly(true);
                    //如果是查询界面，显示只读form
                    document.getElementById("update_form").style.display = "none";
                }else{
                    //反之显示可操作form
                    document.getElementById("query_form").style.display = "none";
                }
            }
        ]]></script>
    </a:view>
</a:screen>
