<?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 model="cont.CON3500.contract_ifrs9_report_flag_query" rootPath="status"/>-->
    <!--    </a:init-procedure>-->
    <a:init-procedure>
        <a:model-query fetchAll="true" model="cont.CON3500.contract_ifrs9_report_flag_query" rootPath="/model/status"/>
        <a:model-query fetchAll="true" model="cont.CON3500.contract_ifrs9_report_temp" rootPath="/model/job_status"/>
    </a:init-procedure>
    <a:view>
        <link href="${/request/@context_path}/lib/bootstrap-3.3.7/css/bootstrap.css" rel="stylesheet" type="text/css"/>

        <a:link id="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code"
                modelaction="update"/>
        <a:link id="save_ifrs9_report_link" model="cont.CON3500.contract_ifrs9_report_temp"
                modelaction="update"/>
        <a:link id="get_job_finish_flag_link" model="cont.CON3500.contract_ifrs9_report_flag_query"
                modelaction="execute"/>
        <a:link id="update_ifrs9_report_link" model="cont.CON3500.contract_ifrs9_report_temp"
                modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}con_contract_query_link"
                url="${/request/@context_path}/modules/cont/CON505/con_contract_modify.lview"/>
        <a:link id="contract_ifrs9_sum_report_link"
                url="${/request/@context_path}/modules/cont/CON3500/contract_ifrs9_sum_report.lview"/>
        <style>
            body{
                font-size: 12px;
            }
        </style>
        <script type="text/javascript"><![CDATA[


        Leaf.onReady(function(){
            //检查保存按钮
            var flag='${/model/status/record/@flag}';
            var job_flag='${/model/job_status/record/@job_run_flag}';

            console.log(flag);

            if(flag=='Y'){
                $("${/parameter/@layout_code}_user_button1").disable();
                $("${/parameter/@layout_code}_user_button2").disable();
                $("${/parameter/@layout_code}_user_button3").disable();
            }else if(job_flag>0){
                $("${/parameter/@layout_code}_user_button1").disable();
                $("${/parameter/@layout_code}_user_button2").disable();
                var reminder=document.getElementsByClassName('screenTopToolbar');
                for(var i=0;i<reminder.length;i=i+1){

                    var t=reminder[i];
                    t.insertAdjacentHTML('afterend', '<div class="alert alert-danger" id="reminder" role="alert" >job正在运行中，请尝试点击获取结果按钮获得结果！</div>');
                }
            }
            else{
                var reminder=document.getElementsByClassName('screenTopToolbar');
                for(var i=0;i<reminder.length;i=i+1){

                    var t=reminder[i];
                    t.insertAdjacentHTML('afterend', '<div class="alert alert-danger" id="reminder" role="alert" >数据只能保存一次，请确认后保存</div>');
                }
            }
            debugger
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_ifrs9');
            $(ds_id).setQueryParameter("period_names",'${/parameter/@period_name}');
            // $(ds_id).setQueryParameter("divisions",'${/parameter/@division1},${/parameter/@division2},${/parameter/@division3},${/parameter/@division4},${/parameter/@division5},${/parameter/@division6},${/parameter/@division7},${/parameter/@division8}');
            $(ds_id).setQueryParameter("division1",'${/parameter/@division1}');
            $(ds_id).setQueryParameter("division2",'${/parameter/@division2}');
            $(ds_id).setQueryParameter("division3",'${/parameter/@division3}');
            $(ds_id).setQueryParameter("division4",'${/parameter/@division4}');
            $(ds_id).setQueryParameter("division5",'${/parameter/@division5}');
            $(ds_id).setQueryParameter("division6",'${/parameter/@division6}');
            $(ds_id).setQueryParameter("division7",'${/parameter/@division7}');
            $(ds_id).setQueryParameter("division8",'${/parameter/@division8}');
	        $(ds_id).setQueryParameter("division9",'${/parameter/@division9}');
            $(ds_id).setQueryParameter("division10",'${/parameter/@division10}');
            $(ds_id).query();
        })
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {

            //先删除之前的数据
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_ifrs9');

            var period_name="${/parameter/@period_name}";
            Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
            Leaf.request({
                url: $('save_ifrs9_report_link').getUrl(),
                para: {
                    period_name:period_name
                },
                success: function(result) {
                    $("${/parameter/@layout_code}_user_button1").disable();
                    $("${/parameter/@layout_code}_user_button2").disable();
                    // Leaf.SideBar.show({
                    //     msg: '数据正在执行中...',
                    //     duration: 2000
                    // });
                    //  Leaf.Masker.unmask(Ext.getBody());
                    Leaf.Masker.unmask(Ext.getBody());
                    document.getElementById("reminder").innerText='数据正在更新中，请稍后（请更新成功后再进行导出操作）';
                    $(ds_id).removeAll();
                },
                failure: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                error: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                scope: this
            });
            //
            // var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_ifrs9');
            // $(ds_id).setQueryUrl('${/request/@context_path}/autocrud/cont.CON3500.contract_ifrs9_report_temp/query');
            // $(ds_id).setQueryParameter('period_name','${/parameter/@period_name}');
            // $(ds_id).query();


        };

        //保存数据
        window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {
            var period_name="${/parameter/@period_name}";
            Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
            Leaf.request({
                url: $('update_ifrs9_report_link').getUrl(),
                para: {
                    period_name:period_name
                },
                success: function() {
                    $("${/parameter/@layout_code}_user_button1").disable();
                    $("${/parameter/@layout_code}_user_button2").disable();
                    $("${/parameter/@layout_code}_user_button3").disable();
                    Leaf.SideBar.show({
                        msg: '保存成功',
                        duration: 2000
                    });
                    Leaf.Masker.unmask(Ext.getBody());
                },
                failure: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                error: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                scope: this
            });
        };
        //获取查询结果
        window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_ifrs9');
            $(ds_id).setQueryParameter("period_name",'${/parameter/@period_name}');
            //查看job有没有返回结果
            Leaf.Masker.mask(Ext.getBody());
            Leaf.request({
                url: $('get_job_finish_flag_link').getUrl(),
                para: {
                    period_name:'${/parameter/@period_name}'
                },
                success: function(res) {
                    Leaf.Masker.unmask(Ext.getBody());
                    debugger;
                    console.log(res)
                    var job_finish_flag=res.result['job_finish_flag'];
                    if(job_finish_flag>0) {
                        $(ds_id).query();
                        $("${/parameter/@layout_code}_user_button1").enable();
                        $("${/parameter/@layout_code}_user_button2").enable();
                        document.getElementById("reminder").innerText='数据更新已经完成，可以进行保存以及导出操作！';

                    }
                },
                failure: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                error: function() {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                scope: this
            });

        };
        //承租人最大逾期天数
        window['${/parameter/@layout_code}_user_button4_layout_dynamic_click'] = function() {
            var  url = $('contract_ifrs9_sum_report_link').getUrl();
            new Leaf.Window({
                id: 'contract_ifrs9_sum_report_link_winid',
                params: {
                    winId: 'contract_ifrs9_sum_report_link_winid',
                    period_name:'${/parameter/@period_name}'
                },
                url: url,
                title: '承租人最大逾期天数',
                width: 900,
                height: 600
            }).on('close', function () {
                null;
            });


        };


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

    </a:view>
</a:screen>
