<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Hongquan.Dai  代宏全
    $Date: 2018-1-22 上午10:36:25  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true">
    <a:init-procedure>
        <a:model-query model="hsbc.HSBC110.get_limited_user" rootPath="limited_user_path"/>
    </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="batch_hsbc_send_link" url="${/request/@context_path}/modules/hsbc/HSBC110/hsbc_paying_batch_send.lsc"/>
        <a:link id="batch_detail_link" url="${/request/@context_path}/modules/hsbc/HSBC110/hsbc_paying_batch_detail.lview"/>
        <a:link id="manual_modify_link" model="hsbc.HSBC110.hsbc_paying_manual_modify" modelaction="execute"/>
        <a:link id="batch_cancel_link" model="hsbc.HSBC110.hsbc_paying_batch_cancel" modelaction="delete"/>
        <script type="text/javascript"><![CDATA[

        function open_batch_win(ds_id, record_id) {
            var record = $(ds_id).findById(record_id);
            var param = record.data;
            param['function_code'] = 'HSBC110D';
            param['maintain_type'] = 'QUERY';
            param['url_title'] = '付款单明细';
            hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, 'batch_detail_link', ds_id);
        }

        window['${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record) {
            if (name == 'reqsn' && value) {
                return '<a href="javascript:open_batch_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
            }
            return value;
        };

        //代付单发送
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
            var ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'G_RESULT', 'hl_hsbc_paying_batch');
            var r_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'R_RESULT', 'hl_hsbc_paying_batch');
            var batch_ds = $(ds_id);
            var records = batch_ds.getSelected();

            var r_batch_ds = $(r_ds_id);
            var r_records = r_batch_ds.getSelected();

            if (records.length != 0 && r_records.length != 0) {
                // Leaf.showMessage('${l:PROMPT}', '不能同时选择铁甲付款单和退款付款单!');
                Leaf.showMessage('${l:PROMPT}', '不能同时选择二手机业务付款单和退款付款单!');
                return;
            }

            if (records.length == 0  && r_records.length == 0) {
                Leaf.showMessage('${l:PROMPT}', '请选择至少一条数据!');
                return;
            }
            var param = {};
            var saveData = [];
            var length = 0;
            var send_url;
            var bank_note='汇丰银行';

            if (records.length != 0){
                for (var i = 0;i < records.length;i++) {
                    var rec;
                    rec = records[i];
                    rec.set('_status', 'insert');
                    saveData.push(rec.data);
                }
                length = records.length;
            }else{
                for (var i = 0;i < r_records.length;i++) {
                    var rec;
                    rec = r_records[i];
                    rec.set('_status', 'insert');
                    saveData.push(rec.data);
                }
                length = r_records.length;
            }
            param['details'] = saveData;

            send_url=$('batch_hsbc_send_link').getUrl();

            Leaf.showConfirm('${l:HLS.PROMPT}', '共选中<font color="red">' + length + '</font>条付款单,确认发送到<font color="red">'+bank_note+'</font>?', function() {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: send_url,
                    para: param,
                    success: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        batch_ds.query();
                        r_batch_ds.query();
                    },
                    failure: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        batch_ds.query();
                    },
                    error: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        batch_ds.query();
                    },
                    scope: this
                });
            });
        };

        //人工支付  --add by 24976
        window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {
            var ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'G_RESULT', 'hl_hsbc_paying_batch');
            var r_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'R_RESULT', 'hl_hsbc_paying_batch');
            var batch_ds = $(ds_id);
            var records = batch_ds.getSelected();

            var r_batch_ds = $(r_ds_id);
            var r_records = r_batch_ds.getSelected();

            var lease_channel;
            var reqsn;
            var batch_id;
            var bank_item;

            if (records.length != 0 && r_records.length != 0) {
                // Leaf.showMessage('${l:PROMPT}', '不能同时选择铁甲付款单和退款付款单!');
                Leaf.showMessage('${l:PROMPT}', '不能同时选择二手机业务付款单和退款付款单!');
                return;
            }

            if (records.length != 1  && r_records.length == 0) {
                Leaf.showMessage('${l:PROMPT}', '请选择一条数据!');
                return;
            }

            if (records.length == 0  && r_records.length != 1) {
                Leaf.showMessage('${l:PROMPT}', '请选择一条数据!');
                return;
            }
            if (records.length != 0){
                reqsn = records[0].get('reqsn');
                batch_id = records[0].get('batch_id');
                bank_item=records[0].get('bank_item');
            }else {
                reqsn = r_records[0].get('reqsn');
                batch_id = r_records[0].get('batch_id');
                bank_item=r_records[0].get('bank_item');
            }

            var win = new Leaf.showConfirm('${l:HLS.PROMPT}', '使用人工支付将不能再使用银企直连,确认进行人工支付?', function() {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('manual_modify_link').getUrl(),
                    para: {
                        batch_id : batch_id,
                        reqsn : reqsn,
                        bank_item : bank_item
                    },
                    success: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        batch_ds.query();
                        r_batch_ds.query();
                    },
                    failure: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            });
        };

        //付款单取消
        window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
            var ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'G_RESULT', 'hl_hsbc_paying_batch');
            var r_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'R_RESULT', 'hl_hsbc_paying_batch');
            var batch_ds = $(ds_id);
            var records = batch_ds.getSelected();

            var r_batch_ds = $(r_ds_id);
            var r_records = r_batch_ds.getSelected();

            if (records.length != 0 && r_records.length != 0) {
                //Leaf.showMessage('${l:PROMPT}', '不能同时选择铁甲付款单和退款付款单!');
                Leaf.showMessage('${l:PROMPT}', '不能同时选择二手机业务付款单和退款付款单!');
                return;
            }

            if (records.length != 1  && r_records.length == 0) {
                Leaf.showMessage('${l:PROMPT}', '请选择一条数据!');
                return;
            }

            if (records.length == 0  && r_records.length != 1) {
                Leaf.showMessage('${l:PROMPT}', '请选择一条数据!');
                return;
            }
            if (records.length != 0){
                reqsn = records[0].get('reqsn');
                batch_id = records[0].get('batch_id');
                bank_item=records[0].get('bank_item');
            }else {
                reqsn = r_records[0].get('reqsn');
                batch_id = r_records[0].get('batch_id');
                bank_item=r_records[0].get('bank_item');
            }

            Leaf.showConfirm('${l:HLS.PROMPT}', '确认取消?', function() {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('batch_cancel_link').getUrl(),
                    para: {
                        batch_id : batch_id,
                        reqsn : reqsn,
                        bank_item : bank_item
                    },
                    success: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        batch_ds.query();
                        r_batch_ds.query();
                    },
                    failure: function(res) {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            });
        };

        Leaf.onReady(function() {
            if ('${/model/limited_user_path/record/@flag}'=='N') {
                $('${/parameter/@layout_code}_user_button1').hide();
                $('${/parameter/@layout_code}_user_button2').hide();
                $('${/parameter/@layout_code}_user_button3').hide();
            }
        });

        window['${/parameter/@layout_code}_on_layout_dynamic_grid_select'] = function(ds, record, bp_seq) {
            var form_ds_id = '${/parameter/@layout_code}_F_QUERY__ds';
            var formr_r_ds_id = '${/parameter/@layout_code}_R_F_QUERY__ds';
            var g_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'G_RESULT', 'hl_hsbc_paying_batch');
            var r_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'R_RESULT', 'hl_hsbc_paying_batch');
            var form_record = $(form_ds_id).getAt(0);
            var form_r_record = $(formr_r_ds_id).getAt(0);
            var g_records = $(g_ds_id).getSelected();
            var r_records = $(r_ds_id).getSelected();

            if (g_ds_id == ds.id) {
                var sum = 0;
                for (var i = 0;i < g_records.length;i++) {
                    sum = plus(sum, g_records[i].get('totalamt'));
                }
                form_record.set('total_unpaid_amount', sum);
            }

            if (r_ds_id == ds.id) {
                var sum = 0;
                for (var i = 0;i < r_records.length;i++) {
                    sum = plus(sum, r_records[i].get('totalamt'));
                }
                form_r_record.set('total_unpaid_amount', sum);
            }
        };

        window['${/parameter/@layout_code}_on_layout_dynamic_grid_unselect'] = function(ds, record, bp_seq) {
            var form_ds_id = '${/parameter/@layout_code}_F_QUERY__ds';
            var formr_r_ds_id = '${/parameter/@layout_code}_R_F_QUERY__ds';
            var g_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'G_RESULT', 'hl_hsbc_paying_batch');
            var r_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'R_RESULT', 'hl_hsbc_paying_batch');
            var form_record = $(form_ds_id).getAt(0);
            var form_r_record = $(formr_r_ds_id).getAt(0);
            var g_records = $(g_ds_id).getSelected();
            var r_records = $(r_ds_id).getSelected();

            if (g_ds_id == ds.id) {
                var sum = 0;
                for (var i = 0;i < g_records.length;i++) {
                    sum = plus(sum, g_records[i].get('totalamt'));
                }
                form_record.set('total_unpaid_amount', sum);
            }

            if (r_ds_id == ds.id) {
                var sum = 0;
                for (var i = 0;i < r_records.length;i++) {
                    sum = plus(sum, r_records[i].get('totalamt'));
                }
                form_r_record.set('total_unpaid_amount', sum);
            }
        };


        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>