<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Eugene Song $
    $Date: 2018年11月2日10:19:17 $
    $Revision: 1.1 $
    $Purpose: 网银代扣管理
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure>
        <a:model-query autoCount="false" fetchAll="true" model="ccb.CCB600.hls_ebank_bank" rootPath="bank_name"/>
    </a:init-procedure>
    <a:view>
        <a:link id="create_ebank_link" url="${/request/@context_path}/modules/ccb/CCB600/ccb_ebank_create.lview"/>
        <a:link id="query_ebank_link" url="${/request/@context_path}/modules/ccb/CCB600/ccb_ebank_query.lview"/>
        <a:link id="delete_ebank_link" model="ccb.CCB600.hls_ebank_batch" modelaction="delete"/>
        <a:link id="create_abc_ebank_link" model="ccb.CCB600.hls_ebank_batch_hd" modelaction="execute"/>
        <a:link id="query_abc_ebank_link" model="ccb.CCB600.hls_ebank_batch_hd" modelaction="update"/>
        <a:link id="withhold_ebank_link"
                url="${/request/@context_path}/modules/ccb/CCB600/ccb_ebank_withhold_manager.lview"/>
        <a:link id="ccb_abc_upload_link"
                url="${/request/@context_path}/modules/ccb/CCB600/ccb_abc_import.lview"/>
        <script type="text/javascript"><![CDATA[
        var header = $jq('meta[name=_csrf_header]').attr('content');
        var token = $jq('meta[name=_csrf]').attr('content');
        $jq(document).ajaxSend(function (e, xhr, options) {
            xhr.setRequestHeader(header, token);
        });

        function open_batch() {
            var win = new Leaf.Window({
                id: 'create_ebank_link_winId',
                url: $('create_ebank_link').getUrl(),
                params: {
                    winid: 'create_ebank_link_winId'
                },
                title: '生成代扣指令',
                draggable: true,
                width: 900,
                height: 680
            });
            win.on('close', function () {
                $('hls_ccb_batch_result_ds').query();
            });
        }

        function post_batch() {
            var ds = $('hls_ccb_batch_result_ds');
            var records = ds.getSelected();
            if (records.length == 0) {
                Leaf.showMessage('${l:HLS.PROMPT}', '至少有一条数据');
                return;
            } else {
                var status = records[0].get('status');
                var batch_id = records[0].get('batch_id');
                var bank_code = records[0].get('bank_code');
                if (status != 'NEW' && status != 'POST_FAIL') {
                    Leaf.showMessage('${l:HLS.PROMPT}', '只有新建或推送失败的数据才能发送!');
                    return;
                } else {
                    Leaf.Masker.mask(Ext.getBody(), '发送代扣指令...'); //锁屏
                    if (bank_code == '008') {
                        Leaf.request({
                            url: $('create_abc_ebank_link').getUrl(),
                            para: {
                                batch_id: batch_id
                            },
                            success: function (res) {
                                Leaf.SideBar.show({
                                    msg: '操作成功',
                                    duration: 2000
                                });
                                Leaf.Masker.unmask(Ext.getBody()); //解锁
                                $('hls_ccb_batch_result_ds').query();

                            },
                            failure: function () {
                            },
                            error: function () {
                            },
                            scope: this
                        });
                    } else if (bank_code == '002') {
                        $jq.ajax({
                            url: '${/request/@context_path}/icbc/interface/record/submitorder',
                            contentType: 'application/x-www-form-urlencoded;charset=utf8',
                            type: 'post',
                            data: {
                                batchId: batch_id,
                                mode: 'PRODUCT_MODE'
                            },
                            success: function (datas) {
                                if (datas.success) {
                                    Leaf.SideBar.show({
                                        msg: '操作成功',
                                        duration: 2000
                                    });
                                    Leaf.Masker.unmask(Ext.getBody()); //解锁
                                    $('hls_ccb_batch_result_ds').query();
                                } else {
                                    Leaf.Masker.unmask(Ext.getBody()); //解锁
                                    Leaf.showErrorMessage('提示', datas.message);
                                }
                            }
                        })
                    }
                }
            }

        }

        function write_off_batch() {
            var record = $('hls_ccb_batch_result_ds').getCurrentRecord();
            var batch_id = record.get("batch_id");
            var bank_code = record.get("bank_code");
            var status_desc = record.get("status_desc");
            if (status_desc == '新建') {
                alert('新建状态无法回传!');
                $('hls_ccb_batch_result_ds').query();
            } else {
                Leaf.Masker.mask(Ext.getBody(), '核销代扣信息中...'); //锁屏
                if (bank_code == '008') {
                    Leaf.request({
                        url: $('query_abc_ebank_link').getUrl(),
                        para: {
                            batch_id: batch_id
                        },
                        success: function (res) {
                            Leaf.SideBar.show({
                                msg: '查询成功',
                                duration: 2000
                            });
                            Leaf.Masker.unmask(Ext.getBody()); //解锁
                            $('hls_ccb_batch_result_ds').query();
                        },
                        failure: function () {
                        },
                        error: function () {
                        },
                        scope: this
                    });

                } else if (bank_code == '002') {
                    $jq.ajax({
                        url: '${/request/@context_path}/icbc/interface/record/queryorder',
                        data: {
                            batchId: batch_id
                        },
                        type: 'post',
                        contentType: 'application/x-www-form-urlencoded;charset=utf8',
                        success: function (datas) {
                            if (datas.success) {
                                Leaf.SideBar.show({
                                    msg: '工行查询成功',
                                    duration: 2000
                                });
                            } else {
                                Leaf.showErrorMessage('提示', datas.message);
                            }

                            Leaf.Masker.unmask(Ext.getBody()); //解锁
                            $('hls_ccb_batch_result_ds').query();

                        }
                    });
                }
            }
        }


        function delete_batch() {
            var ds = $('hls_ccb_batch_result_ds');
            var records = ds.getSelected();
            if (records.length == 0) {
                Leaf.showMessage('${l:HLS.PROMPT}', '至少有一条数据');
                return;
            } else {
                var status = records[0].get('status');
                var batch_id = records[0].get('batch_id');
                if (status != 'NEW' && status != 'POST_FAIL') {
                    Leaf.showMessage('${l:HLS.PROMPT}', '只有新建的数据才能删除!');
                    return;
                } else {
                    Leaf.Masker.mask(Ext.getBody(), '删除中...'); //锁屏
                    Leaf.request({
                        url: $('delete_ebank_link').getUrl(),
                        para: {
                            batch_id: batch_id
                        },
                        success: function (res) {

                            Leaf.SideBar.show({
                                msg: '操作成功',
                                duration: 2000
                            });
                            Leaf.Masker.unmask(Ext.getBody()); //解锁
                            $('hls_ccb_batch_result_ds').query();

                        },
                        failure: function () {
                        },
                        error: function () {
                        },
                        scope: this
                    });
                }
            }

        }

        function detail_renderer(value, record, name) {

            var status = record.get('status');
            return '<a href="javascript:open_detail_window(' + record.get('batch_id') + ',\'' + status + '\')">' + '${l:PROMPT.DETAIL}' + '</a>';
        }

        function open_detail_window(batch_id, status) {
            var win = new Leaf.Window({
                id: 'query_ebank_link_winId',
                url: $('query_ebank_link').getUrl(),
                params: {
                    batch_id: batch_id,
                    status: status,
                    winid: 'query_ebank_link_winId'

                },
                title: '代扣指令查询',
                draggable: true,
                width: 900,
                height: 680
            });
        }

        function query_batch() {
            $('hls_ccb_batch_result_ds').query();
        }


        function withhold_list() {
            var win = new Leaf.Window({
                id: 'withhold_ebank_link_winId',
                url: $('withhold_ebank_link').getUrl(),
                params: {
                    winid: 'withhold_ebank_link_winId'
                },
                title: '手工代扣单',
                draggable: true,
                width: window.screen.availWidth,
                height: window.screen.availHeight
            });

        }

        function export_batch() {
            var r = $('hls_ccb_batch_result_ds').getCurrentRecord();
            if (r) {
                if (r.get('pay_method') == 'CCB_THREE') {//建行
                    $('hls_ccb_batch_merge_result_grid')._export();
                } else if (r.get('pay_method') == 'AGRI_TWO' || r.get('pay_method') == 'AGRI_THREE') {
                    $('hls_abc_batch_merge_result_grid')._export();
                }
            }

        }

        function ccb_abc_import() {
            var record = $('hls_ccb_batch_result_ds').getCurrentRecord();
            if (record.get('status') != 'NEW') {
                Leaf.showMessage('提示', '该数据不是新建状态,不可操作');
                return;
            }
            var str;
            var pay_method = record.get('pay_method');
            var due_date_to = new Date(record.get('due_date')).format('yyyy-mm-dd').toString();
            var bank_type;
            if (pay_method == 'CCB_THREE') {
                bank_type = 'CCB';
                str = '建行';
            } else if (pay_method == 'AGRI_TWO' || pay_method == 'AGRI_THREE') {
                bank_type = 'ABC';
                str = '农行';
            } else {
                Leaf.showInfoMessage("提示", "未知错误,请联系管理员");
                return;
            }
            Leaf.showConfirm("提示", "该批次为<font color=red>" + str + "</font>划扣批次,是否导入<font color=red>" + str + "</font>回盘文件", function () {
                var win = new Leaf.Window({
                    id: 'ccb_abc_upload_link_winId',
                    url: $('ccb_abc_upload_link').getUrl() + '?batch_id=' + record.get('batch_id') + '&bank_type=' + bank_type + '&due_date_to=' + due_date_to,
                    params: {
                        winId: 'ccb_abc_upload_link_winId'
                    },
                    title: '回盘文件导入',
                    width: 420,
                    height: 275
                });
                win.on('close', function () {
                    $('hls_ccb_batch_result_ds').query();
                });
            });
        }

        function query_reset() {
            $('hls_ccb_batch_query_ds').reset();
        }

        function hls_ccb_batch_set_queryPara(ds){
            var r = ds.getCurrentRecord();
            $('hls_ccb_batch_merge_result_ds').setQueryParameter('batch_id',r.get('batch_id'));
        }

        ]]></script>
        <a:dataSets>
            <a:dataSet id="bank_name_combobox_ds">
                <a:datas dataSource="/model/bank_name"/>
            </a:dataSet>
            <a:dataSet id="ebank_status_combobox_ds" lookupCode="CCB600_EBANK_STATUS"/>

            <a:dataSet id="hls_ccb_batch_query_ds">
                <a:fields>
                    <a:field name="bank_name" displayField="code_value_name" options="bank_name_combobox_ds"
                             returnField="bank_code" valueField="code_value"/>

                    <a:field name="status_desc" displayField="code_value_name" options="ebank_status_combobox_ds"
                             returnField="status" valueField="code_value"/>
                </a:fields>
            </a:dataSet>

            <a:dataSet id="hls_ccb_batch_result_ds" autoQuery="true" fetchAll="false"
                       model="ccb.CCB600.hls_ebank_batch" pageSize="15" queryDataSet="hls_ccb_batch_query_ds"
                       queryUrl="${/request/@context_path}/autocrud/ccb.CCB600.hls_ebank_batch/query"
                       selectable="true" selectionModel="single">
                <a:events>
                    <a:event name="indexchange" handler="hls_ccb_batch_set_queryPara"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="hls_ccb_batch_merge_result_ds" fetchAll="true" model="ccb.CCB600.hls_ebank_batch_ln_group"
                       bindName="ccb_out_result" bindTarget="hls_ccb_batch_result_ds">
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar height="29">
                <a:screenTitle/>
                <a:gridButton click="query_batch" text="查询"/>
                <a:gridButton click="query_reset" text="重置"/>
                <a:gridButton click="open_batch" text="生成代扣指令"/>
                <a:gridButton click="delete_batch" text="删除代扣指令"/>
                <a:gridButton click="export_batch" text="导出划扣单"/>
                <a:gridButton click="ccb_abc_import" text="导入回盘文件"/>
                <!--                <a:gridButton click="post_batch" text="发送代扣指令"/>-->
                <!--                <a:gridButton click="write_off_batch" text="刷新代扣信息"/>-->
                <!--                <a:gridButton click="withhold_list" text="手工代扣单"/>-->
            </a:screenTopToolbar>
            <a:form column="3">
                <!--                <a:comboBox name="bank_name" bindTarget="hls_ccb_batch_query_ds" prompt="银行名称"/>-->
                <a:comboBox name="status_desc" bindTarget="hls_ccb_batch_query_ds" prompt="状态"/>
                <a:datePicker name="batch_date" bindTarget="hls_ccb_batch_query_ds" prompt="创建日期"/>
            </a:form>
            <a:grid id="hls_ccb_batch_grid" bindTarget="hls_ccb_batch_result_ds" height="490" marginWidth="50"
                    navBar="true">
                <a:columns>
                    <a:column name="batch_id" align="left" prompt="序号" width="100"/>
                    <a:column name="pay_method_desc" align="center" prompt="支付方式" width="120"/>
                    <a:column name="batch_date" align="center" prompt="创建日期" renderer="Leaf.formatDate" width="150"/>
                    <a:column name="bank_name" align="left" prompt="银行" width="150"/>
                    <a:column name="bank_branch_name" align="left" prompt="支行" width="200"/>
                    <a:column name="bank_account_num" align="left" prompt="账户" width="200"/>
                    <a:column name="status_desc" align="center" prompt="状态" width="100"/>
                    <a:column name="detail" align="center" prompt="明细" renderer="detail_renderer" width="100"/>
                    <a:column name="created_by_n" align="center" prompt="制单人" width="150"/>
                    <a:column name="total_real_amount" align="right" prompt="实扣总金额" renderer="Leaf.formatMoney"
                              width="100"/>
                    <a:column name="total_success_count" align="right" prompt="扣款成功账号数" width="100"/>
                    <a:column name="message" align="right" prompt="错误消息" width="150"/>
                </a:columns>
            </a:grid>
            <a:grid hidden="true" width="1" height="1" id="hls_ccb_batch_merge_result_grid"
                    bindTarget="hls_ccb_batch_merge_result_ds" title="待导出划扣单">
                <a:columns>
                    <a:column name="sequence_num" prompt="序号(必填)"/>
                    <a:column name="bank_account_num" prompt="账号(必填)"/>
                    <a:column name="bank_account_name" prompt="户名(必填)"/>
                    <a:column name="actual_amount" align="right" renderer="Leaf.formatMoney" prompt="金额(必填)"/>
                    <a:column name="multi_bank_flag" prompt="跨行标识(选填 建行填0 他行填1)"/>
                    <a:column prompt="行名(跨行业务与联行行号不能同时为空)"/>
                    <a:column prompt="联行行号(跨行业务与行名不能同时为空)"/>
                    <a:column prompt="多方协议号(跨行代收必填)"/>
                    <a:column prompt="标识号(选填)"/>
                    <a:column name="ccb_summary" prompt="摘要(选填 显示在收款账户流水明细中)"/>
                    <a:column prompt="备注(选填)"/>
                </a:columns>
            </a:grid>
