<?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="con731_calc_ccr_fee_link" model="cont.CON731.calc_ccr_fee" modelaction="execute"/>-->
        <a:link id="calc_leasse_amount_link" model="cont.CON731.calc_leasse_date" modelaction="execute"/>
        <a:link id="calc_pay_amount_link" model="cont.CON731.calc_leasse_pay" modelaction="execute"/>
        <a:link id="calc_tenant_amount_link" model="cont.CON733.calc_tenant_change_req" modelaction="execute"/>
        <a:link id="con731_quote_link"
                url="${/request/@context_path}/modules/cont/CON731/hls_fin_calculator_update_n.lview"/>
        <a:link id="con733_quote_calc_change_req_link" model="cont.CON733.calc_change_req" modelaction="execute"/>
        <a:link id="con731_submit_link" model="cont.CON731.submit_change_req" modelaction="execute"/>
        <a:link id="con731_cancel_link" model="cont.CON731.cancel_change_req" modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}_prj500_cdd_uploadFile_id"
                url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="${/parameter/@layout_code}_prj500_cdd_downloadFile_id"
                url="${/request/@context_path}/downloadFile.lview"/>
        <script src="${/request/@context_path}/javascripts/lightbox.js" type="text/javascript"/>
        <link href="${/request/@context_path}/css/lightbox.css" rel="stylesheet" type="text/css"/>
        <a:link id="${/parameter/@layout_code}${/parameter/@tree_code}_hls_fin_doc_quotation_link_id"
                url="${/request/@context_path}/modules/hls/HLS500/hls_fin_doc_quotation.lsc"/>
        <a:link id="set_sum_unreceived_principal_cancel_link" model="cont.CON731.con_change_calc" modelaction="update"/>
        <a:link id="${/parameter/@layout_code}${/parameter/@pre_layout}prj500_cdd_uploadFile_id"
                url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="${/parameter/@layout_code}${/parameter/@pre_layout}prj500_cdd_downloadFile_id"
                url="${/request/@context_path}/downloadFile.lview"/>
        <script type="text/javascript"><![CDATA[
        Ext.ux.Lightbox.register('a[ref=img]', true);
        Leaf.onReady(function () {
            var ds_con_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_QUOTATION_01', 'con_contract');
            if ($(ds_con_id)) {
                $(ds_con_id).setQueryParameter('contract_id', '${/parameter/@contract_id}');
                $(ds_con_id).query();
            }
        });

        function open_contract_win(ds_id, record_id) {
            var record = $(ds_id).findById(record_id);
            var param = record.data;
            param['function_code'] = 'CON301';
            param['function_usage'] = 'QUERY';
            param['download'] = 'Y';
            param['maintain_type'] = 'UPDATE';
            param['url_title'] = '${l:CON301.CONTRACT_DETAIL}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'con_contract_modify_link', ds_id);
        }

        window['${/parameter/@layout_code}_quote_layout_dynamic_click'] = function () {
            //  $('${/parameter/@layout_code}_quote').disable(); //按钮不可用
            var req_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_QUOTATION_01', 'con_contract');
            var req_record = $(req_ds_id).getAt(0);
            var calc_session_id = req_record.get('calc_session_id');
            var quo_win;
            var calc_type = '${/parameter/@calc_type}';
            var document_category = req_record.get('document_category');
            // 變更起始期數
            var ccr_start_times = req_record.get('ccr_start_times');
            //變更剩餘期數
            var ccr_outstanding_times = req_record.get('ccr_outstanding_times');
            var change_document_type = req_record.get('change_document_type');
            if ($(req_ds_id).validate($('${/parameter/@layout_code}_quote').enable())) {
                if (req_record.dirty) {
                    Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                    return;
                }
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('con733_quote_calc_change_req_link').getUrl(),
                    para: {
                        change_req_id: '${/parameter/@change_req_id}',
                        ccr_start_times: ccr_start_times,
                        ccr_outstanding_times: ccr_outstanding_times,
                        calc_session_id: calc_session_id
                    },
                    success: function (res) {
                        //     $('${/parameter/@layout_code}_quote').enable();
                        req_record.set('calc_session_id', res.result.calc_session_id);
                        req_record.dirty = false;
                        quo_win = new Leaf.Window({
                            id: '${/parameter/@layout_code}_hls_fin_calc_quotation_link_winid',
                            params: {
                                price_list: req_record.get('price_list'),
                                document_id: '${/parameter/@change_req_id}',
                                document_category: document_category,
                                maintain_type: 'UPDATE',
                                calc_session_id: res.result.calc_session_id,
                                quotation_id: null,
                                dsId: req_ds_id,
                                winId: '${/parameter/@layout_code}_hls_fin_calc_quotation_link_winid',
                                global_flag: 'Y',
                                id_num: 0,
                                calc_type: '${/parameter/@calc_type}',
                                recreate_L_formula: 'N'
                            },
                            url: $('con731_quote_link').getUrl(),
                            fullScreen: true,
                            draggable: true
                        });
                        quo_win.addListener('close', function () {
                            $(req_ds_id).query();
                            var cf_ds_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'G_QUOTATION_04', 'con_contract_cashflow');
                            $(cf_ds_id).setQueryParameter('contract_id', '${/parameter/@contract_id}');
                            $(cf_ds_id).query();
                        });
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    failure: function () {
                        //    $('${/parameter/@layout_code}_quote').enable();
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function () {
                        //   $('${/parameter/@layout_code}_quote').enable();
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            }
        };


        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function (ds, record, name, value, old_value, bp_seq) {
            var ds_con_id = get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_QUOTATION_01', 'con_contract');
            if (ds.id == ds_con_id) {
                // 承租人变更
                if ('${/parameter/@layout_code}' == 'CON_TENANT_CHANGE_MODIFY') {
                    // 承租人变更日計算未收金額相關信息
                    if (name == 'ccr_lease_date' && value) {
                        Leaf.request({
                            url: $('calc_tenant_amount_link').getUrl(),
                            para: {
                                change_req_id: ${/parameter/@change_req_id},
                                ccr_lease_date: record.get('ccr_lease_date')
                            },
                            success: function (res) {
                                record.set('ccr_lease_rental', res.result.ccr_lease_rental);
                                record.set('ccr_lease_principal', res.result.ccr_lease_principal);
                                record.set('ccr_lease_interest', res.result.ccr_lease_interest);
                                record.set('ccr_lease_penalty', res.result.ccr_lease_penalty);
                            },
                            error: function () {
                            },
                            failure: function () {
                            },
                            sync: true,
                            scope: this
                        });
                    }
                }
                // 回款賬戶變更
                if ('${/parameter/@layout_code}' == 'CON_ACCOUNT_CHANGE_MODIFY') {
                    if (name == 'pay_method') {
                        if (record.get('pay_method') == 'WIRE_TRANSFER') {
                            record.set('bank_account_id', null);
                            record.set('bank_account_id_n', null);
                            record.getField('bank_account_id').setReadOnly(true);
                            record.getField('bank_account_id_n').setReadOnly(true);
                            record.getField('bank_account_id').setRequired(false);
                            record.getField('bank_account_id_n').setRequired(false);
                            record.set('bank_account_name', null);
                            record.set('bank_branch_num', null);
                            record.set('bank_branch_name', null);
                        } else {
                            record.getField('bank_account_id').setReadOnly(false);
                            record.getField('bank_account_id_n').setReadOnly(false);
                            record.getField('bank_account_id').setRequired(true);
                            record.getField('bank_account_id_n').setRequired(true);
                        }

                    }
                }
                // 租赁计划變更
                if ('${/parameter/@layout_code}' == 'CON_CHANGE_MODIFY') {
                    var lease_times = record.get('lease_times') || 0;
                    if (name == 'ccr_start_times') {
                        Leaf.request({
                            url: $('set_sum_unreceived_principal_cancel_link').getUrl(),
                            para: {
                                contract_id: record.get('contract_id'),
                                ccr_start_times: value
                            },
                            success: function (res) {
                                record.set('sum_unreceived_principal', res.result.sum_unreceived_principal);
                            },
                            failure: function () {

                            },
                            error: function () {

                            },
                            scope: this
                        });
                        var ccr_outstanding_times = record.get('ccr_outstanding_times') || 0;
                        var curr_times = minus(plus(value || 0, ccr_outstanding_times || 0), 1);
                        if (curr_times > lease_times) {
                            record.set('extend_flag', 'Y');
                            record.set('deflation_flag', 'N');
                        }
                    }
                    if (name == 'ccr_outstanding_times') {
                        var ccr_start_times = record.get('ccr_start_times') || 0;
                        var curr_times = minus(plus(value || 0, ccr_start_times || 0), 1);
                        if (curr_times < lease_times) {
                            record.set('extend_flag', 'N');
                            record.set('deflation_flag', 'Y');
                        }
                    }
                }
            }
        };

        //新增和加载时调用form
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_form_add_and_load'] = function (ds, record, config_records, bp_seq) {
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            if (ds_id == ds.id && ds_id) {
                if (record.get('pay_method') == 'WIRE_TRANSFER') {
                    record.set('bank_account_id', null);
                    record.set('bank_account_id_n', null);
                    record.getField('bank_account_id').setReadOnly(true);
                    record.getField('bank_account_id_n').setReadOnly(true);
                    record.getField('bank_account_id').setRequired(false);
                    record.getField('bank_account_id_n').setRequired(false);
                    record.set('bank_account_name', null);
                    record.set('bank_branch_num', null);
                    record.set('bank_branch_name', null);

                }
            }
        };

        window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function () {
            //  $('${/parameter/@layout_code}_submit_approval').disable();
            debugger;
            var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            var req_ds_id2 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_change_req');
            var attch_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'prj_cdd_item_doc_ref');
            var req_record = $(req_ds_id).getAt(0);
            var case_type = '';
            var document_type = '${/parameter/@document_type}';
            var calc_session_id = req_record.get('calc_session_id');
           // window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
           for (var i=0;i<$(attch_id).getAll().length;i++){
               if ($(attch_id).getAll()[i].dirty == true) {
                   Leaf.showMessage('${l:PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                   //  $('${/parameter/@layout_code}_submit_approval').enable();
                  // window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                   return false;
               }
           }
            if (req_record.dirty == true) {
                Leaf.showMessage('${l:PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                //  $('${/parameter/@layout_code}_submit_approval').enable();
               // window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return false;
            }
            if ($(req_ds_id).validate($('${/parameter/@layout_code}_submit_approval').enable()) && $(req_ds_id2).validate($('${/parameter/@layout_code}_submit_approval').enable())) {
                // if (req_record.dirty) {
                //     Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                //     window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                //     return;
                // }
                // 回款賬戶變更
                if ('${/parameter/@layout_code}' == 'CON_ACCOUNT_CHANGE_MODIFY') {
                    var bank_account_num_s = req_record.get('bank_account_num_s');
                    var bank_account_num_c = req_record.get('bank_branch_num');
                    if (bank_account_num_s == bank_account_num_c) {
                        Leaf.showMessage('警告', '现扣款账号和原扣款账号不能重复！');
                   //     window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return false;

                    } else {
                        con731_submit();
                    }
                } else if ('${/parameter/@layout_code}' == 'CON_TENANT_CHANGE_MODIFY') {
                    var bp_id_tenant = req_record.get('bp_id_tenant');
                    var bp_id_tenant_old = req_record.get('bp_id_tenant_old');
                    if (bp_id_tenant == bp_id_tenant_old) {
                        Leaf.showMessage('警告', '变更承租人和原承租人不能重复！');
                    //    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return false;
                    }
                    else {
                        con731_submit();
                    }

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

        function con731_submit() {
            Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认提交审批？', function () {
            var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            var req_record = $(req_ds_id).getAt(0);
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
            Leaf.request({
                url: $('con731_submit_link').getUrl(),
                para: {
                    change_req_id: '${/parameter/@change_req_id}',
                    layout_code: '${/parameter/@layout_code}'
                },
                success: function (res) {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@winid}').close();

                },
                failure: function () {
                    // $('${/parameter/@layout_code}_submit_approval').enable();
                    // $('${/parameter/@layout_code}_save').enable();
                    //  if('${/parameter/@layout_code}' == 'CON_CHANGE_MODIFY'){
                    //          $('${/parameter/@layout_code}_quote').enable();
                    //  }
                    //  $('${/parameter/@layout_code}_user_button1').enable();
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                error: function () {
                    // $('${/parameter/@layout_code}_submit_approval').enable();
                    // $('${/parameter/@layout_code}_save').enable();
                    // if('${/parameter/@layout_code}' == 'CON_CHANGE_MODIFY'){
                    //         $('${/parameter/@layout_code}_quote').enable();
                    // }
                    // $('${/parameter/@layout_code}_user_button1').enable();
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                scope: this
            });
            }, null, 300, 150);
        }

        //保存submitsuccess调用
        window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'] = function (ds, record, res, bp_seq) {
            var ca_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_cashflow');
            if (ca_ds_id) {
                $(ca_ds_id).query();
            }
            // var con_contract_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            // var record = $(con_contract_ds_id).getCurrentRecord();
            // Leaf.request({
            //     url: $('con731_calc_ccr_fee_link').getUrl(),
            //     para: {
            //         contract_id: ${/parameter/@change_req_id},
            //         bp_id: record.get('bp_id_tenant'),
            //         lease_item_id: record.get('lease_item_id'),
            //         pattern: record.get('lease_item_id_n'),
            //         machine_number: record.get('machine_number'),
            //         interim_times: record.get('interim_times'),
            //         ccr_start_times: record.get('ccr_start_times'),
            //         ccr_document_type: record.get('ccr_document_type'),
            //         hd_user_col_v04: record.get('hd_user_col_v04'),
            //         btb_payment_date: record.get('btb_payment_date')
            //     },
            //     success: function(res) {
            //         window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            //     },
            //     failure: function() {
            //         window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            //     },
            //     error: function() {
            //         window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            //     },
            //     scope: this
            // });


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


        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function () {
            //  $('${/parameter/@layout_code}_user_button1').disable();
            Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认取消变更？', function () {
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('con731_cancel_link').getUrl(),
                    para: {
                        change_req_id: ${/parameter/@change_req_id}
                    },
                    success: function (res) {
                        //   $('${/parameter/@layout_code}_user_button1').enable();
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $('${/parameter/@winid}').close();
                    },
                    failure: function () {
                        //    $('${/parameter/@layout_code}_user_button1').enable();
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function () {
                        //      $('${/parameter/@layout_code}_user_button1').enable();
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            });
        };

        //附件上传

        function con_repo001_upload_file(id, name, query_only) {
            var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
            if (record.get('check_id')) {
                var url;
                if (query_only == 'Y' || '${/parameter/@function_usage}' == 'QUERY') {
                    url = $('${/parameter/@layout_code}${/parameter/@pre_layout}prj500_cdd_downloadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                } else {
                    url = $('${/parameter/@layout_code}${/parameter/@pre_layout}prj500_cdd_uploadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                }
                var win = new Leaf.Window({
                    url: url,
                    title: '${l:HLS.SUPPORTING_DOCUMENT}',
                    id: 'prj500_cdd_uploadFile_screen_id',
                    width: 850,
                    height: 400
                });

                win.on('close', function () {
                    record.ds.query();
                });
            } else {
                Leaf.showMessage('${l:HLS.PROMPT}', '请先保存!');
            }
        }

        window['${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record) {
            var link_function = '';
            window['${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
            if (name == 'attachment') {
                link_function = 'con_repo001_upload_file';
                return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '</a>';
            } else if (name == 'attach_file_name') {
                if (value != null) {
                    var link = '${/request/@context_path}/atm_download.lsc?attachment_id=';
                    var str = value.split(';;');
                    var url = '';
                    for (var i = 0; i < str.length; i++) {
                        //
                        var temp = str[i].split('--');
                        if (!Leaf.isEmpty(temp[0])) {

                            var file_name = temp[0].toUpperCase();
                            var file_suffix = temp[0].substr(temp[0].lastIndexOf('.') + 1).toUpperCase();
                            if (file_name.indexOf('.BMP') >= 0 || file_name.indexOf('.JPG') >= 0 || file_name.indexOf('.JPEG') >= 0 || file_name.indexOf('.PNG') >= 0 || file_name.indexOf('.GIF') >= 0) {
                                url = url + '<a href=' + link + temp[1] + ' ref="img">' + temp[0] + '</a>' + ',';
                            } else {
                                url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                            }
                        }
                    }
                    return url;
                }
            }
        };
        //图片渲染
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_dynamic_pic_renderer'] = function (record, name, bp_seq) {
            var result = name.match(/(.*)_pic$/);
            if (result) {
                var pic_value = record.get(result[1]);
                if (pic_value == 'OUTFLOW') {
                    return '<img src="${/request/@context_path}/images/outflow.png" style="margin-top:4px"/>';
                } else if (pic_value == 'INFLOW') {
                    return '<img src="${/request/@context_path}/images/inflow.png" style="margin-top:4px"/>';
                } else if (pic_value == 'NONCASH') {
                    return '<img src="${/request/@context_path}/images/noncash.png" style="margin-top:4px"/>';
                } else if (pic_value == 'CASH') {
                    return '<img src="${/request/@context_path}/images/cash.png" style="margin-top:4px"/>';
                }
            }
        };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
