<?xml version="1.0" encoding="UTF-8"?>

<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true"
          dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.role_id=${/session/@role_id}" fetchAll="true"
                       model="csh.CSH1400.csh_abc_query_role_code" rootPath="role_code"/>
    </a:init-procedure>

    <a:view>
        <a:link id="agricultural_bank_import_link"
                url="${/request/@context_path}/modules/csh/CSH1400/agricultural_bank_import.lview"/>
        <a:link id="csh_abc_import_save_link"
                url="${/request/@context_path}/modules/csh/CSH1400/csh_abc_import_save.lsc"/>
        <a:link id="csh_abc_import_transaction_link"
                url="${/request/@context_path}/modules/csh/CSH1400/csh_abc_import_transaction.lsc"/>
        <a:link id="ocr_query_link_id"
                url="${/request/@context_path}/modules/efile/EFILE1100/efile_doc_import_new.lsc"/>
        <script type="text/javascript"><![CDATA[
        Leaf.onReady(function () {
            //只开放按钮权限给债权内勤
            var role_code = '${/model/role_code/record/@role_code}';
            if (role_code == '0009') {
                $('agricultural_bank_save_id').enable(); // 启用按钮
                $('agricultural_bank_import_id').enable(); // 启用按钮
                $('agricultural_bank_submit_id').enable(); // 启用按钮
                $('query_ocr').enable(); // 启用按钮
            } else {
                $('agricultural_bank_save_id').disable(); // 禁用按钮
                $('agricultural_bank_import_id').disable(); // 禁用按钮
                $('agricultural_bank_submit_id').disable(); // 禁用按钮
                $('query_ocr').disable(); // 禁用按钮
            }
        });

        function returnAbc() {
            // 刷新页面
            $("OcrQueryDs").query();
            $("AbcBankDs").query();
        }
        function returnBack() {
            // 刷新页面
            top.refreshFrame();
        }

        //查询按钮
        function agricultural_bank_query() {
            $("OcrQueryDs").query();
            $("AbcBankDs").query();
        }

        // var record = $('OcrQueryDs').getSelected();

        // var record = $('AbcBankDs').getSelected();

        function change_background_color(record) {
            if (record.get('re_flag') == 'N') {
                return 'background-color:#99FF99;'
            }
        }
        // 字段渲染农行回单
        function checkOcrRender(value, record, name) {
            if (record.get('re_flag') == 'Y' && !Ext.isEmpty(value)) {
                var name_r = name + '_r';
                var value_r = record.get(name_r);//OCR数据
                if (value != value_r) {
                    if (name == 'total_amount') {
                        return '<font color="red">' + Leaf.formatMoney(value) + '</font>'
                    } else if (name == 'bank_receipt_date') {
                        return '<font color="red">' + Leaf.formatDate(value) + '</font>'
                    } else {
                        return '<font color="red">' + value + '</font>'
                    }
                } else {
                    if (name == 'total_amount') {
                        return '<font color="black">' + Leaf.formatMoney(value) + '</font>'
                    } else if (name == 'bank_receipt_date') {
                        return '<font color="black">' + Leaf.formatDate(value) + '</font>'
                    } else {
                        return '<font color="black">' + value + '</font>'
                    }
                }
            } else {
                if (!Ext.isEmpty(value)) {
                    if (name == 'total_amount') {
                        return '<font color="black">' + Leaf.formatMoney(value) + '</font>'
                    } else if (name == 'bank_receipt_date') {
                        return '<font color="black">' + Leaf.formatDate(value) + '</font>'
                    } else {
                        return '<font color="black">' + value + '</font>'
                    }
                }
            }
        }

        // 保存按钮
        function agricultural_bank_save() {
            var orc_save = $('AbcBankDs').getAll();
            var param = {};
            var saveOrc = [];
                for (var i = 0; i < orc_save.length; i++) {
                    var lineOrc = {};
                    lineOrc['receipt_no'] = orc_save[i].get('receipt_no');
                    lineOrc['import_itfc_id'] = orc_save[i].get('import_itfc_id');
                    lineOrc['paid_byother_flag_desc'] = orc_save[i].get('paid_byother_flag_desc');
                    lineOrc['payer_name'] = orc_save[i].get('payer_name');
                    lineOrc['collection_classes_desc'] = orc_save[i].get('collection_classes_desc');
                    lineOrc['guarantor_name_duty'] = orc_save[i].get('guarantor_name_duty');
                    lineOrc['bank_receipt_date'] = orc_save[i].get('bank_receipt_date');
                    lineOrc['bank_num'] = orc_save[i].get('bank_num');
                    lineOrc['payer_number'] = orc_save[i].get('payer_number');
                    lineOrc['bank_name'] = orc_save[i].get('bank_name');
                    lineOrc['beneficiary_name'] = orc_save[i].get('beneficiary_name');
                    lineOrc['total_amount'] = orc_save[i].get('total_amount');
                    lineOrc['bp_id'] = orc_save[i].get('bp_id');
                    lineOrc['bp_category'] = orc_save[i].get('bp_category');
                    saveOrc.push(lineOrc);
                }
                param['AbcOrc'] = saveOrc;
                Leaf.request({
                    url: $('csh_abc_import_save_link').getUrl(),
                    para: {
                        param: param
                    },
                    success: function () {
                        Leaf.SideBar.show({
                            msg: '${l:HLS.SUBMIT_SUCCESS}',
                            duration: 2000
                        });
                        returnAbc();
                    },
                    scope: this
                });
        }


        // 提交按钮
        function agricultural_bank_submit() {
            var orc_save = $('AbcBankDs').getSelected();
            var abc_save = $('OcrQueryDs').getSelected();
            var param = {};
            var saveOrc = [];
            if (orc_save.length <= 0) {
                Leaf.showMessage('提示', '请至少选择一条数据!');
            } else {
                for (var i = 0; i < orc_save.length; i++) {
                    var lineOrc = {};
                    lineOrc['write_off_flag'] = 'DISABLED';
                    lineOrc['exchange_rate'] = '1';
                    lineOrc['currency_code'] = 'CNY';
                    lineOrc['receipt_type'] = 'IMPORT';
                    lineOrc['_status'] = 'insert';
                    lineOrc['bank_account_num'] = orc_save[i].get('bank_num');//收款账号
                    lineOrc['bank_slip_num'] = orc_save[i].get('receipt_no');//银行流水号
                    lineOrc['transaction_amount'] = orc_save[i].get('total_amount');//交易金额
                    lineOrc['bank_receipt_date'] = orc_save[i].get('bank_receipt_date');//交易时间
                    lineOrc['bp_bank_account_num'] = orc_save[i].get('payer_number');//付款账号
                    lineOrc['bp_bank_account_name'] = orc_save[i].get('payer_name');//付款人
                    lineOrc['opposite_band_na'] = orc_save[i].get('bank_name');//付款开户银行
                    lineOrc['description'] = orc_save[i].get('zy');//摘要
                    lineOrc['ref_v05'] = orc_save[i].get('remark');//附言
                    lineOrc['bp_name'] = orc_save[i].get('beneficiary_name');//收款人
                    lineOrc['paid_byother_flag_desc'] = orc_save[i].get('paid_byother_flag_desc');//是否代付
                    lineOrc['guarantor_name_duty'] = orc_save[i].get('guarantor_name_duty');//担保人承担义务
                    lineOrc['collection_classes_desc'] = orc_save[i].get('collection_classes_desc');//款项分类
                    lineOrc['extra_nam'] = orc_save[i].get('extra_nam');//代理店简称
                    lineOrc['bank_account_id'] = orc_save[i].get('bank_account_id');//id
                    lineOrc['bp_id'] = orc_save[i].get('bp_id');//id
                    lineOrc['bp_category'] = orc_save[i].get('bp_category');
                    saveOrc.push(lineOrc);
                    recode = orc_save[i];
                }
                param['AbcOrc'] = saveOrc;
                if(recode.get('re_flag')=='N'){
                    Leaf.showConfirm('特别注意', '日志号, <font color="red">'+recode.get('receipt_no')+'</font>,的单据未匹配到对应回单数据,是否确认提交?', function () {
                        Leaf.request({
                            url: $('csh_abc_import_transaction_link').getUrl(),
                            para: {
                                param: param
                            },
                            success: function () {
                                Leaf.SideBar.show({
                                    msg: '${l:HLS.SUBMIT_SUCCESS}',
                                    duration: 2000
                                });
                                $('AbcBankDs').query();
                                $("OcrQueryDs").query();
                                // returnBack();
                            },
                            scope: this
                        });
                    });
                }else {
                    Leaf.request({
                        url: $('csh_abc_import_transaction_link').getUrl(),
                        para: {
                            param: param
                        },
                        success: function () {
                            Leaf.SideBar.show({
                                msg: '${l:HLS.SUBMIT_SUCCESS}',
                                duration: 2000
                            });
                            $('AbcBankDs').query();
                            $("OcrQueryDs").query();
                            // returnBack();
                        },
                        scope: this
                    });
                }

            }

            if (abc_save.length > 0) {
                Leaf.showMessage('提示', '农行回单OCR页面的单据无法提交!');
            }
        }

        //明细上传按钮
        function agricultural_bank_import() {
            new Leaf.Window({
                id: 'agricultural_bank_import_wind',
                params: {
                    winid: 'agricultural_bank_import_wind',
                    abc_ds:'AbcBankDs',
                    ocr_ds:'OcrQueryDs',
                },
                url: $('agricultural_bank_import_link').getUrl(),
                title: '农行回单',
                width: 430,
                height: 390
            });
        }

        //获取OCR数据
        function query_ocr(){
            Leaf.Masker.mask(Ext.getBody(), '获取OCR数据中');
            var ocr_query_link ='ocr_query_link_id';
            Leaf.request({
                url: $(ocr_query_link ).getUrl(),
                success: function(res) {
                    Leaf.Masker.unmask(Ext.getBody());
                    // Leaf.SideBar.show({
                    //     msg:'获取数据'+res.result.page_number+'页'+'共'+res.result.total+'条',
                    //     duration: 10000
                    // })
                    Leaf.showMessage('提示', '获取数据'+res.result.page_number+'页'+'共'+res.result.total+'条');
                    $("OcrQueryDs").query();
                },
                failure: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                error: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                scope: this
            });
        }


        ]]></script>


        <a:dataSets>
            <!--                        是否代付、担保人履行担保义务下拉框-->
            <a:dataSet id="yes_no_ds" lookupCode="YES_NO"/>
            <!--                        款项分类下拉框-->
            <a:dataSet id="collection_classes_ds" lookupCode="DS_COLLECTION_CLASSES_S"/>

            <a:dataSet id="query_time">
            </a:dataSet>
            <a:dataSet id="OcrQueryDs" autoQuery="true" autoPageSize="true" queryDataSet="query_time"
                       model="csh.CSH1400.csh_ocr_abc_bank"
                       queryUrl="${/request/@context_path}/autocrud/csh.CSH1400.csh_ocr_abc_bank/query"
                       selectable="true">
            </a:dataSet>

            <a:dataSet id="AbcBankDs" autoQuery="true" autoPageSize="true" queryDataSet="query_time"
                       model="csh.CSH1400.csh_import_abc_bank"
                       queryUrl="${/request/@context_path}/autocrud/csh.CSH1400.csh_import_abc_bank/query"
                       selectable="true">
                <a:fields>
                    <!--                    是否代付-->
                    <a:field name="paid_byother_flag_desc" required="true" options="yes_no_ds"
                             returnField="paid_byother_flag" displayField="code_value_name" valueField="code_value"/>
                    <a:field name="paid_byother_flag" required="true"/>
                    <!--                    担保人履行担保义务-->
                    <a:field name="guarantor_name_duty" options="yes_no_ds"
                             returnField="paid_byother_flag" displayField="code_value_name" valueField="code_value"/>
                    <a:field name="paid_byother_flag"/>
                    <!--                    款项分类-->
                    <a:field name="collection_classes_desc" required="true" options="collection_classes_ds"
                             displayField="code_value_name" returnField="collection_classes" valueField="code_value"/>
                    <a:field name="collection_classes"/>

                    <a:field name="beneficiary_name"  lovGridHeight="350" lovHeight="500" lovLabelWidth="90"
                             lovService="basic.hls_bp_master_v_for_agent_lov" lovWidth="550" title="HLS.BP_TITLE">
                        <a:mapping>
                            <a:map from="bp_name" to="beneficiary_name"/>
                            <a:map from="bp_id" to="bp_id"/>
                            <a:map from="extra_nam" to="extra_nam"/>
                            <a:map from="bp_category" to="bp_category"/>
                        </a:mapping>
                    </a:field>


                </a:fields>

            </a:dataSet>

        </a:dataSets>

        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton id="Ocr_Bank_Query" click="agricultural_bank_query" text="HLS.QUERY"/>
                <a:gridButton id="agricultural_bank_save_id" click="agricultural_bank_save" text="保存"/>
                <a:gridButton id="query_ocr" click="query_ocr" text="获取数据"/>
                <a:gridButton id="agricultural_bank_import_id" click="agricultural_bank_import" text="收款导入"/>
                <a:gridButton id="agricultural_bank_submit_id" click="agricultural_bank_submit" text="提交"/>
            </a:screenTopToolbar>

            <a:form column="4" title="查询条件">
                <a:datePicker name="exchange_hour_from" bindTarget="query_time" prompt="交易时间从"
                              renderer="Leaf.formatDate"/>
                <a:datePicker name="exchange_hour_to" bindTarget="query_time" prompt="交易时间到"
                              renderer="Leaf.formatDate"/>
                <a:numberField name="money_from" bindTarget="query_time" prompt="交易金额从"
                               renderer="Leaf.formatMoney"/>
                <a:numberField name="money_to" bindTarget="query_time" prompt="交易金额到"
                               renderer="Leaf.formatMoney"/>
            </a:form>

            <a:tabPanel marginHeight="220" marginWidth="50">
                <a:tabs>

                    <a:tab prompt="OCR数据" width="110">
                        <a:grid id="ocr" bindTarget="OcrQueryDs" marginHeight="200" marginWidth="80" navBar="true" rowRenderer="change_background_color">
                            <a:columns>
                                <a:column name="bank_num" prompt="收款账号" width="150" align="center" renderer="checkOcrRender"/>
                                <a:column name="bank_receipt_date" prompt="交易时间" width="100" align="center"
                                          renderer="checkOcrRender"/>
                                <a:column name="receipt_no" prompt="回单编号" width="80" align="left"/>
                                <a:column name="payer_number" prompt="付款方账号" width="100" align="center" renderer="checkOcrRender"/>
                                <a:column name="payer_name" prompt="付款方户名" width="100" align="left" renderer="checkOcrRender"/>
                                <a:column name="payer_bank" prompt="付款方开户行" width="100" align="left"/>
                                <a:column name="bp_name" prompt="收款对象" width="100" align="left"/>
                                <a:column name="paid_byother_flag_desc" prompt="是否代付" width="70" align="center"/>
                                <a:column name="total_amount" prompt="金额" width="100" align="right" renderer="checkOcrRender"/>
                                <a:column name="zy" prompt="摘要" width="80" align="center"/>
                                <a:column name="collection_classes_desc" prompt="款项分类" width="90" align="center"/>
                                <a:column name="remark" prompt="附言" width="80" align="center"/>
                                <a:column name="extra_nam" prompt="代理店简称" width="100" align="center"/>
                                <a:column name="guarantor_name_duty" prompt="担保人履行担保义务" width="145"
                                          align="center"/>
                            </a:columns>
                        </a:grid>
                    </a:tab>
                    <a:tab prompt="农行导入" width="110">
                        <a:grid id="abc_bank" bindTarget="AbcBankDs" marginHeight="200" marginWidth="80"
                                rowRenderer="change_background_color" navBar="true">
                            <a:columns>
                                <a:column name="bank_num" prompt="账号" width="150"  align="center"
                                          renderer="checkOcrRender"/>
                                <a:column name="bank_receipt_date" prompt="交易时间" width="100"
                                          renderer="checkOcrRender" align="center"/>
                                <a:column name="receipt_no" prompt="日志号" width="80"  align="left"/>
                                <a:column name="payer_number" prompt="对方账号" width="100"  align="center" renderer="checkOcrRender"/>
                                <a:column name="payer_name" prompt="对方账号户名" width="100"  align="left"
                                          renderer="checkOcrRender"/>
                                <a:column name="bank_name" prompt="对方银行名称" width="100"  align="left"/>
                                <a:column name="beneficiary_name" prompt="收款对象" width="100" editor="grid_lov" align="left" required="true"/>
                                <a:column name="paid_byother_flag_desc" prompt="是否代付" width="70" editor="grid_cb"
                                          required="true"
                                          align="center"/>
                                <a:column name="total_amount" prompt="收款金额" width="100"  align="right"
                                          renderer="checkOcrRender"/>
                                <a:column name="zy" prompt="摘要" width="80"  align="center"/>
                                <a:column name="collection_classes_desc" prompt="款项分类" width="90" editor="grid_cb"
                                          required="true"
                                          align="center"/>
                                <a:column name="remark" prompt="附言" width="80" align="center" readonly="true"/>
                                <a:column name="extra_nam" prompt="代理店简称" width="100" align="center" readonly="true"/>
                                <a:column name="guarantor_name_duty" prompt="担保人履行担保义务" width="145"
                                          align="center"/>
                            </a:columns>
                            <a:editors>
                                <a:textField id="txt"/>
                                <a:comboBox id="grid_cb"/>
                                <a:lov id="grid_lov"/>
                                <a:datePicker id="grid_date"/>
                            </a:editors>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
        </a:screenBody>
    </a:view>
</a:screen>