<!--            <a:grid hidden="true" width="1" height="1" id="hls_abc_batch_merge_result_grid"-->
<!--                    bindTarget="hls_ccb_batch_merge_result_ds">-->
<!--                <a:columns>-->
<!--                    <a:column name="abc_sequence_num" prompt="serial-no"/>-->
<!--                    <a:column name="abc_pay_type" prompt="pay-type"/>-->
<!--                    <a:column name="abc_transaction_type" prompt="transaction-type"/>-->
<!--                    <a:column name="csh_bank_account_num" prompt="account-no"/>-->
<!--                    <a:column name="csh_bank_account_name" prompt="account-name"/>-->
<!--                    <a:column name="bank_account_num" prompt="payee-account-to"/>-->
<!--                    <a:column name="bank_account_name" prompt="payee-name"/>-->
<!--                    <a:column name="abc_payee_bank" prompt="payee-bank"/>-->
<!--                    <a:column name="abc_usage" prompt="usage"/>-->
<!--                    <a:column name="abc_fen_code" prompt="fen-code"/>-->
<!--                    <a:column name="abc_curr_flag" prompt="curr-flag"/>-->
<!--                    <a:column name="actual_amount" align="right" renderer="Leaf.formatMoney" prompt="amount"/>-->
<!--                </a:columns>-->
<!--            </a:grid>-->
        </a:screenBody>
    </a:view>
</a:screen>