<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: hsx
    $Date: 2018年8月8日17:29:18
    $Revision: 1.0
    $Purpose:罚息减免新增
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" dynamiccreateenabled="true" trace="true">
    <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"/>
        <!--<script src="${/request/@context_path}/javascripts/jquery-1.6.4.min.js" type="text/javascript"/>-->
        <a:link id="con_contract_penalty_active_flag_link" model="cont.CON1020.con_contract_penalty_active_flag"
                modelaction="batch_update"/>
        <a:link id="con_contract_penalty_cancle_link" model="cont.CON1020.con_contract_penalty_cancle"
                modelaction="batch_update"/>
        <a:link id="${/parameter/@layout_code}_modify_link"
                url="${/request/@context_path}/modules/cont/CON1020/contract_penalty_reduction_maintain.lview"/>
        <a:link id="get_xml" model="cont.CON1020.con_contract_penalty" modelaction="execute"/>
        <a:link id="con_contract_penalty_reduction_link" model="cont.CON1020.con_contract_penalty_reduction"
                modelaction="execute"/>
        <script type="text/javascript"><![CDATA[
        jQuery.noConflict();

        var save_flag=false;

        function open_detail_win(ds_id, record_id) {
            var record = $(ds_id).findById(record_id);
            if (record.dirty || !record.data.contract_id) {
                Leaf.showMessage('${l:HLS.PROMPT}', '请先保存数据再维护明细');
                return;
            }
            var param = record.data;
            var case_status_submit = '${/parameter/@case_status_submit}';

            if (case_status_submit == 'APPROVED' || case_status_submit == 'APPROVING') {
                param['function_code'] = 'CON1020_M_R';
                param['maintain_type'] = 'QUERY';
                param['function_usage'] = 'QUERY';
            } else {
                param['function_code'] = 'CON1020_M';
                param['maintain_type'] = 'UPDATE';
                param['function_usage'] = 'UPDATE';
            }
            //如果是工作流页面进入，设为只读

          if ( '${/parameter/@function_usage}'=='QUERY'){
              param['function_code'] = 'CON1020_M_R';
              param['maintain_type'] = 'QUERY';
              param['function_usage'] = 'QUERY';
           }
            param['penalty_reduce_id'] = record.data.penalty_reduce_id;
            param['copy_contract_id'] = record.data.copy_contract_id;
            param['contract_id'] = record.data.contract_id;
            param['url_title'] = '维护减免明细';
            param['winid'] = '${/parameter/@layout_code}_modify_win_id';
            hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_modify_link', ds_id);
        }
        //超链接渲染
        window['${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record) {
            if (name == 'maintain_detail') {
                return '<a href="javascript:open_detail_win(\'' + record.ds.id + '\',\'' + record.id + '\')"> 维护减免明细 </a>';
            }
            return value;
        };
        //保存按钮
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function () {
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var ds_id1 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_case_record');
            var reduce_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');
            if($(reduce_ds_id).data.length<1){
                $L.showInfoMessage('提示','请维护罚息减免信息！')
                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
            }

            if(!$(ds_id1).validate()){
                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
             }

            var record1 = $(ds_id1).getCurrentRecord();
            if (!record1.get('penalty_batch_number')) {
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/cont.CON1020.con_contract_get_batch_number/execute',
                    para: {},
                    success: function (res) {
                        var penalty_batch_number = res.result.penalty_batch_number;
                        record1.set('penalty_batch_number', penalty_batch_number);
                        save_flag=true;
                        var root_ds = $('${/parameter/@layout_code}_virtual_ds');
                        if (root_ds) {
                            root_ds.submit();
                        }
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        // var root_ds = $('${/parameter/@layout_code}_virtual_ds');
                        // if (root_ds) {
                        //     root_ds.submit();
                        // }
                        window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']();
                    },
                    failure: function () {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function () {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            } else {
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                var root_ds = $('${/parameter/@layout_code}_virtual_ds');
                if (root_ds) {
                    root_ds.submit();
                }
            }
        };

        //提交起案
        window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function () {
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();


            getXml();
           /* $('${/parameter/@layout_code}_user_button1').disable();
            $('${/parameter/@layout_code}_user_button2').disable();
            $('${/parameter/@layout_code}_user_button3').disable();
            $('${/parameter/@layout_code}_user_button4').disable();*/
        };

        function getXml() {
            var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_case_record');
            var reduce_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');
            var req_record = $(req_ds_id).getAt(0);
            var record_id = req_record.data.record_id
            if (!record_id) {
                Leaf.showMessage('${l:HLS.PROMPT}', '请先保存再提交数据！');
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
            }
           //检验必须字段
            var records=$(reduce_ds_id).data;
            for(var i=0;i<records.length;i++){
                //如果没有进行测算，
               if( records[i].get('calc_flag')!='Y'){
                   Leaf.showMessage('${l:HLS.PROMPT}', '所有数据测算后才能提交！');
                   window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                   return;
               }
            }



           //判断所有的单据是否已经进行测算过
            Leaf.request({
                url: $('con_contract_penalty_reduction_link').getUrl(),
                para: {
                    record_id: record_id
                },
                success: function (res) {
                   // var v_status = res.result.v_status;
                    //if (v_status == 1) {
                    Leaf.SideBar.show({
                        msg: '取消成功',
                        duration: 2000
                    });
                    //}
                    /*else {
                        Leaf.request({
                            url: $('get_xml').getUrl(),
                            para: {
                                record_id: record_id
                            },
                            success: function (res) {
                                getUrl(res.result.xml)
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            failure: function () {
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            error: function () {
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            scope: this
                        });
                    }*/
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@winid}').close();
                },
                failure: function () {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                error: function () {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                scope: this
            });

        }


        function getUrl(xml) {
            Leaf.request({
                url: '${/request/@context_path}/doodream/post/url',
                para: {
                    xml: xml
                },
                success: function (res) {
                    window.open(res.message, "", "width=940, height=650, scrollbars=yes, toolbar=no, location=no, status=no, menubar=no");
                    Leaf.SideBar.show({
                        msg: '${l:HLS.SUBMIT_SUCCESS}',
                        duration: 2000
                    });
                },
                failure: function () {
                    Leaf.SideBar.show({
                        msg: 'failure',
                        duration: 2000
                    });
                },
                error: function () {
                    Leaf.SideBar.show({
                        msg: 'error',
                        duration: 2000
                    });
                },
                scope: this
            });
        }

        //生效
        window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function () {
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var ds_id1 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_case_record');
            var record1 = $(ds_id1).getCurrentRecord();
            var ds_id2 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');
            var record2 = $(ds_id2).getSelected();
            if (record2.length == 0) {
                Leaf.showMessage('提示', '请选择一条数据！');
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
            }
            var param_list = [];
            var param = {};
            for (var i = 0; i < record2.length; i++) {
                param['record_id'] = record1.data.record_id;
                param['penalty_reduce_id'] = record2[i].get('penalty_reduce_id');
                param['_status'] = 'update';
                param_list.push(param);
            }
            Leaf.request({
                url: $('con_contract_penalty_active_flag_link').getUrl(),
                para: param_list,
                success: function (res) {
                    Leaf.SideBar.show({
                        msg: '生效成功',
                        duration: 2000
                    });
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                failure: function () {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                error: function () {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                scope: this
            });
        };


        //取消
        window['${/parameter/@layout_code}_user_button4_layout_dynamic_click'] = function () {
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var ds_id1 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_case_record');
            var record1 = $(ds_id1).getCurrentRecord();
            var ds_id2 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');

            var record2 = $(ds_id2).getAll();
            if (record2.length == 0) {
                Leaf.showMessage('提示', '请选择一条数据！');
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
            }
            var param_list = [];

            for (var i = 0; i < record2.length; i++) {
                //放在外面程序会报错，由于param是同一个对象，第二次penalty_reduce_id会将第一次的覆盖
                var param = {};
                param['record_id'] = record1.data.record_id;
                param['penalty_reduce_id'] = record2[i].get('penalty_reduce_id');
                param['_status'] = 'update';
                param_list.push(param);
            }

            $L.showConfirm('提示','确认取消该批次的修改信息?',function () {
                Leaf.request({
                    url: $('con_contract_penalty_cancle_link').getUrl(),
                    para: param_list,
                    success: function (res) {
                        Leaf.SideBar.show({
                            msg: '取消成功',
                            duration: 2000
                        });
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $('${/parameter/@winid}').close();
                        },
                    failure: function () {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function () {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });

            },function () {
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            })

        };

        //保存submitsuccess调用
        window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'] = function (ds, record, res, bp_seq) {
            var ds_id2 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');
            $(ds_id2).query();
            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
        };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
