<?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="create_withhold_ebank_link" model="ccb.CCB600.hls_ebank_withhold_batch" modelaction="execute"/>
        <a:link id="delete_withhold_ebank_link" model="ccb.CCB600.hls_ebank_withhold_batch" modelaction="delete"/>
        <a:link id="create_withhold_link" url="${/request/@context_path}/modules/ccb/CCB600/ccb_ebank_withhold_detail.lview"/>
        <a:link id="query_withhold_link" url="${/request/@context_path}/modules/ccb/CCB600/ccb_ebank_withhold_query.lview"/>
        <a:link id="create_manual_abc_ebank_link" model="ccb.CCB600.hls_ebank_withhold_update" modelaction="execute"/>
        <a:link id="query_manual_abc_ebank_link" model="ccb.CCB600.hls_ebank_withhold_update" modelaction="update"/>
        <script><![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 add_withhold() {
            $('hls_ccb_withhold_result_ds').create(0);
        }

        function save_withhold() {
            var ds = $('hls_ccb_withhold_result_ds');
            var record = ds.getCurrentRecord();

            Leaf.request({
                url: $('create_withhold_ebank_link').getUrl(),
                para: {
                    bank_account_id: record.get('bank_account_id')
                },
                success: function (res) {
                    Leaf.SideBar.show({
                        msg: '操作成功',
                        duration: 2000
                    });
                    Leaf.Masker.unmask(Ext.getBody()); //解锁
                    $('hls_ccb_withhold_result_ds').query();

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


        function delete_withhold() {
            var ds = $('hls_ccb_withhold_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_withhold_ebank_link').getUrl(),
                        para: {
                            batch_id: batch_id
                        },
                        success: function (res) {

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

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

        }


        function post_withhold() {
            var ds = $('hls_ccb_withhold_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_manual_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_withhold_result_ds').query();

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

        }

        function write_off_withhold() {
            var record = $('hls_ccb_withhold_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_withhold_result_ds').query();
            }
            else{
            Leaf.Masker.mask(Ext.getBody(), '核销代扣信息中...'); //锁屏
            if (bank_code == '008') {
                Leaf.request({
                    url: $('query_manual_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_withhold_result_ds').query();
                    },
                    failure: function () {
                    },
                    error: function () {
                    },
                    scope: this
                });

            } else if (bank_code == '002') {
                $jq.ajax({
                    url: '${/request/@context_path}/icbc/interface/record/withhold/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_withhold_result_ds').query();

                    }
                });
            }
            }


        }

        function detail_withhold_renderer(value, record, name) {
            var status = record.get('status');
            if (status === 'NEW') {
                return '<a href="javascript:open_detail_withhold_window(' + record.get('batch_id') + ',\'' + status + '\')">' + '${l:PROMPT.DETAIL}' + '</a>';
            } else {
                return '<a href="javascript:open_query_withhold_window(' + record.get('batch_id') + ',\'' + status + '\')">' + '${l:PROMPT.DETAIL}' + '</a>';
            }


        }

        function open_detail_withhold_window(batch_id, status) {
            var win = new Leaf.Window({
                id: 'create_withhold_link_winId',
                url: $('create_withhold_link').getUrl(),
                params: {
                    batch_id: batch_id,
                    status: status,
                    winid: 'create_withhold_link_winId'

                },
                title: '手工代扣清单创建',
                draggable: true,
                width: 900,
                height: 680
            });
        }

        function open_query_withhold_window(batch_id, status) {
            var win = new Leaf.Window({
                id: 'query_withhold_link_winId',
                url: $('query_withhold_link').getUrl(),
                params: {
                    batch_id: batch_id,
                    status: status,
                    winid: 'query_withhold_link_winId'

                },
                title: '手工代扣清单查询',
                draggable: true,
                width: 900,
                height: 680
            });
        }

        function query_withhold() {
            $('hls_ccb_withhold_result_ds').query();
        }


        function editorFunction(record, name) {
            if (record.isNew) {
                if (name == 'bank_account_num') {
                    return 'grid_lov';
                }
            }
            return '';
        }

        ]]></script>
        <a:dataSets>

            <a:dataSet id="hls_ccb_withhlod_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_withhold_result_ds" autoCount="true" autoQuery="true" fetchAll="false"
                       model="ccb.CCB600.hls_ebank_batch" pageSize="15" queryDataSet="hls_ccb_withhlod_query_ds"
                       queryUrl="${/request/@context_path}/autocrud/ccb.CCB600.hls_ebank_withhold_batch/query"
                       selectable="true" selectionModel="single">
                <a:fields>
                    <a:field name="bank_account_num" autoComplete="true" lovGridHeight="350" lovHeight="550"
                             lovService="ccb.CCB600.hls_ebank_bank" lovWidth="1000" required="true"
                             title="银行账户" prompt="银行账户">
                        <a:mapping>
                            <a:map from="bank_account_name" to="bank_account_name"/>
                            <a:map from="bank_account_num" to="bank_account_num"/>
                            <a:map from="bank_short_name" to="bank_name"/>
                            <a:map from="bank_account_id" to="bank_account_id"/>
                            <a:map from="bank_branch_name" to="bank_branch_name"/>
                            <a:map from="currency_code" to="currency"/>
                        </a:mapping>
                    </a:field>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar height="29">
                <a:screenTitle/>
                <a:gridButton click="query_withhold" text="查询"/>
                <a:gridButton click="add_withhold" text="新增"/>
                <a:gridButton click="save_withhold" text="保存"/>
                <a:gridButton click="delete_withhold" text="删除"/>
                <a:gridButton click="post_withhold" text="发送代扣指令"/>
                <a:gridButton click="write_off_withhold" text="刷新代扣信息"/>
            </a:screenTopToolbar>
            <a:form column="3">
                <a:comboBox name="bank_name" bindTarget="hls_ccb_withhlod_query_ds" prompt="银行名称"/>
                <a:comboBox name="status_desc" bindTarget="hls_ccb_withhlod_query_ds" prompt="状态"/>
                <a:datePicker name="batch_date" bindTarget="hls_ccb_withhlod_query_ds" prompt="创建日期"/>
            </a:form>
            <a:grid id="hls_ccb_withhold_grid" bindTarget="hls_ccb_withhold_result_ds" height="490" width="1400"
                    navBar="true">
                <a:columns>
                    <a:column name="batch_id" align="left" prompt="序号" width="100"/>
                    <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="账户" editorFunction="editorFunction"
                              width="200"/>
                    <a:column name="status_desc" align="center" prompt="状态" width="100"/>
                    <a:column name="detail" align="center" prompt="明细" renderer="detail_withhold_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:editors>
                    <a:comboBox id="grid_cb"/>
                    <a:lov id="grid_lov"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>