<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:view>
        <a:link id="get_xml" model="cont.CON318.contract_loss_share" modelaction="execute"/>
        <script type="text/javascript"><![CDATA[
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
            getXml();
            $('${/parameter/@layout_code}_save').disable();
            $('${/parameter/@layout_code}_user_button1').disable();
        };

        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_loss_share');
            var record = $(ds_id).getAt(0);
            if(name =='car_standard_price') {
                var surplus_rate = record.get('surplus_rate');
                var car_standard_price = record.get('car_standard_price');
                var car_debt_value = car_standard_price * surplus_rate;
                record.set('car_debt_value',car_debt_value);
                var total_receivable_amount = record.get('total_receivable_amount');
                var loss_share_radio = record.get('loss_share_radio');
                var loss_share_ds = (total_receivable_amount - car_debt_value) * loss_share_radio;
                if (loss_share_ds > 0) {
                    record.set('loss_share_ds',loss_share_ds);
                }
                else record.set('loss_share_ds', 0);
            }
            else if (name =='loss_share_radio') {
                var car_debt_value = record.get('car_debt_value');
                var total_receivable_amount = record.get('total_receivable_amount');
                var loss_share_radio = record.get('loss_share_radio');
                var loss_share_ds = (total_receivable_amount - car_debt_value) * loss_share_radio;
                if (loss_share_ds > 0) {
                    record.set('loss_share_ds',loss_share_ds);
                }
                else record.set('loss_share_ds', 0);
            }

        };

        function getXml() {
            var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_loss_share');
            var req_record = $(req_ds_id).getCurrentRecord();
            var contract_loss_id = req_record.get('contract_loss_id');
            if (req_record.dirty) {
                Leaf.showMessage('${l:HLS.PROMPT}', '页面数据存在修改，请先保存再发起起案！');
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
            }
            Leaf.request({
                url: $('get_xml').getUrl(),
                para: {
                    contract_loss_id: contract_loss_id,
                    interface_name: '${/parameter/@case_type}'
                },
                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
            });

        }

        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/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function(ds, record) {

            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_cost_support');
            var records = $(ds_id).getAll();
            if (records) {
                if (records.length > 3) {
                    Leaf.showMessage('${l:HLS.PROMPT}', '每种支援类型的记录最多存在一条！');
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    return false;
                } else {
                    var support_type_i;
                    var support_type_j;
                    var i;
                    var j;
                    for (i = 0; i < records.length; i++) {
                        support_type_i = records[i].get('support_type');
                        for (j = i + 1; j < records.length; j++) {
                            support_type_j = records[j].get('support_type');
                            if (support_type_i == support_type_j) {
                                Leaf.showMessage('${l:HLS.PROMPT}', '每种支援类型的记录最多存在一条！');
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                return false;
                            }
                        }
                    }
                }
            }

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

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